#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__ {
  unsigned int e_phnum;
  unsigned long long e_phoff;
  unsigned int e_phentsize;
};
typedef int /*<<< orphan*/ Elf64_Phdr;
typedef TYPE_1__ Elf64_Ehdr;

/* Variables and functions */
Elf64_Phdr *get_p_header(char *elf, unsigned int index) {
  if (!elf)
    return 0;
  Elf64_Ehdr *ehdr;
  ehdr = (Elf64_Ehdr *)elf;
  if (ehdr->e_phnum <= index)
    return 0;
  unsigned long long addr;
  addr = (unsigned long long)elf + ehdr->e_phoff + (ehdr->e_phentsize * index);
  return (Elf64_Phdr *)addr;
}


x86 assembly (clang, -O0):
<get_p_header>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1123 <get_p_header+0x23>
movq   $0x0,-0x8(%rbp)
jmp    116f <get_p_header+0x6f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
cmp    -0x14(%rbp),%eax
ja     1147 <get_p_header+0x47>
movq   $0x0,-0x8(%rbp)
jmp    116f <get_p_header+0x6f>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
add    0x8(%rcx),%rax
mov    -0x20(%rbp),%rcx
mov    0x10(%rcx),%ecx
imul   -0x14(%rbp),%ecx
mov    %ecx,%ecx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_p_header>:
test   %rdi,%rdi
je     111a <get_p_header+0x1a>
mov    %esi,%eax
cmp    %esi,(%rdi)
jbe    111a <get_p_header+0x1a>
mov    0x8(%rdi),%rcx
add    %rdi,%rcx
imul   0x10(%rdi),%eax
add    %rcx,%rax
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
%struct.TYPE_2__ = type { i32, i64, i32 }
define i32* @get_p_header(i8* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.TYPE_2__*
%7 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %11, label %10
10: ; preds = %2
store i32* null, i32** %3
br label %36
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = bitcast i8* %12 to %struct.TYPE_2__*
store %struct.TYPE_2__* %13, %struct.TYPE_2__** %6
%14 = load %struct.TYPE_2__*, %struct.TYPE_2__** %6
%15 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %14, i32 0, i32 0
%16 = load i32, i32* %15
%17 = load i32, i32* %5
%18 = icmp ule i32 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %11
store i32* null, i32** %3
br label %36
20: ; preds = %11
%21 = load i8*, i8** %4
%22 = ptrtoint i8* %21 to i64
%23 = load %struct.TYPE_2__*, %struct.TYPE_2__** %6
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 1
%25 = load i64, i64* %24
%26 = add i64 %22, %25
%27 = load %struct.TYPE_2__*, %struct.TYPE_2__** %6
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 2
%29 = load i32, i32* %28
%30 = load i32, i32* %5
%31 = mul i32 %29, %30
%32 = zext i32 %31 to i64
%33 = add i64 %26, %32
store i64 %33, i64* %7
%34 = load i64, i64* %7
%35 = inttoptr i64 %34 to i32*
store i32* %35, i32** %3
br label %36
36: ; preds = %20, %19, %10
%37 = load i32*, i32** %3
ret i32* %37
}

LLVM IR (-O2):
define i32* @get_p_header(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %21, label %4
4: ; preds = %2
%5 = bitcast i8* %0 to i32*
%6 = load i32, i32* %5
%7 = icmp ugt i32 %6, %1
br i1 %7, label %8, label %21
8: ; preds = %4
%9 = ptrtoint i8* %0 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 8
%11 = bitcast i8* %10 to i64*
%12 = load i64, i64* %11
%13 = add i64 %12, %9
%14 = getelementptr inbounds i8, i8* %0, i64 16
%15 = bitcast i8* %14 to i32*
%16 = load i32, i32* %15
%17 = mul i32 %16, %1
%18 = zext i32 %17 to i64
%19 = add i64 %13, %18
%20 = inttoptr i64 %19 to i32*
br label %21
21: ; preds = %8, %4, %2
%22 = phi i32* [ null, %2 ], [ %20, %8 ], [ null, %4 ]
ret i32* %22
}