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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SHA1_Final(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA1_Init(int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA1_Update(int /*<<< orphan*/ *, long *, int);
int SHA_DIGEST_LENGTH;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, long *, int);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time();
int main(int argc, char **argv) {
  int fd, length = 4 * 1024;
  void *memptr;
  long *long_array;
  int i;
  SHA_CTX context;
  char md[20];
  long randnum, half;
  if (argc != 3) {
    printf("USAGE: sum <filename> <size>\n");
    exit(-1);
  }
  printf("[SUM] opening file %s\n", argv[1]);
  length = atoi(argv[2]) * 1024 * 1024;
  printf("[SUM] size is %d\n", length);
  fd = open(argv[1], O_RDWR);
  srand(time());
  memset(md, 0, 20);
  SHA1_Init(&context);
  for (i = 0; i < length / sizeof(long); i++) {
    int n;
    n = read(fd, &randnum, sizeof(long));
    SHA1_Update(&context, &randnum, sizeof(long));
    if (n < sizeof(long)) {
      printf("[ERROR] read %d bytes!\n", n);
    }
  }
  SHA1_Final(md, &context);
  close(fd);
  printf("[SUM] ");
  for (i = 0; i < SHA_DIGEST_LENGTH; ++i) {
    unsigned char c = md[i];
    printf("%2.2x", c);
  }
  printf("\n");
  printf("munmap is unmapping %x\n", memptr);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1000,-0x18(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11ff <main+0x3f>
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10c0 <atoi@plt>
shl    $0xa,%eax
shl    $0xa,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1080 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1040 <memset@plt>
lea    -0x30(%rbp),%rdi
call   10a0 <SHA1_Init@plt>
movl   $0x0,-0x2c(%rbp)
movslq -0x2c(%rbp),%rax
movslq -0x18(%rbp),%rcx
shr    $0x3,%rcx
cmp    %rcx,%rax
jae    12f0 <main+0x130>
mov    -0x14(%rbp),%edi
lea    -0x58(%rbp),%rsi
mov    $0x8,%edx
call   1060 <read@plt>
mov    %eax,-0x64(%rbp)
lea    -0x30(%rbp),%rdi
lea    -0x58(%rbp),%rsi
mov    $0x8,%edx
call   1090 <SHA1_Update@plt>
movslq -0x64(%rbp),%rax
cmp    $0x8,%rax
jae    12dd <main+0x11d>
mov    -0x64(%rbp),%esi
lea    0xd71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12e2 <main+0x122>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1283 <main+0xc3>
lea    -0x50(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10e0 <SHA1_Final@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
lea    0xd53(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2cbc(%rip),%rcx
cmp    (%rcx),%eax
jge    1357 <main+0x197>
movslq -0x2c(%rbp),%rax
mov    -0x50(%rbp,%rax,1),%al
mov    %al,-0x65(%rbp)
movzbl -0x65(%rbp),%esi
lea    0xd24(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    131a <main+0x15a>
lea    0xcff(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rsi
lea    0xcfc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
cmp    $0x3,%edi
jne    1364 <main+0x184>
mov    %rsi,%rbx
mov    0x8(%rsi),%rsi
lea    0xdfb(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x10(%rbx),%rdi
call   10e0 <atoi@plt>
mov    %eax,%r14d
mov    %eax,%ebp
shl    $0x14,%ebp
lea    0xdf3(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rbx),%rdi
mov    0x2db0(%rip),%rax
mov    (%rax),%esi
call   10d0 <open@plt>
mov    %eax,%r12d
xor    %eax,%eax
call   10a0 <time@plt>
mov    %eax,%edi
call   1090 <srand@plt>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1060 <memset@plt>
lea    0xc(%rsp),%rdi
call   10c0 <SHA1_Init@plt>
test   %r14d,%r14d
je     12db <main+0xfb>
movslq %ebp,%rax
shr    $0x3,%rax
cmp    $0x2,%rax
mov    $0x1,%ebp
cmovae %rax,%rbp
lea    0x30(%rsp),%r13
lea    0xc(%rsp),%r14
lea    0xd94(%rip),%r15
jmp    12a6 <main+0xc6>
nopw   0x0(%rax,%rax,1)
add    $0xffffffffffffffff,%rbp
je     12db <main+0xfb>
mov    %r12d,%edi
mov    %r13,%rsi
mov    $0x8,%edx
call   1080 <read@plt>
mov    %eax,%ebx
mov    %r14,%rdi
mov    %r13,%rsi
mov    $0x8,%edx
call   10b0 <SHA1_Update@plt>
cmp    $0x7,%ebx
ja     12a0 <main+0xc0>
mov    %r15,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12a0 <main+0xc0>
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rsi
call   1100 <SHA1_Final@plt>
mov    %r12d,%edi
call   1070 <close@plt>
lea    0xd48(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2cd9(%rip),%r14
cmpl   $0x0,(%r14)
jle    133b <main+0x15b>
lea    0xd34(%rip),%rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
movzbl 0x10(%rsp,%rbp,1),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbp
movslq (%r14),%rax
cmp    %rax,%rbp
jl     1320 <main+0x140>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xd02(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcfb(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
@.str = [30 x i8] c"USAGE: sum <filename> <size>\0A\00"
@.str.1 = [23 x i8] c"[SUM] opening file %s\0A\00"
@.str.2 = [18 x i8] c"[SUM] size is %d\0A\00"
@O_RDWR = global i32 0
@.str.3 = [24 x i8] c"[ERROR] read %d bytes!\0A\00"
@.str.4 = [7 x i8] c"[SUM] \00"
@SHA_DIGEST_LENGTH = global i32 0
@.str.5 = [6 x i8] c"%2.2x\00"
@.str.6 = [2 x i8] c"\0A\00"
@.str.7 = [24 x i8] c"munmap is unmapping %x\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 i8*
%9 = alloca i64*
%10 = alloca i32
%11 = alloca i32
%12 = alloca [20 x i8]
%13 = alloca i64
%14 = alloca i64
%15 = alloca i32
%16 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 4096, i32* %7
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, 3
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = call i32 @atoi(i8* %29)
%31 = mul nsw i32 %30, 1024
%32 = mul nsw i32 %31, 1024
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %33)
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = load i32, i32* @O_RDWR
%39 = call i32 @open(i8* %37, i32 %38)
store i32 %39, i32* %6
%40 = call i32 (...) @time()
%41 = call i32 @srand(i32 %40)
%42 = getelementptr inbounds [20 x i8], [20 x i8]* %12, i64 0, i64 0
%43 = call i32 @memset(i8* %42, i32 0, i32 20)
%44 = call i32 @SHA1_Init(i32* %11)
store i32 0, i32* %10
br label %45
45: ; preds = %63, %22
%46 = load i32, i32* %10
%47 = sext i32 %46 to i64
%48 = load i32, i32* %7
%49 = sext i32 %48 to i64
%50 = udiv i64 %49, 8
%51 = icmp ult i64 %47, %50
br i1 %51, label %52, label %66
52: ; preds = %45
%53 = load i32, i32* %6
%54 = call i32 @read(i32 %53, i64* %13, i32 8)
store i32 %54, i32* %15
%55 = call i32 @SHA1_Update(i32* %11, i64* %13, i32 8)
%56 = load i32, i32* %15
%57 = sext i32 %56 to i64
%58 = icmp ult i64 %57, 8
br i1 %58, label %59, label %62
59: ; preds = %52
%60 = load i32, i32* %15
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %60)
br label %62
62: ; preds = %59, %52
br label %63
63: ; preds = %62
%64 = load i32, i32* %10
%65 = add nsw i32 %64, 1
store i32 %65, i32* %10
br label %45
66: ; preds = %45
%67 = getelementptr inbounds [20 x i8], [20 x i8]* %12, i64 0, i64 0
%68 = call i32 @SHA1_Final(i8* %67, i32* %11)
%69 = load i32, i32* %6
%70 = call i32 @close(i32 %69)
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %10
br label %72
72: ; preds = %84, %66
%73 = load i32, i32* %10
%74 = load i32, i32* @SHA_DIGEST_LENGTH
%75 = icmp slt i32 %73, %74
br i1 %75, label %76, label %87
76: ; preds = %72
%77 = load i32, i32* %10
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds [20 x i8], [20 x i8]* %12, i64 0, i64 %78
%80 = load i8, i8* %79
store i8 %80, i8* %16
%81 = load i8, i8* %16
%82 = zext i8 %81 to i32
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %82)
br label %84
84: ; preds = %76
%85 = load i32, i32* %10
%86 = add nsw i32 %85, 1
store i32 %86, i32* %10
br label %72
87: ; preds = %72
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%89 = load i8*, i8** %8
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i8* %89)
%91 = load i32, i32* %3
ret i32 %91
}

LLVM IR (-O2):
@.str.1 = [23 x i8] c"[SUM] opening file %s\0A\00"
@.str.2 = [18 x i8] c"[SUM] size is %d\0A\00"
@O_RDWR = global i32 0
@.str.3 = [24 x i8] c"[ERROR] read %d bytes!\0A\00"
@.str.4 = [7 x i8] c"[SUM] \00"
@SHA_DIGEST_LENGTH = global i32 0
@.str.5 = [6 x i8] c"%2.2x\00"
@.str.7 = [24 x i8] c"munmap is unmapping %x\0A\00"
@str = [29 x i8] c"USAGE: sum <filename> <size>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca [20 x i8]
%5 = alloca i64
%6 = bitcast i32* %3 to i8*
%7 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%8 = bitcast i64* %5 to i8*
%9 = icmp eq i32 %0, 3
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = tail call i32 @atoi(i8* %18)
%20 = shl nsw i32 %19, 20
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = load i8*, i8** %14
%23 = load i32, i32* @O_RDWR
%24 = tail call i32 @open(i8* %22, i32 %23)
%25 = tail call i32 (...) @time()
%26 = tail call i32 @srand(i32 %25)
%27 = call i32 @memset(i8* %7, i32 0, i32 20)
%28 = call i32 @SHA1_Init(i32* %3)
%29 = icmp eq i32 %19, 0
br i1 %29, label %44, label %30
30: ; preds = %13
%31 = sext i32 %20 to i64
%32 = lshr exact i64 %31, 3
%33 = call i64 @llvm.umax.i64(i64 %32, i64 1)
br label %34
34: ; preds = %30, %41
%35 = phi i64 [ 0, %30 ], [ %42, %41 ]
%36 = call i32 @read(i32 %24, i64* %5, i32 8)
%37 = call i32 @SHA1_Update(i32* %3, i64* %5, i32 8)
%38 = icmp ult i32 %36, 8
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %36)
br label %41
41: ; preds = %39, %34
%42 = add nuw nsw i64 %35, 1
%43 = icmp eq i64 %42, %33
br i1 %43, label %44, label %34
44: ; preds = %41, %13
%45 = call i32 @SHA1_Final(i8* %7, i32* %3)
%46 = call i32 @close(i32 %24)
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%48 = load i32, i32* @SHA_DIGEST_LENGTH
%49 = icmp sgt i32 %48, 0
br i1 %49, label %50, label %60
50: ; preds = %44, %50
%51 = phi i64 [ %56, %50 ], [ 0, %44 ]
%52 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 %51
%53 = load i8, i8* %52
%54 = zext i8 %53 to i32
%55 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %54)
%56 = add nuw nsw i64 %51, 1
%57 = load i32, i32* @SHA_DIGEST_LENGTH
%58 = sext i32 %57 to i64
%59 = icmp slt i64 %56, %58
br i1 %59, label %50, label %60
60: ; preds = %50, %44
%61 = call i32 @putchar(i32 10)
%62 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i8* undef)
ret i32 0
}