#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_6__ TYPE_2__;
typedef struct TYPE_5__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ program_header;
struct TYPE_6__ {
  int /*<<< orphan*/ e_phoff;
};
struct TYPE_5__ {
  int p_type;
};
typedef int /*<<< orphan*/ FILE;
typedef TYPE_1__ Elf64_Phdr;
typedef TYPE_2__ Elf64_Ehdr;

/* Variables and functions */
#define PT_DYNAMIC 144
#define PT_GNU_EH_FRAME 143
#define PT_GNU_RELRO 142
#define PT_GNU_STACK 141
#define PT_HIOS 140
#define PT_HIPROC 139
#define PT_INTERP 138
#define PT_LOAD 137
#define PT_LOPROC 136
#define PT_NOTE 135
#define PT_NULL 134
#define PT_NUM 133
#define PT_PHDR 132
#define PT_SHLIB 131
#define PT_SUNWBSS 130
#define PT_SUNWSTACK 129
#define PT_TLS 128
int /*<<< orphan*/ fread(TYPE_1__ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
void parse_program_headers(FILE *fp, Elf64_Ehdr header) {
  printf("====================================================================="
         "===========\n");
  Elf64_Phdr program_header;
  fseek(fp, header.e_phoff, 0);
  fread(&program_header, sizeof(program_header), 1, fp);
  printf("Program type: ");
  switch (program_header.p_type) {
  case PT_NULL:
    printf("Unused\n");
    break;
  case PT_LOAD:
    printf("Loadable program segment\n");
    break;
  case PT_DYNAMIC:
    printf("Dynamic linking information\n");
    break;
  case PT_INTERP:
    printf("Program interpreter\n");
    break;
  case PT_NOTE:
    printf("Auxiliary information\n");
    break;
  case PT_SHLIB:
    printf("Reserved\n");
    break;
  case PT_PHDR:
    printf("Entry for the header table\n");
    break;
  case PT_TLS:
    printf("Thread-local storage segment\n");
    break;
  case PT_NUM:
    printf("Number of defined types\n");
    break;
  case PT_GNU_EH_FRAME:
    printf("GCC .eh_frame_hdr segment");
    break;
  case PT_GNU_STACK:
    printf("Indicates stack executability");
    break;
  case PT_GNU_RELRO:
    printf("Read-only after relocation");
    break;
  case PT_SUNWBSS:
    printf("Sun Specific segment");
    break;
  case PT_SUNWSTACK:
    printf("Stack segment");
    break;
  case PT_HIOS:
    printf("End of OS-specific");
    break;
  case PT_LOPROC:
    printf("Start of processor-specific");
    break;
  case PT_HIPROC:
    printf("End of processor-specific");
    break;
  }
}


x86 assembly (clang, -O0):
<parse_program_headers>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %esi,-0x8(%rbp)
mov    %rdi,-0x10(%rbp)
lea    0xefe(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%esi
xor    %edx,%edx
call   1050 <fseek@plt>
mov    -0x10(%rbp),%rcx
lea    -0x18(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
lea    0xf1f(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0x20(%rbp)
sub    $0x10,%eax
ja     12c1 <parse_program_headers+0x191>
mov    -0x20(%rbp),%rax
lea    0xe64(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xef9(%rip),%rdi
call   1040 <printf@plt>
jmp    12c1 <parse_program_headers+0x191>
lea    0xef0(%rip),%rdi
call   1040 <printf@plt>
jmp    12c1 <parse_program_headers+0x191>
lea    0xef9(%rip),%rdi
call   1040 <printf@plt>
jmp    12c1 <parse_program_headers+0x191>
lea    0xf05(%rip),%rdi
call   1040 <printf@plt>
jmp    12c1 <parse_program_headers+0x191>
lea    0xf09(%rip),%rdi
call   1040 <printf@plt>
jmp    12c1 <parse_program_headers+0x191>
lea    0xf0f(%rip),%rdi
call   1040 <printf@plt>
jmp    12c1 <parse_program_headers+0x191>
lea    0xf08(%rip),%rdi
call   1040 <printf@plt>
jmp    12c1 <parse_program_headers+0x191>
lea    0xf13(%rip),%rdi
call   1040 <printf@plt>
jmp    12c1 <parse_program_headers+0x191>
lea    0xf20(%rip),%rdi
call   1040 <printf@plt>
jmp    12c1 <parse_program_headers+0x191>
lea    0xf28(%rip),%rdi
call   1040 <printf@plt>
jmp    12c1 <parse_program_headers+0x191>
lea    0xf31(%rip),%rdi
call   1040 <printf@plt>
jmp    12c1 <parse_program_headers+0x191>
lea    0xf3e(%rip),%rdi
call   1040 <printf@plt>
jmp    12c1 <parse_program_headers+0x191>
lea    0xf48(%rip),%rdi
call   1040 <printf@plt>
jmp    12c1 <parse_program_headers+0x191>
lea    0xf4c(%rip),%rdi
call   1040 <printf@plt>
jmp    12c1 <parse_program_headers+0x191>
lea    0xf49(%rip),%rdi
call   1040 <printf@plt>
jmp    12c1 <parse_program_headers+0x191>
lea    0xf4b(%rip),%rdi
call   1040 <printf@plt>
jmp    12c1 <parse_program_headers+0x191>
lea    0xf56(%rip),%rdi
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_program_headers>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0xfc3(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %edx,%edx
call   1060 <fseek@plt>
mov    %rsp,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
lea    0xec8(%rip),%rdi
call   1050 <printf@plt>
mov    (%rsp),%eax
add    $0xffffff80,%eax
cmp    $0x10,%eax
ja     11a8 <parse_program_headers+0x68>
lea    0xe6d(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xfd8(%rip),%rdi
jmp    1246 <parse_program_headers+0x106>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xf05(%rip),%rdi
jmp    1234 <parse_program_headers+0xf4>
lea    0xee7(%rip),%rdi
jmp    1234 <parse_program_headers+0xf4>
lea    0xfeb(%rip),%rdi
jmp    1246 <parse_program_headers+0x106>
lea    0xfc7(%rip),%rdi
jmp    1246 <parse_program_headers+0x106>
lea    0xf89(%rip),%rdi
jmp    1246 <parse_program_headers+0x106>
lea    0x1038(%rip),%rdi
jmp    1246 <parse_program_headers+0x106>
lea    0xfd0(%rip),%rdi
jmp    1246 <parse_program_headers+0x106>
lea    0xee7(%rip),%rdi
jmp    1234 <parse_program_headers+0xf4>
lea    0x1004(%rip),%rdi
jmp    1246 <parse_program_headers+0x106>
lea    0xfcb(%rip),%rdi
jmp    1246 <parse_program_headers+0x106>
lea    0xee8(%rip),%rdi
jmp    1234 <parse_program_headers+0xf4>
lea    0xeb0(%rip),%rdi
jmp    1234 <parse_program_headers+0xf4>
lea    0xe4b(%rip),%rdi
jmp    1234 <parse_program_headers+0xf4>
lea    0xe60(%rip),%rdi
jmp    1234 <parse_program_headers+0xf4>
lea    0xe1f(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <printf@plt>
lea    0xfa0(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <puts@plt>

LLVM IR (-O0):
%struct.TYPE_6__ = type { i32 }
%struct.TYPE_5__ = type { i32 }
@.str = [82 x i8] c"================================================================================\0A\00"
@.str.1 = [15 x i8] c"Program type: \00"
@.str.2 = [8 x i8] c"Unused\0A\00"
@.str.3 = [26 x i8] c"Loadable program segment\0A\00"
@.str.4 = [29 x i8] c"Dynamic linking information\0A\00"
@.str.5 = [21 x i8] c"Program interpreter\0A\00"
@.str.6 = [23 x i8] c"Auxiliary information\0A\00"
@.str.7 = [10 x i8] c"Reserved\0A\00"
@.str.8 = [28 x i8] c"Entry for the header table\0A\00"
@.str.9 = [30 x i8] c"Thread-local storage segment\0A\00"
@.str.10 = [25 x i8] c"Number of defined types\0A\00"
@.str.11 = [26 x i8] c"GCC .eh_frame_hdr segment\00"
@.str.12 = [30 x i8] c"Indicates stack executability\00"
@.str.13 = [27 x i8] c"Read-only after relocation\00"
@.str.14 = [21 x i8] c"Sun Specific segment\00"
@.str.15 = [14 x i8] c"Stack segment\00"
@.str.16 = [19 x i8] c"End of OS-specific\00"
@.str.17 = [28 x i8] c"Start of processor-specific\00"
@.str.18 = [26 x i8] c"End of processor-specific\00"
define void @parse_program_headers(i32* %0, i32 %1) {
%3 = alloca %struct.TYPE_6__
%4 = alloca i32*
%5 = alloca %struct.TYPE_5__
%6 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %3, i32 0, i32 0
store i32 %1, i32* %6
store i32* %0, i32** %4
%7 = call i32 @printf(i8* getelementptr inbounds ([82 x i8], [82 x i8]* @.str, i64 0, i64 0))
%8 = load i32*, i32** %4
%9 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %3, i32 0, i32 0
%10 = load i32, i32* %9
%11 = call i32 @fseek(i32* %8, i32 %10, i32 0)
%12 = load i32*, i32** %4
%13 = call i32 @fread(%struct.TYPE_5__* %5, i32 4, i32 1, i32* %12)
%14 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%15 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %5, i32 0, i32 0
%16 = load i32, i32* %15
switch i32 %16, label %51 [
i32 134, label %17
i32 137, label %19
i32 144, label %21
i32 138, label %23
i32 135, label %25
i32 131, label %27
i32 132, label %29
i32 128, label %31
i32 133, label %33
i32 143, label %35
i32 141, label %37
i32 142, label %39
i32 130, label %41
i32 129, label %43
i32 140, label %45
i32 136, label %47
i32 139, label %49
]
17: ; preds = %2
%18 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %51
19: ; preds = %2
%20 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %51
21: ; preds = %2
%22 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %51
23: ; preds = %2
%24 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %51
25: ; preds = %2
%26 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
br label %51
27: ; preds = %2
%28 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
br label %51
29: ; preds = %2
%30 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0))
br label %51
31: ; preds = %2
%32 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.9, i64 0, i64 0))
br label %51
33: ; preds = %2
%34 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.10, i64 0, i64 0))
br label %51
35: ; preds = %2
%36 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.11, i64 0, i64 0))
br label %51
37: ; preds = %2
%38 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.12, i64 0, i64 0))
br label %51
39: ; preds = %2
%40 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.13, i64 0, i64 0))
br label %51
41: ; preds = %2
%42 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.14, i64 0, i64 0))
br label %51
43: ; preds = %2
%44 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.15, i64 0, i64 0))
br label %51
45: ; preds = %2
%46 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.16, i64 0, i64 0))
br label %51
47: ; preds = %2
%48 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.17, i64 0, i64 0))
br label %51
49: ; preds = %2
%50 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.18, i64 0, i64 0))
br label %51
51: ; preds = %2, %49, %47, %45, %43, %41, %39, %37, %35, %33, %31, %29, %27, %25, %23, %21, %19, %17
ret void
}

LLVM IR (-O2):
%struct.TYPE_5__ = type { i32 }
@.str.1 = [15 x i8] c"Program type: \00"
@.str.11 = [26 x i8] c"GCC .eh_frame_hdr segment\00"
@.str.12 = [30 x i8] c"Indicates stack executability\00"
@.str.13 = [27 x i8] c"Read-only after relocation\00"
@.str.14 = [21 x i8] c"Sun Specific segment\00"
@.str.15 = [14 x i8] c"Stack segment\00"
@.str.16 = [19 x i8] c"End of OS-specific\00"
@.str.17 = [28 x i8] c"Start of processor-specific\00"
@.str.18 = [26 x i8] c"End of processor-specific\00"
@str = [81 x i8] c"================================================================================\00"
@str.19 = [24 x i8] c"Number of defined types\00"
@str.20 = [29 x i8] c"Thread-local storage segment\00"
@str.21 = [27 x i8] c"Entry for the header table\00"
@str.22 = [9 x i8] c"Reserved\00"
@str.23 = [22 x i8] c"Auxiliary information\00"
@str.24 = [20 x i8] c"Program interpreter\00"
@str.25 = [28 x i8] c"Dynamic linking information\00"
@str.26 = [25 x i8] c"Loadable program segment\00"
@str.27 = [7 x i8] c"Unused\00"
define void @parse_program_headers(i32* nocapture %0, i32 %1) {
%3 = alloca %struct.TYPE_5__
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([81 x i8], [81 x i8]* @str, i64 0, i64 0))
%5 = bitcast %struct.TYPE_5__* %3 to i8*
%6 = tail call i32 @fseek(i32* %0, i32 %1, i32 0)
%7 = call i32 @fread(%struct.TYPE_5__* %3, i32 4, i32 1, i32* %0)
%8 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%9 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %3, i64 0, i32 0
%10 = load i32, i32* %9
switch i32 %10, label %45 [
i32 134, label %11
i32 137, label %13
i32 144, label %15
i32 138, label %17
i32 135, label %19
i32 131, label %21
i32 132, label %23
i32 128, label %25
i32 133, label %27
i32 143, label %29
i32 141, label %31
i32 142, label %33
i32 130, label %35
i32 129, label %37
i32 140, label %39
i32 136, label %41
i32 139, label %43
]
11: ; preds = %2
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.27, i64 0, i64 0))
br label %45
13: ; preds = %2
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.26, i64 0, i64 0))
br label %45
15: ; preds = %2
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.25, i64 0, i64 0))
br label %45
17: ; preds = %2
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.24, i64 0, i64 0))
br label %45
19: ; preds = %2
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.23, i64 0, i64 0))
br label %45
21: ; preds = %2
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.22, i64 0, i64 0))
br label %45
23: ; preds = %2
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.21, i64 0, i64 0))
br label %45
25: ; preds = %2
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.20, i64 0, i64 0))
br label %45
27: ; preds = %2
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.19, i64 0, i64 0))
br label %45
29: ; preds = %2
%30 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.11, i64 0, i64 0))
br label %45
31: ; preds = %2
%32 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.12, i64 0, i64 0))
br label %45
33: ; preds = %2
%34 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.13, i64 0, i64 0))
br label %45
35: ; preds = %2
%36 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.14, i64 0, i64 0))
br label %45
37: ; preds = %2
%38 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.15, i64 0, i64 0))
br label %45
39: ; preds = %2
%40 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.16, i64 0, i64 0))
br label %45
41: ; preds = %2
%42 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.17, i64 0, i64 0))
br label %45
43: ; preds = %2
%44 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.18, i64 0, i64 0))
br label %45
45: ; preds = %2, %43, %41, %39, %37, %35, %33, %31, %29, %27, %25, %23, %21, %19, %17, %15, %13, %11
ret void
}