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

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(struct boot_file_head *, int, unsigned int,
                         int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
unsigned int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(struct boot_file_head *, int, unsigned int,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ gen_check_sum(struct boot_file_head *);
struct boot_file_head *malloc(unsigned int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  struct boot_file_head *buf;
  unsigned file_size;
  FILE *f;
  if (argc < 3) {
    printf("Usage: %s file.bin file.code\n"
           "calculates BROM checksum in boot header of given .bin file and "
           "writes to .code file\n"
           "",
           argv[0]);
    exit(1);
  }
  f = fopen(argv[1], "rb");
  if (!f) {
    perror("Open input file");
    exit(1);
  }
  fseek(f, 0, SEEK_END);
  file_size = ftell(f);
  rewind(f);
  buf = malloc(file_size);
  fread(buf, 1, file_size, f);
  fclose(f);
  gen_check_sum(buf);
  f = fopen(argv[2], "wb");
  if (!f) {
    perror("Open output file");
    exit(1);
  }
  fwrite(buf, 1, file_size, f);
  fclose(f);
  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)
cmpl   $0x3,-0x8(%rbp)
jge    11fd <main+0x3d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe12(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe66(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1236 <main+0x76>
lea    0xe4e(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    0x2da7(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1080 <ftell@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x1c(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1070 <gen_check_sum@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xde4(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12cb <main+0x10b>
lea    0xdcc(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   10e0 <fwrite@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x2,%edi
jle    1283 <main+0xc3>
mov    0x8(%r14),%rdi
lea    0xe94(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     129c <main+0xdc>
mov    %rax,%rbx
mov    0x2df2(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1060 <rewind@plt>
mov    %r15d,%edi
call   1090 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %rbp,%rdi
call   1070 <gen_check_sum@plt>
mov    0x10(%r14),%rdi
lea    0xe37(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12a5 <main+0xe5>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %rax,%rcx
call   10e0 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r14),%rsi
lea    0xd73(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xdd2(%rip),%rdi
jmp    12ac <main+0xec>
lea    0xddc(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.boot_file_head = type { i32 }
@.str = [114 x i8] c"Usage: %s file.bin file.code\0Acalculates BROM checksum in boot header of given .bin file and writes to .code file\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [16 x i8] c"Open input file\00"
@SEEK_END = global i32 0
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [17 x i8] c"Open output file\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.boot_file_head*
%7 = alloca i32
%8 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 3
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @printf(i8* getelementptr inbounds ([114 x i8], [114 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %8
%22 = load i32*, i32** %8
%23 = icmp ne i32* %22, null
br i1 %23, label %27, label %24
24: ; preds = %17
%25 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %17
%28 = load i32*, i32** %8
%29 = load i32, i32* @SEEK_END
%30 = call i32 @fseek(i32* %28, i32 0, i32 %29)
%31 = load i32*, i32** %8
%32 = call i32 @ftell(i32* %31)
store i32 %32, i32* %7
%33 = load i32*, i32** %8
%34 = call i32 @rewind(i32* %33)
%35 = load i32, i32* %7
%36 = call %struct.boot_file_head* @malloc(i32 %35)
store %struct.boot_file_head* %36, %struct.boot_file_head** %6
%37 = load %struct.boot_file_head*, %struct.boot_file_head** %6
%38 = load i32, i32* %7
%39 = load i32*, i32** %8
%40 = call i32 @fread(%struct.boot_file_head* %37, i32 1, i32 %38, i32* %39)
%41 = load i32*, i32** %8
%42 = call i32 @fclose(i32* %41)
%43 = load %struct.boot_file_head*, %struct.boot_file_head** %6
%44 = call i32 @gen_check_sum(%struct.boot_file_head* %43)
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 2
%47 = load i8*, i8** %46
%48 = call i32* @fopen(i8* %47, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %48, i32** %8
%49 = load i32*, i32** %8
%50 = icmp ne i32* %49, null
br i1 %50, label %54, label %51
51: ; preds = %27
%52 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %27
%55 = load %struct.boot_file_head*, %struct.boot_file_head** %6
%56 = load i32, i32* %7
%57 = load i32*, i32** %8
%58 = call i32 @fwrite(%struct.boot_file_head* %55, i32 1, i32 %56, i32* %57)
%59 = load i32*, i32** %8
%60 = call i32 @fclose(i32* %59)
ret i32 0
}

LLVM IR (-O2):
%struct.boot_file_head = type { i32 }
@.str = [114 x i8] c"Usage: %s file.bin file.code\0Acalculates BROM checksum in boot header of given .bin file and writes to .code file\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [16 x i8] c"Open input file\00"
@SEEK_END = global i32 0
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [17 x i8] c"Open output file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @printf(i8* getelementptr inbounds ([114 x i8], [114 x i8]* @.str, i64 0, i64 0), i8* %5)
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %8
%17 = load i32, i32* @SEEK_END
%18 = tail call i32 @fseek(i32* %11, i32 0, i32 %17)
%19 = tail call i32 @ftell(i32* %11)
%20 = tail call i32 @rewind(i32* %11)
%21 = tail call %struct.boot_file_head* @malloc(i32 %19)
%22 = tail call i32 @fread(%struct.boot_file_head* %21, i32 1, i32 %19, i32* %11)
%23 = tail call i32 @fclose(i32* %11)
%24 = tail call i32 @gen_check_sum(%struct.boot_file_head* %21)
%25 = getelementptr inbounds i8*, i8** %1, i64 2
%26 = load i8*, i8** %25
%27 = tail call i32* @fopen(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %32
29: ; preds = %16
%30 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %16
%33 = tail call i32 @fwrite(%struct.boot_file_head* %21, i32 1, i32 %19, i32* %27)
%34 = tail call i32 @fclose(i32* %27)
ret i32 0
}