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

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int fwrite(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    printf("Invalid parameter count (%d)\n", argc);
    return -1;
  }
  FILE *f = fopen(argv[1], "rb+");
  if (!f) {
    printf("Failed to open file\n");
    return -1;
  }
  uint32_t checksum = 0;
  uint32_t data = 0;
  uint32_t read = 0;
  fseek(f, 0, SEEK_SET);
  while (!feof(f)) {
    if (fread(&data, 4, 1, f) != 1) {
      break;
    }
    checksum ^= data;
    read++;
  }
  data ^= checksum;
  uint64_t footer_magic = 0;
  fseek(f, -8, SEEK_END);
  if ((fread(&footer_magic, 8, 1, f) != 1) ||
      (footer_magic != 0xCCCCCCCCE12FFF13)) {
    printf("Footer magic error (expected 0x%lX, got 0x%lX)\n",
           0xCCCCCCCCE12FFF13, footer_magic);
    return -1;
  }
  fseek(f, -4, SEEK_END);
  if (fwrite(&data, 4, 1, f) != 1) {
    printf("Failed to write\n");
    return -1;
  }
  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   $0x2,-0x8(%rbp)
je     11ad <main+0x3d>
mov    -0x8(%rbp),%esi
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1353 <main+0x1e3>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe62(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ea <main+0x7a>
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1353 <main+0x1e3>
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2dce(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1231 <main+0xc1>
jmp    126d <main+0xfd>
mov    -0x18(%rbp),%rcx
lea    -0x20(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     1256 <main+0xe6>
jmp    126d <main+0xfd>
mov    -0x20(%rbp),%eax
xor    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1213 <main+0xa3>
mov    -0x1c(%rbp),%eax
xor    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2d60(%rip),%rax
mov    (%rax),%edx
mov    $0xfffffff8,%esi
call   1070 <fseek@plt>
mov    -0x18(%rbp),%rcx
lea    -0x28(%rbp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    12cb <main+0x15b>
movslq -0x28(%rbp),%rax
movabs $0xcccccccce12fff13,%rcx
cc cc cc
cmp    %rcx,%rax
je     12f2 <main+0x182>
mov    -0x28(%rbp),%edx
lea    0xd62(%rip),%rdi
movabs $0xcccccccce12fff13,%rsi
cc cc cc
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1353 <main+0x1e3>
mov    -0x18(%rbp),%rdi
mov    0x2ceb(%rip),%rax
mov    (%rax),%edx
mov    $0xfffffffc,%esi
call   1070 <fseek@plt>
mov    -0x18(%rbp),%rcx
lea    -0x20(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1090 <fwrite@plt>
cmp    $0x1,%eax
je     1343 <main+0x1d3>
lea    0xd37(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1353 <main+0x1e3>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    1235 <main+0xd5>
mov    0x8(%rsi),%rdi
lea    0xea6(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1249 <main+0xe9>
mov    %rax,%rbx
movl   $0x0,0x4(%rsp)
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    11e6 <main+0x86>
lea    0x4(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    11e6 <main+0x86>
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %eax,%eax
je     11c0 <main+0x60>
movl   $0x0,(%rsp)
mov    0x2df4(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0xfffffff8,%esi
call   1070 <fseek@plt>
mov    %rsp,%rdi
mov    $0x8,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    (%rsp),%edx
lea    0xe00(%rip),%rdi
movabs $0xcccccccce12fff13,%rsi
cc cc cc
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1255 <main+0xf5>
mov    %edi,%eax
lea    0xdc2(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1255 <main+0xf5>
lea    0xe02(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [30 x i8] c"Invalid parameter count (%d)\0A\00"
@.str.1 = [4 x i8] c"rb+\00"
@.str.2 = [21 x i8] c"Failed to open file\0A\00"
@SEEK_SET = global i32 0
@SEEK_END = global i32 0
@.str.3 = [48 x i8] c"Footer magic error (expected 0x%lX, got 0x%lX)\0A\00"
@.str.4 = [17 x i8] c"Failed to write\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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* %4
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 -1, i32* %3
br label %74
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %20, i32** %6
%21 = load i32*, i32** %6
%22 = icmp ne i32* %21, null
br i1 %22, label %25, label %23
23: ; preds = %16
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %74
25: ; preds = %16
store i32 0, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
%26 = load i32*, i32** %6
%27 = load i32, i32* @SEEK_SET
%28 = call i32 @fseek(i32* %26, i32 0, i32 %27)
br label %29
29: ; preds = %39, %25
%30 = load i32*, i32** %6
%31 = call i32 @feof(i32* %30)
%32 = icmp ne i32 %31, 0
%33 = xor i1 %32, true
br i1 %33, label %34, label %45
34: ; preds = %29
%35 = load i32*, i32** %6
%36 = call i32 @fread(i32* %8, i32 4, i32 1, i32* %35)
%37 = icmp ne i32 %36, 1
br i1 %37, label %38, label %39
38: ; preds = %34
br label %45
39: ; preds = %34
%40 = load i32, i32* %8
%41 = load i32, i32* %7
%42 = xor i32 %41, %40
store i32 %42, i32* %7
%43 = load i32, i32* %9
%44 = add nsw i32 %43, 1
store i32 %44, i32* %9
br label %29
45: ; preds = %38, %29
%46 = load i32, i32* %7
%47 = load i32, i32* %8
%48 = xor i32 %47, %46
store i32 %48, i32* %8
store i32 0, i32* %10
%49 = load i32*, i32** %6
%50 = load i32, i32* @SEEK_END
%51 = call i32 @fseek(i32* %49, i32 -8, i32 %50)
%52 = load i32*, i32** %6
%53 = call i32 @fread(i32* %10, i32 8, i32 1, i32* %52)
%54 = icmp ne i32 %53, 1
br i1 %54, label %59, label %55
55: ; preds = %45
%56 = load i32, i32* %10
%57 = sext i32 %56 to i64
%58 = icmp ne i64 %57, -3689348814399865069
br i1 %58, label %59, label %62
59: ; preds = %55, %45
%60 = load i32, i32* %10
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i64 -3689348814399865069, i32 %60)
store i32 -1, i32* %3
br label %74
62: ; preds = %55
%63 = load i32*, i32** %6
%64 = load i32, i32* @SEEK_END
%65 = call i32 @fseek(i32* %63, i32 -4, i32 %64)
%66 = load i32*, i32** %6
%67 = call i32 @fwrite(i32* %8, i32 4, i32 1, i32* %66)
%68 = icmp ne i32 %67, 1
br i1 %68, label %69, label %71
69: ; preds = %62
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %74
71: ; preds = %62
%72 = load i32*, i32** %6
%73 = call i32 @fclose(i32* %72)
store i32 0, i32* %3
br label %74
74: ; preds = %71, %69, %59, %23, %13
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
@.str = [30 x i8] c"Invalid parameter count (%d)\0A\00"
@.str.1 = [4 x i8] c"rb+\00"
@SEEK_SET = global i32 0
@SEEK_END = global i32 0
@.str.3 = [48 x i8] c"Footer magic error (expected 0x%lX, got 0x%lX)\0A\00"
@str = [20 x i8] c"Failed to open file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = icmp eq i32 %0, 2
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %34
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 ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %34
15: ; preds = %8
%16 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%17 = load i32, i32* @SEEK_SET
%18 = tail call i32 @fseek(i32* %11, i32 0, i32 %17)
%19 = tail call i32 @feof(i32* %11)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %15, %24
%22 = call i32 @fread(i32* %3, i32 4, i32 1, i32* %11)
%23 = icmp eq i32 %22, 1
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = tail call i32 @feof(i32* %11)
%26 = icmp eq i32 %25, 0
br i1 %26, label %21, label %27
27: ; preds = %21, %24, %15
%28 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%29 = load i32, i32* @SEEK_END
%30 = tail call i32 @fseek(i32* %11, i32 -8, i32 %29)
%31 = call i32 @fread(i32* %4, i32 8, i32 1, i32* %11)
%32 = load i32, i32* %4
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i64 -3689348814399865069, i32 %32)
br label %34
34: ; preds = %13, %27, %6
ret i32 -1
}