#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;
  }
  printf("/* BINARY FILE - DO NOT EDIT\n * Produced by mk_elfconfig.c*/\n");
  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    1396 <main+0x246>
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    1396 <main+0x246>
lea    0xdee(%rip),%rdi
call   1040 <printf@plt>
mov    -0x38(%rbp),%rax
mov    0x2d76(%rip),%rcx
mov    (%rcx),%rcx
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0x3c(%rbp)
sub    $0x82,%eax
je     1288 <main+0x138>
jmp    1264 <main+0x114>
mov    -0x3c(%rbp),%eax
sub    $0x83,%eax
jne    1299 <main+0x149>
jmp    1277 <main+0x127>
lea    0xde8(%rip),%rdi
call   1040 <printf@plt>
jmp    12a3 <main+0x153>
lea    0xdfb(%rip),%rdi
call   1040 <printf@plt>
jmp    12a3 <main+0x153>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x38(%rbp),%rax
mov    0x2d22(%rip),%rcx
mov    (%rcx),%rcx
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0x40(%rbp)
sub    $0x80,%eax
je     12ec <main+0x19c>
jmp    12c8 <main+0x178>
mov    -0x40(%rbp),%eax
sub    $0x81,%eax
jne    12fd <main+0x1ad>
jmp    12db <main+0x18b>
lea    0xdcc(%rip),%rdi
call   1040 <printf@plt>
jmp    1307 <main+0x1b7>
lea    0xddf(%rip),%rdi
call   1040 <printf@plt>
jmp    1307 <main+0x1b7>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xde8(%rip),%rdi
call   1040 <printf@plt>
movw   $0x102,-0x28(%rbp)
lea    -0x28(%rbp),%rdi
lea    0xdf4(%rip),%rsi
mov    $0x2,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
jne    1349 <main+0x1f9>
lea    0xddc(%rip),%rdi
call   1040 <printf@plt>
jmp    1388 <main+0x238>
lea    -0x28(%rbp),%rdi
lea    0xde9(%rip),%rsi
mov    $0x2,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
jne    1379 <main+0x229>
lea    0xdd1(%rip),%rdi
call   1040 <printf@plt>
jmp    1383 <main+0x233>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    1388 <main+0x238>
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
lea    0xe39(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%rax
mov    (%rbx,%rax,1),%al
cmp    $0x83,%al
je     121c <main+0xcc>
cmp    $0x82,%al
jne    12b5 <main+0x165>
lea    0xe51(%rip),%rdi
jmp    1223 <main+0xd3>
lea    0xf14(%rip),%rdi
call   1030 <puts@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%rax
mov    (%rbx,%rax,1),%al
cmp    $0x81,%al
je     1246 <main+0xf6>
cmp    $0x80,%al
jne    12b5 <main+0x165>
lea    0xe4a(%rip),%rdi
jmp    124d <main+0xfd>
lea    0xec7(%rip),%rdi
call   1030 <puts@plt>
lea    0xe58(%rip),%rdi
call   1030 <puts@plt>
movw   $0x102,-0x20(%rbp)
lea    0xdbd(%rip),%rsi
lea    -0x20(%rbp),%rdi
mov    $0x2,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
je     12a1 <main+0x151>
lea    0xda6(%rip),%rsi
lea    -0x20(%rbp),%rdi
mov    $0x2,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
jne    12b5 <main+0x165>
lea    0xe33(%rip),%rdi
jmp    12a8 <main+0x158>
lea    0xe4b(%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"
@.str.2 = [62 x i8] c"/* BINARY FILE - DO NOT EDIT\0A * Produced by mk_elfconfig.c*/\0A\00"
@EI_CLASS = global i64 0
@.str.3 = [36 x i8] c"#define KERNEL_ELFCLASS ELFCLASS32\0A\00"
@.str.4 = [36 x i8] c"#define KERNEL_ELFCLASS ELFCLASS64\0A\00"
@EI_DATA = global i64 0
@.str.5 = [36 x i8] c"#define KERNEL_ELFDATA ELFDATA2LSB\0A\00"
@.str.6 = [36 x i8] c"#define KERNEL_ELFDATA ELFDATA2MSB\0A\00"
@.str.7 = [34 x i8] c"#define HOST_ELFCLASS ELFCLASS64\0A\00"
@.str.8 = [3 x i8] c"\01\02\00"
@.str.9 = [34 x i8] c"#define HOST_ELFDATA ELFDATA2MSB\0A\00"
@.str.10 = [3 x i8] c"\02\01\00"
@.str.11 = [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 %73
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 %73
30: ; preds = %22
%31 = call i32 @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.2, i64 0, i64 0))
%32 = load i64, i64* @EI_CLASS
%33 = getelementptr inbounds i8, i8* %13, i64 %32
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
switch i32 %35, label %40 [
i32 131, label %36
i32 130, label %38
]
36: ; preds = %30
%37 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
br label %42
38: ; preds = %30
%39 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
br label %42
40: ; preds = %30
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %38, %36
%43 = load i64, i64* @EI_DATA
%44 = getelementptr inbounds i8, i8* %13, i64 %43
%45 = load i8, i8* %44
%46 = zext i8 %45 to i32
switch i32 %46, label %51 [
i32 129, label %47
i32 128, label %49
]
47: ; preds = %42
%48 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
br label %53
49: ; preds = %42
%50 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0))
br label %53
51: ; preds = %42
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %49, %47
%54 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0))
%55 = bitcast %union.anon* %8 to i16*
store i16 258, i16* %55
%56 = bitcast %union.anon* %8 to [2 x i8]*
%57 = getelementptr inbounds [2 x i8], [2 x i8]* %56, i64 0, i64 0
%58 = call i64 @memcmp(i8* %57, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i32 2)
%59 = icmp eq i64 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %53
%61 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.9, i64 0, i64 0))
br label %72
62: ; preds = %53
%63 = bitcast %union.anon* %8 to [2 x i8]*
%64 = getelementptr inbounds [2 x i8], [2 x i8]* %63, i64 0, i64 0
%65 = call i64 @memcmp(i8* %64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i64 0, i64 0), i32 2)
%66 = icmp eq i64 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %62
%68 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.11, i64 0, i64 0))
br label %71
69: ; preds = %62
%70 = call i32 @exit(i32 1)
unreachable
71: ; preds = %67
br label %72
72: ; preds = %71, %60
store i32 0, i32* %3
store i32 1, i32* %9
br label %73
73: ; preds = %72, %27, %19
%74 = load i8*, i8** %6
%75 = load i32, i32* %3
ret i32 %75
}

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