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

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ memset(char *, unsigned char, int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  uint16_t size = 0;
  uint16_t add = 0;
  char *file_buf = 0;
  char *data_file = 0;
  unsigned char zero_val = 0;
  FILE *file;
  if (argc < 2) {
    printf("Give me an argument! [kernel binary file]\n");
    return 1;
  }
  data_file = argv[1];
  file = fopen(data_file, "rb");
  if (!file) {
    printf("File does not exist!\n");
    return 1;
  }
  fseek(file, 0, SEEK_END);
  size = ftell(file);
  fseek(file, 0, SEEK_SET);
  if (size <= 512)
    add = 512 - size;
  else
    add = 512 - (size % 512);
  file_buf = malloc(size + add);
  fread(file_buf, sizeof(char), size, file);
  printf("Size:\t %d\n", size);
  printf("Add:\t %d\n", add);
  printf("CRC:\t %d\n", size + add);
  memset((file_buf + size), zero_val, add);
  FILE *new_f = fopen("../image/"
                      "kernel",
                      "wb");
  fwrite(file_buf, 1, size + add, new_f);
  fclose(new_f);
  free(file_buf);
  fclose(file);
  return 0;
}


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)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movb   $0x0,-0x29(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11fc <main+0x5c>
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    136b <main+0x1cb>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe18(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1241 <main+0xa1>
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    136b <main+0x1cb>
mov    -0x38(%rbp),%rdi
mov    0x2d9c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x38(%rbp),%rdi
call   1080 <ftell@plt>
mov    %eax,-0x14(%rbp)
mov    -0x38(%rbp),%rdi
mov    0x2d6c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
cmpl   $0x200,-0x14(%rbp)
jg     1292 <main+0xf2>
mov    $0x200,%eax
sub    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    12a7 <main+0x107>
mov    -0x14(%rbp),%eax
mov    $0x200,%ecx
cltd
idiv   %ecx
mov    $0x200,%eax
sub    %edx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
add    -0x18(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x14(%rbp),%esi
lea    0xd6f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%esi
lea    0xd69(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%esi
add    -0x18(%rbp),%esi
lea    0xd5f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
mov    -0x29(%rbp),%al
mov    -0x18(%rbp),%edx
movzbl %al,%esi
call   1070 <memset@plt>
lea    0xd42(%rip),%rdi
lea    0xd4b(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
add    -0x18(%rbp),%edx
mov    -0x40(%rbp),%rcx
mov    $0x1,%esi
call   10c0 <fwrite@plt>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
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
push   %rax
cmp    $0x1,%edi
jg     11cc <main+0x1c>
lea    0xe83(%rip),%rdi
jmp    12ec <main+0x13c>
mov    0x8(%rsi),%rdi
lea    0xe29(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     12e5 <main+0x135>
mov    %rax,%r15
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %r15,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %r15,%rdi
call   1090 <ftell@plt>
mov    %eax,%ebp
mov    0x2dc9(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %ebp,%eax
cmp    $0x201,%ebp
jl     123a <main+0x8a>
lea    0x1ff(%rbp),%ecx
test   %ebp,%ebp
cmovns %ebp,%ecx
and    $0xfffffe00,%ecx
mov    %ebp,%eax
sub    %ecx,%eax
mov    $0x200,%ebx
sub    %eax,%ebx
lea    (%rbx,%rbp,1),%r12d
mov    %r12d,%edi
call   10a0 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r15,%rcx
call   1050 <fread@plt>
lea    0xd9a(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xd95(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xd8f(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1070 <printf@plt>
movslq %ebp,%rdi
add    %r13,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1080 <memset@plt>
lea    0xd79(%rip),%rdi
lea    0xd82(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,%rbx
mov    %r13,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %rax,%rcx
call   10d0 <fwrite@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1060 <fclose@plt>
jmp    12f7 <main+0x147>
lea    0xd49(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [43 x i8] c"Give me an argument! [kernel binary file]\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [22 x i8] c"File does not exist!\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [11 x i8] c"Size:\09 %d\0A\00"
@.str.4 = [10 x i8] c"Add:\09 %d\0A\00"
@.str.5 = [10 x i8] c"CRC:\09 %d\0A\00"
@.str.6 = [16 x i8] c"../image/kernel\00"
@.str.7 = [3 x i8] c"wb\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 i8*
%9 = alloca i8*
%10 = alloca i8
%11 = alloca i32*
%12 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
store i8* null, i8** %8
store i8* null, i8** %9
store i8 0, i8* %10
%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 ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %82
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
store i8* %20, i8** %9
%21 = load i8*, i8** %9
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %11
%23 = load i32*, i32** %11
%24 = icmp ne i32* %23, null
br i1 %24, label %27, label %25
25: ; preds = %17
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %82
27: ; preds = %17
%28 = load i32*, i32** %11
%29 = load i32, i32* @SEEK_END
%30 = call i32 @fseek(i32* %28, i32 0, i32 %29)
%31 = load i32*, i32** %11
%32 = call i32 @ftell(i32* %31)
store i32 %32, i32* %6
%33 = load i32*, i32** %11
%34 = load i32, i32* @SEEK_SET
%35 = call i32 @fseek(i32* %33, i32 0, i32 %34)
%36 = load i32, i32* %6
%37 = icmp sle i32 %36, 512
br i1 %37, label %38, label %41
38: ; preds = %27
%39 = load i32, i32* %6
%40 = sub nsw i32 512, %39
store i32 %40, i32* %7
br label %45
41: ; preds = %27
%42 = load i32, i32* %6
%43 = srem i32 %42, 512
%44 = sub nsw i32 512, %43
store i32 %44, i32* %7
br label %45
45: ; preds = %41, %38
%46 = load i32, i32* %6
%47 = load i32, i32* %7
%48 = add nsw i32 %46, %47
%49 = call i8* @malloc(i32 %48)
store i8* %49, i8** %8
%50 = load i8*, i8** %8
%51 = load i32, i32* %6
%52 = load i32*, i32** %11
%53 = call i32 @fread(i8* %50, i32 1, i32 %51, i32* %52)
%54 = load i32, i32* %6
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %54)
%56 = load i32, i32* %7
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %56)
%58 = load i32, i32* %6
%59 = load i32, i32* %7
%60 = add nsw i32 %58, %59
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %60)
%62 = load i8*, i8** %8
%63 = load i32, i32* %6
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %62, i64 %64
%66 = load i8, i8* %10
%67 = load i32, i32* %7
%68 = call i32 @memset(i8* %65, i8 zeroext %66, i32 %67)
%69 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
store i32* %69, i32** %12
%70 = load i8*, i8** %8
%71 = load i32, i32* %6
%72 = load i32, i32* %7
%73 = add nsw i32 %71, %72
%74 = load i32*, i32** %12
%75 = call i32 @fwrite(i8* %70, i32 1, i32 %73, i32* %74)
%76 = load i32*, i32** %12
%77 = call i32 @fclose(i32* %76)
%78 = load i8*, i8** %8
%79 = call i32 @free(i8* %78)
%80 = load i32*, i32** %11
%81 = call i32 @fclose(i32* %80)
store i32 0, i32* %3
br label %82
82: ; preds = %45, %25, %15
%83 = load i32, i32* %3
ret i32 %83
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [11 x i8] c"Size:\09 %d\0A\00"
@.str.4 = [10 x i8] c"Add:\09 %d\0A\00"
@.str.5 = [10 x i8] c"CRC:\09 %d\0A\00"
@.str.6 = [16 x i8] c"../image/kernel\00"
@.str.7 = [3 x i8] c"wb\00"
@str = [21 x i8] c"File does not exist!\00"
@str.8 = [42 x i8] c"Give me an argument! [kernel binary file]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str.8, i64 0, i64 0))
br label %37
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %37
13: ; preds = %6
%14 = load i32, i32* @SEEK_END
%15 = tail call i32 @fseek(i32* %9, i32 0, i32 %14)
%16 = tail call i32 @ftell(i32* %9)
%17 = load i32, i32* @SEEK_SET
%18 = tail call i32 @fseek(i32* %9, i32 0, i32 %17)
%19 = icmp slt i32 %16, 513
%20 = srem i32 %16, 512
%21 = select i1 %19, i32 %16, i32 %20
%22 = sub nsw i32 512, %21
%23 = add nsw i32 %22, %16
%24 = tail call i8* @malloc(i32 %23)
%25 = tail call i32 @fread(i8* %24, i32 1, i32 %16, i32* %9)
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %22)
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %23)
%29 = sext i32 %16 to i64
%30 = getelementptr inbounds i8, i8* %24, i64 %29
%31 = tail call i32 @memset(i8* %30, i8 zeroext 0, i32 %22)
%32 = tail call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%33 = tail call i32 @fwrite(i8* %24, i32 1, i32 %23, i32* %32)
%34 = tail call i32 @fclose(i32* %32)
%35 = tail call i32 @free(i8* %24)
%36 = tail call i32 @fclose(i32* %9)
br label %37
37: ; preds = %13, %11, %4
%38 = phi i32 [ 1, %4 ], [ 0, %13 ], [ 1, %11 ]
ret i32 %38
}