#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned int *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, unsigned int,
                         int /*<<< orphan*/);
unsigned int *malloc(int);
int /*<<< orphan*/ printf(char *, char *);
int main(int argc, char *argv[]) {
  FILE *kernelImage, *driverImage;
  unsigned int i, j, kernelSize, driverCount, *driverSize;
  int tempChar;
  if (argc < 3) {
    printf("Usage: %s kernel.bin driver1 [driver2 driver3 ...]\n", argv[0]);
    return 0;
  }
  kernelImage = fopen(argv[1], "rb+");
  if (!kernelImage) {
    printf("Error: Couldn't open kernel image '%s'!\n", argv[1]);
    return -1;
  }
  kernelSize = 0;
  while ((tempChar = fgetc(kernelImage)) != EOF) {
    kernelSize++;
    if (kernelSize > 5 && kernelSize < 10) {
      if (tempChar) {
        printf("Error: Kernel image '%s' is already packaged!\n", argv[1]);
        fclose(kernelImage);
        return -1;
      }
    }
  }
  fseek(kernelImage, 5, SEEK_SET);
  fputc((kernelSize & 0xFF), kernelImage);
  fputc(((kernelSize >> 8) & 0xFF), kernelImage);
  fputc(((kernelSize >> 16) & 0xFF), kernelImage);
  fputc(((kernelSize >> 24) & 0xFF), kernelImage);
  fseek(kernelImage, 0, SEEK_END);
  driverCount = argc - 2;
  fputc((driverCount & 0xFF), kernelImage);
  for (i = 0; i < driverCount; i++) {
    for (j = 0; j < 4; j++) {
      fputc(0, kernelImage);
    }
  }
  driverSize = malloc(4 * driverCount);
  for (i = 2; i < argc; i++) {
    driverSize[i - 2] = 0;
    driverImage = fopen(argv[i], "rb");
    if (!driverImage) {
      printf("Error: Could not open driver image '%s'!\n", argv[i]);
      free(driverSize);
      return -1;
    }
    while ((tempChar = fgetc(driverImage)) != EOF) {
      driverSize[i - 2]++;
      fputc(tempChar, kernelImage);
    }
    fclose(driverImage);
  }
  fseek(kernelImage, kernelSize + 1, SEEK_SET);
  for (i = 0; i < driverCount; i++) {
    fputc((driverSize[i] & 0xFF), kernelImage);
    fputc(((driverSize[i] >> 8) & 0xFF), kernelImage);
    fputc(((driverSize[i] >> 16) & 0xFF), kernelImage);
    fputc(((driverSize[i] >> 24) & 0xFF), kernelImage);
  }
  free(driverSize);
  fclose(kernelImage);
}


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   $0x3,-0x8(%rbp)
jge    11bf <main+0x3f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe52(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1501 <main+0x381>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe66(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1202 <main+0x82>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe47(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1501 <main+0x381>
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x3c(%rbp)
mov    0x2dc4(%rip),%rcx
cmp    (%rcx),%eax
je     127e <main+0xfe>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x5,-0x2c(%rbp)
jbe    1279 <main+0xf9>
cmpl   $0xa,-0x2c(%rbp)
jae    1279 <main+0xf9>
cmpl   $0x0,-0x3c(%rbp)
je     1274 <main+0xf4>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe07(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1501 <main+0x381>
jmp    1279 <main+0xf9>
jmp    1209 <main+0x89>
mov    -0x18(%rbp),%rdi
mov    0x2d47(%rip),%rax
mov    (%rax),%edx
mov    $0x5,%esi
call   1090 <fseek@plt>
mov    -0x2c(%rbp),%edi
and    $0xff,%edi
mov    -0x18(%rbp),%rsi
call   1070 <fputc@plt>
mov    -0x2c(%rbp),%edi
shr    $0x8,%edi
and    $0xff,%edi
mov    -0x18(%rbp),%rsi
call   1070 <fputc@plt>
mov    -0x2c(%rbp),%edi
shr    $0x10,%edi
and    $0xff,%edi
mov    -0x18(%rbp),%rsi
call   1070 <fputc@plt>
mov    -0x2c(%rbp),%edi
shr    $0x18,%edi
and    $0xff,%edi
mov    -0x18(%rbp),%rsi
call   1070 <fputc@plt>
mov    -0x18(%rbp),%rdi
mov    0x2cf7(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x8(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edi
and    $0xff,%edi
mov    -0x18(%rbp),%rsi
call   1070 <fputc@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x30(%rbp),%eax
jae    1365 <main+0x1e5>
movl   $0x0,-0x28(%rbp)
cmpl   $0x4,-0x28(%rbp)
jae    1352 <main+0x1d2>
mov    -0x18(%rbp),%rsi
xor    %edi,%edi
call   1070 <fputc@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    132f <main+0x1af>
jmp    1357 <main+0x1d7>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    131c <main+0x19c>
mov    -0x30(%rbp),%edi
shl    $0x2,%edi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
movl   $0x2,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x8(%rbp),%eax
jae    1449 <main+0x2c9>
mov    -0x38(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x2,%ecx
mov    %ecx,%ecx
movl   $0x0,(%rax,%rcx,4)
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    (%rax,%rcx,8),%rdi
lea    0xce4(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    13ec <main+0x26c>
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    (%rax,%rcx,8),%rsi
lea    0xcc1(%rip),%rdi
call   1050 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1501 <main+0x381>
jmp    13f1 <main+0x271>
mov    -0x20(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x3c(%rbp)
mov    0x2bdc(%rip),%rcx
cmp    (%rcx),%eax
je     1432 <main+0x2b2>
mov    -0x38(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x2,%ecx
mov    %ecx,%ecx
mov    (%rax,%rcx,4),%edx
add    $0x1,%edx
mov    %edx,(%rax,%rcx,4)
mov    -0x3c(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1070 <fputc@plt>
jmp    13f1 <main+0x271>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    137b <main+0x1fb>
mov    -0x18(%rbp),%rdi
mov    -0x2c(%rbp),%esi
add    $0x1,%esi
mov    0x2b76(%rip),%rax
mov    (%rax),%edx
call   1090 <fseek@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x30(%rbp),%eax
jae    14ef <main+0x36f>
mov    -0x38(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    (%rax,%rcx,4),%edi
and    $0xff,%edi
mov    -0x18(%rbp),%rsi
call   1070 <fputc@plt>
mov    -0x38(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    (%rax,%rcx,4),%edi
shr    $0x8,%edi
and    $0xff,%edi
mov    -0x18(%rbp),%rsi
call   1070 <fputc@plt>
mov    -0x38(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    (%rax,%rcx,4),%edi
shr    $0x10,%edi
and    $0xff,%edi
mov    -0x18(%rbp),%rsi
call   1070 <fputc@plt>
mov    -0x38(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    (%rax,%rcx,4),%edi
shr    $0x18,%edi
and    $0xff,%edi
mov    -0x18(%rbp),%rsi
call   1070 <fputc@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1468 <main+0x2e8>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
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    $0x28,%rsp
mov    %rsi,%rbp
cmp    $0x2,%edi
jg     11ab <main+0x2b>
mov    0x0(%rbp),%rsi
lea    0xe5f(%rip),%rdi
call   1050 <printf@plt>
jmp    1492 <main+0x312>
mov    %edi,%r13d
mov    0x8(%rbp),%rdi
lea    0xe7b(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     121f <main+0x9f>
mov    %rax,%r12
mov    $0x1,%r15d
mov    0x2e0d(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r12,%rdi
call   1060 <fgetc@plt>
cmp    (%rbx),%eax
je     1239 <main+0xb9>
lea    -0x6(%r15),%ecx
add    $0x1,%r15d
cmp    $0x3,%ecx
ja     11e0 <main+0x60>
test   %eax,%eax
je     11e0 <main+0x60>
mov    0x8(%rbp),%rsi
lea    0xe59(%rip),%rdi
call   1050 <printf@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    $0xffffffff,%eax
jmp    1494 <main+0x314>
mov    0x8(%rbp),%rsi
lea    0xe0e(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%eax
jmp    1494 <main+0x314>
mov    0x2d90(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    $0x5,%esi
call   1090 <fseek@plt>
lea    -0x1(%r15),%eax
movzbl %al,%edi
mov    %eax,%r14d
mov    %r12,%rsi
call   1070 <fputc@plt>
mov    %r14d,%eax
movzbl %ah,%edi
mov    %r12,%rsi
call   1070 <fputc@plt>
mov    %r14d,%eax
shr    $0x10,%eax
movzbl %al,%edi
mov    %r12,%rsi
call   1070 <fputc@plt>
mov    %r14d,%edi
shr    $0x18,%edi
mov    %r12,%rsi
call   1070 <fputc@plt>
mov    0x2d53(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
lea    -0x2(%r13),%r14d
movzbl %r14b,%edi
mov    %r12,%rsi
call   1070 <fputc@plt>
test   %r14d,%r14d
je     1433 <main+0x2b3>
mov    %r14,0x10(%rsp)
mov    %rbp,0x18(%rsp)
mov    $0x2,%ebp
nopl   0x0(%rax)
xor    %edi,%edi
mov    %r12,%rsi
call   1070 <fputc@plt>
xor    %edi,%edi
mov    %r12,%rsi
call   1070 <fputc@plt>
xor    %edi,%edi
mov    %r12,%rsi
call   1070 <fputc@plt>
xor    %edi,%edi
mov    %r12,%rsi
call   1070 <fputc@plt>
add    $0x1,%ebp
cmp    %ebp,%r13d
jne    12d0 <main+0x150>
mov    0x10(%rsp),%rax
lea    0x0(,%rax,4),%edi
call   1080 <malloc@plt>
mov    %rax,0x8(%rsp)
cmp    $0x3,%r13d
mov    0x18(%rsp),%rbp
jb     13ba <main+0x23a>
mov    %r13d,%eax
mov    %rax,0x20(%rsp)
mov    $0x2,%r14d
jmp    1353 <main+0x1d3>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13,%rdi
call   1040 <fclose@plt>
add    $0x1,%r14
cmp    0x20(%rsp),%r14
je     13ba <main+0x23a>
mov    0x8(%rsp),%rax
movl   $0x0,-0x8(%rax,%r14,4)
mov    0x0(%rbp,%r14,8),%rdi
lea    0xd23(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1458 <main+0x2d8>
mov    %rax,%r13
mov    %rax,%rdi
call   1060 <fgetc@plt>
cmp    (%rbx),%eax
je     1340 <main+0x1c0>
xor    %ebp,%ebp
nopl   0x0(%rax)
add    $0x1,%ebp
mov    %eax,%edi
mov    %r12,%rsi
call   1070 <fputc@plt>
mov    %r13,%rdi
call   1060 <fgetc@plt>
cmp    (%rbx),%eax
jne    1390 <main+0x210>
mov    0x8(%rsp),%rax
mov    %ebp,-0x8(%rax,%r14,4)
mov    0x18(%rsp),%rbp
jmp    1340 <main+0x1c0>
mov    0x2c0f(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    %r15d,%esi
call   1090 <fseek@plt>
mov    0x10(%rsp),%rax
test   %eax,%eax
je     147d <main+0x2fd>
mov    %eax,%r14d
xor    %ebp,%ebp
mov    0x8(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x0(%r13,%rbp,4),%ebx
movzbl %bl,%edi
mov    %r12,%rsi
call   1070 <fputc@plt>
movzbl %bh,%edi
mov    %r12,%rsi
call   1070 <fputc@plt>
mov    %ebx,%eax
shr    $0x10,%eax
movzbl %al,%edi
mov    %r12,%rsi
call   1070 <fputc@plt>
shr    $0x18,%ebx
mov    %ebx,%edi
mov    %r12,%rsi
call   1070 <fputc@plt>
add    $0x1,%rbp
cmp    %rbp,%r14
jne    13f0 <main+0x270>
jmp    1482 <main+0x302>
shl    $0x2,%r14d
mov    %r14d,%edi
call   1080 <malloc@plt>
mov    %rax,%r13
mov    0x2b87(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    %r15d,%esi
call   1090 <fseek@plt>
jmp    1482 <main+0x302>
mov    %r14d,%eax
mov    0x0(%rbp,%rax,8),%rsi
lea    0xc2c(%rip),%rdi
call   1050 <printf@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    $0xffffffff,%eax
jmp    1494 <main+0x314>
mov    0x8(%rsp),%r13
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [52 x i8] c"Usage: %s kernel.bin driver1 [driver2 driver3 ...]\0A\00"
@.str.1 = [4 x i8] c"rb+\00"
@.str.2 = [41 x i8] c"Error: Couldn't open kernel image '%s'!\0A\00"
@EOF = global i32 0
@.str.3 = [47 x i8] c"Error: Kernel image '%s' is already packaged!\0A\00"
@SEEK_SET = global i32 0
@SEEK_END = global i32 0
@.str.4 = [3 x i8] c"rb\00"
@.str.5 = [42 x i8] c"Error: Could not open driver image '%s'!\0A\00"
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 i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 3
br i1 %15, label %16, label %21
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i8* %19)
store i32 0, i32* %3
br label %217
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %25, i32** %6
%26 = load i32*, i32** %6
%27 = icmp ne i32* %26, null
br i1 %27, label %33, label %28
28: ; preds = %21
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %31)
store i32 -1, i32* %3
br label %217
33: ; preds = %21
store i32 0, i32* %10
br label %34
34: ; preds = %58, %33
%35 = load i32*, i32** %6
%36 = call i32 @fgetc(i32* %35)
store i32 %36, i32* %13
%37 = load i32, i32* @EOF
%38 = icmp ne i32 %36, %37
br i1 %38, label %39, label %59
39: ; preds = %34
%40 = load i32, i32* %10
%41 = add i32 %40, 1
store i32 %41, i32* %10
%42 = load i32, i32* %10
%43 = icmp ugt i32 %42, 5
br i1 %43, label %44, label %58
44: ; preds = %39
%45 = load i32, i32* %10
%46 = icmp ult i32 %45, 10
br i1 %46, label %47, label %58
47: ; preds = %44
%48 = load i32, i32* %13
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %57
50: ; preds = %47
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 1
%53 = load i8*, i8** %52
%54 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* %53)
%55 = load i32*, i32** %6
%56 = call i32 @fclose(i32* %55)
store i32 -1, i32* %3
br label %217
57: ; preds = %47
br label %58
58: ; preds = %57, %44, %39
br label %34
59: ; preds = %34
%60 = load i32*, i32** %6
%61 = load i32, i32* @SEEK_SET
%62 = call i32 @fseek(i32* %60, i32 5, i32 %61)
%63 = load i32, i32* %10
%64 = and i32 %63, 255
%65 = load i32*, i32** %6
%66 = call i32 @fputc(i32 %64, i32* %65)
%67 = load i32, i32* %10
%68 = lshr i32 %67, 8
%69 = and i32 %68, 255
%70 = load i32*, i32** %6
%71 = call i32 @fputc(i32 %69, i32* %70)
%72 = load i32, i32* %10
%73 = lshr i32 %72, 16
%74 = and i32 %73, 255
%75 = load i32*, i32** %6
%76 = call i32 @fputc(i32 %74, i32* %75)
%77 = load i32, i32* %10
%78 = lshr i32 %77, 24
%79 = and i32 %78, 255
%80 = load i32*, i32** %6
%81 = call i32 @fputc(i32 %79, i32* %80)
%82 = load i32*, i32** %6
%83 = load i32, i32* @SEEK_END
%84 = call i32 @fseek(i32* %82, i32 0, i32 %83)
%85 = load i32, i32* %4
%86 = sub nsw i32 %85, 2
store i32 %86, i32* %11
%87 = load i32, i32* %11
%88 = and i32 %87, 255
%89 = load i32*, i32** %6
%90 = call i32 @fputc(i32 %88, i32* %89)
store i32 0, i32* %8
br label %91
91: ; preds = %106, %59
%92 = load i32, i32* %8
%93 = load i32, i32* %11
%94 = icmp ult i32 %92, %93
br i1 %94, label %95, label %109
95: ; preds = %91
store i32 0, i32* %9
br label %96
96: ; preds = %102, %95
%97 = load i32, i32* %9
%98 = icmp ult i32 %97, 4
br i1 %98, label %99, label %105
99: ; preds = %96
%100 = load i32*, i32** %6
%101 = call i32 @fputc(i32 0, i32* %100)
br label %102
102: ; preds = %99
%103 = load i32, i32* %9
%104 = add i32 %103, 1
store i32 %104, i32* %9
br label %96
105: ; preds = %96
br label %106
106: ; preds = %105
%107 = load i32, i32* %8
%108 = add i32 %107, 1
store i32 %108, i32* %8
br label %91
109: ; preds = %91
%110 = load i32, i32* %11
%111 = mul i32 4, %110
%112 = call i32* @malloc(i32 %111)
store i32* %112, i32** %12
store i32 2, i32* %8
br label %113
113: ; preds = %160, %109
%114 = load i32, i32* %8
%115 = load i32, i32* %4
%116 = icmp ult i32 %114, %115
br i1 %116, label %117, label %163
117: ; preds = %113
%118 = load i32*, i32** %12
%119 = load i32, i32* %8
%120 = sub i32 %119, 2
%121 = zext i32 %120 to i64
%122 = getelementptr inbounds i32, i32* %118, i64 %121
store i32 0, i32* %122
%123 = load i8**, i8*** %5
%124 = load i32, i32* %8
%125 = zext i32 %124 to i64
%126 = getelementptr inbounds i8*, i8** %123, i64 %125
%127 = load i8*, i8** %126
%128 = call i32* @fopen(i8* %127, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %128, i32** %7
%129 = load i32*, i32** %7
%130 = icmp ne i32* %129, null
br i1 %130, label %140, label %131
131: ; preds = %117
%132 = load i8**, i8*** %5
%133 = load i32, i32* %8
%134 = zext i32 %133 to i64
%135 = getelementptr inbounds i8*, i8** %132, i64 %134
%136 = load i8*, i8** %135
%137 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i8* %136)
%138 = load i32*, i32** %12
%139 = call i32 @free(i32* %138)
store i32 -1, i32* %3
br label %217
140: ; preds = %117
br label %141
141: ; preds = %146, %140
%142 = load i32*, i32** %7
%143 = call i32 @fgetc(i32* %142)
store i32 %143, i32* %13
%144 = load i32, i32* @EOF
%145 = icmp ne i32 %143, %144
br i1 %145, label %146, label %157
146: ; preds = %141
%147 = load i32*, i32** %12
%148 = load i32, i32* %8
%149 = sub i32 %148, 2
%150 = zext i32 %149 to i64
%151 = getelementptr inbounds i32, i32* %147, i64 %150
%152 = load i32, i32* %151
%153 = add i32 %152, 1
store i32 %153, i32* %151
%154 = load i32, i32* %13
%155 = load i32*, i32** %6
%156 = call i32 @fputc(i32 %154, i32* %155)
br label %141
157: ; preds = %141
%158 = load i32*, i32** %7
%159 = call i32 @fclose(i32* %158)
br label %160
160: ; preds = %157
%161 = load i32, i32* %8
%162 = add i32 %161, 1
store i32 %162, i32* %8
br label %113
163: ; preds = %113
%164 = load i32*, i32** %6
%165 = load i32, i32* %10
%166 = add i32 %165, 1
%167 = load i32, i32* @SEEK_SET
%168 = call i32 @fseek(i32* %164, i32 %166, i32 %167)
store i32 0, i32* %8
br label %169
169: ; preds = %209, %163
%170 = load i32, i32* %8
%171 = load i32, i32* %11
%172 = icmp ult i32 %170, %171
br i1 %172, label %173, label %212
173: ; preds = %169
%174 = load i32*, i32** %12
%175 = load i32, i32* %8
%176 = zext i32 %175 to i64
%177 = getelementptr inbounds i32, i32* %174, i64 %176
%178 = load i32, i32* %177
%179 = and i32 %178, 255
%180 = load i32*, i32** %6
%181 = call i32 @fputc(i32 %179, i32* %180)
%182 = load i32*, i32** %12
%183 = load i32, i32* %8
%184 = zext i32 %183 to i64
%185 = getelementptr inbounds i32, i32* %182, i64 %184
%186 = load i32, i32* %185
%187 = lshr i32 %186, 8
%188 = and i32 %187, 255
%189 = load i32*, i32** %6
%190 = call i32 @fputc(i32 %188, i32* %189)
%191 = load i32*, i32** %12
%192 = load i32, i32* %8
%193 = zext i32 %192 to i64
%194 = getelementptr inbounds i32, i32* %191, i64 %193
%195 = load i32, i32* %194
%196 = lshr i32 %195, 16
%197 = and i32 %196, 255
%198 = load i32*, i32** %6
%199 = call i32 @fputc(i32 %197, i32* %198)
%200 = load i32*, i32** %12
%201 = load i32, i32* %8
%202 = zext i32 %201 to i64
%203 = getelementptr inbounds i32, i32* %200, i64 %202
%204 = load i32, i32* %203
%205 = lshr i32 %204, 24
%206 = and i32 %205, 255
%207 = load i32*, i32** %6
%208 = call i32 @fputc(i32 %206, i32* %207)
br label %209
209: ; preds = %173
%210 = load i32, i32* %8
%211 = add i32 %210, 1
store i32 %211, i32* %8
br label %169
212: ; preds = %169
%213 = load i32*, i32** %12
%214 = call i32 @free(i32* %213)
%215 = load i32*, i32** %6
%216 = call i32 @fclose(i32* %215)
br label %217
217: ; preds = %212, %131, %50, %28, %16
%218 = load i32, i32* %3
ret i32 %218
}

LLVM IR (-O2):
@.str = [52 x i8] c"Usage: %s kernel.bin driver1 [driver2 driver3 ...]\0A\00"
@.str.1 = [4 x i8] c"rb+\00"
@.str.2 = [41 x i8] c"Error: Couldn't open kernel image '%s'!\0A\00"
@EOF = global i32 0
@.str.3 = [47 x i8] c"Error: Kernel image '%s' is already packaged!\0A\00"
@SEEK_SET = global i32 0
@SEEK_END = global i32 0
@.str.4 = [3 x i8] c"rb\00"
@.str.5 = [42 x i8] c"Error: Could not open driver image '%s'!\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %126
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = load i8*, i8** %8
%14 = tail call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %13)
br label %126
15: ; preds = %7, %20
%16 = phi i32 [ %21, %20 ], [ 0, %7 ]
%17 = tail call i32 @fgetc(i32* %10)
%18 = load i32, i32* @EOF
%19 = icmp eq i32 %17, %18
br i1 %19, label %30, label %20
20: ; preds = %15
%21 = add i32 %16, 1
%22 = add i32 %16, -5
%23 = icmp ult i32 %22, 4
%24 = icmp ne i32 %17, 0
%25 = and i1 %23, %24
br i1 %25, label %26, label %15
26: ; preds = %20
%27 = load i8*, i8** %8
%28 = tail call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* %27)
%29 = tail call i32 @fclose(i32* %10)
br label %126
30: ; preds = %15
%31 = load i32, i32* @SEEK_SET
%32 = tail call i32 @fseek(i32* %10, i32 5, i32 %31)
%33 = and i32 %16, 255
%34 = tail call i32 @fputc(i32 %33, i32* %10)
%35 = lshr i32 %16, 8
%36 = and i32 %35, 255
%37 = tail call i32 @fputc(i32 %36, i32* %10)
%38 = lshr i32 %16, 16
%39 = and i32 %38, 255
%40 = tail call i32 @fputc(i32 %39, i32* %10)
%41 = lshr i32 %16, 24
%42 = tail call i32 @fputc(i32 %41, i32* %10)
%43 = load i32, i32* @SEEK_END
%44 = tail call i32 @fseek(i32* %10, i32 0, i32 %43)
%45 = add nsw i32 %0, -2
%46 = and i32 %45, 255
%47 = tail call i32 @fputc(i32 %46, i32* %10)
%48 = icmp eq i32 %45, 0
br i1 %48, label %49, label %55
49: ; preds = %30
%50 = shl nuw nsw i32 %45, 2
%51 = tail call i32* @malloc(i32 %50)
%52 = add i32 %16, 1
%53 = load i32, i32* @SEEK_SET
%54 = tail call i32 @fseek(i32* %10, i32 %52, i32 %53)
br label %122
55: ; preds = %30, %55
%56 = phi i32 [ %61, %55 ], [ 0, %30 ]
%57 = tail call i32 @fputc(i32 0, i32* %10)
%58 = tail call i32 @fputc(i32 0, i32* %10)
%59 = tail call i32 @fputc(i32 0, i32* %10)
%60 = tail call i32 @fputc(i32 0, i32* %10)
%61 = add nuw i32 %56, 1
%62 = icmp eq i32 %61, %45
br i1 %62, label %63, label %55
63: ; preds = %55
%64 = shl i32 %45, 2
%65 = tail call i32* @malloc(i32 %64)
%66 = icmp ugt i32 %0, 2
br i1 %66, label %67, label %100
67: ; preds = %63
%68 = zext i32 %0 to i64
br label %69
69: ; preds = %67, %96
%70 = phi i64 [ 2, %67 ], [ %98, %96 ]
%71 = add nsw i64 %70, -2
%72 = getelementptr inbounds i32, i32* %65, i64 %71
store i32 0, i32* %72
%73 = getelementptr inbounds i8*, i8** %1, i64 %70
%74 = load i8*, i8** %73
%75 = tail call i32* @fopen(i8* %74, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%76 = icmp eq i32* %75, null
br i1 %76, label %81, label %77
77: ; preds = %69
%78 = tail call i32 @fgetc(i32* %75)
%79 = load i32, i32* @EOF
%80 = icmp eq i32 %78, %79
br i1 %80, label %96, label %87
81: ; preds = %69
%82 = and i64 %70, 4294967295
%83 = getelementptr inbounds i8*, i8** %1, i64 %82
%84 = load i8*, i8** %83
%85 = tail call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i8* %84)
%86 = tail call i32 @free(i32* %65)
br label %126
87: ; preds = %77, %87
%88 = phi i32 [ %90, %87 ], [ 0, %77 ]
%89 = phi i32 [ %92, %87 ], [ %78, %77 ]
%90 = add i32 %88, 1
%91 = tail call i32 @fputc(i32 %89, i32* %10)
%92 = tail call i32 @fgetc(i32* %75)
%93 = load i32, i32* @EOF
%94 = icmp eq i32 %92, %93
br i1 %94, label %95, label %87
95: ; preds = %87
store i32 %90, i32* %72
br label %96
96: ; preds = %95, %77
%97 = tail call i32 @fclose(i32* %75)
%98 = add nuw nsw i64 %70, 1
%99 = icmp eq i64 %98, %68
br i1 %99, label %100, label %69
100: ; preds = %96, %63
%101 = add i32 %16, 1
%102 = load i32, i32* @SEEK_SET
%103 = tail call i32 @fseek(i32* %10, i32 %101, i32 %102)
br i1 %48, label %122, label %104
104: ; preds = %100
%105 = zext i32 %45 to i64
br label %106
106: ; preds = %104, %106
%107 = phi i64 [ 0, %104 ], [ %120, %106 ]
%108 = getelementptr inbounds i32, i32* %65, i64 %107
%109 = load i32, i32* %108
%110 = and i32 %109, 255
%111 = tail call i32 @fputc(i32 %110, i32* %10)
%112 = lshr i32 %109, 8
%113 = and i32 %112, 255
%114 = tail call i32 @fputc(i32 %113, i32* %10)
%115 = lshr i32 %109, 16
%116 = and i32 %115, 255
%117 = tail call i32 @fputc(i32 %116, i32* %10)
%118 = lshr i32 %109, 24
%119 = tail call i32 @fputc(i32 %118, i32* %10)
%120 = add nuw nsw i64 %107, 1
%121 = icmp eq i64 %120, %105
br i1 %121, label %122, label %106
122: ; preds = %106, %49, %100
%123 = phi i32* [ %51, %49 ], [ %65, %100 ], [ %65, %106 ]
%124 = tail call i32 @free(i32* %123)
%125 = tail call i32 @fclose(i32* %10)
br label %126
126: ; preds = %122, %81, %26, %12, %4
%127 = phi i32 [ 0, %4 ], [ -1, %26 ], [ -1, %81 ], [ 0, %122 ], [ -1, %12 ]
ret i32 %127
}