#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 */
typedef int /*<<< orphan*/ uint64_t;
struct TYPE_3__ {
  scalar_t__ p_type;
  unsigned int p_flags;
  int /*<<< orphan*/ p_vaddr;
};
typedef int /*<<< orphan*/ FILE;
typedef TYPE_1__ Elf64_Phdr;

/* Variables and functions */
scalar_t__ PT_LOAD;
int /*<<< orphan*/ SEEK_SET;
int fread(TYPE_1__ *, int, int, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
uint64_t elf64_seg_loadaddr(FILE *fp, uint64_t phoff, int phnum,
                            unsigned int flags) {
  Elf64_Phdr phdr;
  uint64_t load_addr = (uint64_t)(-1);
  int i;
  if (fseek(fp, phoff, SEEK_SET) != 0) {
    goto L_EXIT;
  }
  for (i = 0; i < phnum; i++) {
    if (fread(&phdr, sizeof(Elf64_Phdr), 1, fp) != 1) {
      goto L_EXIT;
    }
    if ((phdr.p_type == PT_LOAD) && ((phdr.p_flags & flags) != 0)) {
      break;
    }
  }
  if (i == phnum) {
    goto L_EXIT;
  }
  load_addr = phdr.p_vaddr;
L_EXIT:
  return (load_addr);
}


x86 assembly (clang, -O0):
<elf64_seg_loadaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    0x2e8e(%rip),%rax
mov    (%rax),%edx
call   1040 <fseek@plt>
cmp    $0x0,%rax
je     1160 <elf64_seg_loadaddr+0x40>
jmp    11ea <elf64_seg_loadaddr+0xca>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11d3 <elf64_seg_loadaddr+0xb3>
mov    -0x8(%rbp),%rcx
lea    -0x28(%rbp),%rdi
mov    $0x10,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     1198 <elf64_seg_loadaddr+0x78>
jmp    11ea <elf64_seg_loadaddr+0xca>
mov    -0x28(%rbp),%rax
mov    0x2e45(%rip),%rcx
cmp    (%rcx),%rax
jne    11c0 <elf64_seg_loadaddr+0xa0>
mov    -0x20(%rbp),%eax
and    -0x14(%rbp),%eax
cmp    $0x0,%eax
je     11c0 <elf64_seg_loadaddr+0xa0>
jmp    11d3 <elf64_seg_loadaddr+0xb3>
jmp    11c5 <elf64_seg_loadaddr+0xa5>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1167 <elf64_seg_loadaddr+0x47>
mov    -0x30(%rbp),%eax
cmp    -0x10(%rbp),%eax
jne    11e4 <elf64_seg_loadaddr+0xc4>
jmp    11ea <elf64_seg_loadaddr+0xca>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<elf64_seg_loadaddr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r15d
mov    %edx,%r13d
mov    %rdi,%rbx
mov    0x2e9a(%rip),%rax
mov    (%rax),%edx
call   1040 <fseek@plt>
mov    $0xffffffff,%ecx
test   %rax,%rax
jne    11b8 <elf64_seg_loadaddr+0x98>
xor    %ebp,%ebp
test   %r13d,%r13d
jle    11a8 <elf64_seg_loadaddr+0x88>
lea    0x8(%rsp),%r12
mov    0x2e86(%rip),%r14
jmp    1178 <elf64_seg_loadaddr+0x58>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%ebp
cmp    %ebp,%r13d
je     11b3 <elf64_seg_loadaddr+0x93>
mov    %r12,%rdi
mov    $0x10,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    11b3 <elf64_seg_loadaddr+0x93>
mov    0x8(%rsp),%rax
cmp    (%r14),%rax
jne    1170 <elf64_seg_loadaddr+0x50>
test   %r15d,0x10(%rsp)
je     1170 <elf64_seg_loadaddr+0x50>
mov    $0xffffffff,%ecx
cmp    %r13d,%ebp
je     11b8 <elf64_seg_loadaddr+0x98>
mov    0x14(%rsp),%ecx
jmp    11b8 <elf64_seg_loadaddr+0x98>
mov    $0xffffffff,%ecx
mov    %ecx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i64, i32, i32 }
@SEEK_SET = global i32 0
@PT_LOAD = global i64 0
define i32 @elf64_seg_loadaddr(i32* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.TYPE_3__
%10 = alloca i32
%11 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 -1, i32* %10
%12 = load i32*, i32** %5
%13 = load i32, i32* %6
%14 = load i32, i32* @SEEK_SET
%15 = call i64 @fseek(i32* %12, i32 %13, i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %4
br label %52
18: ; preds = %4
store i32 0, i32* %11
br label %19
19: ; preds = %41, %18
%20 = load i32, i32* %11
%21 = load i32, i32* %7
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %44
23: ; preds = %19
%24 = load i32*, i32** %5
%25 = call i32 @fread(%struct.TYPE_3__* %9, i32 16, i32 1, i32* %24)
%26 = icmp ne i32 %25, 1
br i1 %26, label %27, label %28
27: ; preds = %23
br label %52
28: ; preds = %23
%29 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %9, i32 0, i32 0
%30 = load i64, i64* %29
%31 = load i64, i64* @PT_LOAD
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %40
33: ; preds = %28
%34 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %9, i32 0, i32 1
%35 = load i32, i32* %34
%36 = load i32, i32* %8
%37 = and i32 %35, %36
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %33
br label %44
40: ; preds = %33, %28
br label %41
41: ; preds = %40
%42 = load i32, i32* %11
%43 = add nsw i32 %42, 1
store i32 %43, i32* %11
br label %19
44: ; preds = %39, %19
%45 = load i32, i32* %11
%46 = load i32, i32* %7
%47 = icmp eq i32 %45, %46
br i1 %47, label %48, label %49
48: ; preds = %44
br label %52
49: ; preds = %44
%50 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %9, i32 0, i32 2
%51 = load i32, i32* %50
store i32 %51, i32* %10
br label %52
52: ; preds = %49, %48, %27, %17
%53 = load i32, i32* %10
ret i32 %53
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i64, i32, i32 }
@SEEK_SET = global i32 0
@PT_LOAD = global i64 0
define i32 @elf64_seg_loadaddr(i32* nocapture %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.TYPE_3__
%6 = bitcast %struct.TYPE_3__* %5 to i8*
%7 = load i32, i32* @SEEK_SET
%8 = tail call i64 @fseek(i32* %0, i32 %1, i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %36
10: ; preds = %4
%11 = icmp sgt i32 %2, 0
br i1 %11, label %12, label %30
12: ; preds = %10
%13 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %5, i64 0, i32 0
%14 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %5, i64 0, i32 1
br label %15
15: ; preds = %12, %27
%16 = phi i32 [ 0, %12 ], [ %28, %27 ]
%17 = call i32 @fread(%struct.TYPE_3__* %5, i32 16, i32 1, i32* %0)
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %36
19: ; preds = %15
%20 = load i64, i64* %13
%21 = load i64, i64* @PT_LOAD
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = load i32, i32* %14
%25 = and i32 %24, %3
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %19, %23
%28 = add nuw nsw i32 %16, 1
%29 = icmp eq i32 %28, %2
br i1 %29, label %36, label %15
30: ; preds = %23, %10
%31 = phi i32 [ 0, %10 ], [ %16, %23 ]
%32 = icmp eq i32 %31, %2
br i1 %32, label %36, label %33
33: ; preds = %30
%34 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %5, i64 0, i32 2
%35 = load i32, i32* %34
br label %36
36: ; preds = %27, %15, %30, %4, %33
%37 = phi i32 [ -1, %4 ], [ -1, %30 ], [ %35, %33 ], [ -1, %15 ], [ -1, %27 ]
ret i32 %37
}