Skip to content

wrong code at -O1/-O2 on x86_64-linux-gnu #95630

Closed as not planned
Closed as not planned
@tangyixuan01

Description

@tangyixuan01

Compiler explorer: https://godbolt.org/z/PP5eaEccb

Clang-18 produces the wrong code when compiling the following code with "-O1" or "-O2", while outputting the correct result with the optimization flag "-O3".

int printf(const char *, ...);
long a;
int b, c, e, g, i;
long *d, *h;
char f = 6;
int main() {
  long j;
  c = 0;
  for (; c != 7; ++c) {
    long k;
    long **l = &d;
    for (; f + i; i++)
      h = &k;
    g = h != (*l = &j);
    int *m = &b;
    *m = g;
    for (; e; a = a + 1)
      ;
  }
  printf("%d\n", b);
}

clang -O2 s.c; ./a.out
0

clang -O3 s.c; ./a.out
1

gcc -O2 s.c; ./a.out
1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions