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

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
scalar_t__ crc64(int /*<<< orphan*/, unsigned char *, scalar_t__);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned char *, scalar_t__, int,
                         int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
scalar_t__ ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(scalar_t__ *, int, int, int /*<<< orphan*/ *);
unsigned char *malloc(scalar_t__);
int /*<<< orphan*/ printf(char *, unsigned long long);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  if (argc > 2) {
    return 1;
  }
  char *filename = argv[1];
  FILE *f = fopen(filename, "r+b");
  fseek(f, 0, SEEK_END);
  uint64_t fsize = ftell(f);
  rewind(f);
  unsigned char *string = malloc(fsize);
  fread(string, fsize, 1, f);
  uint64_t checksum = crc64(0, string, fsize);
  fseek(f, 0, SEEK_END);
  fwrite(&checksum, sizeof(uint64_t), 1, f);
  fclose(f);
  printf("Checksum written: %016llx\n", (unsigned long long)checksum);
  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)
cmpl   $0x2,-0x8(%rbp)
jle    11cc <main+0x2c>
movl   $0x1,-0x4(%rbp)
jmp    1297 <main+0xf7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe1d(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x28(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
xor    %edi,%edi
call   10a0 <crc64@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2d91(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x20(%rbp),%rcx
lea    -0x38(%rbp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
call   10c0 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x38(%rbp),%rsi
lea    0xd79(%rip),%rdi
call   1050 <printf@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   %r12
push   %rbx
sub    $0x10,%rsp
mov    $0x1,%r14d
cmp    $0x2,%edi
jg     1267 <main+0xc7>
mov    0x8(%rsi),%rdi
lea    0xe3a(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbx
mov    0x2e13(%rip),%r12
mov    (%r12),%edx
xor    %r14d,%r14d
mov    %rax,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1060 <rewind@plt>
mov    %rbp,%rdi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbp,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
xor    %edi,%edi
mov    %r15,%rsi
mov    %rbp,%rdx
call   10a0 <crc64@plt>
mov    %rax,0x8(%rsp)
mov    (%r12),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
lea    0x8(%rsp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10c0 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%rsi
lea    0xda2(%rip),%rdi
call   1050 <printf@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"r+b\00"
@SEEK_END = global i32 0
@.str.1 = [27 x i8] c"Checksum written: %016llx\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp sgt i32 %11, 2
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 1, i32* %3
br label %45
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %6
%18 = load i8*, i8** %6
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %7
%20 = load i32*, i32** %7
%21 = load i32, i32* @SEEK_END
%22 = call i32 @fseek(i32* %20, i32 0, i32 %21)
%23 = load i32*, i32** %7
%24 = call i64 @ftell(i32* %23)
store i64 %24, i64* %8
%25 = load i32*, i32** %7
%26 = call i32 @rewind(i32* %25)
%27 = load i64, i64* %8
%28 = call i8* @malloc(i64 %27)
store i8* %28, i8** %9
%29 = load i8*, i8** %9
%30 = load i64, i64* %8
%31 = load i32*, i32** %7
%32 = call i32 @fread(i8* %29, i64 %30, i32 1, i32* %31)
%33 = load i8*, i8** %9
%34 = load i64, i64* %8
%35 = call i64 @crc64(i32 0, i8* %33, i64 %34)
store i64 %35, i64* %10
%36 = load i32*, i32** %7
%37 = load i32, i32* @SEEK_END
%38 = call i32 @fseek(i32* %36, i32 0, i32 %37)
%39 = load i32*, i32** %7
%40 = call i32 @fwrite(i64* %10, i32 8, i32 1, i32* %39)
%41 = load i32*, i32** %7
%42 = call i32 @fclose(i32* %41)
%43 = load i64, i64* %10
%44 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i64 %43)
store i32 0, i32* %3
br label %45
45: ; preds = %14, %13
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@.str = [4 x i8] c"r+b\00"
@SEEK_END = global i32 0
@.str.1 = [27 x i8] c"Checksum written: %016llx\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i64
%4 = icmp sgt i32 %0, 2
br i1 %4, label %23, label %5
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i32* @fopen(i8* %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %8, i32 0, i32 %9)
%11 = tail call i64 @ftell(i32* %8)
%12 = tail call i32 @rewind(i32* %8)
%13 = tail call i8* @malloc(i64 %11)
%14 = tail call i32 @fread(i8* %13, i64 %11, i32 1, i32* %8)
%15 = bitcast i64* %3 to i8*
%16 = tail call i64 @crc64(i32 0, i8* %13, i64 %11)
store i64 %16, i64* %3
%17 = load i32, i32* @SEEK_END
%18 = tail call i32 @fseek(i32* %8, i32 0, i32 %17)
%19 = call i32 @fwrite(i64* %3, i32 8, i32 1, i32* %8)
%20 = tail call i32 @fclose(i32* %8)
%21 = load i64, i64* %3
%22 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i64 %21)
br label %23
23: ; preds = %2, %5
%24 = phi i32 [ 0, %5 ], [ 1, %2 ]
ret i32 %24
}