#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_5__ TYPE_2__;
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
struct TYPE_5__ {
  int e_phnum;
  scalar_t__ e_phentsize;
  scalar_t__ e_phoff;
};
struct TYPE_4__ {
  scalar_t__ p_type;
  int p_flags;
  int p_offset;
  int p_filesz;
};
typedef TYPE_1__ Elf64_Phdr;
typedef TYPE_2__ Elf64_Ehdr;

/* Variables and functions */
scalar_t__ PT_LOAD;
int /*<<< orphan*/ printf(char *, int, ...);
Elf64_Phdr *elfi_find_gap(void *d, int fsize, int *p, int *len) {
  Elf64_Ehdr *elf_hdr = (Elf64_Ehdr *)d;
  Elf64_Phdr *elf_seg, *text_seg;
  int n_seg = elf_hdr->e_phnum;
  int i;
  int text_end, gap = fsize;
  elf_seg =
      (Elf64_Phdr *)((unsigned char *)elf_hdr + (unsigned int)elf_hdr->e_phoff);
  for (i = 0; i < n_seg; i++) {
    if (elf_seg->p_type == PT_LOAD && elf_seg->p_flags & 0x011) {
      printf("+ Found .text segment (#%d)\n", i);
      text_seg = elf_seg;
      text_end = elf_seg->p_offset + elf_seg->p_filesz;
    } else {
      if (elf_seg->p_type == PT_LOAD && (elf_seg->p_offset - text_end) < gap) {
        printf("   * Found LOAD segment (#%d) close to .text (offset: 0x%x)\n",
               i, (unsigned int)elf_seg->p_offset);
        gap = elf_seg->p_offset - text_end;
      }
    }
    elf_seg = (Elf64_Phdr *)((unsigned char *)elf_seg +
                             (unsigned int)elf_hdr->e_phentsize);
  }
  *p = text_end;
  *len = gap;
  printf("+ .text segment gap at offset 0x%x(0x%x bytes available)\n", text_end,
         gap);
  return text_seg;
}


x86 assembly (clang, -O0):
<elfi_find_gap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    0x10(%rcx),%rcx
mov    %ecx,%ecx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jge    1236 <elfi_find_gap+0x126>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    0x2e74(%rip),%rcx
cmp    (%rcx),%rax
jne    11bf <elfi_find_gap+0xaf>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%eax
and    $0x11,%eax
cmp    $0x0,%eax
je     11bf <elfi_find_gap+0xaf>
mov    -0x40(%rbp),%esi
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    0xc(%rax),%eax
mov    -0x30(%rbp),%rcx
add    0x10(%rcx),%eax
mov    %eax,-0x44(%rbp)
jmp    1213 <elfi_find_gap+0x103>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    0x2e1b(%rip),%rcx
cmp    (%rcx),%rax
jne    120e <elfi_find_gap+0xfe>
mov    -0x30(%rbp),%rax
mov    0xc(%rax),%eax
sub    -0x44(%rbp),%eax
cmp    -0x48(%rbp),%eax
jge    120e <elfi_find_gap+0xfe>
mov    -0x40(%rbp),%esi
mov    -0x30(%rbp),%rax
mov    0xc(%rax),%edx
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x30(%rbp),%rax
mov    0xc(%rax),%eax
sub    -0x44(%rbp),%eax
mov    %eax,-0x48(%rbp)
jmp    1213 <elfi_find_gap+0x103>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    0x8(%rcx),%rcx
mov    %ecx,%ecx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    115a <elfi_find_gap+0x4a>
mov    -0x44(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x48(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x44(%rbp),%esi
mov    -0x48(%rbp),%edx
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x38(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<elfi_find_gap>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,0x10(%rsp)
mov    %rdx,0x8(%rsp)
mov    %esi,%r12d
mov    (%rdi),%r14d
test   %r14d,%r14d
jle    11c6 <elfi_find_gap+0xb6>
mov    %rdi,%rbx
mov    0x10(%rdi),%r13d
add    %rdi,%r13
mov    0x2ea0(%rip),%rsi
xor    %r15d,%r15d
jmp    1182 <elfi_find_gap+0x72>
nopl   (%rax)
lea    0xea9(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x2e80(%rip),%rsi
mov    0x10(%r13),%ebp
add    0xc(%r13),%ebp
mov    %r13,%rcx
mov    0x8(%rbx),%eax
add    %rax,%r13
add    $0x1,%r15d
cmp    %r15d,%r14d
je     11c6 <elfi_find_gap+0xb6>
mov    0x0(%r13),%rax
cmp    (%rsi),%rax
jne    1173 <elfi_find_gap+0x63>
testb  $0x11,0x8(%r13)
jne    1150 <elfi_find_gap+0x40>
mov    0xc(%r13),%edx
mov    %edx,%eax
sub    %ebp,%eax
cmp    %r12d,%eax
jge    1173 <elfi_find_gap+0x63>
lea    0xe77(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
mov    %rcx,%r12
call   1030 <printf@plt>
mov    0x2e2e(%rip),%rsi
mov    %r12,%rcx
mov    0xc(%r13),%r12d
sub    %ebp,%r12d
jmp    1173 <elfi_find_gap+0x63>
mov    0x8(%rsp),%rax
mov    %ebp,(%rax)
mov    0x10(%rsp),%rax
mov    %r12d,(%rax)
lea    0xe7e(%rip),%rdi
mov    %ebp,%esi
mov    %r12d,%edx
xor    %eax,%eax
mov    %rcx,%rbx
call   1030 <printf@plt>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_4__ = type { i64, i32, i32, i32 }
%struct.TYPE_5__ = type { i32, i64, i64 }
@PT_LOAD = global i64 0
@.str = [29 x i8] c"+ Found .text segment (#%d)\0A\00"
@.str.1 = [61 x i8] c" * Found LOAD segment (#%d) close to .text (offset: 0x%x)\0A\00"
@.str.2 = [58 x i8] c"+ .text segment gap at offset 0x%x(0x%x bytes available)\0A\00"
define %struct.TYPE_4__* @elfi_find_gap(i8* %0, i32 %1, i32* %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca %struct.TYPE_5__*
%10 = alloca %struct.TYPE_4__*
%11 = alloca %struct.TYPE_4__*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i32* %3, i32** %8
%16 = load i8*, i8** %5
%17 = bitcast i8* %16 to %struct.TYPE_5__*
store %struct.TYPE_5__* %17, %struct.TYPE_5__** %9
%18 = load %struct.TYPE_5__*, %struct.TYPE_5__** %9
%19 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %18, i32 0, i32 0
%20 = load i32, i32* %19
store i32 %20, i32* %12
%21 = load i32, i32* %6
store i32 %21, i32* %15
%22 = load %struct.TYPE_5__*, %struct.TYPE_5__** %9
%23 = bitcast %struct.TYPE_5__* %22 to i8*
%24 = load %struct.TYPE_5__*, %struct.TYPE_5__** %9
%25 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %24, i32 0, i32 2
%26 = load i64, i64* %25
%27 = trunc i64 %26 to i32
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %23, i64 %28
%30 = bitcast i8* %29 to %struct.TYPE_4__*
store %struct.TYPE_4__* %30, %struct.TYPE_4__** %10
store i32 0, i32* %13
br label %31
31: ; preds = %94, %4
%32 = load i32, i32* %13
%33 = load i32, i32* %12
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %97
35: ; preds = %31
%36 = load %struct.TYPE_4__*, %struct.TYPE_4__** %10
%37 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %36, i32 0, i32 0
%38 = load i64, i64* %37
%39 = load i64, i64* @PT_LOAD
%40 = icmp eq i64 %38, %39
br i1 %40, label %41, label %58
41: ; preds = %35
%42 = load %struct.TYPE_4__*, %struct.TYPE_4__** %10
%43 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %42, i32 0, i32 1
%44 = load i32, i32* %43
%45 = and i32 %44, 17
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %58
47: ; preds = %41
%48 = load i32, i32* %13
%49 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %48)
%50 = load %struct.TYPE_4__*, %struct.TYPE_4__** %10
store %struct.TYPE_4__* %50, %struct.TYPE_4__** %11
%51 = load %struct.TYPE_4__*, %struct.TYPE_4__** %10
%52 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %51, i32 0, i32 2
%53 = load i32, i32* %52
%54 = load %struct.TYPE_4__*, %struct.TYPE_4__** %10
%55 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %54, i32 0, i32 3
%56 = load i32, i32* %55
%57 = add nsw i32 %53, %56
store i32 %57, i32* %14
br label %84
58: ; preds = %41, %35
%59 = load %struct.TYPE_4__*, %struct.TYPE_4__** %10
%60 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %59, i32 0, i32 0
%61 = load i64, i64* %60
%62 = load i64, i64* @PT_LOAD
%63 = icmp eq i64 %61, %62
br i1 %63, label %64, label %83
64: ; preds = %58
%65 = load %struct.TYPE_4__*, %struct.TYPE_4__** %10
%66 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %65, i32 0, i32 2
%67 = load i32, i32* %66
%68 = load i32, i32* %14
%69 = sub nsw i32 %67, %68
%70 = load i32, i32* %15
%71 = icmp slt i32 %69, %70
br i1 %71, label %72, label %83
72: ; preds = %64
%73 = load i32, i32* %13
%74 = load %struct.TYPE_4__*, %struct.TYPE_4__** %10
%75 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %74, i32 0, i32 2
%76 = load i32, i32* %75
%77 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0), i32 %73, i32 %76)
%78 = load %struct.TYPE_4__*, %struct.TYPE_4__** %10
%79 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %78, i32 0, i32 2
%80 = load i32, i32* %79
%81 = load i32, i32* %14
%82 = sub nsw i32 %80, %81
store i32 %82, i32* %15
br label %83
83: ; preds = %72, %64, %58
br label %84
84: ; preds = %83, %47
%85 = load %struct.TYPE_4__*, %struct.TYPE_4__** %10
%86 = bitcast %struct.TYPE_4__* %85 to i8*
%87 = load %struct.TYPE_5__*, %struct.TYPE_5__** %9
%88 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %87, i32 0, i32 1
%89 = load i64, i64* %88
%90 = trunc i64 %89 to i32
%91 = zext i32 %90 to i64
%92 = getelementptr inbounds i8, i8* %86, i64 %91
%93 = bitcast i8* %92 to %struct.TYPE_4__*
store %struct.TYPE_4__* %93, %struct.TYPE_4__** %10
br label %94
94: ; preds = %84
%95 = load i32, i32* %13
%96 = add nsw i32 %95, 1
store i32 %96, i32* %13
br label %31
97: ; preds = %31
%98 = load i32, i32* %14
%99 = load i32*, i32** %7
store i32 %98, i32* %99
%100 = load i32, i32* %15
%101 = load i32*, i32** %8
store i32 %100, i32* %101
%102 = load i32, i32* %14
%103 = load i32, i32* %15
%104 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.2, i64 0, i64 0), i32 %102, i32 %103)
%105 = load %struct.TYPE_4__*, %struct.TYPE_4__** %11
ret %struct.TYPE_4__* %105
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i64, i32, i32, i32 }
@PT_LOAD = global i64 0
@.str = [29 x i8] c"+ Found .text segment (#%d)\0A\00"
@.str.1 = [61 x i8] c" * Found LOAD segment (#%d) close to .text (offset: 0x%x)\0A\00"
@.str.2 = [58 x i8] c"+ .text segment gap at offset 0x%x(0x%x bytes available)\0A\00"
define %struct.TYPE_4__* @elfi_find_gap(i8* %0, i32 %1, i32* nocapture writeonly %2, i32* nocapture writeonly %3) {
%5 = bitcast i8* %0 to i32*
%6 = load i32, i32* %5
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %61
8: ; preds = %4
%9 = getelementptr inbounds i8, i8* %0, i64 16
%10 = bitcast i8* %9 to i64*
%11 = load i64, i64* %10
%12 = and i64 %11, 4294967295
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = getelementptr inbounds i8, i8* %0, i64 8
%15 = bitcast i8* %14 to i64*
br label %16
16: ; preds = %8, %52
%17 = phi i32 [ %1, %8 ], [ %55, %52 ]
%18 = phi i32 [ undef, %8 ], [ %54, %52 ]
%19 = phi i32 [ 0, %8 ], [ %59, %52 ]
%20 = phi i8* [ %13, %8 ], [ %58, %52 ]
%21 = phi %struct.TYPE_4__* [ undef, %8 ], [ %53, %52 ]
%22 = bitcast i8* %20 to %struct.TYPE_4__*
%23 = bitcast i8* %20 to i64*
%24 = load i64, i64* %23
%25 = load i64, i64* @PT_LOAD
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %52
27: ; preds = %16
%28 = getelementptr inbounds i8, i8* %20, i64 8
%29 = bitcast i8* %28 to i32*
%30 = load i32, i32* %29
%31 = and i32 %30, 17
%32 = icmp eq i32 %31, 0
br i1 %32, label %42, label %33
33: ; preds = %27
%34 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %19)
%35 = getelementptr inbounds i8, i8* %20, i64 12
%36 = bitcast i8* %35 to i32*
%37 = load i32, i32* %36
%38 = getelementptr inbounds i8, i8* %20, i64 16
%39 = bitcast i8* %38 to i32*
%40 = load i32, i32* %39
%41 = add nsw i32 %40, %37
br label %52
42: ; preds = %27
%43 = getelementptr inbounds i8, i8* %20, i64 12
%44 = bitcast i8* %43 to i32*
%45 = load i32, i32* %44
%46 = sub nsw i32 %45, %18
%47 = icmp slt i32 %46, %17
br i1 %47, label %48, label %52
48: ; preds = %42
%49 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0), i32 %19, i32 %45)
%50 = load i32, i32* %44
%51 = sub nsw i32 %50, %18
br label %52
52: ; preds = %16, %42, %48, %33
%53 = phi %struct.TYPE_4__* [ %22, %33 ], [ %21, %48 ], [ %21, %42 ], [ %21, %16 ]
%54 = phi i32 [ %41, %33 ], [ %18, %48 ], [ %18, %42 ], [ %18, %16 ]
%55 = phi i32 [ %17, %33 ], [ %51, %48 ], [ %17, %42 ], [ %17, %16 ]
%56 = load i64, i64* %15
%57 = and i64 %56, 4294967295
%58 = getelementptr inbounds i8, i8* %20, i64 %57
%59 = add nuw nsw i32 %19, 1
%60 = icmp eq i32 %59, %6
br i1 %60, label %61, label %16
61: ; preds = %52, %4
%62 = phi %struct.TYPE_4__* [ undef, %4 ], [ %53, %52 ]
%63 = phi i32 [ undef, %4 ], [ %54, %52 ]
%64 = phi i32 [ %1, %4 ], [ %55, %52 ]
store i32 %63, i32* %2
store i32 %64, i32* %3
%65 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.2, i64 0, i64 0), i32 %63, i32 %64)
ret %struct.TYPE_4__* %62
}