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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int _filesize(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
scalar_t__ le2int(unsigned char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ stderr;
int checksum(FILE *file) {
  int oldpos = ftell(file);
  int ret = 0, i, filesize = _filesize(file) - 0x40;
  unsigned char *buf;
  buf = (unsigned char *)malloc(filesize);
  if (buf == NULL) {
    fseek(file, oldpos, SEEK_SET);
    fprintf(stderr, "[ERR] Error while allocating memory\n");
    return 0;
  }
  fseek(file, 0x40, SEEK_SET);
  if (fread(buf, filesize, 1, file) != 1) {
    free(buf);
    fseek(file, oldpos, SEEK_SET);
    fprintf(stderr, "[ERR] Error while reading from file\n");
    return 0;
  }
  fprintf(stderr, "[INFO] Computing checksum...");
  for (i = 0; i < filesize; i += 4)
    ret += le2int(&buf[i]);
  free(buf);
  fseek(file, oldpos, SEEK_SET);
  fprintf(stderr, " Done!\n");
  return ret;
}


x86 assembly (clang, -O0):
<checksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <_filesize@plt>
sub    $0x40,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11fb <checksum+0x7b>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
call   10a0 <fseek@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0xe16(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12fb <checksum+0x17b>
mov    -0x10(%rbp),%rdi
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
mov    $0x40,%esi
call   10a0 <fseek@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x1,%eax
je     126f <checksum+0xef>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2d91(%rip),%rax
mov    (%rax),%edx
call   10a0 <fseek@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
lea    0xdc7(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12fb <checksum+0x17b>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    12c2 <checksum+0x142>
mov    -0x28(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
call   1080 <le2int@plt>
mov    %rax,%rcx
movslq -0x18(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x1c(%rbp)
jmp    128b <checksum+0x10b>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2cff(%rip),%rax
mov    (%rax),%edx
call   10a0 <fseek@plt>
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
lea    0xd77(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checksum>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
call   1070 <ftell@plt>
mov    %eax,0x4(%rsp)
mov    %r15,%rdi
call   1050 <_filesize@plt>
mov    %eax,%r14d
lea    -0x40(%r14),%r12d
mov    %r12d,%edi
call   1090 <malloc@plt>
mov    %rax,%r13
mov    0x2e20(%rip),%rax
mov    (%rax),%edx
test   %r13,%r13
je     122d <checksum+0xad>
mov    %r15,%rdi
mov    $0x40,%esi
call   10a0 <fseek@plt>
mov    %r13,%rdi
mov    %r12d,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
xor    %ebp,%ebp
cmp    $0x1,%eax
jne    123d <checksum+0xbd>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
lea    0xe54(%rip),%rsi
call   1060 <fprintf@plt>
cmp    $0x41,%r14d
mov    0x4(%rsp),%r14d
jl     1224 <checksum+0xa4>
xor    %ebx,%ebx
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
lea    (%rbx,%r13,1),%rdi
call   1080 <le2int@plt>
add    %eax,%ebp
add    $0x4,%rbx
cmp    %ebx,%r12d
jg     1210 <checksum+0x90>
lea    0xe3c(%rip),%rbx
jmp    1249 <checksum+0xc9>
xor    %ebp,%ebp
lea    0xdca(%rip),%rbx
mov    0x4(%rsp),%r14d
jmp    125a <checksum+0xda>
lea    0xde1(%rip),%rbx
mov    0x4(%rsp),%r14d
mov    %r13,%rdi
call   1030 <free@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    %r14d,%esi
call   10a0 <fseek@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
call   1060 <fprintf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str = [37 x i8] c"[ERR] Error while allocating memory\0A\00"
@.str.1 = [37 x i8] c"[ERR] Error while reading from file\0A\00"
@.str.2 = [29 x i8] c"[INFO] Computing checksum...\00"
@.str.3 = [8 x i8] c" Done!\0A\00"
define i32 @checksum(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32* %0, i32** %3
%9 = load i32*, i32** %3
%10 = call i32 @ftell(i32* %9)
store i32 %10, i32* %4
store i32 0, i32* %5
%11 = load i32*, i32** %3
%12 = call i32 @_filesize(i32* %11)
%13 = sub nsw i32 %12, 64
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %8
%17 = load i8*, i8** %8
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %26
19: ; preds = %1
%20 = load i32*, i32** %3
%21 = load i32, i32* %4
%22 = load i32, i32* @SEEK_SET
%23 = call i32 @fseek(i32* %20, i32 %21, i32 %22)
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %74
26: ; preds = %1
%27 = load i32*, i32** %3
%28 = load i32, i32* @SEEK_SET
%29 = call i32 @fseek(i32* %27, i32 64, i32 %28)
%30 = load i8*, i8** %8
%31 = load i32, i32* %7
%32 = load i32*, i32** %3
%33 = call i32 @fread(i8* %30, i32 %31, i32 1, i32* %32)
%34 = icmp ne i32 %33, 1
br i1 %34, label %35, label %44
35: ; preds = %26
%36 = load i8*, i8** %8
%37 = call i32 @free(i8* %36)
%38 = load i32*, i32** %3
%39 = load i32, i32* %4
%40 = load i32, i32* @SEEK_SET
%41 = call i32 @fseek(i32* %38, i32 %39, i32 %40)
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %2
br label %74
44: ; preds = %26
%45 = load i32, i32* @stderr
%46 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %6
br label %47
47: ; preds = %61, %44
%48 = load i32, i32* %6
%49 = load i32, i32* %7
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %64
51: ; preds = %47
%52 = load i8*, i8** %8
%53 = load i32, i32* %6
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %52, i64 %54
%56 = call i64 @le2int(i8* %55)
%57 = load i32, i32* %5
%58 = sext i32 %57 to i64
%59 = add nsw i64 %58, %56
%60 = trunc i64 %59 to i32
store i32 %60, i32* %5
br label %61
61: ; preds = %51
%62 = load i32, i32* %6
%63 = add nsw i32 %62, 4
store i32 %63, i32* %6
br label %47
64: ; preds = %47
%65 = load i8*, i8** %8
%66 = call i32 @free(i8* %65)
%67 = load i32*, i32** %3
%68 = load i32, i32* %4
%69 = load i32, i32* @SEEK_SET
%70 = call i32 @fseek(i32* %67, i32 %68, i32 %69)
%71 = load i32, i32* @stderr
%72 = call i32 @fprintf(i32 %71, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%73 = load i32, i32* %5
store i32 %73, i32* %2
br label %74
74: ; preds = %64, %35, %19
%75 = load i32, i32* %2
ret i32 %75
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str = [37 x i8] c"[ERR] Error while allocating memory\0A\00"
@.str.1 = [37 x i8] c"[ERR] Error while reading from file\0A\00"
@.str.2 = [29 x i8] c"[INFO] Computing checksum...\00"
@.str.3 = [8 x i8] c" Done!\0A\00"
define i32 @checksum(i32* %0) {
%2 = tail call i32 @ftell(i32* %0)
%3 = tail call i32 @_filesize(i32* %0)
%4 = add nsw i32 %3, -64
%5 = tail call i64 @malloc(i32 %4)
%6 = inttoptr i64 %5 to i8*
%7 = icmp eq i64 %5, 0
%8 = load i32, i32* @SEEK_SET
br i1 %7, label %32, label %9
9: ; preds = %1
%10 = tail call i32 @fseek(i32* %0, i32 64, i32 %8)
%11 = tail call i32 @fread(i8* %6, i32 %4, i32 1, i32* %0)
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %27
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp sgt i32 %3, 64
br i1 %16, label %17, label %27
17: ; preds = %13, %17
%18 = phi i64 [ %24, %17 ], [ 0, %13 ]
%19 = phi i32 [ %23, %17 ], [ 0, %13 ]
%20 = getelementptr inbounds i8, i8* %6, i64 %18
%21 = tail call i64 @le2int(i8* %20)
%22 = trunc i64 %21 to i32
%23 = add i32 %19, %22
%24 = add nuw i64 %18, 4
%25 = trunc i64 %24 to i32
%26 = icmp sgt i32 %4, %25
br i1 %26, label %17, label %27
27: ; preds = %17, %13, %9
%28 = phi i8* [ getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), %9 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), %13 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), %17 ]
%29 = phi i32 [ 0, %9 ], [ 0, %13 ], [ %23, %17 ]
%30 = tail call i32 @free(i8* %6)
%31 = load i32, i32* @SEEK_SET
br label %32
32: ; preds = %27, %1
%33 = phi i32 [ %8, %1 ], [ %31, %27 ]
%34 = phi i8* [ getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), %1 ], [ %28, %27 ]
%35 = phi i32 [ 0, %1 ], [ %29, %27 ]
%36 = tail call i32 @fseek(i32* %0, i32 %2, i32 %33)
%37 = load i32, i32* @stderr
%38 = tail call i32 @fprintf(i32 %37, i8* %34)
ret i32 %35
}