S
Szabolcs Nagy
in the code below i thought the function call in g() could be easily
optimized out so that g() becomes the same as h() (which becomes
{return 0;})
executing 'gcc -O3 -S' i found that gcc does not do this
now i'm wondering: is there something in the standard (eg c99) that
prevents this optimization (theoretically)
#include <stdio.h>
#include <stdlib.h>
static inline int c(int a, int b) {
return a == b;
}
static int f(int a, int b, int(*c)(int, int)) {
return c(a, b) - c(b, a);
}
int g(int a, int b) {
return f(a, b, c);
}
int h(int a, int b) {
return c(a, b) - c(b, a);
}
int main(int argc, char *argv[]) {
int a, b;
if (argc < 3)
return printf("usage: %s a b\n", argv[0]);
a = atoi(argv[1]);
b = atoi(argv[2]);
printf("f: %d\n", f(a, b, c));
printf("g: %d\n", g(a, b));
printf("h: %d\n", h(a, b));
return 0;
}
optimized out so that g() becomes the same as h() (which becomes
{return 0;})
executing 'gcc -O3 -S' i found that gcc does not do this
now i'm wondering: is there something in the standard (eg c99) that
prevents this optimization (theoretically)
#include <stdio.h>
#include <stdlib.h>
static inline int c(int a, int b) {
return a == b;
}
static int f(int a, int b, int(*c)(int, int)) {
return c(a, b) - c(b, a);
}
int g(int a, int b) {
return f(a, b, c);
}
int h(int a, int b) {
return c(a, b) - c(b, a);
}
int main(int argc, char *argv[]) {
int a, b;
if (argc < 3)
return printf("usage: %s a b\n", argv[0]);
a = atoi(argv[1]);
b = atoi(argv[2]);
printf("f: %d\n", f(a, b, c));
printf("g: %d\n", g(a, b));
printf("h: %d\n", h(a, b));
return 0;
}