public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/112665] New: I am getting incorrect output values at optimization level 2 in GCC for the s390x architecture.
@ 2023-11-22  7:43 shinwogud12 at gmail dot com
  2023-11-22  7:45 ` [Bug c/112665] " shinwogud12 at gmail dot com
  2023-12-14  7:52 ` [Bug target/112665] " krebbel at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: shinwogud12 at gmail dot com @ 2023-11-22  7:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112665

            Bug ID: 112665
           Summary: I am getting incorrect output values at optimization
                    level 2 in GCC for the s390x architecture.
           Product: gcc
           Version: 11.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: shinwogud12 at gmail dot com
  Target Milestone: ---

The provided C code includes a main function and a helper function i(), along
with a struct definition and several global variables. The code primarily
involves conditional logic and a loop to manipulate these global variables.

###PoC(Proof of Concept)
```
#include <stdint.h>
#include <stdio.h>

struct a {
  uint64_t b;
};
int c = 1;
int d, e, h, f, g, l = 0;
volatile struct a k[1];

void i() {
  for (; l < 1; l++)
    f = d <= 0;
}

int main() {
  for (e = 9; e; --e){
    i();
    c && (g = ((int16_t)(k[0], f)) <= e);
  }
  printf("g_200 value %d\n", g);
  return 0;
}
```

**Struct Definition**

```
struct a {
  uint64_t b;
};
```
- Defines a struct **`a`** with a single member **`b`** of type **`uint64_t`**.

**Global Variables**

```c
int c = 1;
int d, e, h, f, g, l = 0;
volatile struct a k[1];
```

- A loop that runs once, setting **`f`** to 1 if **`d`** is less than or equal
to 0.

**Main Function**

```c
cCopy code
int main() {
  for (e = 9; e; --e){
    i();
    c && (g = ((int16_t)(k[0], f)) <= e);
  }
  printf("g_200 value %d\n", g);
  return 0;
}

```

- A loop starts with **`e`** at 9, decrementing until it reaches 0.
- Calls function **`i()`**, which sets **`f`** based on the condition **`d <=
0`**.
- The expression **`c && (g = ((int16_t)(k[0], f)) <= e)`** uses the comma
operator, resulting in **`f`** being evaluated and cast to **`int16_t`**. Since
**`c`** is always 1, **`g`** is set to 1 if **`f`** is less than or equal to
**`e`**.
- The final value of **`g`** is printed.

### Expected Result

- Since **`d`** is initialized to 0 and never modified, **`f`** will always be
set to 1 in the **`i()`** function.
- In the main function, **`g`** is set to 1 in each iteration of the loop
because **`f`** (which is 1) is always less than or equal to **`e`** (which
decrements from 9 to 1).
- The final output of the program will be **`g_200 value 1`**.

Why does optimization level 2 in GCC output 0?

Godbolt Link: https://www.godbolt.org/z/r6c4oo18a

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-12-14  7:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-22  7:43 [Bug c/112665] New: I am getting incorrect output values at optimization level 2 in GCC for the s390x architecture shinwogud12 at gmail dot com
2023-11-22  7:45 ` [Bug c/112665] " shinwogud12 at gmail dot com
2023-12-14  7:52 ` [Bug target/112665] " krebbel at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).