#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_phoff;
  int e_phnum;
};
struct TYPE_4__ {
  scalar_t__ p_type;
  int p_vaddr;
  int p_filesz;
  int p_offset;
};
typedef TYPE_1__ Elf64_Phdr;
typedef int Elf64_Off;
typedef int Elf64_Half;
typedef TYPE_2__ Elf64_Ehdr;

/* Variables and functions */
scalar_t__ PT_LOAD;
int /*<<< orphan*/ _mmap(void *, unsigned long, unsigned long);
int /*<<< orphan*/ copy(char *, char *, int, int /*<<< orphan*/);
void pt_load(Elf64_Ehdr *ehdr) {
  Elf64_Phdr *phdr;
  Elf64_Off phoff;
  Elf64_Half phnum;
  unsigned long addr;
  unsigned long size;
  unsigned long off;
  int i;
  phoff = ehdr->e_phoff;
  phnum = ehdr->e_phnum;
  phdr = (Elf64_Phdr *)((char *)ehdr + phoff);
  for (i = 0; i < phnum; ++i, ++phdr) {
    if (phdr->p_type != PT_LOAD) {
      continue;
    }
    addr = phdr->p_vaddr & ~(4096 - 1);
    size = phdr->p_filesz + (phdr->p_vaddr & (4096 - 1));
    size = (size + (4096 - 1) & ~(4096 - 1));
    off = phdr->p_offset - (phdr->p_vaddr & (4096 - 1));
    _mmap((void *)addr, size, off);
    copy((char *)phdr->p_vaddr, ((char *)ehdr + phdr->p_offset), phdr->p_filesz,
         0);
  }
}


x86 assembly (clang, -O0):
<pt_load>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    122b <pt_load+0x10b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e79(%rip),%rcx
cmp    (%rcx),%rax
je     117d <pt_load+0x5d>
jmp    1211 <pt_load+0xf1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%eax
and    $0xfffff000,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%eax
mov    -0x10(%rbp),%rcx
mov    0x8(%rcx),%ecx
and    $0xfff,%ecx
add    %ecx,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0xfff,%rax
and    $0xfffffffffffff000,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%eax
mov    -0x10(%rbp),%rcx
mov    0x8(%rcx),%ecx
and    $0xfff,%ecx
sub    %ecx,%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1040 <_mmap@plt>
mov    -0x10(%rbp),%rax
movslq 0x8(%rax),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rax
movslq 0x10(%rax),%rax
add    %rax,%rsi
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%edx
xor    %ecx,%ecx
call   1030 <copy@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
add    $0x18,%rax
mov    %rax,-0x10(%rbp)
jmp    1155 <pt_load+0x35>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pt_load>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x4(%rdi),%ebp
test   %ebp,%ebp
jle    11ac <pt_load+0x8c>
mov    %rdi,%r15
movslq (%rdi),%rcx
mov    0x2ead(%rip),%r14
mov    (%r14),%rax
lea    (%rdi,%rcx,1),%rbx
add    $0x10,%rbx
jmp    1159 <pt_load+0x39>
nopl   0x0(%rax,%rax,1)
add    $0x18,%rbx
add    $0xffffffff,%ebp
je     11ac <pt_load+0x8c>
cmp    %rax,-0x10(%rbx)
jne    1150 <pt_load+0x30>
movslq -0x8(%rbx),%rax
mov    %rax,%rdi
and    $0xfffffffffffff000,%rdi
movslq -0x4(%rbx),%rcx
and    $0xfff,%eax
lea    (%rcx,%rax,1),%rsi
add    $0xfff,%rsi
and    $0xfffffffffffff000,%rsi
movslq (%rbx),%rdx
sub    %rax,%rdx
call   1040 <_mmap@plt>
movslq -0x8(%rbx),%rdi
movslq (%rbx),%rsi
add    %r15,%rsi
mov    -0x4(%rbx),%edx
xor    %ecx,%ecx
call   1030 <copy@plt>
mov    (%r14),%rax
jmp    1150 <pt_load+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_5__ = type { i32, i32 }
%struct.TYPE_4__ = type { i64, i32, i32, i32 }
@PT_LOAD = global i64 0
define void @pt_load(%struct.TYPE_5__* %0) {
%2 = alloca %struct.TYPE_5__*
%3 = alloca %struct.TYPE_4__*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
store %struct.TYPE_5__* %0, %struct.TYPE_5__** %2
%10 = load %struct.TYPE_5__*, %struct.TYPE_5__** %2
%11 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 0
%12 = load i32, i32* %11
store i32 %12, i32* %4
%13 = load %struct.TYPE_5__*, %struct.TYPE_5__** %2
%14 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %13, i32 0, i32 1
%15 = load i32, i32* %14
store i32 %15, i32* %5
%16 = load %struct.TYPE_5__*, %struct.TYPE_5__** %2
%17 = bitcast %struct.TYPE_5__* %16 to i8*
%18 = load i32, i32* %4
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = bitcast i8* %20 to %struct.TYPE_4__*
store %struct.TYPE_4__* %21, %struct.TYPE_4__** %3
store i32 0, i32* %9
br label %22
22: ; preds = %81, %1
%23 = load i32, i32* %9
%24 = load i32, i32* %5
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %86
26: ; preds = %22
%27 = load %struct.TYPE_4__*, %struct.TYPE_4__** %3
%28 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %27, i32 0, i32 0
%29 = load i64, i64* %28
%30 = load i64, i64* @PT_LOAD
%31 = icmp ne i64 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %26
br label %81
33: ; preds = %26
%34 = load %struct.TYPE_4__*, %struct.TYPE_4__** %3
%35 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %34, i32 0, i32 1
%36 = load i32, i32* %35
%37 = and i32 %36, -4096
%38 = sext i32 %37 to i64
store i64 %38, i64* %6
%39 = load %struct.TYPE_4__*, %struct.TYPE_4__** %3
%40 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %39, i32 0, i32 2
%41 = load i32, i32* %40
%42 = load %struct.TYPE_4__*, %struct.TYPE_4__** %3
%43 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %42, i32 0, i32 1
%44 = load i32, i32* %43
%45 = and i32 %44, 4095
%46 = add nsw i32 %41, %45
%47 = sext i32 %46 to i64
store i64 %47, i64* %7
%48 = load i64, i64* %7
%49 = add i64 %48, 4095
%50 = and i64 %49, -4096
store i64 %50, i64* %7
%51 = load %struct.TYPE_4__*, %struct.TYPE_4__** %3
%52 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %51, i32 0, i32 3
%53 = load i32, i32* %52
%54 = load %struct.TYPE_4__*, %struct.TYPE_4__** %3
%55 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %54, i32 0, i32 1
%56 = load i32, i32* %55
%57 = and i32 %56, 4095
%58 = sub nsw i32 %53, %57
%59 = sext i32 %58 to i64
store i64 %59, i64* %8
%60 = load i64, i64* %6
%61 = inttoptr i64 %60 to i8*
%62 = load i64, i64* %7
%63 = load i64, i64* %8
%64 = call i32 @_mmap(i8* %61, i64 %62, i64 %63)
%65 = load %struct.TYPE_4__*, %struct.TYPE_4__** %3
%66 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %65, i32 0, i32 1
%67 = load i32, i32* %66
%68 = sext i32 %67 to i64
%69 = inttoptr i64 %68 to i8*
%70 = load %struct.TYPE_5__*, %struct.TYPE_5__** %2
%71 = bitcast %struct.TYPE_5__* %70 to i8*
%72 = load %struct.TYPE_4__*, %struct.TYPE_4__** %3
%73 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %72, i32 0, i32 3
%74 = load i32, i32* %73
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8, i8* %71, i64 %75
%77 = load %struct.TYPE_4__*, %struct.TYPE_4__** %3
%78 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %77, i32 0, i32 2
%79 = load i32, i32* %78
%80 = call i32 @copy(i8* %69, i8* %76, i32 %79, i32 0)
br label %81
81: ; preds = %33, %32
%82 = load i32, i32* %9
%83 = add nsw i32 %82, 1
store i32 %83, i32* %9
%84 = load %struct.TYPE_4__*, %struct.TYPE_4__** %3
%85 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %84, i32 1
store %struct.TYPE_4__* %85, %struct.TYPE_4__** %3
br label %22
86: ; preds = %22
ret void
}

LLVM IR (-O2):
%struct.TYPE_5__ = type { i32, i32 }
%struct.TYPE_4__ = type { i64, i32, i32, i32 }
@PT_LOAD = global i64 0
define void @pt_load(%struct.TYPE_5__* %0) {
%2 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %0, i64 0, i32 1
%3 = load i32, i32* %2
%4 = bitcast %struct.TYPE_5__* %0 to i8*
%5 = icmp sgt i32 %3, 0
br i1 %5, label %6, label %52
6: ; preds = %1
%7 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %0, i64 0, i32 0
%8 = load i32, i32* %7
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %4, i64 %9
%11 = bitcast i8* %10 to %struct.TYPE_4__*
%12 = load i64, i64* @PT_LOAD
br label %13
13: ; preds = %6, %47
%14 = phi i64 [ %48, %47 ], [ %12, %6 ]
%15 = phi i32 [ %49, %47 ], [ 0, %6 ]
%16 = phi %struct.TYPE_4__* [ %50, %47 ], [ %11, %6 ]
%17 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %16, i64 0, i32 0
%18 = load i64, i64* %17
%19 = icmp eq i64 %18, %14
br i1 %19, label %20, label %47
20: ; preds = %13
%21 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %16, i64 0, i32 1
%22 = load i32, i32* %21
%23 = and i32 %22, -4096
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %16, i64 0, i32 2
%26 = load i32, i32* %25
%27 = and i32 %22, 4095
%28 = add nsw i32 %26, %27
%29 = sext i32 %28 to i64
%30 = add nsw i64 %29, 4095
%31 = and i64 %30, -4096
%32 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %16, i64 0, i32 3
%33 = load i32, i32* %32
%34 = sub nsw i32 %33, %27
%35 = sext i32 %34 to i64
%36 = inttoptr i64 %24 to i8*
%37 = tail call i32 @_mmap(i8* %36, i64 %31, i64 %35)
%38 = load i32, i32* %21
%39 = sext i32 %38 to i64
%40 = inttoptr i64 %39 to i8*
%41 = load i32, i32* %32
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %4, i64 %42
%44 = load i32, i32* %25
%45 = tail call i32 @copy(i8* %40, i8* %43, i32 %44, i32 0)
%46 = load i64, i64* @PT_LOAD
br label %47
47: ; preds = %13, %20
%48 = phi i64 [ %14, %13 ], [ %46, %20 ]
%49 = add nuw nsw i32 %15, 1
%50 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %16, i64 1
%51 = icmp eq i32 %49, %3
br i1 %51, label %52, label %13
52: ; preds = %47, %1
ret void
}