#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 */
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  unsigned char *e_ident;
  int e_type;
  unsigned char const e_version;
  int e_ehsize;
  scalar_t__ e_phoff;
  int e_phentsize;
  scalar_t__ e_shoff;
  int e_shentsize;
};
typedef int /*<<< orphan*/ FILE;
typedef int /*<<< orphan*/ Elf64_Shdr;
typedef int /*<<< orphan*/ Elf64_Phdr;
typedef TYPE_1__ Elf64_Ehdr;
typedef int /*<<< orphan*/ Elf32_Shdr;
typedef int /*<<< orphan*/ Elf32_Phdr;
typedef int /*<<< orphan*/ Elf32_Ehdr;

/* Variables and functions */
size_t EI_CLASS;
size_t EI_DATA;
size_t EI_VERSION;
unsigned char const ELFCLASS32;
unsigned char const ELFCLASS64;
unsigned char const ELFDATA2LSB;
unsigned char const ELFDATA2MSB;
int /*<<< orphan*/ ELFMAG;
#define ET_CORE 131
#define ET_DYN 130
#define ET_EXEC 129
#define ET_REL 128
unsigned char const EV_CURRENT;
int /*<<< orphan*/ SELFMAG;
scalar_t__ elfrw_read_Ehdr(int /*<<< orphan*/ *, TYPE_1__ *);
scalar_t__ memcmp(unsigned char const *, int /*<<< orphan*/,
                  int /*<<< orphan*/);
int readelfhdr(FILE *fp, Elf64_Ehdr *elffhdr) {
  const unsigned char *id = elffhdr->e_ident;
  if (elfrw_read_Ehdr(fp, elffhdr) < 0) {
    return -1;
  }
  if (memcmp(id, ELFMAG, SELFMAG) ||
      (id[EI_CLASS] != ELFCLASS32 && id[EI_CLASS] != ELFCLASS64) ||
      (id[EI_DATA] != ELFDATA2MSB && id[EI_DATA] != ELFDATA2LSB) ||
      (id[EI_VERSION] != EV_CURRENT))
    return -1;
  switch (elffhdr->e_type) {
  case ET_EXEC:
  case ET_DYN:
    break;
  case ET_CORE:
  case ET_REL:
  default:
    return -1;
  }
  if (elffhdr->e_version != EV_CURRENT)
    return -1;
  if (elffhdr->e_ehsize != sizeof(Elf32_Ehdr) &&
      elffhdr->e_ehsize != sizeof(Elf64_Ehdr))
    return -1;
  if (elffhdr->e_phoff != 0 && elffhdr->e_phentsize != sizeof(Elf32_Phdr) &&
      elffhdr->e_phentsize != sizeof(Elf64_Phdr))
    return -1;
  if (elffhdr->e_shoff != 0 && elffhdr->e_shentsize != sizeof(Elf32_Shdr) &&
      elffhdr->e_shentsize != sizeof(Elf64_Shdr))
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<readelfhdr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <elfrw_read_Ehdr@plt>
cmp    $0x0,%rax
jge    115e <readelfhdr+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1377 <readelfhdr+0x257>
mov    -0x20(%rbp),%rdi
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
call   1030 <memcmp@plt>
cmp    $0x0,%rax
jne    1237 <readelfhdr+0x117>
mov    -0x20(%rbp),%rax
mov    0x2e22(%rip),%rcx
mov    (%rcx),%rcx
movzbl (%rax,%rcx,1),%eax
mov    0x2e04(%rip),%rcx
movzbl (%rcx),%ecx
cmp    %ecx,%eax
je     11cb <readelfhdr+0xab>
mov    -0x20(%rbp),%rax
mov    0x2dfe(%rip),%rcx
mov    (%rcx),%rcx
movzbl (%rax,%rcx,1),%eax
mov    0x2e18(%rip),%rcx
movzbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1237 <readelfhdr+0x117>
mov    -0x20(%rbp),%rax
mov    0x2df2(%rip),%rcx
mov    (%rcx),%rcx
movzbl (%rax,%rcx,1),%eax
mov    0x2db4(%rip),%rcx
movzbl (%rcx),%ecx
cmp    %ecx,%eax
je     1213 <readelfhdr+0xf3>
mov    -0x20(%rbp),%rax
mov    0x2dce(%rip),%rcx
mov    (%rcx),%rcx
movzbl (%rax,%rcx,1),%eax
mov    0x2de8(%rip),%rcx
movzbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1237 <readelfhdr+0x117>
mov    -0x20(%rbp),%rax
mov    0x2da2(%rip),%rcx
mov    (%rcx),%rcx
movzbl (%rax,%rcx,1),%eax
mov    0x2da4(%rip),%rcx
movzbl (%rcx),%ecx
cmp    %ecx,%eax
je     1243 <readelfhdr+0x123>
movl   $0xffffffff,-0x4(%rbp)
jmp    1377 <readelfhdr+0x257>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x24(%rbp)
sub    $0x80,%eax
je     128b <readelfhdr+0x16b>
jmp    125d <readelfhdr+0x13d>
mov    -0x24(%rbp),%eax
add    $0xffffff7f,%eax
sub    $0x2,%eax
jb     1286 <readelfhdr+0x166>
jmp    1273 <readelfhdr+0x153>
mov    -0x24(%rbp),%eax
sub    $0x83,%eax
je     128b <readelfhdr+0x16b>
jmp    1290 <readelfhdr+0x170>
jmp    129c <readelfhdr+0x17c>
jmp    1290 <readelfhdr+0x170>
movl   $0xffffffff,-0x4(%rbp)
jmp    1377 <readelfhdr+0x257>
mov    -0x18(%rbp),%rax
movzbl 0xc(%rax),%eax
mov    0x2d25(%rip),%rcx
movzbl (%rcx),%ecx
cmp    %ecx,%eax
je     12c2 <readelfhdr+0x1a2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1377 <readelfhdr+0x257>
mov    -0x18(%rbp),%rax
movslq 0x10(%rax),%rax
cmp    $0x4,%rax
je     12f2 <readelfhdr+0x1d2>
mov    -0x18(%rbp),%rax
movslq 0x10(%rax),%rax
cmp    $0x38,%rax
je     12f2 <readelfhdr+0x1d2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1377 <readelfhdr+0x257>
mov    -0x18(%rbp),%rax
cmpq   $0x0,0x18(%rax)
je     1331 <readelfhdr+0x211>
mov    -0x18(%rbp),%rax
movslq 0x20(%rax),%rax
cmp    $0x4,%rax
je     1331 <readelfhdr+0x211>
mov    -0x18(%rbp),%rax
movslq 0x20(%rax),%rax
cmp    $0x4,%rax
je     1331 <readelfhdr+0x211>
movl   $0xffffffff,-0x4(%rbp)
jmp    1377 <readelfhdr+0x257>
mov    -0x18(%rbp),%rax
cmpq   $0x0,0x28(%rax)
je     1370 <readelfhdr+0x250>
mov    -0x18(%rbp),%rax
movslq 0x30(%rax),%rax
cmp    $0x4,%rax
je     1370 <readelfhdr+0x250>
mov    -0x18(%rbp),%rax
movslq 0x30(%rax),%rax
cmp    $0x4,%rax
je     1370 <readelfhdr+0x250>
movl   $0xffffffff,-0x4(%rbp)
jmp    1377 <readelfhdr+0x257>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readelfhdr>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    (%rsi),%rbp
call   1040 <elfrw_read_Ehdr@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
js     119f <readelfhdr+0x7f>
mov    0x2ea7(%rip),%rax
mov    (%rax),%esi
mov    0x2e76(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
call   1030 <memcmp@plt>
test   %rax,%rax
jne    119f <readelfhdr+0x7f>
mov    0x2e68(%rip),%rax
mov    (%rax),%rax
cmpb   $0x0,0x0(%rbp,%rax,1)
jne    119f <readelfhdr+0x7f>
mov    0x2e6f(%rip),%rax
mov    (%rax),%rax
cmpb   $0x0,0x0(%rbp,%rax,1)
jne    119f <readelfhdr+0x7f>
mov    0x2e56(%rip),%rax
mov    (%rax),%rax
cmpb   $0x0,0x0(%rbp,%rax,1)
jne    119f <readelfhdr+0x7f>
mov    $0xffffff7f,%eax
add    0x8(%rbx),%eax
cmp    $0x1,%eax
ja     119f <readelfhdr+0x7f>
cmpb   $0x0,0xc(%rbx)
je     11a7 <readelfhdr+0x87>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x10(%rbx),%eax
cmp    $0x38,%eax
je     11b4 <readelfhdr+0x94>
cmp    $0x4,%eax
jne    119f <readelfhdr+0x7f>
cmpq   $0x0,0x18(%rbx)
je     11c1 <readelfhdr+0xa1>
cmpl   $0x4,0x20(%rbx)
jne    119f <readelfhdr+0x7f>
cmpq   $0x0,0x28(%rbx)
je     11ce <readelfhdr+0xae>
cmpl   $0x4,0x30(%rbx)
jne    119f <readelfhdr+0x7f>
xor    %r14d,%r14d
jmp    119f <readelfhdr+0x7f>

LLVM IR (-O0):
%struct.TYPE_4__ = type { i8*, i32, i8, i32, i64, i32, i64, i32 }
@ELFMAG = global i32 0
@SELFMAG = global i32 0
@EI_CLASS = global i64 0
@ELFCLASS32 = constant i8 0
@ELFCLASS64 = constant i8 0
@EI_DATA = global i64 0
@ELFDATA2MSB = constant i8 0
@ELFDATA2LSB = constant i8 0
@EI_VERSION = global i64 0
@EV_CURRENT = constant i8 0
define i32 @readelfhdr(i32* %0, %struct.TYPE_4__* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca %struct.TYPE_4__*
%6 = alloca i8*
store i32* %0, i32** %4
store %struct.TYPE_4__* %1, %struct.TYPE_4__** %5
%7 = load %struct.TYPE_4__*, %struct.TYPE_4__** %5
%8 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %7, i32 0, i32 0
%9 = load i8*, i8** %8
store i8* %9, i8** %6
%10 = load i32*, i32** %4
%11 = load %struct.TYPE_4__*, %struct.TYPE_4__** %5
%12 = call i64 @elfrw_read_Ehdr(i32* %10, %struct.TYPE_4__* %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 -1, i32* %3
br label %133
15: ; preds = %2
%16 = load i8*, i8** %6
%17 = load i32, i32* @ELFMAG
%18 = load i32, i32* @SELFMAG
%19 = call i64 @memcmp(i8* %16, i32 %17, i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %66, label %21
21: ; preds = %15
%22 = load i8*, i8** %6
%23 = load i64, i64* @EI_CLASS
%24 = getelementptr inbounds i8, i8* %22, i64 %23
%25 = load i8, i8* %24
%26 = zext i8 %25 to i32
%27 = load i8, i8* @ELFCLASS32
%28 = zext i8 %27 to i32
%29 = icmp ne i32 %26, %28
br i1 %29, label %30, label %39
30: ; preds = %21
%31 = load i8*, i8** %6
%32 = load i64, i64* @EI_CLASS
%33 = getelementptr inbounds i8, i8* %31, i64 %32
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
%36 = load i8, i8* @ELFCLASS64
%37 = zext i8 %36 to i32
%38 = icmp ne i32 %35, %37
br i1 %38, label %66, label %39
39: ; preds = %30, %21
%40 = load i8*, i8** %6
%41 = load i64, i64* @EI_DATA
%42 = getelementptr inbounds i8, i8* %40, i64 %41
%43 = load i8, i8* %42
%44 = zext i8 %43 to i32
%45 = load i8, i8* @ELFDATA2MSB
%46 = zext i8 %45 to i32
%47 = icmp ne i32 %44, %46
br i1 %47, label %48, label %57
48: ; preds = %39
%49 = load i8*, i8** %6
%50 = load i64, i64* @EI_DATA
%51 = getelementptr inbounds i8, i8* %49, i64 %50
%52 = load i8, i8* %51
%53 = zext i8 %52 to i32
%54 = load i8, i8* @ELFDATA2LSB
%55 = zext i8 %54 to i32
%56 = icmp ne i32 %53, %55
br i1 %56, label %66, label %57
57: ; preds = %48, %39
%58 = load i8*, i8** %6
%59 = load i64, i64* @EI_VERSION
%60 = getelementptr inbounds i8, i8* %58, i64 %59
%61 = load i8, i8* %60
%62 = zext i8 %61 to i32
%63 = load i8, i8* @EV_CURRENT
%64 = zext i8 %63 to i32
%65 = icmp ne i32 %62, %64
br i1 %65, label %66, label %67
66: ; preds = %57, %48, %30, %15
store i32 -1, i32* %3
br label %133
67: ; preds = %57
%68 = load %struct.TYPE_4__*, %struct.TYPE_4__** %5
%69 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %68, i32 0, i32 1
%70 = load i32, i32* %69
switch i32 %70, label %73 [
i32 129, label %71
i32 130, label %71
i32 131, label %72
i32 128, label %72
]
71: ; preds = %67, %67
br label %74
72: ; preds = %67, %67
br label %73
73: ; preds = %67, %72
store i32 -1, i32* %3
br label %133
74: ; preds = %71
%75 = load %struct.TYPE_4__*, %struct.TYPE_4__** %5
%76 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %75, i32 0, i32 2
%77 = load i8, i8* %76
%78 = zext i8 %77 to i32
%79 = load i8, i8* @EV_CURRENT
%80 = zext i8 %79 to i32
%81 = icmp ne i32 %78, %80
br i1 %81, label %82, label %83
82: ; preds = %74
store i32 -1, i32* %3
br label %133
83: ; preds = %74
%84 = load %struct.TYPE_4__*, %struct.TYPE_4__** %5
%85 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %84, i32 0, i32 3
%86 = load i32, i32* %85
%87 = sext i32 %86 to i64
%88 = icmp ne i64 %87, 4
br i1 %88, label %89, label %96
89: ; preds = %83
%90 = load %struct.TYPE_4__*, %struct.TYPE_4__** %5
%91 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %90, i32 0, i32 3
%92 = load i32, i32* %91
%93 = sext i32 %92 to i64
%94 = icmp ne i64 %93, 56
br i1 %94, label %95, label %96
95: ; preds = %89
store i32 -1, i32* %3
br label %133
96: ; preds = %89, %83
%97 = load %struct.TYPE_4__*, %struct.TYPE_4__** %5
%98 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %97, i32 0, i32 4
%99 = load i64, i64* %98
%100 = icmp ne i64 %99, 0
br i1 %100, label %101, label %114
101: ; preds = %96
%102 = load %struct.TYPE_4__*, %struct.TYPE_4__** %5
%103 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %102, i32 0, i32 5
%104 = load i32, i32* %103
%105 = sext i32 %104 to i64
%106 = icmp ne i64 %105, 4
br i1 %106, label %107, label %114
107: ; preds = %101
%108 = load %struct.TYPE_4__*, %struct.TYPE_4__** %5
%109 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %108, i32 0, i32 5
%110 = load i32, i32* %109
%111 = sext i32 %110 to i64
%112 = icmp ne i64 %111, 4
br i1 %112, label %113, label %114
113: ; preds = %107
store i32 -1, i32* %3
br label %133
114: ; preds = %107, %101, %96
%115 = load %struct.TYPE_4__*, %struct.TYPE_4__** %5
%116 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %115, i32 0, i32 6
%117 = load i64, i64* %116
%118 = icmp ne i64 %117, 0
br i1 %118, label %119, label %132
119: ; preds = %114
%120 = load %struct.TYPE_4__*, %struct.TYPE_4__** %5
%121 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %120, i32 0, i32 7
%122 = load i32, i32* %121
%123 = sext i32 %122 to i64
%124 = icmp ne i64 %123, 4
br i1 %124, label %125, label %132
125: ; preds = %119
%126 = load %struct.TYPE_4__*, %struct.TYPE_4__** %5
%127 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %126, i32 0, i32 7
%128 = load i32, i32* %127
%129 = sext i32 %128 to i64
%130 = icmp ne i64 %129, 4
br i1 %130, label %131, label %132
131: ; preds = %125
store i32 -1, i32* %3
br label %133
132: ; preds = %125, %119, %114
store i32 0, i32* %3
br label %133
133: ; preds = %132, %131, %113, %95, %82, %73, %66, %14
%134 = load i32, i32* %3
ret i32 %134
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i8*, i32, i8, i32, i64, i32, i64, i32 }
@ELFMAG = global i32 0
@SELFMAG = global i32 0
@EI_CLASS = global i64 0
@ELFCLASS32 = constant i8 0
@ELFCLASS64 = constant i8 0
@EI_DATA = global i64 0
@ELFDATA2MSB = constant i8 0
@ELFDATA2LSB = constant i8 0
@EI_VERSION = global i64 0
@EV_CURRENT = constant i8 0
define i32 @readelfhdr(i32* %0, %struct.TYPE_4__* %1) {
%3 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %1, i64 0, i32 0
%4 = load i8*, i8** %3
%5 = tail call i64 @elfrw_read_Ehdr(i32* %0, %struct.TYPE_4__* %1)
%6 = icmp slt i64 %5, 0
br i1 %6, label %56, label %7
7: ; preds = %2
%8 = load i32, i32* @ELFMAG
%9 = load i32, i32* @SELFMAG
%10 = tail call i64 @memcmp(i8* %4, i32 %8, i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %56
12: ; preds = %7
%13 = load i64, i64* @EI_CLASS
%14 = getelementptr inbounds i8, i8* %4, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 0
br i1 %16, label %17, label %56
17: ; preds = %12
%18 = load i64, i64* @EI_DATA
%19 = getelementptr inbounds i8, i8* %4, i64 %18
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 0
br i1 %21, label %22, label %56
22: ; preds = %17
%23 = load i64, i64* @EI_VERSION
%24 = getelementptr inbounds i8, i8* %4, i64 %23
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 0
br i1 %26, label %27, label %56
27: ; preds = %22
%28 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %1, i64 0, i32 1
%29 = load i32, i32* %28
%30 = add i32 %29, -129
%31 = icmp ult i32 %30, 2
br i1 %31, label %32, label %56
32: ; preds = %27
%33 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %1, i64 0, i32 2
%34 = load i8, i8* %33
%35 = icmp eq i8 %34, 0
br i1 %35, label %36, label %56
36: ; preds = %32
%37 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %1, i64 0, i32 3
%38 = load i32, i32* %37
switch i32 %38, label %56 [
i32 4, label %39
i32 56, label %39
]
39: ; preds = %36, %36
%40 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %1, i64 0, i32 4
%41 = load i64, i64* %40
%42 = icmp eq i64 %41, 0
br i1 %42, label %47, label %43
43: ; preds = %39
%44 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %1, i64 0, i32 5
%45 = load i32, i32* %44
%46 = icmp eq i32 %45, 4
br i1 %46, label %47, label %56
47: ; preds = %43, %39
%48 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %1, i64 0, i32 6
%49 = load i64, i64* %48
%50 = icmp eq i64 %49, 0
br i1 %50, label %55, label %51
51: ; preds = %47
%52 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %1, i64 0, i32 7
%53 = load i32, i32* %52
%54 = icmp eq i32 %53, 4
br i1 %54, label %55, label %56
55: ; preds = %51, %47
br label %56
56: ; preds = %51, %43, %36, %32, %27, %7, %22, %12, %17, %2, %55
%57 = phi i32 [ 0, %55 ], [ -1, %2 ], [ -1, %17 ], [ -1, %12 ], [ -1, %22 ], [ -1, %7 ], [ -1, %27 ], [ -1, %32 ], [ -1, %36 ], [ -1, %43 ], [ -1, %51 ]
ret i32 %57
}