#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 /*<<< orphan*/ path;

/* Variables and functions */
int /*<<< orphan*/ cpio_write_dir(char *);
int /*<<< orphan*/ cpio_write_file(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ cpio_write_nod(char *, int, int, int);
int /*<<< orphan*/ cpio_write_tail();
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
char *malloc(int /*<<< orphan*/);
scalar_t__ read_file(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  char path[10], *filebuffer, *filename;
  struct stat st;
  int i;
  if (argc != 2) {
    fprintf(stderr, "Usage: cpioinit <init program>\n");
    return -1;
  }
  filename = argv[1];
  if (stat(filename, &st)) {
    fprintf(stderr, "Cannot open file: %s (%s)\n", filename, strerror(errno));
    return -1;
  }
  filebuffer = malloc(st.st_size);
  if (filebuffer == NULL) {
    fprintf(stderr, "Malloc %lu bytes failed\n", st.st_size);
    return -1;
  }
  if (read_file(filename, filebuffer, st.st_size))
    return -1;
  cpio_write_file("init", filebuffer, st.st_size);
  free(filebuffer);
  cpio_write_dir("dev");
  cpio_write_dir("proc");
  cpio_write_dir("sys");
  cpio_write_dir("mnt");
  cpio_write_nod("dev/console", 5, 1, 1);
  cpio_write_nod("dev/null", 1, 3, 1);
  cpio_write_nod("dev/sda", 8, 0, 0);
  for (i = 1; i < 15; i++) {
    snprintf(path, sizeof(path), "dev/sda%d", i);
    cpio_write_nod(path, 8, i, 0);
  }
  cpio_write_tail();
  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)
cmpl   $0x2,-0x8(%rbp)
je     11f3 <main+0x43>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a8 <main+0x1f8>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     125c <main+0xac>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xdd7(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a8 <main+0x1f8>
mov    -0x38(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1299 <main+0xe9>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%edx
lea    0xdb5(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a8 <main+0x1f8>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%edx
call   1080 <read_file@plt>
cmp    $0x0,%rax
je     12bf <main+0x10f>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a8 <main+0x1f8>
mov    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%edx
lea    0xd87(%rip),%rdi
call   1040 <cpio_write_file@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
lea    0xd77(%rip),%rdi
call   10a0 <cpio_write_dir@plt>
lea    0xd6f(%rip),%rdi
call   10a0 <cpio_write_dir@plt>
lea    0xd68(%rip),%rdi
call   10a0 <cpio_write_dir@plt>
lea    0xd60(%rip),%rdi
call   10a0 <cpio_write_dir@plt>
lea    0xd58(%rip),%rdi
mov    $0x5,%esi
mov    $0x1,%ecx
mov    %ecx,%edx
call   10b0 <cpio_write_nod@plt>
lea    0xd4c(%rip),%rdi
mov    $0x1,%ecx
mov    $0x3,%edx
mov    %ecx,%esi
call   10b0 <cpio_write_nod@plt>
lea    0xd3d(%rip),%rdi
mov    $0x8,%esi
xor    %ecx,%ecx
mov    %ecx,%edx
call   10b0 <cpio_write_nod@plt>
movl   $0x1,-0x3c(%rbp)
cmpl   $0xf,-0x3c(%rbp)
jge    139a <main+0x1ea>
lea    -0x1a(%rbp),%rdi
mov    -0x3c(%rbp),%ecx
mov    $0xa,%esi
lea    0xd13(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x1a(%rbp),%rdi
mov    -0x3c(%rbp),%edx
mov    $0x8,%esi
xor    %ecx,%ecx
call   10b0 <cpio_write_nod@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1357 <main+0x1a7>
mov    $0x0,%al
call   10c0 <cpio_write_tail@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jne    1204 <main+0x54>
mov    0x8(%rsi),%rbx
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
test   %rax,%rax
je     122d <main+0x7d>
mov    0x2e04(%rip),%rax
mov    (%rax),%ebp
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xe2d(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    121b <main+0x6b>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%ebp
mov    %ebp,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     1349 <main+0x199>
mov    %rax,%r14
mov    %rbx,%rdi
mov    %rax,%rsi
mov    %ebp,%edx
call   1080 <read_file@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    1220 <main+0x70>
mov    0x8(%rsp),%edx
lea    0xdee(%rip),%rdi
mov    %r14,%rsi
call   1040 <cpio_write_file@plt>
mov    %r14,%rdi
call   1030 <free@plt>
lea    0xddc(%rip),%rdi
call   10a0 <cpio_write_dir@plt>
lea    0xdd4(%rip),%rdi
call   10a0 <cpio_write_dir@plt>
lea    0xdcd(%rip),%rdi
call   10a0 <cpio_write_dir@plt>
lea    0xdc5(%rip),%rdi
call   10a0 <cpio_write_dir@plt>
lea    0xdbd(%rip),%rdi
mov    $0x1,%ebx
mov    $0x5,%esi
mov    $0x1,%edx
mov    $0x1,%ecx
call   10b0 <cpio_write_nod@plt>
lea    0xda9(%rip),%rdi
mov    $0x1,%esi
mov    $0x3,%edx
mov    $0x1,%ecx
call   10b0 <cpio_write_nod@plt>
lea    0xd97(%rip),%rdi
mov    $0x8,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   10b0 <cpio_write_nod@plt>
lea    0xd8a(%rip),%r14
lea    0xe(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rdi
mov    $0xa,%esi
mov    %r14,%rdx
mov    %ebx,%ecx
call   1050 <snprintf@plt>
mov    %r15,%rdi
mov    $0x8,%esi
mov    %ebx,%edx
xor    %ecx,%ecx
call   10b0 <cpio_write_nod@plt>
add    $0x1,%ebx
cmp    $0xf,%ebx
jne    1310 <main+0x160>
xor    %ebx,%ebx
xor    %eax,%eax
call   10c0 <cpio_write_tail@plt>
jmp    1220 <main+0x70>
mov    0x2c90(%rip),%rax
mov    (%rax),%edi
lea    0xce2(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    121b <main+0x6b>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [32 x i8] c"Usage: cpioinit <init program>\0A\00"
@.str.1 = [27 x i8] c"Cannot open file: %s (%s)\0A\00"
@errno = global i32 0
@.str.2 = [25 x i8] c"Malloc %lu bytes failed\0A\00"
@.str.3 = [5 x i8] c"init\00"
@.str.4 = [4 x i8] c"dev\00"
@.str.5 = [5 x i8] c"proc\00"
@.str.6 = [4 x i8] c"sys\00"
@.str.7 = [4 x i8] c"mnt\00"
@.str.8 = [12 x i8] c"dev/console\00"
@.str.9 = [9 x i8] c"dev/null\00"
@.str.10 = [8 x i8] c"dev/sda\00"
@.str.11 = [10 x i8] c"dev/sda%d\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [10 x i8]
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca %struct.stat
%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, 2
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %77
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
store i8* %19, i8** %8
%20 = load i8*, i8** %8
%21 = call i64 @stat(i8* %20, %struct.stat* %9)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %16
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %8
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %25, i8* %27)
store i32 -1, i32* %3
br label %77
29: ; preds = %16
%30 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i8* @malloc(i32 %31)
store i8* %32, i8** %7
%33 = load i8*, i8** %7
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %40
35: ; preds = %29
%36 = load i32, i32* @stderr
%37 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%38 = load i32, i32* %37
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %38)
store i32 -1, i32* %3
br label %77
40: ; preds = %29
%41 = load i8*, i8** %8
%42 = load i8*, i8** %7
%43 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i64 @read_file(i8* %41, i8* %42, i32 %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %40
store i32 -1, i32* %3
br label %77
48: ; preds = %40
%49 = load i8*, i8** %7
%50 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i32 @cpio_write_file(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %49, i32 %51)
%53 = load i8*, i8** %7
%54 = call i32 @free(i8* %53)
%55 = call i32 @cpio_write_dir(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%56 = call i32 @cpio_write_dir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%57 = call i32 @cpio_write_dir(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%58 = call i32 @cpio_write_dir(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0))
%59 = call i32 @cpio_write_nod(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i32 5, i32 1, i32 1)
%60 = call i32 @cpio_write_nod(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0), i32 1, i32 3, i32 1)
%61 = call i32 @cpio_write_nod(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.10, i64 0, i64 0), i32 8, i32 0, i32 0)
store i32 1, i32* %10
br label %62
62: ; preds = %72, %48
%63 = load i32, i32* %10
%64 = icmp slt i32 %63, 15
br i1 %64, label %65, label %75
65: ; preds = %62
%66 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%67 = load i32, i32* %10
%68 = call i32 @snprintf(i8* %66, i32 10, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.11, i64 0, i64 0), i32 %67)
%69 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%70 = load i32, i32* %10
%71 = call i32 @cpio_write_nod(i8* %69, i32 8, i32 %70, i32 0)
br label %72
72: ; preds = %65
%73 = load i32, i32* %10
%74 = add nsw i32 %73, 1
store i32 %74, i32* %10
br label %62
75: ; preds = %62
%76 = call i32 (...) @cpio_write_tail()
store i32 0, i32* %3
br label %77
77: ; preds = %75, %47, %35, %23, %13
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [32 x i8] c"Usage: cpioinit <init program>\0A\00"
@.str.1 = [27 x i8] c"Cannot open file: %s (%s)\0A\00"
@errno = global i32 0
@.str.2 = [25 x i8] c"Malloc %lu bytes failed\0A\00"
@.str.3 = [5 x i8] c"init\00"
@.str.4 = [4 x i8] c"dev\00"
@.str.5 = [5 x i8] c"proc\00"
@.str.6 = [4 x i8] c"sys\00"
@.str.7 = [4 x i8] c"mnt\00"
@.str.8 = [12 x i8] c"dev/console\00"
@.str.9 = [9 x i8] c"dev/null\00"
@.str.10 = [8 x i8] c"dev/sda\00"
@.str.11 = [10 x i8] c"dev/sda%d\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [10 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.stat* %4 to i8*
%7 = icmp eq i32 %0, 2
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([32 x i8], [32 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* %4)
%15 = icmp eq i64 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %11
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %13, i8* %19)
br label %51
21: ; preds = %11
%22 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%23 = load i32, i32* %22
%24 = call i8* @malloc(i32 %23)
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %23)
br label %51
29: ; preds = %21
%30 = call i64 @read_file(i8* %13, i8* %24, i32 %23)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %51
32: ; preds = %29
%33 = load i32, i32* %22
%34 = call i32 @cpio_write_file(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %24, i32 %33)
%35 = call i32 @free(i8* %24)
%36 = call i32 @cpio_write_dir(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%37 = call i32 @cpio_write_dir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%38 = call i32 @cpio_write_dir(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%39 = call i32 @cpio_write_dir(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0))
%40 = call i32 @cpio_write_nod(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i32 5, i32 1, i32 1)
%41 = call i32 @cpio_write_nod(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0), i32 1, i32 3, i32 1)
%42 = call i32 @cpio_write_nod(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.10, i64 0, i64 0), i32 8, i32 0, i32 0)
br label %43
43: ; preds = %32, %43
%44 = phi i32 [ 1, %32 ], [ %47, %43 ]
%45 = call i32 @snprintf(i8* %5, i32 10, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.11, i64 0, i64 0), i32 %44)
%46 = call i32 @cpio_write_nod(i8* %5, i32 8, i32 %44, i32 0)
%47 = add nuw nsw i32 %44, 1
%48 = icmp eq i32 %47, 15
br i1 %48, label %49, label %43
49: ; preds = %43
%50 = call i32 (...) @cpio_write_tail()
br label %51
51: ; preds = %29, %49, %26, %16, %8
%52 = phi i32 [ -1, %8 ], [ -1, %16 ], [ -1, %26 ], [ 0, %49 ], [ -1, %29 ]
ret i32 %52
}