#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*/ uint8_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*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fread(int /*<<< orphan*/ *, size_t, int,
                         int /*<<< orphan*/ *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(int /*<<< orphan*/ *, size_t, int,
                          int /*<<< orphan*/ *);
scalar_t__ getu32(int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putu32(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  FILE *bin;
  uint32_t data;
  size_t len;
  int count;
  uint8_t *buffer;
  if (argc < 2) {
    fprintf(stderr,
            "Usage: %s <binfile>\nThe original file will be modified!\n",
            argv[0]);
    return 1;
  }
  if ((bin = fopen(argv[1], "rb")) == NULL) {
    perror("could not open input file");
    return 1;
  }
  fseek(bin, 0, SEEK_END);
  len = ftell(bin);
  fseek(bin, 0, SEEK_SET);
  if ((buffer = malloc(len)) == NULL) {
    perror("could not reserve memory");
    fclose(bin);
    return 1;
  }
  fread(buffer, len, 1, bin);
  fclose(bin);
  data = 0;
  for (count = 0; count < 7; count++) {
    data += getu32(buffer + 4 * count);
  }
  printf("data=%x chksum=%x\n", data, ~data + 1);
  putu32(buffer + 28, ~data + 1);
  if ((bin = fopen(argv[1], "wb")) == NULL) {
    perror("could not open output file");
    free(buffer);
    return 1;
  }
  fwrite(buffer, len, 1, bin);
  fclose(bin);
  printf("done\n");
  free(buffer);
  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)
jge    1218 <main+0x48>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdf9(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d9 <main+0x209>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe12(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1252 <main+0x82>
lea    0xdfb(%rip),%rdi
call   10e0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d9 <main+0x209>
mov    -0x18(%rbp),%rdi
mov    0x2d8b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1090 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2d52(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    12bf <main+0xef>
lea    0xdb1(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d9 <main+0x209>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x7,-0x2c(%rbp)
jge    1329 <main+0x159>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%eax
shl    $0x2,%eax
cltq
shl    $0x2,%rax
add    %rax,%rdi
call   10b0 <getu32@plt>
mov    %rax,%rcx
movslq -0x1c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    12ec <main+0x11c>
mov    -0x1c(%rbp),%esi
mov    -0x1c(%rbp),%edx
xor    $0xffffffff,%edx
add    $0x1,%edx
lea    0xd33(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x38(%rbp),%rdi
add    $0x70,%rdi
mov    -0x1c(%rbp),%esi
xor    $0xffffffff,%esi
add    $0x1,%esi
call   1070 <putu32@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xd1a(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    139c <main+0x1cc>
lea    0xd03(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d9 <main+0x209>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   10f0 <fwrite@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xcde(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@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   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x1,%edi
jg     1216 <main+0x36>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xdfa(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%r12d
jmp    1353 <main+0x173>
mov    0x8(%r14),%rdi
lea    0xe18(%rip),%rsi
call   10e0 <fopen@plt>
test   %rax,%rax
je     1365 <main+0x185>
mov    %rax,%rbp
mov    0x2daf(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    %rbp,%rdi
call   10a0 <ftell@plt>
mov    %rax,%r15
mov    0x2d79(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    %r15,%rdi
call   10b0 <malloc@plt>
test   %rax,%rax
je     1379 <main+0x199>
mov    %rax,%r13
mov    $0x1,%r12d
mov    %rax,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1050 <fread@plt>
mov    %rbp,%rdi
call   1060 <fclose@plt>
mov    %r13,%rdi
call   10c0 <getu32@plt>
mov    %rax,%rbp
lea    0x10(%r13),%rdi
call   10c0 <getu32@plt>
add    %eax,%ebp
lea    0x20(%r13),%rdi
call   10c0 <getu32@plt>
add    %eax,%ebp
lea    0x30(%r13),%rdi
call   10c0 <getu32@plt>
add    %eax,%ebp
lea    0x40(%r13),%rdi
call   10c0 <getu32@plt>
add    %eax,%ebp
lea    0x50(%r13),%rdi
call   10c0 <getu32@plt>
add    %eax,%ebp
lea    0x60(%r13),%rdi
call   10c0 <getu32@plt>
add    %eax,%ebp
mov    %ebp,%ebx
neg    %ebx
lea    0xd7f(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r13,%rdi
add    $0x70,%rdi
mov    %ebx,%esi
call   1080 <putu32@plt>
mov    0x8(%r14),%rdi
lea    0xd6e(%rip),%rsi
call   10e0 <fopen@plt>
test   %rax,%rax
je     1395 <main+0x1b5>
mov    %rax,%rbp
mov    %r13,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
mov    %rax,%rcx
call   1100 <fwrite@plt>
mov    %rbp,%rdi
call   1060 <fclose@plt>
lea    0xd5d(%rip),%rdi
call   1040 <puts@plt>
mov    %r13,%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcd0(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%r12d
jmp    1353 <main+0x173>
lea    0xcd6(%rip),%rdi
call   10f0 <perror@plt>
mov    %rbp,%rdi
call   1060 <fclose@plt>
mov    $0x1,%r12d
jmp    1353 <main+0x173>
lea    0xce9(%rip),%rdi
call   10f0 <perror@plt>
mov    %r13,%rdi
call   1030 <free@plt>
jmp    1353 <main+0x173>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [57 x i8] c"Usage: %s <binfile>\0AThe original file will be modified!\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [26 x i8] c"could not open input file\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [25 x i8] c"could not reserve memory\00"
@.str.4 = [19 x i8] c"data=%x chksum=%x\0A\00"
@.str.5 = [3 x i8] c"wb\00"
@.str.6 = [27 x i8] c"could not open output file\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 i32*
%7 = alloca i32
%8 = alloca i64
%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 slt i32 %11, 2
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 1, i32* %3
br label %98
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %23, i32** %6
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %98
27: ; preds = %19
%28 = load i32*, i32** %6
%29 = load i32, i32* @SEEK_END
%30 = call i32 @fseek(i32* %28, i32 0, i32 %29)
%31 = load i32*, i32** %6
%32 = call i64 @ftell(i32* %31)
store i64 %32, i64* %8
%33 = load i32*, i32** %6
%34 = load i32, i32* @SEEK_SET
%35 = call i32 @fseek(i32* %33, i32 0, i32 %34)
%36 = load i64, i64* %8
%37 = call i32* @malloc(i64 %36)
store i32* %37, i32** %10
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %43
39: ; preds = %27
%40 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%41 = load i32*, i32** %6
%42 = call i32 @fclose(i32* %41)
store i32 1, i32* %3
br label %98
43: ; preds = %27
%44 = load i32*, i32** %10
%45 = load i64, i64* %8
%46 = load i32*, i32** %6
%47 = call i32 @fread(i32* %44, i64 %45, i32 1, i32* %46)
%48 = load i32*, i32** %6
%49 = call i32 @fclose(i32* %48)
store i32 0, i32* %7
store i32 0, i32* %9
br label %50
50: ; preds = %64, %43
%51 = load i32, i32* %9
%52 = icmp slt i32 %51, 7
br i1 %52, label %53, label %67
53: ; preds = %50
%54 = load i32*, i32** %10
%55 = load i32, i32* %9
%56 = mul nsw i32 4, %55
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i32, i32* %54, i64 %57
%59 = call i64 @getu32(i32* %58)
%60 = load i32, i32* %7
%61 = sext i32 %60 to i64
%62 = add nsw i64 %61, %59
%63 = trunc i64 %62 to i32
store i32 %63, i32* %7
br label %64
64: ; preds = %53
%65 = load i32, i32* %9
%66 = add nsw i32 %65, 1
store i32 %66, i32* %9
br label %50
67: ; preds = %50
%68 = load i32, i32* %7
%69 = load i32, i32* %7
%70 = xor i32 %69, -1
%71 = add nsw i32 %70, 1
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %68, i32 %71)
%73 = load i32*, i32** %10
%74 = getelementptr inbounds i32, i32* %73, i64 28
%75 = load i32, i32* %7
%76 = xor i32 %75, -1
%77 = add nsw i32 %76, 1
%78 = call i32 @putu32(i32* %74, i32 %77)
%79 = load i8**, i8*** %5
%80 = getelementptr inbounds i8*, i8** %79, i64 1
%81 = load i8*, i8** %80
%82 = call i32* @fopen(i8* %81, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
store i32* %82, i32** %6
%83 = icmp eq i32* %82, null
br i1 %83, label %84, label %88
84: ; preds = %67
%85 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
%86 = load i32*, i32** %10
%87 = call i32 @free(i32* %86)
store i32 1, i32* %3
br label %98
88: ; preds = %67
%89 = load i32*, i32** %10
%90 = load i64, i64* %8
%91 = load i32*, i32** %6
%92 = call i32 @fwrite(i32* %89, i64 %90, i32 1, i32* %91)
%93 = load i32*, i32** %6
%94 = call i32 @fclose(i32* %93)
%95 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%96 = load i32*, i32** %10
%97 = call i32 @free(i32* %96)
store i32 0, i32* %3
br label %98
98: ; preds = %88, %84, %39, %25, %13
%99 = load i32, i32* %3
ret i32 %99
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [57 x i8] c"Usage: %s <binfile>\0AThe original file will be modified!\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [26 x i8] c"could not open input file\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [25 x i8] c"could not reserve memory\00"
@.str.4 = [19 x i8] c"data=%x chksum=%x\0A\00"
@.str.5 = [3 x i8] c"wb\00"
@.str.6 = [27 x i8] c"could not open output file\00"
@str = [5 x i8] c"done\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %70
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 ([3 x i8], [3 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 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %70
15: ; preds = %8
%16 = load i32, i32* @SEEK_END
%17 = tail call i32 @fseek(i32* %11, i32 0, i32 %16)
%18 = tail call i64 @ftell(i32* %11)
%19 = load i32, i32* @SEEK_SET
%20 = tail call i32 @fseek(i32* %11, i32 0, i32 %19)
%21 = tail call i32* @malloc(i64 %18)
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %15
%24 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%25 = tail call i32 @fclose(i32* %11)
br label %70
26: ; preds = %15
%27 = tail call i32 @fread(i32* %21, i64 %18, i32 1, i32* %11)
%28 = tail call i32 @fclose(i32* %11)
%29 = tail call i64 @getu32(i32* %21)
%30 = trunc i64 %29 to i32
%31 = getelementptr inbounds i32, i32* %21, i64 4
%32 = tail call i64 @getu32(i32* %31)
%33 = trunc i64 %32 to i32
%34 = add i32 %30, %33
%35 = getelementptr inbounds i32, i32* %21, i64 8
%36 = tail call i64 @getu32(i32* %35)
%37 = trunc i64 %36 to i32
%38 = add i32 %34, %37
%39 = getelementptr inbounds i32, i32* %21, i64 12
%40 = tail call i64 @getu32(i32* %39)
%41 = trunc i64 %40 to i32
%42 = add i32 %38, %41
%43 = getelementptr inbounds i32, i32* %21, i64 16
%44 = tail call i64 @getu32(i32* %43)
%45 = trunc i64 %44 to i32
%46 = add i32 %42, %45
%47 = getelementptr inbounds i32, i32* %21, i64 20
%48 = tail call i64 @getu32(i32* %47)
%49 = trunc i64 %48 to i32
%50 = add i32 %46, %49
%51 = getelementptr inbounds i32, i32* %21, i64 24
%52 = tail call i64 @getu32(i32* %51)
%53 = trunc i64 %52 to i32
%54 = add i32 %50, %53
%55 = sub i32 0, %54
%56 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %54, i32 %55)
%57 = getelementptr inbounds i32, i32* %21, i64 28
%58 = tail call i32 @putu32(i32* %57, i32 %55)
%59 = load i8*, i8** %9
%60 = tail call i32* @fopen(i8* %59, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%61 = icmp eq i32* %60, null
br i1 %61, label %62, label %65
62: ; preds = %26
%63 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
%64 = tail call i32 @free(i32* %21)
br label %70
65: ; preds = %26
%66 = tail call i32 @fwrite(i32* %21, i64 %18, i32 1, i32* %60)
%67 = tail call i32 @fclose(i32* %60)
%68 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%69 = tail call i32 @free(i32* %21)
br label %70
70: ; preds = %65, %62, %23, %13, %4
%71 = phi i32 [ 1, %4 ], [ 1, %13 ], [ 1, %23 ], [ 1, %62 ], [ 0, %65 ]
ret i32 %71
}