#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(unsigned long *, int, int, int /*<<< orphan*/ *);
unsigned long getFileCRC(int /*<<< orphan*/ *);
int /*<<< orphan*/ parsing_regfile(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  unsigned long crc;
  int ret;
  char *fileName = "s5k4ecgx.bin";
  FILE *file_crc;
  if (argc != 2) {
    printf("Usage: %s input_file \n", argv[0]);
    return -1;
  }
  printf("Making firmware: %s\n", fileName);
  parsing_regfile(argv[1], fileName);
  file_crc = fopen(fileName, "rb");
  crc = getFileCRC(file_crc);
  printf("Got CRC value %#lx (size %ld)\n", crc, sizeof(unsigned long));
  fclose(file_crc);
  printf("Try to append CRC to %s\n", fileName);
  file_crc = fopen(fileName, "a+");
  fwrite(&crc, 4, 1, file_crc);
  fclose(file_crc);
  printf("Done\n");
  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)
lea    0xe83(%rip),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11ac <main+0x4c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126b <main+0x10b>
mov    -0x28(%rbp),%rsi
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <parsing_regfile@plt>
mov    -0x28(%rbp),%rdi
lea    0xe5f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1070 <getFileCRC@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe41(%rip),%rdi
mov    $0x8,%edx
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x28(%rbp),%rsi
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
lea    0xe47(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rcx
lea    -0x18(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbp
cmp    $0x2,%edi
jne    1238 <main+0xc8>
lea    0xe99(%rip),%rdi
lea    0xe6e(%rip),%rbx
xor    %r14d,%r14d
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rbp),%rdi
mov    %rbx,%rsi
call   1060 <parsing_regfile@plt>
lea    0xe87(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1080 <getFileCRC@plt>
mov    %rax,0x8(%rsp)
lea    0xe6b(%rip),%rdi
mov    $0x8,%edx
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
lea    0xe6c(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe74(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1090 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xe46(%rip),%rdi
call   1030 <puts@plt>
jmp    1250 <main+0xe0>
mov    0x0(%rbp),%rsi
lea    0xdca(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"s5k4ecgx.bin\00"
@.str.1 = [23 x i8] c"Usage: %s input_file \0A\00"
@.str.2 = [21 x i8] c"Making firmware: %s\0A\00"
@.str.3 = [3 x i8] c"rb\00"
@.str.4 = [31 x i8] c"Got CRC value %#lx (size %ld)\0A\00"
@.str.5 = [25 x i8] c"Try to append CRC to %s\0A\00"
@.str.6 = [3 x i8] c"a+\00"
@.str.7 = [6 x i8] c"Done\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %8
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %15)
store i32 -1, i32* %3
br label %42
17: ; preds = %2
%18 = load i8*, i8** %8
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = load i8*, i8** %8
%24 = call i32 @parsing_regfile(i8* %22, i8* %23)
%25 = load i8*, i8** %8
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %26, i32** %9
%27 = load i32*, i32** %9
%28 = call i64 @getFileCRC(i32* %27)
store i64 %28, i64* %6
%29 = load i64, i64* %6
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i64 %29, i64 8)
%31 = load i32*, i32** %9
%32 = call i32 @fclose(i32* %31)
%33 = load i8*, i8** %8
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %33)
%35 = load i8*, i8** %8
%36 = call i32* @fopen(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
store i32* %36, i32** %9
%37 = load i32*, i32** %9
%38 = call i32 @fwrite(i64* %6, i32 4, i32 1, i32* %37)
%39 = load i32*, i32** %9
%40 = call i32 @fclose(i32* %39)
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %3
br label %42
42: ; preds = %17, %12
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@.str = [13 x i8] c"s5k4ecgx.bin\00"
@.str.1 = [23 x i8] c"Usage: %s input_file \0A\00"
@.str.2 = [21 x i8] c"Making firmware: %s\0A\00"
@.str.3 = [3 x i8] c"rb\00"
@.str.4 = [31 x i8] c"Got CRC value %#lx (size %ld)\0A\00"
@.str.5 = [25 x i8] c"Try to append CRC to %s\0A\00"
@.str.6 = [3 x i8] c"a+\00"
@str = [5 x i8] c"Done\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %7)
br label %23
9: ; preds = %2
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32 @parsing_regfile(i8* %12, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%15 = tail call i64 @getFileCRC(i32* %14)
store i64 %15, i64* %3
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i64 %15, i64 8)
%17 = tail call i32 @fclose(i32* %14)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%20 = call i32 @fwrite(i64* %3, i32 4, i32 1, i32* %19)
%21 = tail call i32 @fclose(i32* %19)
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
br label %23
23: ; preds = %9, %6
%24 = phi i32 [ -1, %6 ], [ 0, %9 ]
ret i32 %24
}