#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 {
  unsigned int st_size;
};
typedef int /*<<< orphan*/ magic;

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ unlink(char *);
int write(int, unsigned int *, int);
int main(int argc, char *argv[]) {
  struct stat s;
  unsigned size, base;
  unsigned magic[10];
  int fd;
  if (argc != 3) {
    fprintf(stderr, "usage: mkheader <bin> <hdr>\n");
    return -1;
  }
  if (stat(argv[1], &s)) {
    perror("cannot stat binary");
    return -1;
  }
  size = s.st_size;
  base = 0xE0000000;
  magic[0] = 0x00000005;
  magic[1] = 0x00000002;
  magic[2] = 0x00000000;
  magic[3] = base;
  magic[4] = size;
  magic[5] = size;
  magic[6] = size + base;
  magic[7] = 0x00000000;
  magic[8] = size + base;
  magic[9] = 0x00000000;
  fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
  if (fd < 0) {
    perror("cannot open header for writing");
    return -1;
  }
  if (write(fd, magic, sizeof(magic)) != sizeof(magic)) {
    perror("cannot write header");
    close(fd);
    unlink(argv[2]);
    return -1;
  }
  close(fd);
  return 0;
}


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)
cmpl   $0x3,-0x8(%rbp)
je     11b1 <main+0x41>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e3 <main+0x173>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     11e4 <main+0x74>
lea    0xe4a(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e3 <main+0x173>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
movl   $0xe0000000,-0x20(%rbp)
movl   $0x5,-0x50(%rbp)
movl   $0x2,-0x4c(%rbp)
movl   $0x0,-0x48(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x38(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
mov    0x2da8(%rip),%rax
or     (%rax),%esi
mov    0x2d97(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jge    128a <main+0x11a>
lea    0xdb7(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e3 <main+0x173>
mov    -0x54(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x28,%edx
call   1040 <write@plt>
cltq
cmp    $0x28,%rax
je     12d4 <main+0x164>
lea    0xda1(%rip),%rdi
call   1090 <perror@plt>
mov    -0x54(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e3 <main+0x173>
mov    -0x54(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
cmp    $0x3,%edi
jne    121e <main+0xae>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0x8(%rsp),%rsi
call   1070 <stat@plt>
test   %rax,%rax
jne    123f <main+0xcf>
mov    0x8(%rsp),%eax
movaps 0xe5c(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
mov    %eax,0x20(%rsp)
mov    %eax,0x24(%rsp)
add    $0xe0000000,%eax
mov    %eax,0x28(%rsp)
movl   $0x0,0x2c(%rsp)
mov    %eax,0x30(%rsp)
movl   $0x0,0x34(%rsp)
mov    0x2deb(%rip),%rax
mov    0x2e1c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x10(%rbx),%rdi
mov    0x2e05(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
test   %eax,%eax
js     1248 <main+0xd8>
mov    %eax,%ebp
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x28,%edx
call   1040 <write@plt>
cmp    $0x28,%eax
jne    1256 <main+0xe6>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    1238 <main+0xc8>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
lea    0xde2(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xde7(%rip),%rdi
jmp    124f <main+0xdf>
lea    0xdf1(%rip),%rdi
call   1090 <perror@plt>
jmp    1233 <main+0xc3>
lea    0xe02(%rip),%rdi
call   1090 <perror@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x10(%rbx),%rdi
call   1030 <unlink@plt>
jmp    1233 <main+0xc3>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [29 x i8] c"usage: mkheader <bin> <hdr>\0A\00"
@.str.1 = [19 x i8] c"cannot stat binary\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [31 x i8] c"cannot open header for writing\00"
@.str.3 = [20 x i8] c"cannot write header\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i32
%9 = alloca [10 x i32]
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %76
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i64 @stat(i8* %19, %struct.stat* %6)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %76
24: ; preds = %16
%25 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%26 = load i32, i32* %25
store i32 %26, i32* %7
store i32 -536870912, i32* %8
%27 = getelementptr inbounds [10 x i32], [10 x i32]* %9, i64 0, i64 0
store i32 5, i32* %27
%28 = getelementptr inbounds [10 x i32], [10 x i32]* %9, i64 0, i64 1
store i32 2, i32* %28
%29 = getelementptr inbounds [10 x i32], [10 x i32]* %9, i64 0, i64 2
store i32 0, i32* %29
%30 = load i32, i32* %8
%31 = getelementptr inbounds [10 x i32], [10 x i32]* %9, i64 0, i64 3
store i32 %30, i32* %31
%32 = load i32, i32* %7
%33 = getelementptr inbounds [10 x i32], [10 x i32]* %9, i64 0, i64 4
store i32 %32, i32* %33
%34 = load i32, i32* %7
%35 = getelementptr inbounds [10 x i32], [10 x i32]* %9, i64 0, i64 5
store i32 %34, i32* %35
%36 = load i32, i32* %7
%37 = load i32, i32* %8
%38 = add i32 %36, %37
%39 = getelementptr inbounds [10 x i32], [10 x i32]* %9, i64 0, i64 6
store i32 %38, i32* %39
%40 = getelementptr inbounds [10 x i32], [10 x i32]* %9, i64 0, i64 7
store i32 0, i32* %40
%41 = load i32, i32* %7
%42 = load i32, i32* %8
%43 = add i32 %41, %42
%44 = getelementptr inbounds [10 x i32], [10 x i32]* %9, i64 0, i64 8
store i32 %43, i32* %44
%45 = getelementptr inbounds [10 x i32], [10 x i32]* %9, i64 0, i64 9
store i32 0, i32* %45
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 2
%48 = load i8*, i8** %47
%49 = load i32, i32* @O_WRONLY
%50 = load i32, i32* @O_CREAT
%51 = or i32 %49, %50
%52 = load i32, i32* @O_TRUNC
%53 = or i32 %51, %52
%54 = call i32 @open(i8* %48, i32 %53, i32 420)
store i32 %54, i32* %10
%55 = load i32, i32* %10
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %24
%58 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %76
59: ; preds = %24
%60 = load i32, i32* %10
%61 = getelementptr inbounds [10 x i32], [10 x i32]* %9, i64 0, i64 0
%62 = call i32 @write(i32 %60, i32* %61, i32 40)
%63 = sext i32 %62 to i64
%64 = icmp ne i64 %63, 40
br i1 %64, label %65, label %73
65: ; preds = %59
%66 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%67 = load i32, i32* %10
%68 = call i32 @close(i32 %67)
%69 = load i8**, i8*** %5
%70 = getelementptr inbounds i8*, i8** %69, i64 2
%71 = load i8*, i8** %70
%72 = call i32 @unlink(i8* %71)
store i32 -1, i32* %3
br label %76
73: ; preds = %59
%74 = load i32, i32* %10
%75 = call i32 @close(i32 %74)
store i32 0, i32* %3
br label %76
76: ; preds = %73, %65, %57, %22, %13
%77 = load i32, i32* %3
ret i32 %77
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [29 x i8] c"usage: mkheader <bin> <hdr>\0A\00"
@.str.1 = [19 x i8] c"cannot stat binary\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [31 x i8] c"cannot open header for writing\00"
@.str.3 = [20 x i8] c"cannot write header\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca [10 x i32]
%5 = bitcast %struct.stat* %3 to i8*
%6 = bitcast [10 x i32]* %4 to i8*
%7 = icmp eq i32 %0, 3
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %51
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = call i64 @stat(i8* %13, %struct.stat* %3)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %11
%17 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %51
18: ; preds = %11
%19 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%20 = load i32, i32* %19
%21 = bitcast [10 x i32]* %4 to <4 x i32>*
store <4 x i32> <i32 5, i32 2, i32 0, i32 -536870912>, <4 x i32>* %21
%22 = getelementptr inbounds [10 x i32], [10 x i32]* %4, i64 0, i64 4
store i32 %20, i32* %22
%23 = getelementptr inbounds [10 x i32], [10 x i32]* %4, i64 0, i64 5
store i32 %20, i32* %23
%24 = add i32 %20, -536870912
%25 = getelementptr inbounds [10 x i32], [10 x i32]* %4, i64 0, i64 6
store i32 %24, i32* %25
%26 = getelementptr inbounds [10 x i32], [10 x i32]* %4, i64 0, i64 7
store i32 0, i32* %26
%27 = getelementptr inbounds [10 x i32], [10 x i32]* %4, i64 0, i64 8
store i32 %24, i32* %27
%28 = getelementptr inbounds [10 x i32], [10 x i32]* %4, i64 0, i64 9
store i32 0, i32* %28
%29 = getelementptr inbounds i8*, i8** %1, i64 2
%30 = load i8*, i8** %29
%31 = load i32, i32* @O_WRONLY
%32 = load i32, i32* @O_CREAT
%33 = or i32 %32, %31
%34 = load i32, i32* @O_TRUNC
%35 = or i32 %33, %34
%36 = call i32 @open(i8* %30, i32 %35, i32 420)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %18
%39 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %51
40: ; preds = %18
%41 = getelementptr inbounds [10 x i32], [10 x i32]* %4, i64 0, i64 0
%42 = call i32 @write(i32 %36, i32* %41, i32 40)
%43 = icmp eq i32 %42, 40
br i1 %43, label %49, label %44
44: ; preds = %40
%45 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%46 = call i32 @close(i32 %36)
%47 = load i8*, i8** %29
%48 = call i32 @unlink(i8* %47)
br label %51
49: ; preds = %40
%50 = call i32 @close(i32 %36)
br label %51
51: ; preds = %49, %44, %38, %16, %8
%52 = phi i32 [ -1, %8 ], [ -1, %16 ], [ -1, %38 ], [ -1, %44 ], [ 0, %49 ]
ret i32 %52
}