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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int strlen(char *);
int /*<<< orphan*/ wrt(char *, char *, int);
int main(int argc, char *argv[]) {
  char *buf = (char *)malloc(16384 * 1024);
  ;
  if (argc < 2) {
    printf("usage: unpboot boot.img\n");
    return 1;
  }
  int ifd = open(argv[1], O_RDONLY);
  if (ifd < 0) {
    printf("cannot open %s\n", argv[1]);
    return 1;
  }
  int len = read(ifd, buf, 16384 * 1024);
  printf("img len=%x\n", len);
  close(ifd);
  wrt("header.bin", buf, 512);
  wrt("cmdline", buf + 512 + 480, strlen(buf + 512 + 480));
  int klen = *(int *)(buf + 0x7e0);
  int rlen = *(int *)(buf + 0x7e4);
  buf += 0x13e0;
  wrt("bootstub", buf, 8192);
  buf += 8192;
  wrt("bzImage", buf, klen);
  wrt("initrd.cpio.gz", buf + klen, rlen);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x1000000,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11b8 <main+0x48>
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fb <main+0x18b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11fd <main+0x8d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fb <main+0x18b>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1000000,%edx
call   1060 <read@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rsi
lea    0xe00(%rip),%rdi
mov    $0x200,%edx
call   1090 <wrt@plt>
mov    -0x18(%rbp),%rax
add    $0x200,%rax
add    $0x1e0,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x200,%rdi
add    $0x1e0,%rdi
call   1030 <strlen@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
lea    0xdc9(%rip),%rdi
call   1090 <wrt@plt>
mov    -0x18(%rbp),%rax
mov    0x7e0(%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    0x7e4(%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
add    $0x13e0,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xd99(%rip),%rdi
mov    $0x2000,%edx
call   1090 <wrt@plt>
mov    -0x18(%rbp),%rax
add    $0x2000,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
lea    0xd7c(%rip),%rdi
call   1090 <wrt@plt>
mov    -0x18(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
mov    -0x28(%rbp),%edx
lea    0xd6a(%rip),%rdi
call   1090 <wrt@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    %edi,%ebx
mov    $0x1000000,%edi
call   1080 <malloc@plt>
cmp    $0x1,%ebx
jg     11ac <main+0x2c>
lea    0xead(%rip),%rdi
call   1030 <puts@plt>
jmp    1292 <main+0x112>
mov    %rax,%r15
mov    0x8(%rbp),%rdi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
js     1280 <main+0x100>
mov    %eax,%r14d
mov    %eax,%edi
mov    %r15,%rsi
mov    $0x1000000,%edx
call   1070 <read@plt>
lea    0xe2e(%rip),%rdi
xor    %ebp,%ebp
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14d,%edi
call   1060 <close@plt>
lea    0xe20(%rip),%rdi
mov    %r15,%rsi
mov    $0x200,%edx
call   10a0 <wrt@plt>
lea    0x3e0(%r15),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0xe08(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   10a0 <wrt@plt>
movslq 0x7e0(%r15),%rbx
mov    0x7e4(%r15),%r14d
lea    0x13e0(%r15),%rsi
lea    0xdea(%rip),%rdi
mov    $0x2000,%edx
call   10a0 <wrt@plt>
lea    0x33e0(%r15),%rsi
lea    0xddb(%rip),%rdi
mov    %ebx,%edx
call   10a0 <wrt@plt>
lea    (%r15,%rbx,1),%rsi
add    $0x33e0,%rsi
lea    0xdca(%rip),%rdi
mov    %r14d,%edx
call   10a0 <wrt@plt>
jmp    1297 <main+0x117>
mov    0x8(%rbp),%rsi
lea    0xd75(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"usage: unpboot boot.img\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [16 x i8] c"cannot open %s\0A\00"
@.str.2 = [12 x i8] c"img len=%x\0A\00"
@.str.3 = [11 x i8] c"header.bin\00"
@.str.4 = [8 x i8] c"cmdline\00"
@.str.5 = [9 x i8] c"bootstub\00"
@.str.6 = [8 x i8] c"bzImage\00"
@.str.7 = [15 x i8] c"initrd.cpio.gz\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i64 @malloc(i32 16777216)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %6
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %71
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %17
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %28)
store i32 1, i32* %3
br label %71
30: ; preds = %17
%31 = load i32, i32* %7
%32 = load i8*, i8** %6
%33 = call i32 @read(i32 %31, i8* %32, i32 16777216)
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %34)
%36 = load i32, i32* %7
%37 = call i32 @close(i32 %36)
%38 = load i8*, i8** %6
%39 = call i32 @wrt(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %38, i32 512)
%40 = load i8*, i8** %6
%41 = getelementptr inbounds i8, i8* %40, i64 512
%42 = getelementptr inbounds i8, i8* %41, i64 480
%43 = load i8*, i8** %6
%44 = getelementptr inbounds i8, i8* %43, i64 512
%45 = getelementptr inbounds i8, i8* %44, i64 480
%46 = call i32 @strlen(i8* %45)
%47 = call i32 @wrt(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %42, i32 %46)
%48 = load i8*, i8** %6
%49 = getelementptr inbounds i8, i8* %48, i64 2016
%50 = bitcast i8* %49 to i32*
%51 = load i32, i32* %50
store i32 %51, i32* %9
%52 = load i8*, i8** %6
%53 = getelementptr inbounds i8, i8* %52, i64 2020
%54 = bitcast i8* %53 to i32*
%55 = load i32, i32* %54
store i32 %55, i32* %10
%56 = load i8*, i8** %6
%57 = getelementptr inbounds i8, i8* %56, i64 5088
store i8* %57, i8** %6
%58 = load i8*, i8** %6
%59 = call i32 @wrt(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* %58, i32 8192)
%60 = load i8*, i8** %6
%61 = getelementptr inbounds i8, i8* %60, i64 8192
store i8* %61, i8** %6
%62 = load i8*, i8** %6
%63 = load i32, i32* %9
%64 = call i32 @wrt(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i8* %62, i32 %63)
%65 = load i8*, i8** %6
%66 = load i32, i32* %9
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i8, i8* %65, i64 %67
%69 = load i32, i32* %10
%70 = call i32 @wrt(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i8* %68, i32 %69)
store i32 0, i32* %3
br label %71
71: ; preds = %30, %25, %15
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str.1 = [16 x i8] c"cannot open %s\0A\00"
@.str.2 = [12 x i8] c"img len=%x\0A\00"
@.str.3 = [11 x i8] c"header.bin\00"
@.str.4 = [8 x i8] c"cmdline\00"
@.str.5 = [9 x i8] c"bootstub\00"
@.str.6 = [8 x i8] c"bzImage\00"
@.str.7 = [15 x i8] c"initrd.cpio.gz\00"
@str = [24 x i8] c"usage: unpboot boot.img\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i64 @malloc(i32 16777216)
%4 = inttoptr i64 %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %38
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %10, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %8
%15 = load i8*, i8** %9
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %15)
br label %38
17: ; preds = %8
%18 = tail call i32 @read(i32 %12, i8* %4, i32 16777216)
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = tail call i32 @close(i32 %12)
%21 = tail call i32 @wrt(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %4, i32 512)
%22 = getelementptr inbounds i8, i8* %4, i64 992
%23 = tail call i32 @strlen(i8* %22)
%24 = tail call i32 @wrt(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %22, i32 %23)
%25 = getelementptr inbounds i8, i8* %4, i64 2016
%26 = bitcast i8* %25 to i32*
%27 = load i32, i32* %26
%28 = getelementptr inbounds i8, i8* %4, i64 2020
%29 = bitcast i8* %28 to i32*
%30 = load i32, i32* %29
%31 = getelementptr inbounds i8, i8* %4, i64 5088
%32 = tail call i32 @wrt(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* %31, i32 8192)
%33 = getelementptr inbounds i8, i8* %4, i64 13280
%34 = tail call i32 @wrt(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i8* %33, i32 %27)
%35 = sext i32 %27 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = tail call i32 @wrt(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i8* %36, i32 %30)
br label %38
38: ; preds = %14, %17, %6
%39 = phi i32 [ 1, %6 ], [ 1, %14 ], [ 0, %17 ]
ret i32 %39
}