#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
size_t EI_CLASS;
size_t EI_DATA;
int EI_NIDENT;
#define ELFCLASS32 131
#define ELFCLASS64 130
#define ELFDATA2LSB 129
#define ELFDATA2MSB 128
char *ELFMAG;
int SELFMAG;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/);
scalar_t__ memcmp(unsigned char *, char *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int main(int argc, char **argv) {
  unsigned char ei[EI_NIDENT];
  union {
    short s;
    char c[2];
  } endian_test;
  if (fread(ei, 1, EI_NIDENT, stdin) != EI_NIDENT) {
    fprintf(stderr, "Error: input truncated\n");
    return 1;
  }
  if (memcmp(ei, ELFMAG, SELFMAG) != 0) {
    fprintf(stderr, "Error: not ELF\n");
    return 1;
  }
  switch (ei[EI_CLASS]) {
  case ELFCLASS32:
    printf("#define KERNEL_ELFCLASS ELFCLASS32\n");
    break;
  case ELFCLASS64:
    printf("#define KERNEL_ELFCLASS ELFCLASS64\n");
    break;
  default:
    exit(1);
  }
  switch (ei[EI_DATA]) {
  case ELFDATA2LSB:
    printf("#define KERNEL_ELFDATA ELFDATA2LSB\n");
    break;
  case ELFDATA2MSB:
    printf("#define KERNEL_ELFDATA ELFDATA2MSB\n");
    break;
  default:
    exit(1);
  }
  if (sizeof(unsigned long) == 4) {
    printf("#define HOST_ELFCLASS ELFCLASS32\n");
  } else if (sizeof(unsigned long) == 8) {
    printf("#define HOST_ELFCLASS ELFCLASS64\n");
  }
  endian_test.s = 0x0102;
  if (memcmp(endian_test.c, "\x01\x02", 2) == 0)
    printf("#define HOST_ELFDATA ELFDATA2MSB\n");
  else if (memcmp(endian_test.c, "\x02\x01", 2) == 0)
    printf("#define HOST_ELFDATA ELFDATA2LSB\n");
  else
    exit(1);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
mov    0x2e36(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    0x2e2b(%rip),%rcx
cmp    (%rcx),%eax
je     11e5 <main+0x95>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    0xe33(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    138a <main+0x23a>
mov    -0x38(%rbp),%rdi
mov    0x2df8(%rip),%rax
mov    (%rax),%rsi
mov    0x2db6(%rip),%rax
mov    (%rax),%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
je     1233 <main+0xe3>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
lea    0xdfd(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    138a <main+0x23a>
mov    -0x38(%rbp),%rax
mov    0x2d82(%rip),%rcx
mov    (%rcx),%rcx
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0x3c(%rbp)
sub    $0x82,%eax
je     127c <main+0x12c>
jmp    1258 <main+0x108>
mov    -0x3c(%rbp),%eax
sub    $0x83,%eax
jne    128d <main+0x13d>
jmp    126b <main+0x11b>
lea    0xdb6(%rip),%rdi
call   1040 <printf@plt>
jmp    1297 <main+0x147>
lea    0xdc9(%rip),%rdi
call   1040 <printf@plt>
jmp    1297 <main+0x147>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x38(%rbp),%rax
mov    0x2d2e(%rip),%rcx
mov    (%rcx),%rcx
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0x40(%rbp)
sub    $0x80,%eax
je     12e0 <main+0x190>
jmp    12bc <main+0x16c>
mov    -0x40(%rbp),%eax
sub    $0x81,%eax
jne    12f1 <main+0x1a1>
jmp    12cf <main+0x17f>
lea    0xd9a(%rip),%rdi
call   1040 <printf@plt>
jmp    12fb <main+0x1ab>
lea    0xdad(%rip),%rdi
call   1040 <printf@plt>
jmp    12fb <main+0x1ab>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xdb6(%rip),%rdi
call   1040 <printf@plt>
movw   $0x102,-0x28(%rbp)
lea    -0x28(%rbp),%rdi
lea    0xdc2(%rip),%rsi
mov    $0x2,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
jne    133d <main+0x1ed>
lea    0xdaa(%rip),%rdi
call   1040 <printf@plt>
jmp    137c <main+0x22c>
lea    -0x28(%rbp),%rdi
lea    0xdb7(%rip),%rsi
mov    $0x2,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
jne    136d <main+0x21d>
lea    0xd9f(%rip),%rdi
call   1040 <printf@plt>
jmp    1377 <main+0x227>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    137c <main+0x22c>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e7f(%rip),%r15
mov    (%r15),%edx
mov    %rsp,%rbx
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e59(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%r14d
mov    %rbx,%rdi
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    (%r15),%eax
jne    11cb <main+0x7b>
mov    0x2e48(%rip),%rax
mov    (%rax),%rsi
mov    0x2e06(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
call   1050 <memcmp@plt>
test   %rax,%rax
je     11ee <main+0x9e>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
jmp    11db <main+0x8b>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xe25(%rip),%rsi
call   1060 <fprintf@plt>
mov    %r14d,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dcb(%rip),%rax
mov    (%rax),%rax
mov    (%rbx,%rax,1),%al
cmp    $0x83,%al
je     1210 <main+0xc0>
cmp    $0x82,%al
jne    12a9 <main+0x159>
lea    0xe20(%rip),%rdi
jmp    1217 <main+0xc7>
lea    0xee3(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%rax
mov    (%rbx,%rax,1),%al
cmp    $0x81,%al
je     123a <main+0xea>
cmp    $0x80,%al
jne    12a9 <main+0x159>
lea    0xe19(%rip),%rdi
jmp    1241 <main+0xf1>
lea    0xe96(%rip),%rdi
call   1030 <puts@plt>
lea    0xe27(%rip),%rdi
call   1030 <puts@plt>
movw   $0x102,-0x20(%rbp)
lea    0xdc9(%rip),%rsi
lea    -0x20(%rbp),%rdi
mov    $0x2,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
je     1295 <main+0x145>
lea    0xdb2(%rip),%rsi
lea    -0x20(%rbp),%rdi
mov    $0x2,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
jne    12a9 <main+0x159>
lea    0xe02(%rip),%rdi
jmp    129c <main+0x14c>
lea    0xe1a(%rip),%rdi
call   1030 <puts@plt>
xor    %r14d,%r14d
jmp    11e0 <main+0x90>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%union.anon = type { i16 }
@EI_NIDENT = global i32 0
@stdin = global i32 0
@stderr = global i32 0
@.str = [24 x i8] c"Error: input truncated\0A\00"
@ELFMAG = global i8* null
@SELFMAG = global i32 0
@.str.1 = [16 x i8] c"Error: not ELF\0A\00"
@EI_CLASS = global i64 0
@.str.2 = [36 x i8] c"#define KERNEL_ELFCLASS ELFCLASS32\0A\00"
@.str.3 = [36 x i8] c"#define KERNEL_ELFCLASS ELFCLASS64\0A\00"
@EI_DATA = global i64 0
@.str.4 = [36 x i8] c"#define KERNEL_ELFDATA ELFDATA2LSB\0A\00"
@.str.5 = [36 x i8] c"#define KERNEL_ELFDATA ELFDATA2MSB\0A\00"
@.str.6 = [34 x i8] c"#define HOST_ELFCLASS ELFCLASS64\0A\00"
@.str.7 = [3 x i8] c"\01\02\00"
@.str.8 = [34 x i8] c"#define HOST_ELFDATA ELFDATA2MSB\0A\00"
@.str.9 = [3 x i8] c"\02\01\00"
@.str.10 = [34 x i8] c"#define HOST_ELFDATA ELFDATA2LSB\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %union.anon
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @EI_NIDENT
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %6
%13 = alloca i8, i64 %11
store i64 %11, i64* %7
%14 = load i32, i32* @EI_NIDENT
%15 = load i32, i32* @stdin
%16 = call i32 @fread(i8* %13, i32 1, i32 %14, i32 %15)
%17 = load i32, i32* @EI_NIDENT
%18 = icmp ne i32 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %9
br label %72
22: ; preds = %2
%23 = load i8*, i8** @ELFMAG
%24 = load i32, i32* @SELFMAG
%25 = call i64 @memcmp(i8* %13, i8* %23, i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %9
br label %72
30: ; preds = %22
%31 = load i64, i64* @EI_CLASS
%32 = getelementptr inbounds i8, i8* %13, i64 %31
%33 = load i8, i8* %32
%34 = zext i8 %33 to i32
switch i32 %34, label %39 [
i32 131, label %35
i32 130, label %37
]
35: ; preds = %30
%36 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
br label %41
37: ; preds = %30
%38 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
br label %41
39: ; preds = %30
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %37, %35
%42 = load i64, i64* @EI_DATA
%43 = getelementptr inbounds i8, i8* %13, i64 %42
%44 = load i8, i8* %43
%45 = zext i8 %44 to i32
switch i32 %45, label %50 [
i32 129, label %46
i32 128, label %48
]
46: ; preds = %41
%47 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
br label %52
48: ; preds = %41
%49 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
br label %52
50: ; preds = %41
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %48, %46
%53 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0))
%54 = bitcast %union.anon* %8 to i16*
store i16 258, i16* %54
%55 = bitcast %union.anon* %8 to [2 x i8]*
%56 = getelementptr inbounds [2 x i8], [2 x i8]* %55, i64 0, i64 0
%57 = call i64 @memcmp(i8* %56, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i32 2)
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %52
%60 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0))
br label %71
61: ; preds = %52
%62 = bitcast %union.anon* %8 to [2 x i8]*
%63 = getelementptr inbounds [2 x i8], [2 x i8]* %62, i64 0, i64 0
%64 = call i64 @memcmp(i8* %63, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i32 2)
%65 = icmp eq i64 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %61
%67 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.10, i64 0, i64 0))
br label %70
68: ; preds = %61
%69 = call i32 @exit(i32 1)
unreachable
70: ; preds = %66
br label %71
71: ; preds = %70, %59
store i32 0, i32* %3
store i32 1, i32* %9
br label %72
72: ; preds = %71, %27, %19
%73 = load i8*, i8** %6
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
%union.anon = type { i16 }
@EI_NIDENT = global i32 0
@stdin = global i32 0
@stderr = global i32 0
@.str = [24 x i8] c"Error: input truncated\0A\00"
@ELFMAG = global i8* null
@SELFMAG = global i32 0
@.str.1 = [16 x i8] c"Error: not ELF\0A\00"
@EI_CLASS = global i64 0
@EI_DATA = global i64 0
@.str.7 = [3 x i8] c"\01\02\00"
@.str.9 = [3 x i8] c"\02\01\00"
@str = [35 x i8] c"#define KERNEL_ELFCLASS ELFCLASS64\00"
@str.11 = [35 x i8] c"#define KERNEL_ELFDATA ELFDATA2MSB\00"
@str.12 = [33 x i8] c"#define HOST_ELFCLASS ELFCLASS64\00"
@str.13 = [33 x i8] c"#define HOST_ELFDATA ELFDATA2LSB\00"
@str.14 = [33 x i8] c"#define HOST_ELFDATA ELFDATA2MSB\00"
@str.15 = [35 x i8] c"#define KERNEL_ELFDATA ELFDATA2LSB\00"
@str.16 = [35 x i8] c"#define KERNEL_ELFCLASS ELFCLASS32\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %union.anon
%4 = load i32, i32* @EI_NIDENT
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast %union.anon* %3 to i8*
%8 = load i32, i32* @stdin
%9 = call i32 @fread(i8* %6, i32 1, i32 %4, i32 %8)
%10 = load i32, i32* @EI_NIDENT
%11 = icmp eq i32 %9, %10
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %55
15: ; preds = %2
%16 = load i8*, i8** @ELFMAG
%17 = load i32, i32* @SELFMAG
%18 = call i64 @memcmp(i8* %6, i8* %16, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %55
23: ; preds = %15
%24 = load i64, i64* @EI_CLASS
%25 = getelementptr inbounds i8, i8* %6, i64 %24
%26 = load i8, i8* %25
switch i8 %26, label %28 [
i8 -125, label %30
i8 -126, label %27
]
27: ; preds = %23
br label %30
28: ; preds = %23
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %23, %27
%31 = phi i8* [ getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0), %27 ], [ getelementptr inbounds ([35 x i8], [35 x i8]* @str.16, i64 0, i64 0), %23 ]
%32 = call i32 @puts(i8* dereferenceable(1) %31)
%33 = load i64, i64* @EI_DATA
%34 = getelementptr inbounds i8, i8* %6, i64 %33
%35 = load i8, i8* %34
switch i8 %35, label %37 [
i8 -127, label %39
i8 -128, label %36
]
36: ; preds = %30
br label %39
37: ; preds = %30
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %30, %36
%40 = phi i8* [ getelementptr inbounds ([35 x i8], [35 x i8]* @str.11, i64 0, i64 0), %36 ], [ getelementptr inbounds ([35 x i8], [35 x i8]* @str.15, i64 0, i64 0), %30 ]
%41 = call i32 @puts(i8* dereferenceable(1) %40)
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.12, i64 0, i64 0))
%43 = getelementptr inbounds %union.anon, %union.anon* %3, i64 0, i32 0
store i16 258, i16* %43
%44 = call i64 @memcmp(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i32 2)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %39
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.14, i64 0, i64 0))
br label %55
48: ; preds = %39
%49 = call i64 @memcmp(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i32 2)
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %48
%52 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.13, i64 0, i64 0))
br label %55
53: ; preds = %48
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %46, %51, %20, %12
%56 = phi i32 [ 1, %12 ], [ 1, %20 ], [ 0, %51 ], [ 0, %46 ]
ret i32 %56
}