#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 */
struct test {
  int dummy;
};
typedef int /*<<< orphan*/ SHA_CTX;

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ SHA1_Final(unsigned char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA1_Init(int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA1_Update(int /*<<< orphan*/ *, void *, long);
int SHA_DIGEST_LENGTH;
int atol(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getpagesize();
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
void *mmap(int /*<<< orphan*/ *, long, int, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ munmap(void *, long);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
char *strdup(char *);
int main(int argc, char **argv) {
  long size;
  char *file;
  void *map_region;
  long *long_array;
  int i, fd;
  SHA_CTX context;
  unsigned char md[20];
  struct test *myptr;
  if (argc != 3) {
    fprintf(stderr, "USAGE: sum <file> <size in MB>\n");
    exit(-1);
  }
  size = atol(argv[2]) * 1024 * 1024;
  file = strdup(argv[1]);
  printf("[SUM] reading %d bytes from %s\n", size, file);
  if ((fd = open(file, O_RDWR)) < 0) {
    fprintf(stderr, "ERROR: cannot open file\n");
    exit(-1);
  }
  if ((map_region = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
                         1 * getpagesize())) < 0) {
    fprintf(stderr, "ERROR: cannot mmap file\n");
  } else {
    printf("[SUM] mapped to %p\n", map_region);
  }
  memset(md, 0, 20);
  SHA1_Init(&context);
  SHA1_Update(&context, map_region, size);
  SHA1_Final(md, &context);
  printf("[SUM] ");
  for (i = 0; i < SHA_DIGEST_LENGTH; ++i) {
    unsigned char c = md[i];
    printf("%2.2x", c);
  }
  printf("\n");
  munmap(map_region, size);
  close(fd);
  printf("[SUM] Exiting...\n");
}


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)
cmpl   $0x3,-0x8(%rbp)
je     121f <main+0x3f>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10b0 <atol@plt>
shl    $0xa,%eax
shl    $0xa,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10f0 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d6e(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
jge    129c <main+0xbc>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
lea    0xdb3(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    0x2d4d(%rip),%rax
mov    (%rax),%eax
mov    0x2d04(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x68(%rbp)
mov    0x2d28(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x60(%rbp)
mov    $0x0,%al
call   10d0 <getpagesize@plt>
mov    -0x70(%rbp),%rsi
mov    -0x68(%rbp),%edx
mov    -0x64(%rbp),%ecx
mov    -0x60(%rbp),%r8d
mov    %eax,%r9d
shl    $0x0,%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jae    1318 <main+0x138>
mov    0x2cbb(%rip),%rax
mov    (%rax),%edi
lea    0xd4b(%rip),%rsi
call   1070 <fprintf@plt>
jmp    132a <main+0x14a>
mov    -0x28(%rbp),%rsi
lea    0xd4f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1050 <memset@plt>
lea    -0x3c(%rbp),%rdi
call   10a0 <SHA1_Init@plt>
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    -0x3c(%rbp),%rdi
call   1080 <SHA1_Update@plt>
lea    -0x50(%rbp),%rdi
lea    -0x3c(%rbp),%rsi
call   1100 <SHA1_Final@plt>
lea    0xd1e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    0x2c50(%rip),%rcx
cmp    (%rcx),%eax
jge    13b3 <main+0x1d3>
movslq -0x34(%rbp),%rax
mov    -0x50(%rbp,%rax,1),%al
mov    %al,-0x59(%rbp)
movzbl -0x59(%rbp),%esi
lea    0xcef(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1376 <main+0x196>
lea    0xce9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <munmap@plt>
mov    -0x38(%rbp),%edi
call   1060 <close@plt>
lea    0xcb6(%rip),%rdi
mov    $0x0,%al
call   1040 <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    $0x28,%rsp
cmp    $0x3,%edi
jne    1366 <main+0x166>
mov    %rsi,%rbx
mov    0x10(%rsi),%rdi
call   10d0 <atol@plt>
shl    $0x14,%eax
movslq %eax,%r15
mov    0x8(%rbx),%rdi
call   1110 <strdup@plt>
mov    %rax,%rbx
lea    0xde4(%rip),%rdi
mov    %r15,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10e0 <open@plt>
test   %eax,%eax
js     1378 <main+0x178>
mov    %eax,%r14d
mov    0x2d8c(%rip),%rax
mov    0x2d45(%rip),%rcx
mov    (%rcx),%ebx
or     (%rax),%ebx
mov    0x2d6a(%rip),%rax
mov    (%rax),%ebp
xor    %eax,%eax
call   10f0 <getpagesize@plt>
xor    %edi,%edi
mov    %r15,%rsi
mov    %ebx,%edx
mov    %ebp,%ecx
mov    %r14d,%r8d
mov    %eax,%r9d
call   1050 <mmap@plt>
mov    %rax,%r13
lea    0xdb4(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x10(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1070 <memset@plt>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   10c0 <SHA1_Init@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r15,%rdx
call   10a0 <SHA1_Update@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1120 <SHA1_Final@plt>
lea    0xd7d(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2cd2(%rip),%r12
cmpl   $0x0,(%r12)
jle    132c <main+0x12c>
lea    0xd68(%rip),%rbx
xor    %ebp,%ebp
xchg   %ax,%ax
movzbl 0x10(%rsp,%rbp,1),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%rbp
movslq (%r12),%rax
cmp    %rax,%rbp
jl     1310 <main+0x110>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r13,%rdi
mov    %r15,%rsi
call   10b0 <munmap@plt>
mov    %r14d,%edi
call   1080 <close@plt>
lea    0xd2a(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c53(%rip),%rax
mov    (%rax),%edi
lea    0xc8a(%rip),%rsi
jmp    1388 <main+0x188>
mov    0x2c41(%rip),%rax
mov    (%rax),%edi
lea    0xcb8(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0xffffffff,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
%struct.test = type { i32 }
@stderr = global i32 0
@.str = [32 x i8] c"USAGE: sum <file> <size in MB>\0A\00"
@.str.1 = [32 x i8] c"[SUM] reading %d bytes from %s\0A\00"
@O_RDWR = global i32 0
@.str.2 = [25 x i8] c"ERROR: cannot open file\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [25 x i8] c"ERROR: cannot mmap file\0A\00"
@.str.4 = [20 x i8] c"[SUM] mapped to %p\0A\00"
@.str.5 = [7 x i8] c"[SUM] \00"
@SHA_DIGEST_LENGTH = global i32 0
@.str.6 = [6 x i8] c"%2.2x\00"
@.str.7 = [2 x i8] c"\0A\00"
@.str.8 = [18 x i8] c"[SUM] Exiting...\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [20 x i8]
%14 = alloca %struct.test*
%15 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 3
br i1 %17, label %18, label %22
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([32 x i8], [32 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 2
%25 = load i8*, i8** %24
%26 = call i32 @atol(i8* %25)
%27 = mul nsw i32 %26, 1024
%28 = mul nsw i32 %27, 1024
%29 = sext i32 %28 to i64
store i64 %29, i64* %6
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i8* @strdup(i8* %32)
store i8* %33, i8** %7
%34 = load i64, i64* %6
%35 = load i8*, i8** %7
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i64 %34, i8* %35)
%37 = load i8*, i8** %7
%38 = load i32, i32* @O_RDWR
%39 = call i32 @open(i8* %37, i32 %38)
store i32 %39, i32* %11
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %22
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%44 = call i32 @exit(i32 -1)
unreachable
45: ; preds = %22
%46 = load i64, i64* %6
%47 = load i32, i32* @PROT_READ
%48 = load i32, i32* @PROT_WRITE
%49 = or i32 %47, %48
%50 = load i32, i32* @MAP_SHARED
%51 = load i32, i32* %11
%52 = call i32 (...) @getpagesize()
%53 = mul nsw i32 1, %52
%54 = call i8* @mmap(i32* null, i64 %46, i32 %49, i32 %50, i32 %51, i32 %53)
store i8* %54, i8** %8
%55 = icmp ult i8* %54, null
br i1 %55, label %56, label %59
56: ; preds = %45
%57 = load i32, i32* @stderr
%58 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %62
59: ; preds = %45
%60 = load i8*, i8** %8
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %60)
br label %62
62: ; preds = %59, %56
%63 = getelementptr inbounds [20 x i8], [20 x i8]* %13, i64 0, i64 0
%64 = call i32 @memset(i8* %63, i32 0, i32 20)
%65 = call i32 @SHA1_Init(i32* %12)
%66 = load i8*, i8** %8
%67 = load i64, i64* %6
%68 = call i32 @SHA1_Update(i32* %12, i8* %66, i64 %67)
%69 = getelementptr inbounds [20 x i8], [20 x i8]* %13, i64 0, i64 0
%70 = call i32 @SHA1_Final(i8* %69, i32* %12)
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %10
br label %72
72: ; preds = %84, %62
%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]* %13, i64 0, i64 %78
%80 = load i8, i8* %79
store i8 %80, i8* %15
%81 = load i8, i8* %15
%82 = zext i8 %81 to i32
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, 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.7, i64 0, i64 0))
%89 = load i8*, i8** %8
%90 = load i64, i64* %6
%91 = call i32 @munmap(i8* %89, i64 %90)
%92 = load i32, i32* %11
%93 = call i32 @close(i32 %92)
%94 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
%95 = load i32, i32* %3
ret i32 %95
}

LLVM IR (-O2):
%struct.test = type { i32 }
@stderr = global i32 0
@.str = [32 x i8] c"USAGE: sum <file> <size in MB>\0A\00"
@.str.1 = [32 x i8] c"[SUM] reading %d bytes from %s\0A\00"
@O_RDWR = global i32 0
@.str.2 = [25 x i8] c"ERROR: cannot open file\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.4 = [20 x i8] c"[SUM] mapped to %p\0A\00"
@.str.5 = [7 x i8] c"[SUM] \00"
@SHA_DIGEST_LENGTH = global i32 0
@.str.6 = [6 x i8] c"%2.2x\00"
@str = [17 x i8] c"[SUM] Exiting...\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca [20 x i8]
%5 = bitcast i32* %3 to i8*
%6 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%7 = icmp eq i32 %0, 3
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = tail call i32 @atol(i8* %14)
%16 = shl nsw i32 %15, 20
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = tail call i8* @strdup(i8* %19)
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i64 %17, i8* %20)
%22 = load i32, i32* @O_RDWR
%23 = tail call i32 @open(i8* %20, i32 %22)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %12
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%28 = tail call i32 @exit(i32 -1)
unreachable
29: ; preds = %12
%30 = load i32, i32* @PROT_READ
%31 = load i32, i32* @PROT_WRITE
%32 = or i32 %31, %30
%33 = load i32, i32* @MAP_SHARED
%34 = tail call i32 (...) @getpagesize()
%35 = tail call i8* @mmap(i32* null, i64 %17, i32 %32, i32 %33, i32 %23, i32 %34)
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %35)
%37 = call i32 @memset(i8* %6, i32 0, i32 20)
%38 = call i32 @SHA1_Init(i32* %3)
%39 = call i32 @SHA1_Update(i32* %3, i8* %35, i64 %17)
%40 = call i32 @SHA1_Final(i8* %6, i32* %3)
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%42 = load i32, i32* @SHA_DIGEST_LENGTH
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %54
44: ; preds = %29, %44
%45 = phi i64 [ %50, %44 ], [ 0, %29 ]
%46 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 %45
%47 = load i8, i8* %46
%48 = zext i8 %47 to i32
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 %48)
%50 = add nuw nsw i64 %45, 1
%51 = load i32, i32* @SHA_DIGEST_LENGTH
%52 = sext i32 %51 to i64
%53 = icmp slt i64 %50, %52
br i1 %53, label %44, label %54
54: ; preds = %44, %29
%55 = call i32 @putchar(i32 10)
%56 = call i32 @munmap(i8* %35, i64 %17)
%57 = call i32 @close(i32 %23)
%58 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
ret i32 0
}