#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_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int *e_ident;
  int e_phoff;
  int e_phentsize;
  unsigned long e_entry;
  int /*<<< orphan*/ e_phnum;
};
typedef int /*<<< orphan*/ Elf64_Phdr;
typedef TYPE_1__ Elf64_Ehdr;

/* Variables and functions */
int /*<<< orphan*/ load_programs(unsigned char *, int /*<<< orphan*/ *,
                                 int /*<<< orphan*/);
unsigned long elfboot_main(unsigned char *image) {
  Elf64_Ehdr *hdr;
  hdr = (Elf64_Ehdr *)image;
  if (hdr->e_ident[0] != 0x7f || hdr->e_ident[1] != 'E' ||
      hdr->e_ident[2] != 'L' || hdr->e_ident[3] != 'F') {
    return 0;
  }
  if (!hdr->e_phoff || hdr->e_phentsize != sizeof(Elf64_Phdr)) {
    return 0;
  }
  load_programs(image, (Elf64_Phdr *)(image + hdr->e_phoff), hdr->e_phnum);
  return hdr->e_entry;
}


x86 assembly (clang, -O0):
<elfboot_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
cmpl   $0x7f,(%rax)
jne    1167 <elfboot_main+0x57>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
cmpl   $0x45,0x4(%rax)
jne    1167 <elfboot_main+0x57>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
cmpl   $0x4c,0x8(%rax)
jne    1167 <elfboot_main+0x57>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
cmpl   $0x46,0xc(%rax)
je     1174 <elfboot_main+0x64>
movq   $0x0,-0x8(%rbp)
jmp    11cc <elfboot_main+0xbc>
mov    -0x18(%rbp),%rax
cmpl   $0x0,0x8(%rax)
je     1194 <elfboot_main+0x84>
mov    -0x18(%rbp),%rax
movslq 0xc(%rax),%rax
cmp    $0x4,%rax
je     11a1 <elfboot_main+0x91>
movq   $0x0,-0x8(%rbp)
jmp    11cc <elfboot_main+0xbc>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
movslq 0x8(%rax),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rax
mov    0x18(%rax),%edx
call   1030 <load_programs@plt>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<elfboot_main>:
push   %rbx
mov    (%rdi),%rax
cmpl   $0x7f,(%rax)
jne    1151 <elfboot_main+0x41>
cmpl   $0x45,0x4(%rax)
jne    1151 <elfboot_main+0x41>
cmpl   $0x4c,0x8(%rax)
jne    1151 <elfboot_main+0x41>
cmpl   $0x46,0xc(%rax)
jne    1151 <elfboot_main+0x41>
mov    %rdi,%rbx
movslq 0x8(%rdi),%rsi
test   %rsi,%rsi
je     1151 <elfboot_main+0x41>
cmpl   $0x4,0xc(%rbx)
jne    1151 <elfboot_main+0x41>
add    %rbx,%rsi
mov    0x18(%rbx),%edx
mov    %rbx,%rdi
call   1030 <load_programs@plt>
mov    0x10(%rbx),%rax
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.TYPE_2__ = type { i32*, i32, i32, i64, i32 }
define i64 @elfboot_main(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca %struct.TYPE_2__*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = bitcast i8* %5 to %struct.TYPE_2__*
store %struct.TYPE_2__* %6, %struct.TYPE_2__** %4
%7 = load %struct.TYPE_2__*, %struct.TYPE_2__** %4
%8 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %7, i32 0, i32 0
%9 = load i32*, i32** %8
%10 = getelementptr inbounds i32, i32* %9, i64 0
%11 = load i32, i32* %10
%12 = icmp ne i32 %11, 127
br i1 %12, label %34, label %13
13: ; preds = %1
%14 = load %struct.TYPE_2__*, %struct.TYPE_2__** %4
%15 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %14, i32 0, i32 0
%16 = load i32*, i32** %15
%17 = getelementptr inbounds i32, i32* %16, i64 1
%18 = load i32, i32* %17
%19 = icmp ne i32 %18, 69
br i1 %19, label %34, label %20
20: ; preds = %13
%21 = load %struct.TYPE_2__*, %struct.TYPE_2__** %4
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
%23 = load i32*, i32** %22
%24 = getelementptr inbounds i32, i32* %23, i64 2
%25 = load i32, i32* %24
%26 = icmp ne i32 %25, 76
br i1 %26, label %34, label %27
27: ; preds = %20
%28 = load %struct.TYPE_2__*, %struct.TYPE_2__** %4
%29 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %28, i32 0, i32 0
%30 = load i32*, i32** %29
%31 = getelementptr inbounds i32, i32* %30, i64 3
%32 = load i32, i32* %31
%33 = icmp ne i32 %32, 70
br i1 %33, label %34, label %35
34: ; preds = %27, %20, %13, %1
store i64 0, i64* %2
br label %63
35: ; preds = %27
%36 = load %struct.TYPE_2__*, %struct.TYPE_2__** %4
%37 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %36, i32 0, i32 1
%38 = load i32, i32* %37
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %35
%41 = load %struct.TYPE_2__*, %struct.TYPE_2__** %4
%42 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %41, i32 0, i32 2
%43 = load i32, i32* %42
%44 = sext i32 %43 to i64
%45 = icmp ne i64 %44, 4
br i1 %45, label %46, label %47
46: ; preds = %40, %35
store i64 0, i64* %2
br label %63
47: ; preds = %40
%48 = load i8*, i8** %3
%49 = load i8*, i8** %3
%50 = load %struct.TYPE_2__*, %struct.TYPE_2__** %4
%51 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %50, i32 0, i32 1
%52 = load i32, i32* %51
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %49, i64 %53
%55 = bitcast i8* %54 to i32*
%56 = load %struct.TYPE_2__*, %struct.TYPE_2__** %4
%57 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %56, i32 0, i32 4
%58 = load i32, i32* %57
%59 = call i32 @load_programs(i8* %48, i32* %55, i32 %58)
%60 = load %struct.TYPE_2__*, %struct.TYPE_2__** %4
%61 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %60, i32 0, i32 3
%62 = load i64, i64* %61
store i64 %62, i64* %2
br label %63
63: ; preds = %47, %46, %34
%64 = load i64, i64* %2
ret i64 %64
}

LLVM IR (-O2):
define i64 @elfboot_main(i8* %0) {
%2 = bitcast i8* %0 to i32**
%3 = load i32*, i32** %2
%4 = load i32, i32* %3
%5 = icmp eq i32 %4, 127
br i1 %5, label %6, label %39
6: ; preds = %1
%7 = getelementptr inbounds i32, i32* %3, i64 1
%8 = load i32, i32* %7
%9 = icmp eq i32 %8, 69
br i1 %9, label %10, label %39
10: ; preds = %6
%11 = getelementptr inbounds i32, i32* %3, i64 2
%12 = load i32, i32* %11
%13 = icmp eq i32 %12, 76
br i1 %13, label %14, label %39
14: ; preds = %10
%15 = getelementptr inbounds i32, i32* %3, i64 3
%16 = load i32, i32* %15
%17 = icmp eq i32 %16, 70
br i1 %17, label %18, label %39
18: ; preds = %14
%19 = getelementptr inbounds i8, i8* %0, i64 8
%20 = bitcast i8* %19 to i32*
%21 = load i32, i32* %20
%22 = icmp eq i32 %21, 0
br i1 %22, label %39, label %23
23: ; preds = %18
%24 = getelementptr inbounds i8, i8* %0, i64 12
%25 = bitcast i8* %24 to i32*
%26 = load i32, i32* %25
%27 = icmp eq i32 %26, 4
br i1 %27, label %28, label %39
28: ; preds = %23
%29 = sext i32 %21 to i64
%30 = getelementptr inbounds i8, i8* %0, i64 %29
%31 = bitcast i8* %30 to i32*
%32 = getelementptr inbounds i8, i8* %0, i64 24
%33 = bitcast i8* %32 to i32*
%34 = load i32, i32* %33
%35 = tail call i32 @load_programs(i8* %0, i32* %31, i32 %34)
%36 = getelementptr inbounds i8, i8* %0, i64 16
%37 = bitcast i8* %36 to i64*
%38 = load i64, i64* %37
br label %39
39: ; preds = %18, %23, %1, %6, %10, %14, %28
%40 = phi i64 [ %38, %28 ], [ 0, %14 ], [ 0, %10 ], [ 0, %6 ], [ 0, %1 ], [ 0, %23 ], [ 0, %18 ]
ret i64 %40
}