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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, ...);
int fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ stderr;
char *strerror(int);
int update_vector_checksum(const char *filename) {
  uint32_t sum;
  uint32_t header[8];
  FILE *fs;
  int i;
  if ((fs = fopen(filename, "r+b")) == NULL) {
    fprintf(stderr, "Unable to open '%s' for reading/writing (%d): %s\n",
            filename, errno, strerror(errno));
    return 0;
  }
  if (fread(header, sizeof(header), 1, fs) != 1) {
    fprintf(stderr,
            "Failed to read header from '%s' (perhaps the file is too small?)",
            filename);
    fclose(fs);
    return 0;
  }
  sum = 0;
  for (i = 0; i < 7; i++) {
    sum += header[i];
  }
  printf("sum = 0x%08x, value to write = 0x%08x\n", sum, -sum);
  sum = sum * -1;
  printf("Writing 0x08x\n", sum);
  fseek(fs, 0x1C, SEEK_SET);
  fwrite(&sum, 4, 1, fs);
  fclose(fs);
  return 1;
}


x86 assembly (clang, -O0):
<update_vector_checksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jne    11ff <update_vector_checksum+0x7f>
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    -0x50(%rbp),%ecx
mov    %rax,%r8
lea    0xe18(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ef <update_vector_checksum+0x16f>
lea    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rcx
mov    $0x20,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     124f <update_vector_checksum+0xcf>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe03(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x48(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ef <update_vector_checksum+0x16f>
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x4c(%rbp)
cmpl   $0x7,-0x4c(%rbp)
jge    1283 <update_vector_checksum+0x103>
movslq -0x4c(%rbp),%rax
mov    -0x40(%rbp,%rax,4),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    125d <update_vector_checksum+0xdd>
mov    -0x14(%rbp),%esi
xor    %edx,%edx
sub    -0x14(%rbp),%edx
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
imul   $0xffffffff,-0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x48(%rbp),%rdi
mov    0x2d14(%rip),%rax
mov    (%rax),%edx
mov    $0x1c,%esi
call   1070 <fseek@plt>
mov    -0x48(%rbp),%rcx
lea    -0x14(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    -0x48(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<update_vector_checksum>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%rbp
lea    0xe6c(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     124a <update_vector_checksum+0xca>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x1,%r14d
mov    $0x20,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    1282 <update_vector_checksum+0x102>
movdqa 0x10(%rsp),%xmm0
pshufd $0xee,%xmm0,%xmm1
paddd  %xmm0,%xmm1
pshufd $0x55,%xmm1,%xmm0
paddd  %xmm1,%xmm0
movd   %xmm0,%esi
add    0x20(%rsp),%esi
add    0x24(%rsp),%esi
add    0x28(%rsp),%esi
mov    %esi,%ebp
neg    %ebp
lea    0xe79(%rip),%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,0xc(%rsp)
lea    0xe8c(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x1c,%esi
call   1070 <fseek@plt>
lea    0xc(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
jmp    129f <update_vector_checksum+0x11f>
mov    0x2d8f(%rip),%rax
mov    (%rax),%r15d
mov    0x2d7d(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   10a0 <strerror@plt>
lea    0xd99(%rip),%rsi
xor    %r14d,%r14d
mov    %r15d,%edi
mov    %rbp,%rdx
mov    %ebx,%ecx
mov    %rax,%r8
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12a7 <update_vector_checksum+0x127>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
lea    0xda4(%rip),%rsi
xor    %r14d,%r14d
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"r+b\00"
@stderr = global i32 0
@.str.1 = [50 x i8] c"Unable to open '%s' for reading/writing (%d): %s\0A\00"
@errno = global i32 0
@.str.2 = [65 x i8] c"Failed to read header from '%s' (perhaps the file is too small?)\00"
@.str.3 = [39 x i8] c"sum = 0x%08x, value to write = 0x%08x\0A\00"
@.str.4 = [15 x i8] c"Writing 0x08x\0A\00"
@SEEK_SET = global i32 0
define i32 @update_vector_checksum(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [8 x i32]
%6 = alloca i32*
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %6
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %18
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %3
%14 = load i32, i32* @errno
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i8* %13, i32 %14, i8* %16)
store i32 0, i32* %2
br label %59
18: ; preds = %1
%19 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 0
%20 = load i32*, i32** %6
%21 = call i32 @fread(i32* %19, i32 32, i32 1, i32* %20)
%22 = icmp ne i32 %21, 1
br i1 %22, label %23, label %29
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %3
%26 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = load i32*, i32** %6
%28 = call i32 @fclose(i32* %27)
store i32 0, i32* %2
br label %59
29: ; preds = %18
store i32 0, i32* %4
store i32 0, i32* %7
br label %30
30: ; preds = %40, %29
%31 = load i32, i32* %7
%32 = icmp slt i32 %31, 7
br i1 %32, label %33, label %43
33: ; preds = %30
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 %35
%37 = load i32, i32* %36
%38 = load i32, i32* %4
%39 = add nsw i32 %38, %37
store i32 %39, i32* %4
br label %40
40: ; preds = %33
%41 = load i32, i32* %7
%42 = add nsw i32 %41, 1
store i32 %42, i32* %7
br label %30
43: ; preds = %30
%44 = load i32, i32* %4
%45 = load i32, i32* %4
%46 = sub nsw i32 0, %45
%47 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 %44, i32 %46)
%48 = load i32, i32* %4
%49 = mul nsw i32 %48, -1
store i32 %49, i32* %4
%50 = load i32, i32* %4
%51 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %50)
%52 = load i32*, i32** %6
%53 = load i32, i32* @SEEK_SET
%54 = call i32 @fseek(i32* %52, i32 28, i32 %53)
%55 = load i32*, i32** %6
%56 = call i32 @fwrite(i32* %4, i32 4, i32 1, i32* %55)
%57 = load i32*, i32** %6
%58 = call i32 @fclose(i32* %57)
store i32 1, i32* %2
br label %59
59: ; preds = %43, %23, %11
%60 = load i32, i32* %2
ret i32 %60
}

LLVM IR (-O2):
@.str = [4 x i8] c"r+b\00"
@stderr = global i32 0
@.str.1 = [50 x i8] c"Unable to open '%s' for reading/writing (%d): %s\0A\00"
@errno = global i32 0
@.str.2 = [65 x i8] c"Failed to read header from '%s' (perhaps the file is too small?)\00"
@.str.3 = [39 x i8] c"sum = 0x%08x, value to write = 0x%08x\0A\00"
@.str.4 = [15 x i8] c"Writing 0x08x\0A\00"
@SEEK_SET = global i32 0
define i32 @update_vector_checksum(i8* %0) {
%2 = alloca i32
%3 = alloca [8 x i32]
%4 = bitcast i32* %2 to i8*
%5 = bitcast [8 x i32]* %3 to i8*
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %10, i8* %11)
br label %41
13: ; preds = %1
%14 = getelementptr inbounds [8 x i32], [8 x i32]* %3, i64 0, i64 0
%15 = call i32 @fread(i32* %14, i32 32, i32 1, i32* %6)
%16 = icmp eq i32 %15, 1
br i1 %16, label %21, label %17
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%20 = tail call i32 @fclose(i32* %6)
br label %41
21: ; preds = %13
%22 = bitcast [8 x i32]* %3 to <4 x i32>*
%23 = load <4 x i32>, <4 x i32>* %22
%24 = getelementptr inbounds [8 x i32], [8 x i32]* %3, i64 0, i64 4
%25 = load i32, i32* %24
%26 = getelementptr inbounds [8 x i32], [8 x i32]* %3, i64 0, i64 5
%27 = load i32, i32* %26
%28 = getelementptr inbounds [8 x i32], [8 x i32]* %3, i64 0, i64 6
%29 = load i32, i32* %28
%30 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %23)
%31 = add nsw i32 %30, %25
%32 = add nsw i32 %31, %27
%33 = add nsw i32 %32, %29
%34 = sub nsw i32 0, %33
%35 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 %33, i32 %34)
store i32 %34, i32* %2
%36 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %34)
%37 = load i32, i32* @SEEK_SET
%38 = tail call i32 @fseek(i32* %6, i32 28, i32 %37)
%39 = call i32 @fwrite(i32* %2, i32 4, i32 1, i32* %6)
%40 = tail call i32 @fclose(i32* %6)
br label %41
41: ; preds = %21, %17, %8
%42 = phi i32 [ 0, %8 ], [ 0, %17 ], [ 1, %21 ]
ret i32 %42
}