#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 */

/* Variables and functions */
int /*<<< orphan*/ F_OK;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
scalar_t__ read(int, char **, int);
scalar_t__ write(int, unsigned int *, int);
int main(int argc, char **argv) {
  int filehandle;
  unsigned int sum = 0, i = 0;
  char buffer[28];
  if (argc > 1 && access(argv[1], F_OK) != -1) {
    if ((filehandle = open(argv[1], O_RDWR))) {
      if (read(filehandle, &buffer, 28)) {
        for (; i < 28; i += 4) {
          sum += *((int *)(buffer + i));
        }
        sum = (~sum) + 1;
        lseek(filehandle, 28, SEEK_SET);
        if (write(filehandle, &sum, 4)) {
          printf("Checksum added to firmware file\n");
          return 1;
        }
      } else {
      }
      close(filehandle);
    }
  }
  printf("Error: Firmware file could not be read\n");
  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)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1290 <main+0x120>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0xffffffff,%eax
je     1290 <main+0x120>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     128b <main+0x11b>
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1c,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
je     127e <main+0x10e>
jmp    11ff <main+0x8f>
cmpl   $0x1c,-0x1c(%rbp)
jae    1224 <main+0xb4>
mov    -0x1c(%rbp),%eax
mov    -0x40(%rbp,%rax,1),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ff <main+0x8f>
mov    -0x18(%rbp),%eax
xor    $0xffffffff,%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d9e(%rip),%rax
mov    (%rax),%edx
mov    $0x1c,%esi
call   1050 <lseek@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
je     1279 <main+0x109>
lea    0xd98(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a3 <main+0x133>
jmp    1283 <main+0x113>
jmp    1283 <main+0x113>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
jmp    1290 <main+0x120>
lea    0xd8a(%rip),%rdi
call   1040 <printf@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   %rbx
sub    $0x38,%rsp
xor    %r15d,%r15d
lea    0xe7c(%rip),%rbx
cmp    $0x2,%edi
jl     1240 <main+0xd0>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0xffffffff,%eax
je     1240 <main+0xd0>
mov    0x8(%rbp),%rdi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
je     1240 <main+0xd0>
mov    %eax,%r14d
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x1c,%edx
call   1070 <read@plt>
test   %rax,%rax
je     1238 <main+0xc8>
mov    0x10(%rsp),%eax
add    0x14(%rsp),%eax
add    0x18(%rsp),%eax
add    0x1c(%rsp),%eax
add    0x20(%rsp),%eax
add    0x24(%rsp),%eax
add    0x28(%rsp),%eax
neg    %eax
mov    %eax,0xc(%rsp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    $0x1c,%esi
call   1050 <lseek@plt>
lea    0xc(%rsp),%rsi
mov    %r14d,%edi
mov    $0x4,%edx
call   1040 <write@plt>
test   %rax,%rax
je     1238 <main+0xc8>
mov    $0x1,%r15d
lea    0xdf1(%rip),%rbx
jmp    1240 <main+0xd0>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %r15d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@F_OK = global i32 0
@O_RDWR = global i32 0
@SEEK_SET = global i32 0
@.str = [33 x i8] c"Checksum added to firmware file\0A\00"
@.str.1 = [40 x i8] c"Error: Firmware file could not be read\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 [28 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
store i32 0, i32* %8
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 1
br i1 %11, label %12, label %65
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @F_OK
%17 = call i32 @access(i8* %15, i32 %16)
%18 = icmp ne i32 %17, -1
br i1 %18, label %19, label %65
19: ; preds = %12
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDWR
%24 = call i32 @open(i8* %22, i32 %23)
store i32 %24, i32* %6
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %64
26: ; preds = %19
%27 = load i32, i32* %6
%28 = bitcast [28 x i8]* %9 to i8**
%29 = call i64 @read(i32 %27, i8** %28, i32 28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %60
31: ; preds = %26
br label %32
32: ; preds = %44, %31
%33 = load i32, i32* %8
%34 = icmp ult i32 %33, 28
br i1 %34, label %35, label %47
35: ; preds = %32
%36 = getelementptr inbounds [28 x i8], [28 x i8]* %9, i64 0, i64 0
%37 = load i32, i32* %8
%38 = zext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = bitcast i8* %39 to i32*
%41 = load i32, i32* %40
%42 = load i32, i32* %7
%43 = add i32 %42, %41
store i32 %43, i32* %7
br label %44
44: ; preds = %35
%45 = load i32, i32* %8
%46 = add i32 %45, 4
store i32 %46, i32* %8
br label %32
47: ; preds = %32
%48 = load i32, i32* %7
%49 = xor i32 %48, -1
%50 = add i32 %49, 1
store i32 %50, i32* %7
%51 = load i32, i32* %6
%52 = load i32, i32* @SEEK_SET
%53 = call i32 @lseek(i32 %51, i32 28, i32 %52)
%54 = load i32, i32* %6
%55 = call i64 @write(i32 %54, i32* %7, i32 4)
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %47
%58 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %67
59: ; preds = %47
br label %61
60: ; preds = %26
br label %61
61: ; preds = %60, %59
%62 = load i32, i32* %6
%63 = call i32 @close(i32 %62)
br label %64
64: ; preds = %61, %19
br label %65
65: ; preds = %64, %12, %2
%66 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %67
67: ; preds = %65, %57
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@F_OK = global i32 0
@O_RDWR = global i32 0
@SEEK_SET = global i32 0
@str = [39 x i8] c"Error: Firmware file could not be read\00"
@str.2 = [32 x i8] c"Checksum added to firmware file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca [28 x i8]
%5 = bitcast i32* %3 to i8*
%6 = getelementptr inbounds [28 x i8], [28 x i8]* %4, i64 0, i64 0
%7 = icmp sgt i32 %0, 1
br i1 %7, label %8, label %57
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @F_OK
%12 = tail call i32 @access(i8* %10, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %57, label %14
14: ; preds = %8
%15 = load i8*, i8** %9
%16 = load i32, i32* @O_RDWR
%17 = tail call i32 @open(i8* %15, i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %57, label %19
19: ; preds = %14
%20 = bitcast [28 x i8]* %4 to i8**
%21 = call i64 @read(i32 %17, i8** %20, i32 28)
%22 = icmp eq i64 %21, 0
br i1 %22, label %55, label %23
23: ; preds = %19
%24 = bitcast [28 x i8]* %4 to i32*
%25 = load i32, i32* %24
%26 = getelementptr inbounds [28 x i8], [28 x i8]* %4, i64 0, i64 4
%27 = bitcast i8* %26 to i32*
%28 = load i32, i32* %27
%29 = add i32 %25, %28
%30 = getelementptr inbounds [28 x i8], [28 x i8]* %4, i64 0, i64 8
%31 = bitcast i8* %30 to i32*
%32 = load i32, i32* %31
%33 = add i32 %29, %32
%34 = getelementptr inbounds [28 x i8], [28 x i8]* %4, i64 0, i64 12
%35 = bitcast i8* %34 to i32*
%36 = load i32, i32* %35
%37 = add i32 %33, %36
%38 = getelementptr inbounds [28 x i8], [28 x i8]* %4, i64 0, i64 16
%39 = bitcast i8* %38 to i32*
%40 = load i32, i32* %39
%41 = add i32 %37, %40
%42 = getelementptr inbounds [28 x i8], [28 x i8]* %4, i64 0, i64 20
%43 = bitcast i8* %42 to i32*
%44 = load i32, i32* %43
%45 = add i32 %41, %44
%46 = getelementptr inbounds [28 x i8], [28 x i8]* %4, i64 0, i64 24
%47 = bitcast i8* %46 to i32*
%48 = load i32, i32* %47
%49 = add i32 %45, %48
%50 = sub i32 0, %49
store i32 %50, i32* %3
%51 = load i32, i32* @SEEK_SET
%52 = tail call i32 @lseek(i32 %17, i32 28, i32 %51)
%53 = call i64 @write(i32 %17, i32* %3, i32 4)
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %19, %23
%56 = tail call i32 @close(i32 %17)
br label %57
57: ; preds = %2, %8, %55, %14, %23
%58 = phi i8* [ getelementptr inbounds ([32 x i8], [32 x i8]* @str.2, i64 0, i64 0), %23 ], [ getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0), %14 ], [ getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0), %55 ], [ getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0), %8 ], [ getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0), %2 ]
%59 = phi i32 [ 1, %23 ], [ 0, %14 ], [ 0, %55 ], [ 0, %8 ], [ 0, %2 ]
%60 = tail call i32 @puts(i8* dereferenceable(1) %58)
ret i32 %59
}