#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 /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
void *mmap(int /*<<< orphan*/ *, long, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
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, 0)) < 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    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     120f <main+0x3f>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10d0 <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   10e0 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
jge    128c <main+0xbc>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
lea    0xdc3(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    -0x18(%rbp),%rsi
mov    0x2d61(%rip),%rax
mov    (%rax),%edx
mov    0x2d18(%rip),%rax
or     (%rax),%edx
mov    0x2d3f(%rip),%rax
mov    (%rax),%ecx
mov    -0x38(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jae    12e3 <main+0x113>
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
lea    0xd80(%rip),%rsi
call   1070 <fprintf@plt>
jmp    12f5 <main+0x125>
mov    -0x28(%rbp),%rsi
lea    0xd84(%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   10f0 <SHA1_Final@plt>
lea    0xd53(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    0x2c85(%rip),%rcx
cmp    (%rcx),%eax
jge    137e <main+0x1ae>
movslq -0x34(%rbp),%rax
mov    -0x50(%rbp,%rax,1),%al
mov    %al,-0x59(%rbp)
movzbl -0x59(%rbp),%esi
lea    0xd24(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1341 <main+0x171>
lea    0xd1e(%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    0xceb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x60,%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    1356 <main+0x166>
mov    %rsi,%rbx
mov    0x10(%rsi),%rdi
call   10d0 <atol@plt>
shl    $0x14,%eax
movslq %eax,%r15
mov    0x8(%rbx),%rdi
call   1100 <strdup@plt>
mov    %rax,%rbx
lea    0xdf4(%rip),%rdi
mov    %r15,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10e0 <open@plt>
test   %eax,%eax
js     1368 <main+0x178>
mov    %eax,%r14d
mov    0x2d9c(%rip),%rax
mov    0x2d55(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d7a(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15,%rsi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%r13
lea    0xdcf(%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   1110 <SHA1_Final@plt>
lea    0xd98(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2ced(%rip),%r12
cmpl   $0x0,(%r12)
jle    131c <main+0x12c>
lea    0xd83(%rip),%rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
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     1300 <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    0xd3a(%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    0x2c63(%rip),%rax
mov    (%rax),%edi
lea    0xc9a(%rip),%rsi
jmp    1378 <main+0x188>
mov    0x2c51(%rip),%rax
mov    (%rax),%edi
lea    0xcc8(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0xffffffff,%edi
call   10f0 <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 i8* @mmap(i32* null, i64 %46, i32 %49, i32 %50, i32 %51, i32 0)
store i8* %52, i8** %8
%53 = icmp ult i8* %52, null
br i1 %53, label %54, label %57
54: ; preds = %45
%55 = load i32, i32* @stderr
%56 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %60
57: ; preds = %45
%58 = load i8*, i8** %8
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %58)
br label %60
60: ; preds = %57, %54
%61 = getelementptr inbounds [20 x i8], [20 x i8]* %13, i64 0, i64 0
%62 = call i32 @memset(i8* %61, i32 0, i32 20)
%63 = call i32 @SHA1_Init(i32* %12)
%64 = load i8*, i8** %8
%65 = load i64, i64* %6
%66 = call i32 @SHA1_Update(i32* %12, i8* %64, i64 %65)
%67 = getelementptr inbounds [20 x i8], [20 x i8]* %13, i64 0, i64 0
%68 = call i32 @SHA1_Final(i8* %67, i32* %12)
%69 = 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 %70
70: ; preds = %82, %60
%71 = load i32, i32* %10
%72 = load i32, i32* @SHA_DIGEST_LENGTH
%73 = icmp slt i32 %71, %72
br i1 %73, label %74, label %85
74: ; preds = %70
%75 = load i32, i32* %10
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds [20 x i8], [20 x i8]* %13, i64 0, i64 %76
%78 = load i8, i8* %77
store i8 %78, i8* %15
%79 = load i8, i8* %15
%80 = zext i8 %79 to i32
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 %80)
br label %82
82: ; preds = %74
%83 = load i32, i32* %10
%84 = add nsw i32 %83, 1
store i32 %84, i32* %10
br label %70
85: ; preds = %70
%86 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%87 = load i8*, i8** %8
%88 = load i64, i64* %6
%89 = call i32 @munmap(i8* %87, i64 %88)
%90 = load i32, i32* %11
%91 = call i32 @close(i32 %90)
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
%93 = load i32, i32* %3
ret i32 %93
}

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 i8* @mmap(i32* null, i64 %17, i32 %32, i32 %33, i32 %23, i32 0)
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %34)
%36 = call i32 @memset(i8* %6, i32 0, i32 20)
%37 = call i32 @SHA1_Init(i32* %3)
%38 = call i32 @SHA1_Update(i32* %3, i8* %34, i64 %17)
%39 = call i32 @SHA1_Final(i8* %6, i32* %3)
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%41 = load i32, i32* @SHA_DIGEST_LENGTH
%42 = icmp sgt i32 %41, 0
br i1 %42, label %43, label %53
43: ; preds = %29, %43
%44 = phi i64 [ %49, %43 ], [ 0, %29 ]
%45 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 %44
%46 = load i8, i8* %45
%47 = zext i8 %46 to i32
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 %47)
%49 = add nuw nsw i64 %44, 1
%50 = load i32, i32* @SHA_DIGEST_LENGTH
%51 = sext i32 %50 to i64
%52 = icmp slt i64 %49, %51
br i1 %52, label %43, label %53
53: ; preds = %43, %29
%54 = call i32 @putchar(i32 10)
%55 = call i32 @munmap(i8* %34, i64 %17)
%56 = call i32 @close(i32 %23)
%57 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
ret i32 0
}