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

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
char *basename(char *);
int /*<<< orphan*/ close(int);
int errno;
unsigned int htonl(int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ strtoul(char *, char **, int /*<<< orphan*/);
int write(int, unsigned int *, int);
int main(int argc, char **argv) {
  char *prog = basename(argv[0]);
  struct stat initrd_sbuf;
  unsigned int zero = 0;
  unsigned int addr, size;
  int fd, rc;
  char *tmp;
  mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
  if (argc != 4) {
    printf("Usage: %s [address] [initrd file] [output file]\n", prog);
    printf("Example: %s 0x2000000 initrd.img initrd.addrsize\n", prog);
    return 0;
  }
  rc = stat(argv[2], &initrd_sbuf);
  if (rc) {
    perror("Error getting initrd stats ");
    return rc;
  }
  addr = htonl(strtoul(argv[1], &tmp, 0));
  size = htonl(initrd_sbuf.st_size);
  fd = open(argv[3], O_CREAT | O_RDWR, mode);
  if (write(fd, &zero, sizeof(int)) == -1) {
    perror("writing first zero");
    return errno;
  }
  if (write(fd, &addr, sizeof(int)) == -1) {
    perror("writing addr");
    return errno;
  }
  if (write(fd, &zero, sizeof(int)) == -1) {
    perror("writing second zero");
    return errno;
  }
  if (write(fd, &size, sizeof(int)) == -1) {
    perror("writing size");
    return errno;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <basename@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    0x2e0b(%rip),%rcx
or     (%rcx),%eax
mov    0x2dea(%rip),%rcx
or     (%rcx),%eax
mov    0x2dd9(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x44(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     121a <main+0x8a>
mov    -0x18(%rbp),%rsi
lea    0xe07(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rsi
lea    0xe28(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1387 <main+0x1f7>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <stat@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     124f <main+0xbf>
lea    0xe24(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1387 <main+0x1f7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x40(%rbp),%rsi
xor    %edx,%edx
call   10b0 <strtoul@plt>
mov    %eax,%edi
call   1050 <htonl@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%edi
call   1050 <htonl@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
mov    0x2d51(%rip),%rax
or     (%rax),%esi
mov    -0x44(%rbp),%edx
call   1090 <open@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    12d3 <main+0x143>
lea    0xdc2(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1387 <main+0x1f7>
mov    -0x30(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    130a <main+0x17a>
lea    0xd9e(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cc8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1387 <main+0x1f7>
mov    -0x30(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1341 <main+0x1b1>
lea    0xd74(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c91(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1387 <main+0x1f7>
mov    -0x30(%rbp),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1378 <main+0x1e8>
lea    0xd51(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1387 <main+0x1f7>
mov    -0x30(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    (%rsi),%rdi
call   1070 <basename@plt>
movl   $0x0,0x4(%rsp)
cmp    $0x4,%ebp
jne    129c <main+0x10c>
mov    0x2df3(%rip),%rax
mov    0x2e14(%rip),%rcx
mov    (%rcx),%ebp
or     (%rax),%ebp
mov    0x2df1(%rip),%rax
or     (%rax),%ebp
mov    0x2de0(%rip),%rax
or     (%rax),%ebp
mov    0x10(%r14),%rdi
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
test   %eax,%eax
jne    12ca <main+0x13a>
mov    0x8(%r14),%rdi
lea    0x18(%rsp),%rsi
xor    %edx,%edx
call   10b0 <strtoul@plt>
mov    %eax,%edi
call   1050 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    0x8(%rsp),%edi
call   1050 <htonl@plt>
mov    %eax,0x10(%rsp)
mov    0x18(%r14),%rdi
mov    0x2dd5(%rip),%rax
mov    0x2db6(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %ebp,%edx
call   1090 <open@plt>
mov    %eax,%ebx
lea    0x4(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     12da <main+0x14a>
lea    0x14(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     12e3 <main+0x153>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     12ec <main+0x15c>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     12f5 <main+0x165>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    12bd <main+0x12d>
mov    %rax,%rbx
lea    0xd5a(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
lea    0xd7c(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %eax,%ebx
lea    0xd90(%rip),%rdi
call   10a0 <perror@plt>
jmp    12bf <main+0x12f>
lea    0xd9e(%rip),%rdi
jmp    12fc <main+0x16c>
lea    0xda8(%rip),%rdi
jmp    12fc <main+0x16c>
lea    0xdac(%rip),%rdi
jmp    12fc <main+0x16c>
lea    0xdb7(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cc0(%rip),%rax
mov    (%rax),%ebx
jmp    12bf <main+0x12f>

LLVM IR (-O0):
%struct.stat = type { i32 }
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [49 x i8] c"Usage: %s [address] [initrd file] [output file]\0A\00"
@.str.1 = [50 x i8] c"Example: %s 0x2000000 initrd.img initrd.addrsize\0A\00"
@.str.2 = [28 x i8] c"Error getting initrd stats \00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.3 = [19 x i8] c"writing first zero\00"
@errno = global i32 0
@.str.4 = [13 x i8] c"writing addr\00"
@.str.5 = [20 x i8] c"writing second zero\00"
@.str.6 = [13 x i8] c"writing size\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca %struct.stat
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i8* @basename(i8* %17)
store i8* %18, i8** %6
store i32 0, i32* %8
%19 = load i32, i32* @S_IRUSR
%20 = load i32, i32* @S_IWUSR
%21 = or i32 %19, %20
%22 = load i32, i32* @S_IRGRP
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IROTH
%25 = or i32 %23, %24
store i32 %25, i32* %14
%26 = load i32, i32* %4
%27 = icmp ne i32 %26, 4
br i1 %27, label %28, label %33
28: ; preds = %2
%29 = load i8*, i8** %6
%30 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i8* %29)
%31 = load i8*, i8** %6
%32 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i8* %31)
store i32 0, i32* %3
br label %90
33: ; preds = %2
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 2
%36 = load i8*, i8** %35
%37 = call i32 @stat(i8* %36, %struct.stat* %7)
store i32 %37, i32* %12
%38 = load i32, i32* %12
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %33
%41 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%42 = load i32, i32* %12
store i32 %42, i32* %3
br label %90
43: ; preds = %33
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 1
%46 = load i8*, i8** %45
%47 = call i32 @strtoul(i8* %46, i8** %13, i32 0)
%48 = call i32 @htonl(i32 %47)
store i32 %48, i32* %9
%49 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%50 = load i32, i32* %49
%51 = call i32 @htonl(i32 %50)
store i32 %51, i32* %10
%52 = load i8**, i8*** %5
%53 = getelementptr inbounds i8*, i8** %52, i64 3
%54 = load i8*, i8** %53
%55 = load i32, i32* @O_CREAT
%56 = load i32, i32* @O_RDWR
%57 = or i32 %55, %56
%58 = load i32, i32* %14
%59 = call i32 @open(i8* %54, i32 %57, i32 %58)
store i32 %59, i32* %11
%60 = load i32, i32* %11
%61 = call i32 @write(i32 %60, i32* %8, i32 4)
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %66
63: ; preds = %43
%64 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%65 = load i32, i32* @errno
store i32 %65, i32* %3
br label %90
66: ; preds = %43
%67 = load i32, i32* %11
%68 = call i32 @write(i32 %67, i32* %9, i32 4)
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %73
70: ; preds = %66
%71 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%72 = load i32, i32* @errno
store i32 %72, i32* %3
br label %90
73: ; preds = %66
%74 = load i32, i32* %11
%75 = call i32 @write(i32 %74, i32* %8, i32 4)
%76 = icmp eq i32 %75, -1
br i1 %76, label %77, label %80
77: ; preds = %73
%78 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%79 = load i32, i32* @errno
store i32 %79, i32* %3
br label %90
80: ; preds = %73
%81 = load i32, i32* %11
%82 = call i32 @write(i32 %81, i32* %10, i32 4)
%83 = icmp eq i32 %82, -1
br i1 %83, label %84, label %87
84: ; preds = %80
%85 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
%86 = load i32, i32* @errno
store i32 %86, i32* %3
br label %90
87: ; preds = %80
%88 = load i32, i32* %11
%89 = call i32 @close(i32 %88)
store i32 0, i32* %3
br label %90
90: ; preds = %87, %84, %77, %70, %63, %40, %28
%91 = load i32, i32* %3
ret i32 %91
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [49 x i8] c"Usage: %s [address] [initrd file] [output file]\0A\00"
@.str.1 = [50 x i8] c"Example: %s 0x2000000 initrd.img initrd.addrsize\0A\00"
@.str.2 = [28 x i8] c"Error getting initrd stats \00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.3 = [19 x i8] c"writing first zero\00"
@errno = global i32 0
@.str.4 = [13 x i8] c"writing addr\00"
@.str.5 = [20 x i8] c"writing second zero\00"
@.str.6 = [13 x i8] c"writing size\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = load i8*, i8** %1
%9 = tail call i8* @basename(i8* %8)
%10 = bitcast %struct.stat* %3 to i8*
%11 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%12 = bitcast i32* %5 to i8*
%13 = bitcast i32* %6 to i8*
%14 = bitcast i8** %7 to i8*
%15 = load i32, i32* @S_IRUSR
%16 = load i32, i32* @S_IWUSR
%17 = or i32 %16, %15
%18 = load i32, i32* @S_IRGRP
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IROTH
%21 = or i32 %19, %20
%22 = icmp eq i32 %0, 4
br i1 %22, label %26, label %23
23: ; preds = %2
%24 = tail call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i8* %9)
%25 = tail call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i8* %9)
br label %72
26: ; preds = %2
%27 = getelementptr inbounds i8*, i8** %1, i64 2
%28 = load i8*, i8** %27
%29 = call i32 @stat(i8* %28, %struct.stat* %3)
%30 = icmp eq i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %26
%32 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %72
33: ; preds = %26
%34 = getelementptr inbounds i8*, i8** %1, i64 1
%35 = load i8*, i8** %34
%36 = call i32 @strtoul(i8* %35, i8** %7, i32 0)
%37 = tail call i32 @htonl(i32 %36)
store i32 %37, i32* %5
%38 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%39 = load i32, i32* %38
%40 = tail call i32 @htonl(i32 %39)
store i32 %40, i32* %6
%41 = getelementptr inbounds i8*, i8** %1, i64 3
%42 = load i8*, i8** %41
%43 = load i32, i32* @O_CREAT
%44 = load i32, i32* @O_RDWR
%45 = or i32 %44, %43
%46 = tail call i32 @open(i8* %42, i32 %45, i32 %21)
%47 = call i32 @write(i32 %46, i32* %4, i32 4)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %52
49: ; preds = %33
%50 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%51 = load i32, i32* @errno
br label %72
52: ; preds = %33
%53 = call i32 @write(i32 %46, i32* %5, i32 4)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %58
55: ; preds = %52
%56 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%57 = load i32, i32* @errno
br label %72
58: ; preds = %52
%59 = call i32 @write(i32 %46, i32* %4, i32 4)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %64
61: ; preds = %58
%62 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%63 = load i32, i32* @errno
br label %72
64: ; preds = %58
%65 = call i32 @write(i32 %46, i32* %6, i32 4)
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %70
67: ; preds = %64
%68 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
%69 = load i32, i32* @errno
br label %72
70: ; preds = %64
%71 = tail call i32 @close(i32 %46)
br label %72
72: ; preds = %70, %67, %61, %55, %49, %31, %23
%73 = phi i32 [ 0, %23 ], [ %29, %31 ], [ %51, %49 ], [ %57, %55 ], [ %63, %61 ], [ %69, %67 ], [ 0, %70 ]
ret i32 %73
}