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

/* Variables and functions */
int EXIT_FAILURE;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fread(unsigned int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned int *);
int /*<<< orphan*/ fwrite(unsigned int *, int, int, int /*<<< orphan*/ *);
scalar_t__ htole32(unsigned int);
unsigned int *malloc(int);
int /*<<< orphan*/ perror(char *);
int stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ usage(char *);
int main(int argc, char *argv[]) {
  FILE *ifile, *ofile;
  unsigned int *buf;
  const char *infile;
  const char *outfile;
  struct stat st;
  unsigned int i;
  unsigned long sum = 0;
  if (argc != 3) {
    usage(argv[0]);
    exit(1);
  }
  infile = argv[1];
  outfile = argv[2];
  if (stat(infile, &st) == -1) {
    perror("stat");
    exit(EXIT_FAILURE);
  }
  buf = malloc(st.st_size);
  if (!buf) {
    fprintf(stderr, "Unable to allocate buffer\n");
    return -1;
  }
  ifile = fopen(infile, "rb");
  if (!ifile) {
    fprintf(stderr, "Cannot open %s for reading\n", infile);
    free(buf);
    exit(EXIT_FAILURE);
  }
  ofile = fopen(outfile, "wb");
  if (!ofile) {
    fprintf(stderr, "Cannot open %s for writing\n", outfile);
    fclose(ifile);
    free(buf);
    exit(EXIT_FAILURE);
  }
  fread(buf, 4, st.st_size, ifile);
  for (i = 0x8; i < 0x12; i++)
    sum += htole32(buf[i]);
  sum = ~sum;
  buf[i] = sum;
  fwrite(buf, st.st_size / 4, 4, ofile);
  fclose(ofile);
  fclose(ifile);
  free(buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x50(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11fe <main+0x3e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10a0 <usage@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
jne    1246 <main+0x86>
lea    0xdcd(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x40(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1280 <main+0xc0>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
lea    0xd98(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d3 <main+0x213>
mov    -0x30(%rbp),%rdi
lea    0xd95(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12d1 <main+0x111>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xd70(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    0x2d0e(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x38(%rbp),%rdi
lea    0xd63(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    132b <main+0x16b>
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
lea    0xd3e(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    0x2cb4(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x40(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x4,%esi
call   1050 <fread@plt>
movl   $0x8,-0x44(%rbp)
cmpl   $0x12,-0x44(%rbp)
jae    1376 <main+0x1b6>
mov    -0x28(%rbp),%rax
mov    -0x44(%rbp),%ecx
mov    (%rax,%rcx,4),%edi
call   1030 <htole32@plt>
add    -0x50(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1347 <main+0x187>
mov    -0x50(%rbp),%rax
xor    $0xffffffffffffffff,%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
mov    %eax,%edx
mov    -0x28(%rbp),%rax
mov    -0x44(%rbp),%ecx
mov    %edx,(%rax,%rcx,4)
mov    -0x28(%rbp),%rdi
mov    -0x40(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %eax,%esi
mov    -0x20(%rbp),%rcx
mov    $0x4,%edx
call   10e0 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    1347 <main+0x187>
mov    0x8(%rsi),%r12
mov    0x10(%rsi),%r13
mov    %rsp,%rsi
mov    %r12,%rdi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
je     1359 <main+0x199>
mov    (%rsp),%r15d
mov    %r15d,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     131c <main+0x15c>
mov    %rax,%r14
lea    0xe11(%rip),%rsi
mov    %r12,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1367 <main+0x1a7>
mov    %rax,%rbx
lea    0xe15(%rip),%rsi
mov    %r13,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1383 <main+0x1c3>
mov    %rax,%r12
mov    %r14,%rdi
mov    $0x4,%esi
mov    %r15d,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
mov    0x20(%r14),%edi
call   1030 <htole32@plt>
mov    %rax,%r13
mov    0x24(%r14),%edi
call   1030 <htole32@plt>
mov    %rax,%rbp
add    %r13d,%ebp
mov    0x28(%r14),%edi
call   1030 <htole32@plt>
mov    %rbx,%r13
mov    %rax,%rbx
add    %ebp,%ebx
mov    0x2c(%r14),%edi
call   1030 <htole32@plt>
mov    %rax,%rbp
add    %ebx,%ebp
mov    0x30(%r14),%edi
call   1030 <htole32@plt>
mov    %rax,%rbx
add    %ebp,%ebx
mov    0x34(%r14),%edi
call   1030 <htole32@plt>
mov    %rax,%rbp
add    %ebx,%ebp
mov    0x38(%r14),%edi
call   1030 <htole32@plt>
mov    %rax,%rbx
add    %ebp,%ebx
mov    0x3c(%r14),%edi
call   1030 <htole32@plt>
mov    %rax,%rbp
add    %ebx,%ebp
mov    0x40(%r14),%edi
call   1030 <htole32@plt>
mov    %rax,%rbx
add    %ebp,%ebx
mov    0x44(%r14),%edi
call   1030 <htole32@plt>
add    %ebx,%eax
not    %eax
mov    %eax,0x48(%r14)
lea    0x3(%r15),%esi
test   %r15d,%r15d
cmovns %r15d,%esi
sar    $0x2,%esi
mov    %r14,%rdi
mov    $0x4,%edx
mov    %r12,%rcx
call   10e0 <fwrite@plt>
mov    %r12,%rdi
call   1060 <fclose@plt>
mov    %r13,%rdi
call   1060 <fclose@plt>
mov    %r14,%rdi
call   1040 <free@plt>
xor    %eax,%eax
jmp    1338 <main+0x178>
mov    0x2cbd(%rip),%rax
mov    (%rax),%edi
lea    0xcd9(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsi),%rdi
call   10a0 <usage@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xca0(%rip),%rdi
call   10c0 <perror@plt>
jmp    13ad <main+0x1ed>
mov    0x2c72(%rip),%rax
mov    (%rax),%edi
lea    0xcac(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    13a5 <main+0x1e5>
mov    0x2c56(%rip),%rax
mov    (%rax),%edi
lea    0xcaf(%rip),%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %r14,%rdi
call   1040 <free@plt>
mov    0x2c24(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [5 x i8] c"stat\00"
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"Unable to allocate buffer\0A\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [28 x i8] c"Cannot open %s for reading\0A\00"
@.str.4 = [3 x i8] c"wb\00"
@.str.5 = [28 x i8] c"Cannot open %s for writing\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 i8*
%10 = alloca i8*
%11 = alloca %struct.stat
%12 = alloca i32
%13 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %13
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 3
br i1 %15, label %16, label %22
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 @usage(i8* %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
store i8* %25, i8** %9
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
store i8* %28, i8** %10
%29 = load i8*, i8** %9
%30 = call i32 @stat(i8* %29, %struct.stat* %11)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %22
%33 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %22
%37 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%38 = load i32, i32* %37
%39 = call i32* @malloc(i32 %38)
store i32* %39, i32** %8
%40 = load i32*, i32** %8
%41 = icmp ne i32* %40, null
br i1 %41, label %45, label %42
42: ; preds = %36
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %115
45: ; preds = %36
%46 = load i8*, i8** %9
%47 = call i32* @fopen(i8* %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %47, i32** %6
%48 = load i32*, i32** %6
%49 = icmp ne i32* %48, null
br i1 %49, label %58, label %50
50: ; preds = %45
%51 = load i32, i32* @stderr
%52 = load i8*, i8** %9
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %52)
%54 = load i32*, i32** %8
%55 = call i32 @free(i32* %54)
%56 = load i32, i32* @EXIT_FAILURE
%57 = call i32 @exit(i32 %56)
unreachable
58: ; preds = %45
%59 = load i8*, i8** %10
%60 = call i32* @fopen(i8* %59, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %60, i32** %7
%61 = load i32*, i32** %7
%62 = icmp ne i32* %61, null
br i1 %62, label %73, label %63
63: ; preds = %58
%64 = load i32, i32* @stderr
%65 = load i8*, i8** %10
%66 = call i32 (i32, i8*, ...) @fprintf(i32 %64, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %65)
%67 = load i32*, i32** %6
%68 = call i32 @fclose(i32* %67)
%69 = load i32*, i32** %8
%70 = call i32 @free(i32* %69)
%71 = load i32, i32* @EXIT_FAILURE
%72 = call i32 @exit(i32 %71)
unreachable
73: ; preds = %58
%74 = load i32*, i32** %8
%75 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%76 = load i32, i32* %75
%77 = load i32*, i32** %6
%78 = call i32 @fread(i32* %74, i32 4, i32 %76, i32* %77)
store i32 8, i32* %12
br label %79
79: ; preds = %91, %73
%80 = load i32, i32* %12
%81 = icmp ult i32 %80, 18
br i1 %81, label %82, label %94
82: ; preds = %79
%83 = load i32*, i32** %8
%84 = load i32, i32* %12
%85 = zext i32 %84 to i64
%86 = getelementptr inbounds i32, i32* %83, i64 %85
%87 = load i32, i32* %86
%88 = call i64 @htole32(i32 %87)
%89 = load i64, i64* %13
%90 = add i64 %89, %88
store i64 %90, i64* %13
br label %91
91: ; preds = %82
%92 = load i32, i32* %12
%93 = add i32 %92, 1
store i32 %93, i32* %12
br label %79
94: ; preds = %79
%95 = load i64, i64* %13
%96 = xor i64 %95, -1
store i64 %96, i64* %13
%97 = load i64, i64* %13
%98 = trunc i64 %97 to i32
%99 = load i32*, i32** %8
%100 = load i32, i32* %12
%101 = zext i32 %100 to i64
%102 = getelementptr inbounds i32, i32* %99, i64 %101
store i32 %98, i32* %102
%103 = load i32*, i32** %8
%104 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%105 = load i32, i32* %104
%106 = sdiv i32 %105, 4
%107 = load i32*, i32** %7
%108 = call i32 @fwrite(i32* %103, i32 %106, i32 4, i32* %107)
%109 = load i32*, i32** %7
%110 = call i32 @fclose(i32* %109)
%111 = load i32*, i32** %6
%112 = call i32 @fclose(i32* %111)
%113 = load i32*, i32** %8
%114 = call i32 @free(i32* %113)
store i32 0, i32* %3
br label %115
115: ; preds = %94, %42
%116 = load i32, i32* %3
ret i32 %116
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [5 x i8] c"stat\00"
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"Unable to allocate buffer\0A\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [28 x i8] c"Cannot open %s for reading\0A\00"
@.str.4 = [3 x i8] c"wb\00"
@.str.5 = [28 x i8] c"Cannot open %s for writing\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i32 %0, 3
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @usage(i8* %7)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = call i32 @stat(i8* %12, %struct.stat* %3)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %10
%18 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %10
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%23 = load i32, i32* %22
%24 = tail call i32* @malloc(i32 %23)
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %97
29: ; preds = %21
%30 = tail call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %38
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %12)
%35 = tail call i32 @free(i32* %24)
%36 = load i32, i32* @EXIT_FAILURE
%37 = tail call i32 @exit(i32 %36)
unreachable
38: ; preds = %29
%39 = tail call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%40 = icmp eq i32* %39, null
br i1 %40, label %41, label %48
41: ; preds = %38
%42 = load i32, i32* @stderr
%43 = tail call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %14)
%44 = tail call i32 @fclose(i32* %30)
%45 = tail call i32 @free(i32* %24)
%46 = load i32, i32* @EXIT_FAILURE
%47 = tail call i32 @exit(i32 %46)
unreachable
48: ; preds = %38
%49 = tail call i32 @fread(i32* %24, i32 4, i32 %23, i32* %30)
%50 = getelementptr inbounds i32, i32* %24, i64 8
%51 = load i32, i32* %50
%52 = tail call i64 @htole32(i32 %51)
%53 = getelementptr inbounds i32, i32* %24, i64 9
%54 = load i32, i32* %53
%55 = tail call i64 @htole32(i32 %54)
%56 = add i64 %55, %52
%57 = getelementptr inbounds i32, i32* %24, i64 10
%58 = load i32, i32* %57
%59 = tail call i64 @htole32(i32 %58)
%60 = add i64 %59, %56
%61 = getelementptr inbounds i32, i32* %24, i64 11
%62 = load i32, i32* %61
%63 = tail call i64 @htole32(i32 %62)
%64 = add i64 %63, %60
%65 = getelementptr inbounds i32, i32* %24, i64 12
%66 = load i32, i32* %65
%67 = tail call i64 @htole32(i32 %66)
%68 = add i64 %67, %64
%69 = getelementptr inbounds i32, i32* %24, i64 13
%70 = load i32, i32* %69
%71 = tail call i64 @htole32(i32 %70)
%72 = add i64 %71, %68
%73 = getelementptr inbounds i32, i32* %24, i64 14
%74 = load i32, i32* %73
%75 = tail call i64 @htole32(i32 %74)
%76 = add i64 %75, %72
%77 = getelementptr inbounds i32, i32* %24, i64 15
%78 = load i32, i32* %77
%79 = tail call i64 @htole32(i32 %78)
%80 = add i64 %79, %76
%81 = getelementptr inbounds i32, i32* %24, i64 16
%82 = load i32, i32* %81
%83 = tail call i64 @htole32(i32 %82)
%84 = add i64 %83, %80
%85 = getelementptr inbounds i32, i32* %24, i64 17
%86 = load i32, i32* %85
%87 = tail call i64 @htole32(i32 %86)
%88 = add i64 %87, %84
%89 = trunc i64 %88 to i32
%90 = xor i32 %89, -1
%91 = getelementptr inbounds i32, i32* %24, i64 18
store i32 %90, i32* %91
%92 = sdiv i32 %23, 4
%93 = tail call i32 @fwrite(i32* %24, i32 %92, i32 4, i32* %39)
%94 = tail call i32 @fclose(i32* %39)
%95 = tail call i32 @fclose(i32* %30)
%96 = tail call i32 @free(i32* %24)
br label %97
97: ; preds = %48, %26
%98 = phi i32 [ 0, %48 ], [ -1, %26 ]
ret i32 %98
}