#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) {
    return 0;
  }
  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    1375 <main+0x225>
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     121e <main+0xce>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1375 <main+0x225>
mov    -0x38(%rbp),%rax
mov    0x2d97(%rip),%rcx
mov    (%rcx),%rcx
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0x3c(%rbp)
sub    $0x82,%eax
je     1267 <main+0x117>
jmp    1243 <main+0xf3>
mov    -0x3c(%rbp),%eax
sub    $0x83,%eax
jne    1278 <main+0x128>
jmp    1256 <main+0x106>
lea    0xdbb(%rip),%rdi
call   1040 <printf@plt>
jmp    1282 <main+0x132>
lea    0xdce(%rip),%rdi
call   1040 <printf@plt>
jmp    1282 <main+0x132>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x38(%rbp),%rax
mov    0x2d43(%rip),%rcx
mov    (%rcx),%rcx
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0x40(%rbp)
sub    $0x80,%eax
je     12cb <main+0x17b>
jmp    12a7 <main+0x157>
mov    -0x40(%rbp),%eax
sub    $0x81,%eax
jne    12dc <main+0x18c>
jmp    12ba <main+0x16a>
lea    0xd9f(%rip),%rdi
call   1040 <printf@plt>
jmp    12e6 <main+0x196>
lea    0xdb2(%rip),%rdi
call   1040 <printf@plt>
jmp    12e6 <main+0x196>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xdbb(%rip),%rdi
call   1040 <printf@plt>
movw   $0x102,-0x28(%rbp)
lea    -0x28(%rbp),%rdi
lea    0xdc7(%rip),%rsi
mov    $0x2,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
jne    1328 <main+0x1d8>
lea    0xdaf(%rip),%rdi
call   1040 <printf@plt>
jmp    1367 <main+0x217>
lea    -0x28(%rbp),%rdi
lea    0xdbc(%rip),%rsi
mov    $0x2,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
jne    1358 <main+0x208>
lea    0xda4(%rip),%rdi
call   1040 <printf@plt>
jmp    1362 <main+0x212>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    1367 <main+0x217>
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    11e2 <main+0x92>
mov    0x2e48(%rip),%rax
mov    (%rax),%rsi
mov    0x2e06(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
call   1050 <memcmp@plt>
xor    %r14d,%r14d
test   %rax,%rax
jne    128d <main+0x13d>
mov    0x2df9(%rip),%rax
mov    (%rax),%rax
mov    (%rbx,%rax,1),%al
cmp    $0x83,%al
je     11fc <main+0xac>
cmp    $0x82,%al
jne    129b <main+0x14b>
lea    0xe3e(%rip),%rdi
jmp    1203 <main+0xb3>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xe0e(%rip),%rsi
call   1060 <fprintf@plt>
jmp    128d <main+0x13d>
lea    0xee7(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%rax
mov    (%rbx,%rax,1),%al
cmp    $0x81,%al
je     1226 <main+0xd6>
cmp    $0x80,%al
jne    129b <main+0x14b>
lea    0xe1d(%rip),%rdi
jmp    122d <main+0xdd>
lea    0xe9a(%rip),%rdi
call   1030 <puts@plt>
lea    0xe2b(%rip),%rdi
call   1030 <puts@plt>
movw   $0x102,-0x20(%rbp)
lea    0xdcd(%rip),%rsi
lea    -0x20(%rbp),%rdi
mov    $0x2,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
je     1281 <main+0x131>
lea    0xdb6(%rip),%rsi
lea    -0x20(%rbp),%rdi
mov    $0x2,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
jne    129b <main+0x14b>
lea    0xe06(%rip),%rdi
jmp    1288 <main+0x138>
lea    0xe1e(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
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
@EI_CLASS = global i64 0
@.str.1 = [36 x i8] c"#define KERNEL_ELFCLASS ELFCLASS32\0A\00"
@.str.2 = [36 x i8] c"#define KERNEL_ELFCLASS ELFCLASS64\0A\00"
@EI_DATA = global i64 0
@.str.3 = [36 x i8] c"#define KERNEL_ELFDATA ELFDATA2LSB\0A\00"
@.str.4 = [36 x i8] c"#define KERNEL_ELFDATA ELFDATA2MSB\0A\00"
@.str.5 = [34 x i8] c"#define HOST_ELFCLASS ELFCLASS64\0A\00"
@.str.6 = [3 x i8] c"\01\02\00"
@.str.7 = [34 x i8] c"#define HOST_ELFDATA ELFDATA2MSB\0A\00"
@.str.8 = [3 x i8] c"\02\01\00"
@.str.9 = [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 %70
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 %28
27: ; preds = %22
store i32 0, i32* %3
store i32 1, i32* %9
br label %70
28: ; preds = %22
%29 = load i64, i64* @EI_CLASS
%30 = getelementptr inbounds i8, i8* %13, i64 %29
%31 = load i8, i8* %30
%32 = zext i8 %31 to i32
switch i32 %32, label %37 [
i32 131, label %33
i32 130, label %35
]
33: ; preds = %28
%34 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %39
35: ; preds = %28
%36 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
br label %39
37: ; preds = %28
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %35, %33
%40 = load i64, i64* @EI_DATA
%41 = getelementptr inbounds i8, i8* %13, i64 %40
%42 = load i8, i8* %41
%43 = zext i8 %42 to i32
switch i32 %43, label %48 [
i32 129, label %44
i32 128, label %46
]
44: ; preds = %39
%45 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
br label %50
46: ; preds = %39
%47 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
br label %50
48: ; preds = %39
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %46, %44
%51 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0))
%52 = bitcast %union.anon* %8 to i16*
store i16 258, i16* %52
%53 = bitcast %union.anon* %8 to [2 x i8]*
%54 = getelementptr inbounds [2 x i8], [2 x i8]* %53, i64 0, i64 0
%55 = call i64 @memcmp(i8* %54, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 2)
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %50
%58 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0))
br label %69
59: ; preds = %50
%60 = bitcast %union.anon* %8 to [2 x i8]*
%61 = getelementptr inbounds [2 x i8], [2 x i8]* %60, i64 0, i64 0
%62 = call i64 @memcmp(i8* %61, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i32 2)
%63 = icmp eq i64 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %59
%65 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.9, i64 0, i64 0))
br label %68
66: ; preds = %59
%67 = call i32 @exit(i32 1)
unreachable
68: ; preds = %64
br label %69
69: ; preds = %68, %57
store i32 0, i32* %3
store i32 1, i32* %9
br label %70
70: ; preds = %69, %27, %19
%71 = load i8*, i8** %6
%72 = load i32, i32* %3
ret i32 %72
}

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
@EI_CLASS = global i64 0
@EI_DATA = global i64 0
@.str.6 = [3 x i8] c"\01\02\00"
@.str.8 = [3 x i8] c"\02\01\00"
@str = [35 x i8] c"#define KERNEL_ELFCLASS ELFCLASS64\00"
@str.10 = [35 x i8] c"#define KERNEL_ELFDATA ELFDATA2MSB\00"
@str.11 = [33 x i8] c"#define HOST_ELFCLASS ELFCLASS64\00"
@str.12 = [33 x i8] c"#define HOST_ELFDATA ELFDATA2LSB\00"
@str.13 = [33 x i8] c"#define HOST_ELFDATA ELFDATA2MSB\00"
@str.14 = [35 x i8] c"#define KERNEL_ELFDATA ELFDATA2LSB\00"
@str.15 = [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 %52
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 %20, label %52
20: ; preds = %15
%21 = load i64, i64* @EI_CLASS
%22 = getelementptr inbounds i8, i8* %6, i64 %21
%23 = load i8, i8* %22
switch i8 %23, label %25 [
i8 -125, label %27
i8 -126, label %24
]
24: ; preds = %20
br label %27
25: ; preds = %20
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %20, %24
%28 = phi i8* [ getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0), %24 ], [ getelementptr inbounds ([35 x i8], [35 x i8]* @str.15, i64 0, i64 0), %20 ]
%29 = call i32 @puts(i8* dereferenceable(1) %28)
%30 = load i64, i64* @EI_DATA
%31 = getelementptr inbounds i8, i8* %6, i64 %30
%32 = load i8, i8* %31
switch i8 %32, label %34 [
i8 -127, label %36
i8 -128, label %33
]
33: ; preds = %27
br label %36
34: ; preds = %27
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %27, %33
%37 = phi i8* [ getelementptr inbounds ([35 x i8], [35 x i8]* @str.10, i64 0, i64 0), %33 ], [ getelementptr inbounds ([35 x i8], [35 x i8]* @str.14, i64 0, i64 0), %27 ]
%38 = call i32 @puts(i8* dereferenceable(1) %37)
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.11, i64 0, i64 0))
%40 = getelementptr inbounds %union.anon, %union.anon* %3, i64 0, i32 0
store i16 258, i16* %40
%41 = call i64 @memcmp(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 2)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %36
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.13, i64 0, i64 0))
br label %52
45: ; preds = %36
%46 = call i64 @memcmp(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i32 2)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.12, i64 0, i64 0))
br label %52
50: ; preds = %45
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %43, %48, %15, %12
%53 = phi i32 [ 1, %12 ], [ 0, %15 ], [ 0, %48 ], [ 0, %43 ]
ret i32 %53
}