#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__ 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(scalar_t__ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int fwrite(scalar_t__ *, 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;
  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    12e3 <main+0x173>
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    12e3 <main+0x173>
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2dcb(%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    1234 <main+0xc4>
jmp    1276 <main+0x106>
mov    -0x18(%rbp),%rcx
lea    -0x28(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     1259 <main+0xe9>
jmp    1276 <main+0x106>
mov    -0x28(%rbp),%rax
xor    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1216 <main+0xa6>
mov    -0x20(%rbp),%rax
xor    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2d5b(%rip),%rax
mov    (%rax),%edx
mov    $0xfffffffc,%esi
call   1070 <fseek@plt>
mov    -0x18(%rbp),%rcx
lea    -0x28(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1090 <fwrite@plt>
cmp    $0x1,%eax
je     12d3 <main+0x163>
lea    0xd77(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e3 <main+0x173>
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   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    1250 <main+0xd0>
mov    0x8(%rsi),%rdi
lea    0xe82(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1264 <main+0xe4>
mov    %rax,%r15
movq   $0x0,(%rsp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
xor    %r12d,%r12d
mov    %r15,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r15,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    1210 <main+0x90>
xor    %ebx,%ebx
mov    %rsp,%r14
nopl   0x0(%rax)
mov    %r14,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    (%rsp),%r12
cmp    $0x1,%eax
jne    120d <main+0x8d>
xor    %r12,%rbx
mov    %r15,%rdi
call   1070 <feof@plt>
test   %eax,%eax
je     11e0 <main+0x60>
xor    %rbx,%r12
mov    %r12,(%rsp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    $0xfffffffc,%esi
call   1080 <fseek@plt>
mov    %rsp,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   10a0 <fwrite@plt>
cmp    $0x1,%eax
jne    126d <main+0xed>
mov    %r15,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
jmp    127e <main+0xfe>
mov    %edi,%eax
lea    0xda7(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1279 <main+0xf9>
lea    0xdb7(%rip),%rdi
jmp    1274 <main+0xf4>
lea    0xdc2(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
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 = [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 i64
%8 = alloca i64
%9 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* %4
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 -1, i32* %3
br label %59
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %6
%20 = load i32*, i32** %6
%21 = icmp ne i32* %20, null
br i1 %21, label %24, label %22
22: ; preds = %15
%23 = 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 %59
24: ; preds = %15
store i64 0, i64* %7
store i64 0, i64* %8
store i64 0, i64* %9
%25 = load i32*, i32** %6
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @fseek(i32* %25, i32 0, i32 %26)
br label %28
28: ; preds = %38, %24
%29 = load i32*, i32** %6
%30 = call i32 @feof(i32* %29)
%31 = icmp ne i32 %30, 0
%32 = xor i1 %31, true
br i1 %32, label %33, label %44
33: ; preds = %28
%34 = load i32*, i32** %6
%35 = call i32 @fread(i64* %8, i32 4, i32 1, i32* %34)
%36 = icmp ne i32 %35, 1
br i1 %36, label %37, label %38
37: ; preds = %33
br label %44
38: ; preds = %33
%39 = load i64, i64* %8
%40 = load i64, i64* %7
%41 = xor i64 %40, %39
store i64 %41, i64* %7
%42 = load i64, i64* %9
%43 = add nsw i64 %42, 1
store i64 %43, i64* %9
br label %28
44: ; preds = %37, %28
%45 = load i64, i64* %7
%46 = load i64, i64* %8
%47 = xor i64 %46, %45
store i64 %47, i64* %8
%48 = load i32*, i32** %6
%49 = load i32, i32* @SEEK_END
%50 = call i32 @fseek(i32* %48, i32 -4, i32 %49)
%51 = load i32*, i32** %6
%52 = call i32 @fwrite(i64* %8, i32 4, i32 1, i32* %51)
%53 = icmp ne i32 %52, 1
br i1 %53, label %54, label %56
54: ; preds = %44
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %59
56: ; preds = %44
%57 = load i32*, i32** %6
%58 = call i32 @fclose(i32* %57)
store i32 0, i32* %3
br label %59
59: ; preds = %56, %54, %22, %12
%60 = load i32, i32* %3
ret i32 %60
}

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 = [20 x i8] c"Failed to open file\00"
@str.4 = [16 x i8] c"Failed to write\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i64
%4 = icmp eq i32 %0, 2
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = 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 %44
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %44
14: ; preds = %7
%15 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%16 = load i32, i32* @SEEK_SET
%17 = tail call i32 @fseek(i32* %10, i32 0, i32 %16)
%18 = tail call i32 @feof(i32* %10)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %32
20: ; preds = %14, %25
%21 = phi i64 [ %26, %25 ], [ 0, %14 ]
%22 = call i32 @fread(i64* %3, i32 4, i32 1, i32* %10)
%23 = icmp eq i32 %22, 1
%24 = load i64, i64* %3
br i1 %23, label %25, label %29
25: ; preds = %20
%26 = xor i64 %24, %21
%27 = tail call i32 @feof(i32* %10)
%28 = icmp eq i32 %27, 0
br i1 %28, label %20, label %29
29: ; preds = %25, %20
%30 = phi i64 [ %21, %20 ], [ %26, %25 ]
%31 = xor i64 %24, %30
br label %32
32: ; preds = %29, %14
%33 = phi i64 [ %31, %29 ], [ 0, %14 ]
store i64 %33, i64* %3
%34 = load i32, i32* @SEEK_END
%35 = tail call i32 @fseek(i32* %10, i32 -4, i32 %34)
%36 = call i32 @fwrite(i64* %3, i32 4, i32 1, i32* %10)
%37 = icmp eq i32 %36, 1
br i1 %37, label %40, label %38
38: ; preds = %32
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.4, i64 0, i64 0))
br label %42
40: ; preds = %32
%41 = tail call i32 @fclose(i32* %10)
br label %42
42: ; preds = %40, %38
%43 = phi i32 [ -1, %38 ], [ 0, %40 ]
br label %44
44: ; preds = %12, %42, %5
%45 = phi i32 [ -1, %5 ], [ %43, %42 ], [ -1, %12 ]
ret i32 %45
}