#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_4__ TYPE_2__;
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct stat {
  int st_size;
};
struct TYPE_4__ {
  int e_phoff;
  int e_phnum;
};
struct TYPE_3__ {
  scalar_t__ p_type;
  int p_offset;
  int p_vaddr;
  int p_filesz;
  int p_memsz;
};
typedef TYPE_1__ Elf32_Phdr;
typedef TYPE_2__ Elf32_Ehdr;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int MAP_ANONYMOUS;
unsigned char *MAP_FAILED;
int MAP_PRIVATE;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
int PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
scalar_t__ PT_LOAD;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fstat(int, struct stat *);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
unsigned char *mmap(int /*<<< orphan*/ *, int, int, int, int,
                    int /*<<< orphan*/);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int /*<<< orphan*/);
int write(int, unsigned char *, int);
int main(int argc, char **argv) {
  int fd;
  struct stat statbuf;
  unsigned char *data;
  unsigned char *mapping;
  Elf32_Ehdr *ehdr;
  Elf32_Phdr *phdr;
  int i;
  int used = 0;
  unsigned char *source, *dest;
  int len;
  if (argc < 3) {
    printf("elf2bin [input file] [output file]\n");
    exit(EXIT_FAILURE);
  }
  fd = open(argv[1], O_RDONLY);
  if (fd == -1) {
    printf("Failed to open %s: %s\n", argv[1], strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (fstat(fd, &statbuf) == -1) {
    printf("Failed to fstat(fd): %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  data = mmap(NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  if (data == MAP_FAILED) {
    printf("Unable to read ELF file in: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  close(fd);
  mapping = mmap(NULL, 0x1000000, PROT_READ | PROT_WRITE | PROT_EXEC,
                 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  if (mapping == MAP_FAILED) {
    printf("Failed to mmap(): %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  memset(mapping, 0, 0x1000000);
  ehdr = (Elf32_Ehdr *)data;
  phdr = (Elf32_Phdr *)(data + ehdr->e_phoff);
  printf("data @ %p, mapping @ %p\n", data, mapping);
  for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
    if (phdr->p_type == PT_LOAD) {
      source = data + phdr->p_offset;
      dest = mapping + phdr->p_vaddr;
      len = phdr->p_filesz;
      printf("memcpy(%p, %p, %08x)\n", dest, source, len);
      memcpy(dest, source, len);
      used = phdr->p_memsz + phdr->p_vaddr;
    }
  }
  fd = open(argv[2], O_RDWR | O_TRUNC | O_CREAT, 0644);
  if (fd == -1) {
    printf("Unable to dump memory: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (write(fd, mapping, used) != used) {
    printf("Unable to complete memory dump\n");
    exit(EXIT_FAILURE);
  }
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x40(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11e3 <main+0x43>
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1245 <main+0xa5>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x60(%rbp)
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x60(%rbp),%rsi
mov    %rax,%rdx
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10c0 <fstat@plt>
cmp    $0xffffffff,%eax
jne    1287 <main+0xe7>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xdc9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%esi
mov    0x2d57(%rip),%rax
mov    (%rax),%edx
mov    0x2cee(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2cc5(%rip),%rcx
cmp    (%rcx),%rax
jne    12f1 <main+0x151>
mov    0x2cd5(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xd78(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2cae(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    0x2ce8(%rip),%rax
mov    (%rax),%edx
mov    0x2c87(%rip),%rax
or     (%rax),%edx
mov    0x2c9e(%rip),%rax
or     (%rax),%edx
mov    0x2c9d(%rip),%rax
mov    (%rax),%ecx
mov    0x2c64(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1000000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2c34(%rip),%rcx
cmp    (%rcx),%rax
jne    1382 <main+0x1e2>
mov    0x2c44(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xd07(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2c1d(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000000,%edx
call   1060 <memset@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
movslq (%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xccf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    -0x30(%rbp),%rcx
cmp    0x4(%rcx),%eax
jge    1479 <main+0x2d9>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    0x2bd9(%rip),%rcx
cmp    (%rcx),%rax
jne    145a <main+0x2ba>
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%rcx
movslq 0x8(%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%rcx
movslq 0xc(%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x38(%rbp),%rax
mov    0x10(%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x50(%rbp),%rsi
mov    -0x48(%rbp),%rdx
mov    -0x54(%rbp),%ecx
lea    0xc71(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x54(%rbp),%edx
call   1080 <memcpy@plt>
mov    -0x38(%rbp),%rax
mov    0x14(%rax),%eax
mov    -0x38(%rbp),%rcx
add    0xc(%rcx),%eax
mov    %eax,-0x40(%rbp)
jmp    145f <main+0x2bf>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%rax
add    $0x18,%rax
mov    %rax,-0x38(%rbp)
jmp    13c9 <main+0x229>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2b48(%rip),%rax
mov    (%rax),%esi
mov    0x2b5f(%rip),%rax
or     (%rax),%esi
mov    0x2b5e(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    14e2 <main+0x342>
mov    0x2ae4(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xbec(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2abd(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x40(%rbp),%edx
call   1030 <write@plt>
cmp    -0x40(%rbp),%eax
je     1516 <main+0x376>
lea    0xbd3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2a89(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x2,%edi
jle    13bf <main+0x20f>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     13cb <main+0x21b>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %eax,%edi
call   10d0 <fstat@plt>
cmp    $0xffffffff,%eax
je     13f3 <main+0x243>
mov    %rbp,0x18(%rsp)
mov    0x10(%rsp),%esi
mov    0x2dda(%rip),%rbp
mov    0x0(%rbp),%edx
mov    0x2d70(%rip),%r15
mov    (%r15),%ecx
xor    %edi,%edi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    0x2d51(%rip),%r14
mov    %rax,%rcx
mov    %rax,0x8(%rsp)
cmp    (%r14),%rax
je     140a <main+0x25a>
mov    %ebx,%edi
call   1080 <close@plt>
mov    0x2d42(%rip),%rax
mov    (%rax),%edx
or     0x0(%rbp),%edx
mov    0x2d56(%rip),%rax
or     (%rax),%edx
mov    0x2d55(%rip),%rax
mov    (%r15),%ecx
or     (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    $0x1000000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
cmp    (%r14),%rax
je     1421 <main+0x271>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x1000000,%edx
call   1070 <memset@plt>
mov    0x8(%rsp),%rbp
movslq 0x0(%rbp),%r15
lea    0xdbc(%rip),%rdi
mov    %rbp,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x4(%rbp),%eax
test   %eax,%eax
jle    1358 <main+0x1a8>
mov    0x2cf7(%rip),%rcx
mov    (%rcx),%rcx
mov    0x8(%rsp),%rdx
lea    (%r15,%rdx,1),%r13
add    $0x14,%r13
xor    %r15d,%r15d
xor    %ebx,%ebx
mov    %r14,0x20(%rsp)
mov    0x8(%rsp),%r12
jmp    12fd <main+0x14d>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r15d
add    $0x18,%r13
cmp    %eax,%r15d
jge    1358 <main+0x1a8>
cmp    %rcx,-0x14(%r13)
jne    12f0 <main+0x140>
movslq -0xc(%r13),%rbx
add    %r12,%rbx
movslq -0x8(%r13),%rbp
add    %r14,%rbp
mov    -0x4(%r13),%r14d
lea    0xd63(%rip),%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
mov    %r14d,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
mov    0x20(%rsp),%r14
call   1090 <memcpy@plt>
mov    -0x8(%r13),%ebx
add    0x0(%r13),%ebx
mov    0x2c72(%rip),%rax
mov    (%rax),%rcx
mov    0x4(%r12),%eax
jmp    12f0 <main+0x140>
mov    0x2c71(%rip),%rax
mov    0x2c8a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x18(%rsp),%rax
mov    0x10(%rax),%rdi
mov    0x2c7e(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
xor    %eax,%eax
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1438 <main+0x288>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
cmp    %ebx,%eax
jne    1459 <main+0x2a9>
mov    %ebp,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd09(%rip),%rdi
jmp    1460 <main+0x2b0>
mov    0x8(%rbp),%rbx
mov    0x2bca(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xc1c(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1465 <main+0x2b5>
mov    0x2ba6(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xc0f(%rip),%rdi
jmp    144d <main+0x29d>
mov    0x2b8f(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xc11(%rip),%rdi
jmp    144d <main+0x29d>
mov    0x2b78(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xc1a(%rip),%rdi
jmp    144d <main+0x29d>
mov    0x2b61(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xc48(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1465 <main+0x2b5>
lea    0xc50(%rip),%rdi
call   1030 <puts@plt>
mov    0x2b2c(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.TYPE_4__ = type { i32, i32 }
%struct.TYPE_3__ = type { i64, i32, i32, i32, i32 }
@.str = [36 x i8] c"elf2bin [input file] [output file]\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [23 x i8] c"Failed to open %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [25 x i8] c"Failed to fstat(fd): %s\0A\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [32 x i8] c"Unable to read ELF file in: %s\0A\00"
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@MAP_ANONYMOUS = global i32 0
@.str.4 = [22 x i8] c"Failed to mmap(): %s\0A\00"
@.str.5 = [25 x i8] c"data @ %p, mapping @ %p\0A\00"
@PT_LOAD = global i64 0
@.str.6 = [22 x i8] c"memcpy(%p, %p, %08x)\0A\00"
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str.7 = [27 x i8] c"Unable to dump memory: %s\0A\00"
@.str.8 = [32 x i8] c"Unable to complete memory dump\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca %struct.TYPE_4__*
%11 = alloca %struct.TYPE_3__*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i8*
%16 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %13
%17 = load i32, i32* %4
%18 = icmp slt i32 %17, 3
br i1 %18, label %19, label %23
19: ; preds = %2
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = load i32, i32* @O_RDONLY
%28 = call i32 (i8*, i32, ...) @open(i8* %26, i32 %27)
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %40
31: ; preds = %23
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %34, i8* %36)
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %23
%41 = load i32, i32* %6
%42 = call i32 @fstat(i32 %41, %struct.stat* %7)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %50
44: ; preds = %40
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %46)
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %40
%51 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%52 = load i32, i32* %51
%53 = load i32, i32* @PROT_READ
%54 = load i32, i32* @MAP_PRIVATE
%55 = load i32, i32* %6
%56 = call i8* @mmap(i32* null, i32 %52, i32 %53, i32 %54, i32 %55, i32 0)
store i8* %56, i8** %8
%57 = load i8*, i8** %8
%58 = load i8*, i8** @MAP_FAILED
%59 = icmp eq i8* %57, %58
br i1 %59, label %60, label %66
60: ; preds = %50
%61 = load i32, i32* @errno
%62 = call i8* @strerror(i32 %61)
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %62)
%64 = load i32, i32* @EXIT_FAILURE
%65 = call i32 @exit(i32 %64)
unreachable
66: ; preds = %50
%67 = load i32, i32* %6
%68 = call i32 @close(i32 %67)
%69 = load i32, i32* @PROT_READ
%70 = load i32, i32* @PROT_WRITE
%71 = or i32 %69, %70
%72 = load i32, i32* @PROT_EXEC
%73 = or i32 %71, %72
%74 = load i32, i32* @MAP_ANONYMOUS
%75 = load i32, i32* @MAP_PRIVATE
%76 = or i32 %74, %75
%77 = call i8* @mmap(i32* null, i32 16777216, i32 %73, i32 %76, i32 -1, i32 0)
store i8* %77, i8** %9
%78 = load i8*, i8** %9
%79 = load i8*, i8** @MAP_FAILED
%80 = icmp eq i8* %78, %79
br i1 %80, label %81, label %87
81: ; preds = %66
%82 = load i32, i32* @errno
%83 = call i8* @strerror(i32 %82)
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %83)
%85 = load i32, i32* @EXIT_FAILURE
%86 = call i32 @exit(i32 %85)
unreachable
87: ; preds = %66
%88 = load i8*, i8** %9
%89 = call i32 @memset(i8* %88, i32 0, i32 16777216)
%90 = load i8*, i8** %8
%91 = bitcast i8* %90 to %struct.TYPE_4__*
store %struct.TYPE_4__* %91, %struct.TYPE_4__** %10
%92 = load i8*, i8** %8
%93 = load %struct.TYPE_4__*, %struct.TYPE_4__** %10
%94 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %93, i32 0, i32 0
%95 = load i32, i32* %94
%96 = sext i32 %95 to i64
%97 = getelementptr inbounds i8, i8* %92, i64 %96
%98 = bitcast i8* %97 to %struct.TYPE_3__*
store %struct.TYPE_3__* %98, %struct.TYPE_3__** %11
%99 = load i8*, i8** %8
%100 = load i8*, i8** %9
%101 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %99, i8* %100)
store i32 0, i32* %12
br label %102
102: ; preds = %146, %87
%103 = load i32, i32* %12
%104 = load %struct.TYPE_4__*, %struct.TYPE_4__** %10
%105 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %104, i32 0, i32 1
%106 = load i32, i32* %105
%107 = icmp slt i32 %103, %106
br i1 %107, label %108, label %151
108: ; preds = %102
%109 = load %struct.TYPE_3__*, %struct.TYPE_3__** %11
%110 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %109, i32 0, i32 0
%111 = load i64, i64* %110
%112 = load i64, i64* @PT_LOAD
%113 = icmp eq i64 %111, %112
br i1 %113, label %114, label %145
114: ; preds = %108
%115 = load i8*, i8** %8
%116 = load %struct.TYPE_3__*, %struct.TYPE_3__** %11
%117 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %116, i32 0, i32 1
%118 = load i32, i32* %117
%119 = sext i32 %118 to i64
%120 = getelementptr inbounds i8, i8* %115, i64 %119
store i8* %120, i8** %14
%121 = load i8*, i8** %9
%122 = load %struct.TYPE_3__*, %struct.TYPE_3__** %11
%123 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %122, i32 0, i32 2
%124 = load i32, i32* %123
%125 = sext i32 %124 to i64
%126 = getelementptr inbounds i8, i8* %121, i64 %125
store i8* %126, i8** %15
%127 = load %struct.TYPE_3__*, %struct.TYPE_3__** %11
%128 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %127, i32 0, i32 3
%129 = load i32, i32* %128
store i32 %129, i32* %16
%130 = load i8*, i8** %15
%131 = load i8*, i8** %14
%132 = load i32, i32* %16
%133 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i8* %130, i8* %131, i32 %132)
%134 = load i8*, i8** %15
%135 = load i8*, i8** %14
%136 = load i32, i32* %16
%137 = call i32 @memcpy(i8* %134, i8* %135, i32 %136)
%138 = load %struct.TYPE_3__*, %struct.TYPE_3__** %11
%139 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %138, i32 0, i32 4
%140 = load i32, i32* %139
%141 = load %struct.TYPE_3__*, %struct.TYPE_3__** %11
%142 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %141, i32 0, i32 2
%143 = load i32, i32* %142
%144 = add nsw i32 %140, %143
store i32 %144, i32* %13
br label %145
145: ; preds = %114, %108
br label %146
146: ; preds = %145
%147 = load i32, i32* %12
%148 = add nsw i32 %147, 1
store i32 %148, i32* %12
%149 = load %struct.TYPE_3__*, %struct.TYPE_3__** %11
%150 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %149, i32 1
store %struct.TYPE_3__* %150, %struct.TYPE_3__** %11
br label %102
151: ; preds = %102
%152 = load i8**, i8*** %5
%153 = getelementptr inbounds i8*, i8** %152, i64 2
%154 = load i8*, i8** %153
%155 = load i32, i32* @O_RDWR
%156 = load i32, i32* @O_TRUNC
%157 = or i32 %155, %156
%158 = load i32, i32* @O_CREAT
%159 = or i32 %157, %158
%160 = call i32 (i8*, i32, ...) @open(i8* %154, i32 %159, i32 420)
store i32 %160, i32* %6
%161 = load i32, i32* %6
%162 = icmp eq i32 %161, -1
br i1 %162, label %163, label %169
163: ; preds = %151
%164 = load i32, i32* @errno
%165 = call i8* @strerror(i32 %164)
%166 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0), i8* %165)
%167 = load i32, i32* @EXIT_FAILURE
%168 = call i32 @exit(i32 %167)
unreachable
169: ; preds = %151
%170 = load i32, i32* %6
%171 = load i8*, i8** %9
%172 = load i32, i32* %13
%173 = call i32 @write(i32 %170, i8* %171, i32 %172)
%174 = load i32, i32* %13
%175 = icmp ne i32 %173, %174
br i1 %175, label %176, label %180
176: ; preds = %169
%177 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0))
%178 = load i32, i32* @EXIT_FAILURE
%179 = call i32 @exit(i32 %178)
unreachable
180: ; preds = %169
%181 = load i32, i32* %6
%182 = call i32 @close(i32 %181)
%183 = load i32, i32* %3
ret i32 %183
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.TYPE_3__ = type { i64, i32, i32, i32, i32 }
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [23 x i8] c"Failed to open %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [25 x i8] c"Failed to fstat(fd): %s\0A\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [32 x i8] c"Unable to read ELF file in: %s\0A\00"
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@MAP_ANONYMOUS = global i32 0
@.str.4 = [22 x i8] c"Failed to mmap(): %s\0A\00"
@.str.5 = [25 x i8] c"data @ %p, mapping @ %p\0A\00"
@PT_LOAD = global i64 0
@.str.6 = [22 x i8] c"memcpy(%p, %p, %08x)\0A\00"
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str.7 = [27 x i8] c"Unable to dump memory: %s\0A\00"
@str = [31 x i8] c"Unable to complete memory dump\00"
@str.9 = [35 x i8] c"elf2bin [input file] [output file]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp slt i32 %0, 3
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.9, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 (i8*, i32, ...) @open(i8* %12, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %23
16: ; preds = %10
%17 = load i8*, i8** %11
%18 = load i32, i32* @errno
%19 = tail call i8* @strerror(i32 %18)
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %17, i8* %19)
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %10
%24 = call i32 @fstat(i32 %14, %struct.stat* %3)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %32
26: ; preds = %23
%27 = load i32, i32* @errno
%28 = tail call i8* @strerror(i32 %27)
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %28)
%30 = load i32, i32* @EXIT_FAILURE
%31 = tail call i32 @exit(i32 %30)
unreachable
32: ; preds = %23
%33 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%34 = load i32, i32* %33
%35 = load i32, i32* @PROT_READ
%36 = load i32, i32* @MAP_PRIVATE
%37 = tail call i8* @mmap(i32* null, i32 %34, i32 %35, i32 %36, i32 %14, i32 0)
%38 = load i8*, i8** @MAP_FAILED
%39 = icmp eq i8* %37, %38
br i1 %39, label %40, label %46
40: ; preds = %32
%41 = load i32, i32* @errno
%42 = tail call i8* @strerror(i32 %41)
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %42)
%44 = load i32, i32* @EXIT_FAILURE
%45 = tail call i32 @exit(i32 %44)
unreachable
46: ; preds = %32
%47 = tail call i32 @close(i32 %14)
%48 = load i32, i32* @PROT_READ
%49 = load i32, i32* @PROT_WRITE
%50 = or i32 %49, %48
%51 = load i32, i32* @PROT_EXEC
%52 = or i32 %50, %51
%53 = load i32, i32* @MAP_ANONYMOUS
%54 = load i32, i32* @MAP_PRIVATE
%55 = or i32 %54, %53
%56 = tail call i8* @mmap(i32* null, i32 16777216, i32 %52, i32 %55, i32 -1, i32 0)
%57 = load i8*, i8** @MAP_FAILED
%58 = icmp eq i8* %56, %57
br i1 %58, label %59, label %65
59: ; preds = %46
%60 = load i32, i32* @errno
%61 = tail call i8* @strerror(i32 %60)
%62 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %61)
%63 = load i32, i32* @EXIT_FAILURE
%64 = tail call i32 @exit(i32 %63)
unreachable
65: ; preds = %46
%66 = tail call i32 @memset(i8* %56, i32 0, i32 16777216)
%67 = bitcast i8* %37 to i32*
%68 = load i32, i32* %67
%69 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %37, i8* %56)
%70 = getelementptr inbounds i8, i8* %37, i64 4
%71 = bitcast i8* %70 to i32*
%72 = load i32, i32* %71
%73 = icmp sgt i32 %72, 0
br i1 %73, label %74, label %114
74: ; preds = %65
%75 = sext i32 %68 to i64
%76 = getelementptr inbounds i8, i8* %37, i64 %75
%77 = bitcast i8* %76 to %struct.TYPE_3__*
%78 = load i64, i64* @PT_LOAD
br label %79
79: ; preds = %74, %107
%80 = phi i32 [ %108, %107 ], [ %72, %74 ]
%81 = phi i64 [ %109, %107 ], [ %78, %74 ]
%82 = phi %struct.TYPE_3__* [ %112, %107 ], [ %77, %74 ]
%83 = phi i32 [ %111, %107 ], [ 0, %74 ]
%84 = phi i32 [ %110, %107 ], [ 0, %74 ]
%85 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %82, i64 0, i32 0
%86 = load i64, i64* %85
%87 = icmp eq i64 %86, %81
br i1 %87, label %88, label %107
88: ; preds = %79
%89 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %82, i64 0, i32 1
%90 = load i32, i32* %89
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds i8, i8* %37, i64 %91
%93 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %82, i64 0, i32 2
%94 = load i32, i32* %93
%95 = sext i32 %94 to i64
%96 = getelementptr inbounds i8, i8* %56, i64 %95
%97 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %82, i64 0, i32 3
%98 = load i32, i32* %97
%99 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i8* %96, i8* %92, i32 %98)
%100 = tail call i32 @memcpy(i8* %96, i8* %92, i32 %98)
%101 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %82, i64 0, i32 4
%102 = load i32, i32* %101
%103 = load i32, i32* %93
%104 = add nsw i32 %103, %102
%105 = load i64, i64* @PT_LOAD
%106 = load i32, i32* %71
br label %107
107: ; preds = %79, %88
%108 = phi i32 [ %106, %88 ], [ %80, %79 ]
%109 = phi i64 [ %105, %88 ], [ %81, %79 ]
%110 = phi i32 [ %104, %88 ], [ %84, %79 ]
%111 = add nuw nsw i32 %83, 1
%112 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %82, i64 1
%113 = icmp slt i32 %111, %108
br i1 %113, label %79, label %114
114: ; preds = %107, %65
%115 = phi i32 [ 0, %65 ], [ %110, %107 ]
%116 = getelementptr inbounds i8*, i8** %1, i64 2
%117 = load i8*, i8** %116
%118 = load i32, i32* @O_RDWR
%119 = load i32, i32* @O_TRUNC
%120 = or i32 %119, %118
%121 = load i32, i32* @O_CREAT
%122 = or i32 %120, %121
%123 = tail call i32 (i8*, i32, ...) @open(i8* %117, i32 %122, i32 420)
%124 = icmp eq i32 %123, -1
br i1 %124, label %125, label %131
125: ; preds = %114
%126 = load i32, i32* @errno
%127 = tail call i8* @strerror(i32 %126)
%128 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0), i8* %127)
%129 = load i32, i32* @EXIT_FAILURE
%130 = tail call i32 @exit(i32 %129)
unreachable
131: ; preds = %114
%132 = tail call i32 @write(i32 %123, i8* %56, i32 %115)
%133 = icmp eq i32 %132, %115
br i1 %133, label %138, label %134
134: ; preds = %131
%135 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%136 = load i32, i32* @EXIT_FAILURE
%137 = tail call i32 @exit(i32 %136)
unreachable
138: ; preds = %131
%139 = tail call i32 @close(i32 %123)
ret i32 0
}