#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 */
typedef int uint64_t;
typedef scalar_t__ ssize_t;
typedef scalar_t__ off_t;
typedef int /*<<< orphan*/ ident;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int get_load_addr_32(int);
int get_load_addr_64(int);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ read(int, unsigned char *, int);
int /*<<< orphan*/ warnx(char *, ...);
uint64_t parse_header(int fd) {
  unsigned char ident[16];
  ssize_t s;
  off_t o;
  uint64_t addr;
  s = read(fd, ident, sizeof(ident));
  if (s != (ssize_t)sizeof(ident)) {
    warnx("cannot read ELF ident");
    return -1;
  }
  if (ident[0] != 0x7f || ident[1] != 'E' || ident[2] != 'L' ||
      ident[3] != 'F') {
    warnx("not an ELF file");
    return -1;
  }
  o = lseek(fd, 0, SEEK_SET);
  if (o != 0) {
    warnx("cannot reset to ELF header start");
    return -1;
  }
  switch (ident[4]) {
  case 1:
    addr = get_load_addr_32(fd);
    break;
  case 2:
    addr = get_load_addr_64(fd);
    break;
  default:
    warnx("unknown ELF class %d", ident[4]);
    addr = -1;
  }
  return addr;
}


x86 assembly (clang, -O0):
<parse_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1070 <read@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x10,-0x28(%rbp)
je     1195 <parse_header+0x45>
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1050 <warnx@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1284 <parse_header+0x134>
movzbl -0x20(%rbp),%eax
cmp    $0x7f,%eax
jne    11c9 <parse_header+0x79>
movzbl -0x1f(%rbp),%eax
cmp    $0x45,%eax
jne    11c9 <parse_header+0x79>
movzbl -0x1e(%rbp),%eax
cmp    $0x4c,%eax
jne    11c9 <parse_header+0x79>
movzbl -0x1d(%rbp),%eax
cmp    $0x46,%eax
je     11e3 <parse_header+0x93>
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1050 <warnx@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1284 <parse_header+0x134>
mov    -0x8(%rbp),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     121f <parse_header+0xcf>
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1050 <warnx@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1284 <parse_header+0x134>
movzbl -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
sub    $0x1,%eax
je     1245 <parse_header+0xf5>
jmp    1234 <parse_header+0xe4>
mov    -0x38(%rbp),%eax
sub    $0x2,%eax
je     1255 <parse_header+0x105>
jmp    1265 <parse_header+0x115>
mov    -0x8(%rbp),%edi
call   1030 <get_load_addr_32@plt>
mov    %eax,-0x34(%rbp)
jmp    127e <parse_header+0x12e>
mov    -0x8(%rbp),%edi
call   1040 <get_load_addr_64@plt>
mov    %eax,-0x34(%rbp)
jmp    127e <parse_header+0x12e>
movzbl -0x1c(%rbp),%esi
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1050 <warnx@plt>
movl   $0xffffffff,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_header>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    %rsp,%rsi
mov    $0x10,%edx
call   1070 <read@plt>
cmp    $0x10,%rax
jne    119a <parse_header+0x4a>
movdqa (%rsp),%xmm0
movd   %xmm0,%eax
cmp    $0x464c457f,%eax
jne    11a3 <parse_header+0x53>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
test   %rax,%rax
je     11bc <parse_header+0x6c>
lea    0xe8e(%rip),%rdi
jmp    11aa <parse_header+0x5a>
lea    0xe5f(%rip),%rdi
jmp    11aa <parse_header+0x5a>
lea    0xe6c(%rip),%rdi
xor    %eax,%eax
call   1050 <warnx@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret
movzbl 0x4(%rsp),%esi
cmp    $0x2,%esi
je     11d8 <parse_header+0x88>
cmp    $0x1,%sil
jne    11e4 <parse_header+0x94>
mov    %ebx,%edi
add    $0x10,%rsp
pop    %rbx
jmp    1030 <get_load_addr_32@plt>
mov    %ebx,%edi
add    $0x10,%rsp
pop    %rbx
jmp    1040 <get_load_addr_64@plt>
lea    0xe5c(%rip),%rdi
xor    %eax,%eax
call   1050 <warnx@plt>
jmp    11b1 <parse_header+0x61>

LLVM IR (-O0):
@.str = [22 x i8] c"cannot read ELF ident\00"
@.str.1 = [16 x i8] c"not an ELF file\00"
@SEEK_SET = global i32 0
@.str.2 = [33 x i8] c"cannot reset to ELF header start\00"
@.str.3 = [21 x i8] c"unknown ELF class %d\00"
define i32 @parse_header(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [16 x i8]
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
store i32 %0, i32* %3
%8 = load i32, i32* %3
%9 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%10 = call i64 @read(i32 %8, i8* %9, i32 16)
store i64 %10, i64* %5
%11 = load i64, i64* %5
%12 = icmp ne i64 %11, 16
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %62
15: ; preds = %1
%16 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%17 = load i8, i8* %16
%18 = zext i8 %17 to i32
%19 = icmp ne i32 %18, 127
br i1 %19, label %35, label %20
20: ; preds = %15
%21 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 1
%22 = load i8, i8* %21
%23 = zext i8 %22 to i32
%24 = icmp ne i32 %23, 69
br i1 %24, label %35, label %25
25: ; preds = %20
%26 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 2
%27 = load i8, i8* %26
%28 = zext i8 %27 to i32
%29 = icmp ne i32 %28, 76
br i1 %29, label %35, label %30
30: ; preds = %25
%31 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 3
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = icmp ne i32 %33, 70
br i1 %34, label %35, label %37
35: ; preds = %30, %25, %20, %15
%36 = call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %62
37: ; preds = %30
%38 = load i32, i32* %3
%39 = load i32, i32* @SEEK_SET
%40 = call i64 @lseek(i32 %38, i32 0, i32 %39)
store i64 %40, i64* %6
%41 = load i64, i64* %6
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %37
%44 = call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %62
45: ; preds = %37
%46 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 4
%47 = load i8, i8* %46
%48 = zext i8 %47 to i32
switch i32 %48, label %55 [
i32 1, label %49
i32 2, label %52
]
49: ; preds = %45
%50 = load i32, i32* %3
%51 = call i32 @get_load_addr_32(i32 %50)
store i32 %51, i32* %7
br label %60
52: ; preds = %45
%53 = load i32, i32* %3
%54 = call i32 @get_load_addr_64(i32 %53)
store i32 %54, i32* %7
br label %60
55: ; preds = %45
%56 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 4
%57 = load i8, i8* %56
%58 = zext i8 %57 to i32
%59 = call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %58)
store i32 -1, i32* %7
br label %60
60: ; preds = %55, %52, %49
%61 = load i32, i32* %7
store i32 %61, i32* %2
br label %62
62: ; preds = %60, %43, %35, %13
%63 = load i32, i32* %2
ret i32 %63
}

LLVM IR (-O2):
@.str = [22 x i8] c"cannot read ELF ident\00"
@.str.1 = [16 x i8] c"not an ELF file\00"
@SEEK_SET = global i32 0
@.str.2 = [33 x i8] c"cannot reset to ELF header start\00"
@.str.3 = [21 x i8] c"unknown ELF class %d\00"
define i32 @parse_header(i32 %0) {
%2 = alloca [16 x i8]
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%4 = call i64 @read(i32 %0, i8* %3, i32 16)
%5 = icmp eq i64 %4, 16
br i1 %5, label %8, label %6
6: ; preds = %1
%7 = tail call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %32
8: ; preds = %1
%9 = bitcast [16 x i8]* %2 to <4 x i8>*
%10 = load <4 x i8>, <4 x i8>* %9
%11 = freeze <4 x i8> %10
%12 = bitcast <4 x i8> %11 to i32
%13 = icmp eq i32 %12, 1179403647
br i1 %13, label %16, label %14
14: ; preds = %8
%15 = tail call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %32
16: ; preds = %8
%17 = load i32, i32* @SEEK_SET
%18 = tail call i64 @lseek(i32 %0, i32 0, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %16
%21 = tail call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %32
22: ; preds = %16
%23 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 4
%24 = load i8, i8* %23
switch i8 %24, label %29 [
i8 1, label %25
i8 2, label %27
]
25: ; preds = %22
%26 = tail call i32 @get_load_addr_32(i32 %0)
br label %32
27: ; preds = %22
%28 = tail call i32 @get_load_addr_64(i32 %0)
br label %32
29: ; preds = %22
%30 = zext i8 %24 to i32
%31 = tail call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %25, %27, %29, %20, %14, %6
%33 = phi i32 [ -1, %6 ], [ -1, %14 ], [ -1, %20 ], [ -1, %29 ], [ %28, %27 ], [ %26, %25 ]
ret i32 %33
}