#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_CUR;
int /*<<< orphan*/ SEEK_END;
int atoi(char *);
scalar_t__ endian_swap(unsigned int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  FILE *in;
  long filesize;
  unsigned int sum = 0;
  unsigned int buffer;
  int count = 0;
  int start = 0;
  int end = 0;
  if (argc < 2) {
    printf("Usage: addsum inputfile [startoffset [endoffset]]\n");
    return -1;
  }
  if ((in = fopen(argv[1], "rb")) == NULL) {
    printf("Cant't open file name %s\n", argv[1]);
    return -1;
  }
  if (argc >= 3) {
    start = atoi(argv[2]);
    if (argc >= 4) {
      end = atoi(argv[3]);
    }
  }
  fseek(in, 0, SEEK_END);
  filesize = ftell(in);
  rewind(in);
  printf("file size: %d (0x%08X) bytes\n", filesize, filesize);
  if (end == 0) {
    end = filesize;
  } else if (end < 0) {
    end = filesize - end;
  }
  printf("calc range: %d - %d = %d bytes (0x%08X - 0x%08X = 0x%08X)\n", start,
         end, end - start, start, end, end - start);
  fseek(in, start, SEEK_CUR);
  for (count = 0; count < (end - start) / 4; count++) {
    fread(&buffer, 4, 1, in);
    sum += endian_swap(buffer);
  }
  printf("checksum = 0x%08X\n", sum, count * 4);
  fclose(in);
  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,-0x24(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11e6 <main+0x56>
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139e <main+0x20e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe3e(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    122a <main+0x9a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139e <main+0x20e>
cmpl   $0x3,-0x8(%rbp)
jl     1263 <main+0xd3>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x4,-0x8(%rbp)
jl     125e <main+0xce>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x34(%rbp)
jmp    1263 <main+0xd3>
mov    -0x18(%rbp),%rdi
mov    0x2d7a(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <rewind@plt>
mov    -0x20(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xdb4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpl   $0x0,-0x34(%rbp)
jne    12b9 <main+0x129>
mov    -0x20(%rbp),%rax
mov    %eax,-0x34(%rbp)
jmp    12d6 <main+0x146>
cmpl   $0x0,-0x34(%rbp)
jge    12d1 <main+0x141>
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x34(%rbp)
jmp    12d6 <main+0x146>
mov    -0x30(%rbp),%esi
mov    -0x34(%rbp),%edx
mov    -0x34(%rbp),%ecx
sub    -0x30(%rbp),%ecx
mov    -0x30(%rbp),%r8d
mov    -0x34(%rbp),%r9d
mov    -0x34(%rbp),%eax
sub    -0x30(%rbp),%eax
lea    0xd77(%rip),%rdi
mov    %eax,(%rsp)
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%esi
mov    0x2cd1(%rip),%rax
mov    (%rax),%edx
call   1090 <fseek@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x34(%rbp),%eax
sub    -0x30(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
cmp    %ecx,%eax
jge    1377 <main+0x1e7>
mov    -0x18(%rbp),%rcx
lea    -0x28(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x28(%rbp),%edi
call   1040 <endian_swap@plt>
mov    %rax,%rcx
mov    -0x24(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x24(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    131d <main+0x18d>
mov    -0x24(%rbp),%esi
mov    -0x2c(%rbp),%edx
shl    $0x2,%edx
lea    0xd22(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@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
sub    $0x18,%rsp
cmp    $0x1,%edi
jg     11ca <main+0x2a>
lea    0xecf(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r15d
jmp    1324 <main+0x184>
mov    %rsi,%rbp
mov    %edi,%ebx
mov    0x8(%rsi),%rdi
lea    0xe26(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     120e <main+0x6e>
mov    %rax,%r13
xor    %r12d,%r12d
cmp    $0x3,%ebx
jb     122b <main+0x8b>
mov    0x10(%rbp),%rdi
call   10c0 <atoi@plt>
mov    %eax,%r14d
cmp    $0x3,%ebx
je     122e <main+0x8e>
mov    0x18(%rbp),%rdi
call   10c0 <atoi@plt>
mov    %eax,%r12d
jmp    122e <main+0x8e>
mov    0x8(%rbp),%rsi
lea    0xdea(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xffffffff,%r15d
jmp    1324 <main+0x184>
xor    %r14d,%r14d
mov    0x2db3(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r13,%rdi
call   1090 <ftell@plt>
mov    %rax,%rbx
mov    %r13,%rdi
call   1080 <rewind@plt>
lea    0xdc2(%rip),%rdi
mov    %rbx,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
test   %r12d,%r12d
je     1272 <main+0xd2>
jns    1275 <main+0xd5>
sub    %r12d,%ebx
mov    %ebx,%r12d
mov    %r12d,%ebx
sub    %r14d,%ebx
mov    %ebx,(%rsp)
lea    0xdb6(%rip),%rdi
xor    %r15d,%r15d
mov    %r14d,%esi
mov    %r12d,%edx
mov    %ebx,%ecx
mov    %r14d,%r8d
mov    %r12d,%r9d
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
mov    %r14d,%esi
call   10a0 <fseek@plt>
lea    0x3(%rbx),%r14d
test   %ebx,%ebx
cmovns %ebx,%r14d
mov    $0x0,%ebp
mov    $0x0,%edx
cmp    $0x4,%ebx
jl     130c <main+0x16c>
sar    $0x2,%r14d
xor    %ebp,%ebp
lea    0x14(%rsp),%r12
mov    %r14d,%ebx
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1040 <fread@plt>
mov    0x14(%rsp),%edi
call   1050 <endian_swap@plt>
add    %eax,%ebp
add    $0xffffffff,%ebx
jne    12e0 <main+0x140>
shl    $0x2,%r14d
mov    %r14d,%edx
lea    0xd63(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r13,%rdi
call   1060 <fclose@plt>
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [51 x i8] c"Usage: addsum inputfile [startoffset [endoffset]]\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [26 x i8] c"Cant't open file name %s\0A\00"
@SEEK_END = global i32 0
@.str.3 = [30 x i8] c"file size: %d (0x%08X) bytes\0A\00"
@.str.4 = [59 x i8] c"calc range: %d - %d = %d bytes (0x%08X - 0x%08X = 0x%08X)\0A\00"
@SEEK_CUR = global i32 0
@.str.5 = [19 x i8] c"checksum = 0x%08X\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %8
store i32 0, i32* %10
store i32 0, i32* %11
store i32 0, i32* %12
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %112
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %6
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %28
23: ; preds = %17
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %26)
store i32 -1, i32* %3
br label %112
28: ; preds = %17
%29 = load i32, i32* %4
%30 = icmp sge i32 %29, 3
br i1 %30, label %31, label %44
31: ; preds = %28
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 2
%34 = load i8*, i8** %33
%35 = call i32 @atoi(i8* %34)
store i32 %35, i32* %11
%36 = load i32, i32* %4
%37 = icmp sge i32 %36, 4
br i1 %37, label %38, label %43
38: ; preds = %31
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 3
%41 = load i8*, i8** %40
%42 = call i32 @atoi(i8* %41)
store i32 %42, i32* %12
br label %43
43: ; preds = %38, %31
br label %44
44: ; preds = %43, %28
%45 = load i32*, i32** %6
%46 = load i32, i32* @SEEK_END
%47 = call i32 @fseek(i32* %45, i32 0, i32 %46)
%48 = load i32*, i32** %6
%49 = call i64 @ftell(i32* %48)
store i64 %49, i64* %7
%50 = load i32*, i32** %6
%51 = call i32 @rewind(i32* %50)
%52 = load i64, i64* %7
%53 = load i64, i64* %7
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i64 %52, i64 %53)
%55 = load i32, i32* %12
%56 = icmp eq i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %44
%58 = load i64, i64* %7
%59 = trunc i64 %58 to i32
store i32 %59, i32* %12
br label %70
60: ; preds = %44
%61 = load i32, i32* %12
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %69
63: ; preds = %60
%64 = load i64, i64* %7
%65 = load i32, i32* %12
%66 = sext i32 %65 to i64
%67 = sub nsw i64 %64, %66
%68 = trunc i64 %67 to i32
store i32 %68, i32* %12
br label %69
69: ; preds = %63, %60
br label %70
70: ; preds = %69, %57
%71 = load i32, i32* %11
%72 = load i32, i32* %12
%73 = load i32, i32* %12
%74 = load i32, i32* %11
%75 = sub nsw i32 %73, %74
%76 = load i32, i32* %11
%77 = load i32, i32* %12
%78 = load i32, i32* %12
%79 = load i32, i32* %11
%80 = sub nsw i32 %78, %79
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.4, i64 0, i64 0), i32 %71, i32 %72, i32 %75, i32 %76, i32 %77, i32 %80)
%82 = load i32*, i32** %6
%83 = load i32, i32* %11
%84 = load i32, i32* @SEEK_CUR
%85 = call i32 @fseek(i32* %82, i32 %83, i32 %84)
store i32 0, i32* %10
br label %86
86: ; preds = %102, %70
%87 = load i32, i32* %10
%88 = load i32, i32* %12
%89 = load i32, i32* %11
%90 = sub nsw i32 %88, %89
%91 = sdiv i32 %90, 4
%92 = icmp slt i32 %87, %91
br i1 %92, label %93, label %105
93: ; preds = %86
%94 = load i32*, i32** %6
%95 = call i32 @fread(i32* %9, i32 4, i32 1, i32* %94)
%96 = load i32, i32* %9
%97 = call i64 @endian_swap(i32 %96)
%98 = load i32, i32* %8
%99 = zext i32 %98 to i64
%100 = add nsw i64 %99, %97
%101 = trunc i64 %100 to i32
store i32 %101, i32* %8
br label %102
102: ; preds = %93
%103 = load i32, i32* %10
%104 = add nsw i32 %103, 1
store i32 %104, i32* %10
br label %86
105: ; preds = %86
%106 = load i32, i32* %8
%107 = load i32, i32* %10
%108 = mul nsw i32 %107, 4
%109 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %106, i32 %108)
%110 = load i32*, i32** %6
%111 = call i32 @fclose(i32* %110)
store i32 0, i32* %3
br label %112
112: ; preds = %105, %23, %15
%113 = load i32, i32* %3
ret i32 %113
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [26 x i8] c"Cant't open file name %s\0A\00"
@SEEK_END = global i32 0
@.str.3 = [30 x i8] c"file size: %d (0x%08X) bytes\0A\00"
@.str.4 = [59 x i8] c"calc range: %d - %d = %d bytes (0x%08X - 0x%08X = 0x%08X)\0A\00"
@SEEK_CUR = global i32 0
@.str.5 = [19 x i8] c"checksum = 0x%08X\0A\00"
@str = [50 x i8] c"Usage: addsum inputfile [startoffset [endoffset]]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str, i64 0, i64 0))
br label %68
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 %16
13: ; preds = %8
%14 = load i8*, i8** %9
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %14)
br label %68
16: ; preds = %8
%17 = icmp ugt i32 %0, 2
br i1 %17, label %18, label %27
18: ; preds = %16
%19 = getelementptr inbounds i8*, i8** %1, i64 2
%20 = load i8*, i8** %19
%21 = tail call i32 @atoi(i8* %20)
%22 = icmp eq i32 %0, 3
br i1 %22, label %27, label %23
23: ; preds = %18
%24 = getelementptr inbounds i8*, i8** %1, i64 3
%25 = load i8*, i8** %24
%26 = tail call i32 @atoi(i8* %25)
br label %27
27: ; preds = %18, %23, %16
%28 = phi i32 [ %21, %23 ], [ %21, %18 ], [ 0, %16 ]
%29 = phi i32 [ %26, %23 ], [ 0, %18 ], [ 0, %16 ]
%30 = load i32, i32* @SEEK_END
%31 = tail call i32 @fseek(i32* %11, i32 0, i32 %30)
%32 = tail call i64 @ftell(i32* %11)
%33 = tail call i32 @rewind(i32* %11)
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i64 %32, i64 %32)
%35 = icmp eq i32 %29, 0
br i1 %35, label %36, label %38
36: ; preds = %27
%37 = trunc i64 %32 to i32
br label %43
38: ; preds = %27
%39 = icmp slt i32 %29, 0
br i1 %39, label %40, label %43
40: ; preds = %38
%41 = trunc i64 %32 to i32
%42 = sub i32 %41, %29
br label %43
43: ; preds = %38, %40, %36
%44 = phi i32 [ %37, %36 ], [ %42, %40 ], [ %29, %38 ]
%45 = sub nsw i32 %44, %28
%46 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([59 x i8], [59 x i8]* @.str.4, i64 0, i64 0), i32 %28, i32 %44, i32 %45, i32 %28, i32 %44, i32 %45)
%47 = load i32, i32* @SEEK_CUR
%48 = tail call i32 @fseek(i32* %11, i32 %28, i32 %47)
%49 = sdiv i32 %45, 4
%50 = icmp sgt i32 %45, 3
br i1 %50, label %51, label %63
51: ; preds = %43, %51
%52 = phi i32 [ %59, %51 ], [ 0, %43 ]
%53 = phi i32 [ %58, %51 ], [ 0, %43 ]
%54 = call i32 @fread(i32* %3, i32 4, i32 1, i32* %11)
%55 = load i32, i32* %3
%56 = tail call i64 @endian_swap(i32 %55)
%57 = trunc i64 %56 to i32
%58 = add i32 %53, %57
%59 = add nuw nsw i32 %52, 1
%60 = icmp eq i32 %59, %49
br i1 %60, label %61, label %51
61: ; preds = %51
%62 = shl nsw i32 %49, 2
br label %63
63: ; preds = %61, %43
%64 = phi i32 [ 0, %43 ], [ %58, %61 ]
%65 = phi i32 [ 0, %43 ], [ %62, %61 ]
%66 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %64, i32 %65)
%67 = tail call i32 @fclose(i32* %11)
br label %68
68: ; preds = %63, %13, %6
%69 = phi i32 [ -1, %6 ], [ -1, %13 ], [ 0, %63 ]
ret i32 %69
}