#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 */

/* Type definitions */
struct stat {
  int st_size;
  int st_blksize;
};

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int S_IRUSR;
int S_IWUSR;
int S_IXUSR;
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ execv(char *, char **);
int fork();
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ memmove(char **, char **, int);
char *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ wait(int *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  int fd, tfd, status;
  char *file;
  struct stat s;
  if (argc == 1) {
    puts("Usage: hbin FILE");
    return EXIT_SUCCESS;
  }
  if ((fd = open(argv[1], O_RDWR)) == -1) {
    perror("open argv[1]");
    return EXIT_FAILURE;
  }
  if ((tfd = open("/tmp/hbin_binary", O_RDWR | O_CREAT,
                  S_IRUSR | S_IWUSR | S_IXUSR)) == -1) {
    perror("open TMP_FILE");
    return EXIT_FAILURE;
  }
  stat(argv[1], &s);
  file = mmap(NULL, s.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  write(tfd, file + s.st_size, (s.st_blksize - s.st_size) % s.st_blksize);
  memmove(argv, &argv[1], argc);
  argv[argc - 1] = NULL;
  close(tfd);
  munmap(file, s.st_size);
  switch (fork()) {
  case -1:
    perror("fork");
    break;
  case 0:
    execv("/tmp/hbin_binary", argv);
    return EXIT_FAILURE;
  default:
    wait(&status);
    if (!WIFEXITED(status)) {
      fputs("Failed to run the program\n", stderr);
    }
    unlink("/tmp/hbin_binary");
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    122d <main+0x3d>
lea    0xde9(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1419 <main+0x229>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   10c0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    126e <main+0x7e>
lea    0xdb9(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2d44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1419 <main+0x229>
mov    0x2d5b(%rip),%rax
mov    (%rax),%esi
mov    0x2d7a(%rip),%rax
or     (%rax),%esi
mov    0x2d11(%rip),%rax
mov    (%rax),%edx
mov    0x2d30(%rip),%rax
or     (%rax),%edx
mov    0x2d2f(%rip),%rax
or     (%rax),%edx
lea    0xd7c(%rip),%rdi
mov    $0x0,%al
call   10c0 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    12d2 <main+0xe2>
lea    0xd73(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2ce0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1419 <main+0x229>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x30(%rbp),%rsi
call   1090 <stat@plt>
mov    -0x30(%rbp),%esi
mov    0x2d03(%rip),%rax
mov    (%rax),%edx
mov    0x2caa(%rip),%rax
mov    (%rax),%eax
or     %eax,%edx
mov    0x2cdf(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,-0x38(%rbp)
mov    %r9d,%edi
call   1060 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x28(%rbp),%rsi
movslq -0x30(%rbp),%rax
mov    %eax,%edx
add    %rax,%rsi
mov    -0x2c(%rbp),%ecx
mov    %ecx,%eax
sub    %edx,%eax
cltd
idiv   %ecx
call   1050 <write@plt>
mov    -0x10(%rbp),%rdi
mov    %rdi,%rsi
add    $0x8,%rsi
mov    -0x8(%rbp),%edx
call   10b0 <memmove@plt>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%ecx
add    $0xffffffff,%ecx
movslq %ecx,%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%esi
call   10a0 <munmap@plt>
mov    -0x38(%rbp),%eax
call   1110 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x34(%rbp)
sub    $0xffffffff,%eax
je     13a0 <main+0x1b0>
jmp    1390 <main+0x1a0>
mov    -0x34(%rbp),%eax
test   %eax,%eax
je     13b1 <main+0x1c1>
jmp    13d2 <main+0x1e2>
lea    0xc96(%rip),%rdi
call   10d0 <perror@plt>
jmp    140d <main+0x21d>
mov    -0x10(%rbp),%rsi
lea    0xc62(%rip),%rdi
call   1100 <execv@plt>
mov    0x2be0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1419 <main+0x229>
lea    -0x1c(%rbp),%rdi
call   10f0 <wait@plt>
mov    -0x1c(%rbp),%edi
call   10e0 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    1401 <main+0x211>
mov    0x2bbd(%rip),%rax
mov    (%rax),%esi
lea    0xc46(%rip),%rdi
call   1070 <fputs@plt>
lea    0xc16(%rip),%rdi
call   1030 <unlink@plt>
mov    0x2b7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x1,%edi
jne    1214 <main+0x24>
lea    0xdf6(%rip),%rdi
call   1040 <puts@plt>
jmp    1352 <main+0x162>
mov    %rsi,%r13
mov    %edi,%r12d
mov    0x8(%rsi),%rdi
mov    0x2dab(%rip),%rbx
mov    (%rbx),%esi
xor    %eax,%eax
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     136c <main+0x17c>
mov    %eax,%r15d
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
or     (%rbx),%esi
mov    0x2d4c(%rip),%rax
mov    0x2d6d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d6a(%rip),%rax
or     (%rax),%edx
lea    0xdb7(%rip),%rdi
xor    %eax,%eax
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     1375 <main+0x185>
mov    %eax,%r14d
lea    0x8(%r13),%rax
mov    %rax,0x10(%rsp)
mov    0x8(%r13),%rdi
lea    0x8(%rsp),%rsi
call   1090 <stat@plt>
movslq 0x8(%rsp),%rbp
mov    0x2d53(%rip),%rax
mov    0x2cfc(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d31(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%esi
mov    %r15d,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,%rbx
mov    %rbp,%rsi
add    %rax,%rsi
mov    0xc(%rsp),%ecx
mov    %ecx,%eax
sub    %ebp,%eax
cltd
idiv   %ecx
mov    %r14d,%edi
call   1050 <write@plt>
mov    %r13,%rdi
mov    0x10(%rsp),%rsi
mov    %r12d,%edx
call   10b0 <memmove@plt>
movslq %r12d,%rax
movq   $0x0,-0x8(%r13,%rax,8)
mov    %r14d,%edi
call   1080 <close@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   10a0 <munmap@plt>
xor    %eax,%eax
call   1110 <fork@plt>
test   %eax,%eax
je     135b <main+0x16b>
cmp    $0xffffffff,%eax
je     1399 <main+0x1a9>
lea    0x4(%rsp),%rdi
call   10f0 <wait@plt>
mov    0x4(%rsp),%edi
call   10e0 <WIFEXITED@plt>
test   %eax,%eax
jne    1346 <main+0x156>
mov    0x2c78(%rip),%rax
mov    (%rax),%esi
lea    0xd01(%rip),%rdi
call   1070 <fputs@plt>
lea    0xcd1(%rip),%rdi
call   1030 <unlink@plt>
mov    0x2c37(%rip),%rax
jmp    1388 <main+0x198>
lea    0xcbc(%rip),%rdi
mov    %r13,%rsi
call   1100 <execv@plt>
jmp    1381 <main+0x191>
lea    0xc9e(%rip),%rdi
jmp    137c <main+0x18c>
lea    0xcb3(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2c20(%rip),%rax
mov    (%rax),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc9d(%rip),%rdi
call   10d0 <perror@plt>
jmp    1352 <main+0x162>

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@.str = [17 x i8] c"Usage: hbin FILE\00"
@EXIT_SUCCESS = global i32 0
@O_RDWR = global i32 0
@.str.1 = [13 x i8] c"open argv[1]\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [17 x i8] c"/tmp/hbin_binary\00"
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IXUSR = global i32 0
@.str.3 = [14 x i8] c"open TMP_FILE\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.4 = [5 x i8] c"fork\00"
@.str.5 = [27 x i8] c"Failed to run the program\0A\00"
@stderr = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @puts(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_SUCCESS
store i32 %15, i32* %3
br label %103
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_RDWR
%21 = call i32 (i8*, i32, ...) @open(i8* %19, i32 %20)
store i32 %21, i32* %6
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
store i32 %25, i32* %3
br label %103
26: ; preds = %16
%27 = load i32, i32* @O_RDWR
%28 = load i32, i32* @O_CREAT
%29 = or i32 %27, %28
%30 = load i32, i32* @S_IRUSR
%31 = load i32, i32* @S_IWUSR
%32 = or i32 %30, %31
%33 = load i32, i32* @S_IXUSR
%34 = or i32 %32, %33
%35 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %29, i32 %34)
store i32 %35, i32* %7
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %26
%38 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
store i32 %39, i32* %3
br label %103
40: ; preds = %26
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 1
%43 = load i8*, i8** %42
%44 = call i32 @stat(i8* %43, %struct.stat* %10)
%45 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%46 = load i32, i32* %45
%47 = load i32, i32* @PROT_READ
%48 = load i32, i32* @PROT_WRITE
%49 = or i32 %47, %48
%50 = load i32, i32* @MAP_SHARED
%51 = load i32, i32* %6
%52 = call i8* @mmap(i32* null, i32 %46, i32 %49, i32 %50, i32 %51, i32 0)
store i8* %52, i8** %9
%53 = load i32, i32* %7
%54 = load i8*, i8** %9
%55 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%56 = load i32, i32* %55
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %54, i64 %57
%59 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 1
%60 = load i32, i32* %59
%61 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%62 = load i32, i32* %61
%63 = sub nsw i32 %60, %62
%64 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 1
%65 = load i32, i32* %64
%66 = srem i32 %63, %65
%67 = call i32 @write(i32 %53, i8* %58, i32 %66)
%68 = load i8**, i8*** %5
%69 = load i8**, i8*** %5
%70 = getelementptr inbounds i8*, i8** %69, i64 1
%71 = load i32, i32* %4
%72 = call i32 @memmove(i8** %68, i8** %70, i32 %71)
%73 = load i8**, i8*** %5
%74 = load i32, i32* %4
%75 = sub nsw i32 %74, 1
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i8*, i8** %73, i64 %76
store i8* null, i8** %77
%78 = load i32, i32* %7
%79 = call i32 @close(i32 %78)
%80 = load i8*, i8** %9
%81 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%82 = load i32, i32* %81
%83 = call i32 @munmap(i8* %80, i32 %82)
%84 = call i32 (...) @fork()
switch i32 %84, label %91 [
i32 -1, label %85
i32 0, label %87
]
85: ; preds = %40
%86 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %101
87: ; preds = %40
%88 = load i8**, i8*** %5
%89 = call i32 @execv(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8** %88)
%90 = load i32, i32* @EXIT_FAILURE
store i32 %90, i32* %3
br label %103
91: ; preds = %40
%92 = call i32 @wait(i32* %8)
%93 = load i32, i32* %8
%94 = call i32 @WIFEXITED(i32 %93)
%95 = icmp ne i32 %94, 0
br i1 %95, label %99, label %96
96: ; preds = %91
%97 = load i32, i32* @stderr
%98 = call i32 @fputs(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i32 %97)
br label %99
99: ; preds = %96, %91
%100 = call i32 @unlink(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %101
101: ; preds = %99, %85
%102 = load i32, i32* @EXIT_SUCCESS
store i32 %102, i32* %3
br label %103
103: ; preds = %101, %87, %37, %23, %13
%104 = load i32, i32* %3
ret i32 %104
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@.str = [17 x i8] c"Usage: hbin FILE\00"
@EXIT_SUCCESS = global i32 0
@O_RDWR = global i32 0
@.str.1 = [13 x i8] c"open argv[1]\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [17 x i8] c"/tmp/hbin_binary\00"
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IXUSR = global i32 0
@.str.3 = [14 x i8] c"open TMP_FILE\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.4 = [5 x i8] c"fork\00"
@.str.5 = [27 x i8] c"Failed to run the program\0A\00"
@stderr = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca %struct.stat
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.stat* %4 to i8*
%7 = icmp eq i32 %0, 1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %69
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDWR
%14 = tail call i32 (i8*, i32, ...) @open(i8* %12, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %69
18: ; preds = %10
%19 = load i32, i32* @O_RDWR
%20 = load i32, i32* @O_CREAT
%21 = or i32 %20, %19
%22 = load i32, i32* @S_IRUSR
%23 = load i32, i32* @S_IWUSR
%24 = or i32 %23, %22
%25 = load i32, i32* @S_IXUSR
%26 = or i32 %24, %25
%27 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %21, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %18
%30 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %69
31: ; preds = %18
%32 = load i8*, i8** %11
%33 = call i32 @stat(i8* %32, %struct.stat* %4)
%34 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%35 = load i32, i32* %34
%36 = load i32, i32* @PROT_READ
%37 = load i32, i32* @PROT_WRITE
%38 = or i32 %37, %36
%39 = load i32, i32* @MAP_SHARED
%40 = tail call i8* @mmap(i32* null, i32 %35, i32 %38, i32 %39, i32 %14, i32 0)
%41 = sext i32 %35 to i64
%42 = getelementptr inbounds i8, i8* %40, i64 %41
%43 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%44 = load i32, i32* %43
%45 = sub nsw i32 %44, %35
%46 = srem i32 %45, %44
%47 = tail call i32 @write(i32 %27, i8* %42, i32 %46)
%48 = tail call i32 @memmove(i8** %1, i8** %11, i32 %0)
%49 = add nsw i32 %0, -1
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8*, i8** %1, i64 %50
store i8* null, i8** %51
%52 = tail call i32 @close(i32 %27)
%53 = tail call i32 @munmap(i8* %40, i32 %35)
%54 = tail call i32 (...) @fork()
switch i32 %54, label %59 [
i32 -1, label %55
i32 0, label %57
]
55: ; preds = %31
%56 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %69
57: ; preds = %31
%58 = tail call i32 @execv(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8** %1)
br label %69
59: ; preds = %31
%60 = call i32 @wait(i32* %3)
%61 = load i32, i32* %3
%62 = call i32 @WIFEXITED(i32 %61)
%63 = icmp eq i32 %62, 0
br i1 %63, label %64, label %67
64: ; preds = %59
%65 = load i32, i32* @stderr
%66 = call i32 @fputs(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i32 %65)
br label %67
67: ; preds = %64, %59
%68 = call i32 @unlink(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %69
69: ; preds = %55, %67, %57, %29, %16, %8
%70 = phi i32* [ @EXIT_SUCCESS, %8 ], [ @EXIT_FAILURE, %16 ], [ @EXIT_FAILURE, %29 ], [ @EXIT_FAILURE, %57 ], [ @EXIT_SUCCESS, %67 ], [ @EXIT_SUCCESS, %55 ]
%71 = load i32, i32* %70
ret i32 %71
}