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

/* Type definitions */
struct TYPE_3__ {
  int e_phnum;
  int /*<<< orphan*/ e_phoff;
};
typedef int /*<<< orphan*/ FILE;
typedef int /*<<< orphan*/ Elf64_Phdr;
typedef TYPE_1__ Elf64_Ehdr;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int elfrw_read_Phdrs(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ *emalloc(int);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
Elf64_Phdr *get_proghdrs(FILE *fp, Elf64_Ehdr *elffhdr) {
  Elf64_Phdr *ret;
  int n;
  n = elffhdr->e_phnum;
  ret = emalloc(n * sizeof(Elf64_Phdr));
  if (fseek(fp, elffhdr->e_phoff, SEEK_SET) < 0 ||
      elfrw_read_Phdrs(fp, ret, n) != n) {
    free(ret);
    return NULL;
  }
  return ret;
}


x86 assembly (clang, -O0):
<get_proghdrs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <emalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%esi
mov    0x2e62(%rip),%rax
mov    (%rax),%edx
call   1050 <fseek@plt>
cmp    $0x0,%rax
jl     11a8 <get_proghdrs+0x68>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1040 <elfrw_read_Phdrs@plt>
cmp    -0x24(%rbp),%eax
je     11be <get_proghdrs+0x7e>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11c6 <get_proghdrs+0x86>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_proghdrs>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r15
mov    (%rsi),%r14d
lea    0x0(,%r14,4),%edi
call   1060 <emalloc@plt>
mov    %rax,%r12
mov    0x4(%rbx),%esi
mov    0x2e75(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
call   1050 <fseek@plt>
test   %rax,%rax
js     118d <get_proghdrs+0x4d>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14d,%edx
call   1040 <elfrw_read_Phdrs@plt>
cmp    %r14d,%eax
je     1198 <get_proghdrs+0x58>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i32 }
@SEEK_SET = global i32 0
define i32* @get_proghdrs(i32* %0, %struct.TYPE_3__* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca %struct.TYPE_3__*
%6 = alloca i32*
%7 = alloca i32
store i32* %0, i32** %4
store %struct.TYPE_3__* %1, %struct.TYPE_3__** %5
%8 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%9 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %8, i32 0, i32 0
%10 = load i32, i32* %9
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = sext i32 %11 to i64
%13 = mul i64 %12, 4
%14 = trunc i64 %13 to i32
%15 = call i32* @emalloc(i32 %14)
store i32* %15, i32** %6
%16 = load i32*, i32** %4
%17 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%18 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %17, i32 0, i32 1
%19 = load i32, i32* %18
%20 = load i32, i32* @SEEK_SET
%21 = call i64 @fseek(i32* %16, i32 %19, i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %30, label %23
23: ; preds = %2
%24 = load i32*, i32** %4
%25 = load i32*, i32** %6
%26 = load i32, i32* %7
%27 = call i32 @elfrw_read_Phdrs(i32* %24, i32* %25, i32 %26)
%28 = load i32, i32* %7
%29 = icmp ne i32 %27, %28
br i1 %29, label %30, label %33
30: ; preds = %23, %2
%31 = load i32*, i32** %6
%32 = call i32 @free(i32* %31)
store i32* null, i32** %3
br label %35
33: ; preds = %23
%34 = load i32*, i32** %6
store i32* %34, i32** %3
br label %35
35: ; preds = %33, %30
%36 = load i32*, i32** %3
ret i32* %36
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32 }
@SEEK_SET = global i32 0
define i32* @get_proghdrs(i32* %0, %struct.TYPE_3__* nocapture %1) {
%3 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 0
%4 = load i32, i32* %3
%5 = shl i32 %4, 2
%6 = tail call i32* @emalloc(i32 %5)
%7 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 1
%8 = load i32, i32* %7
%9 = load i32, i32* @SEEK_SET
%10 = tail call i64 @fseek(i32* %0, i32 %8, i32 %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = tail call i32 @elfrw_read_Phdrs(i32* %0, i32* %6, i32 %4)
%14 = icmp eq i32 %13, %4
br i1 %14, label %17, label %15
15: ; preds = %12, %2
%16 = tail call i32 @free(i32* %6)
br label %17
17: ; preds = %12, %15
%18 = phi i32* [ null, %15 ], [ %6, %12 ]
ret i32* %18
}