#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 void *caddr_t;
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(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA1_Init(int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA1_Update(int /*<<< orphan*/ *, void *, int);
int SHA_DIGEST_LENGTH;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
void *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
long rand();
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];
  if (argc != 3) {
    printf("USAGE: dump <filename> <size>\n");
    exit(-1);
  }
  printf("[DUMP] opening file %s\n", argv[1]);
  length = atoi(argv[2]) * 1024 * 1024;
  printf("[DUMP] size is %d\n", length);
  fd = open(argv[1], O_RDWR);
  if ((memptr = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
                     0)) == (caddr_t)-1) {
    printf("mmap failed (0x%x)\n", memptr);
    close(fd);
    exit(-1);
  }
  srand(time());
  long_array = (long *)memptr;
  for (i = 0; i < length / sizeof(long); i++) {
    long_array[i] = rand();
  }
  memset(md, 0, 20);
  SHA1_Init(&context);
  SHA1_Update(&context, memptr, length);
  SHA1_Final(md, &context);
  printf("[DUMP] ");
  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);
  munmap(memptr, length);
  close(fd);
}


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)
movl   $0x1000,-0x18(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     121f <main+0x3f>
lea    0xdf2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10d0 <atoi@plt>
shl    $0xa,%eax
shl    $0xa,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%esi
mov    0x2d79(%rip),%rax
mov    (%rax),%edx
mov    0x2d38(%rip),%rax
or     (%rax),%edx
mov    0x2d57(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    12db <main+0xfb>
mov    -0x20(%rbp),%rsi
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
mov    $0x0,%al
call   1080 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movslq -0x2c(%rbp),%rax
movslq -0x18(%rbp),%rcx
shr    $0x3,%rcx
cmp    %rcx,%rax
jae    1331 <main+0x151>
mov    $0x0,%al
call   1100 <rand@plt>
mov    %rax,%rdx
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    12f8 <main+0x118>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1050 <memset@plt>
lea    -0x30(%rbp),%rdi
call   10b0 <SHA1_Init@plt>
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
lea    -0x30(%rbp),%rdi
call   1090 <SHA1_Update@plt>
lea    -0x50(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10f0 <SHA1_Final@plt>
lea    0xcf0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2c4a(%rip),%rcx
cmp    (%rcx),%eax
jge    13b9 <main+0x1d9>
movslq -0x2c(%rbp),%rax
mov    -0x50(%rbp,%rax,1),%al
mov    %al,-0x51(%rbp)
movzbl -0x51(%rbp),%esi
lea    0xcc2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    137c <main+0x19c>
lea    0xc9c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rsi
lea    0xc9a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   10a0 <munmap@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@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    13aa <main+0x1aa>
mov    %rsi,%rbp
mov    0x8(%rsi),%rsi
lea    0xddb(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x10(%rbp),%rdi
call   10f0 <atoi@plt>
mov    %eax,%r15d
mov    %eax,%r12d
shl    $0x14,%r12d
lea    0xdd2(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x8(%rbp),%rdi
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
call   10e0 <open@plt>
mov    %eax,%r14d
mov    0x2d8c(%rip),%rax
mov    0x2d4d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d6a(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r12d,%esi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     13c0 <main+0x1c0>
mov    %rax,%r13
xor    %eax,%eax
call   10a0 <time@plt>
mov    %eax,%edi
call   1090 <srand@plt>
test   %r15d,%r15d
je     12e5 <main+0xe5>
movslq %r12d,%rax
shr    $0x3,%rax
cmp    $0x2,%rax
mov    $0x1,%ebx
cmovae %rax,%rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1120 <rand@plt>
mov    %rax,0x0(%r13,%rbp,8)
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    12d0 <main+0xd0>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1070 <memset@plt>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   10d0 <SHA1_Init@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r12d,%edx
call   10b0 <SHA1_Update@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1110 <SHA1_Final@plt>
lea    0xd19(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2c9c(%rip),%r15
cmpl   $0x0,(%r15)
jle    136b <main+0x16b>
lea    0xd06(%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 (%r15),%rax
cmp    %rax,%rbp
jl     1350 <main+0x150>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xcd1(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r13,%rdi
mov    %r12d,%esi
call   10c0 <munmap@plt>
mov    %r14d,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcb4(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   1100 <exit@plt>
lea    0xc64(%rip),%rdi
mov    $0xffffffffffffffff,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14d,%edi
call   1080 <close@plt>
mov    $0xffffffff,%edi
call   1100 <exit@plt>

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

LLVM IR (-O2):
@.str.1 = [24 x i8] c"[DUMP] opening file %s\0A\00"
@.str.2 = [19 x i8] c"[DUMP] size is %d\0A\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [20 x i8] c"mmap failed (0x%x)\0A\00"
@.str.4 = [8 x i8] c"[DUMP] \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 = [30 x i8] c"USAGE: dump <filename> <size>\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 %11, label %8
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = getelementptr inbounds i8*, i8** %1, i64 2
%16 = load i8*, i8** %15
%17 = tail call i32 @atoi(i8* %16)
%18 = shl nsw i32 %17, 20
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = load i8*, i8** %12
%21 = load i32, i32* @O_RDWR
%22 = tail call i32 @open(i8* %20, i32 %21)
%23 = load i32, i32* @PROT_READ
%24 = load i32, i32* @PROT_WRITE
%25 = or i32 %24, %23
%26 = load i32, i32* @MAP_SHARED
%27 = tail call i8* @mmap(i32* null, i32 %18, i32 %25, i32 %26, i32 %22, i32 0)
%28 = icmp eq i8* %27, inttoptr (i64 -1 to i8*)
br i1 %28, label %29, label %33
29: ; preds = %11
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* inttoptr (i64 -1 to i8*))
%31 = tail call i32 @close(i32 %22)
%32 = tail call i32 @exit(i32 -1)
unreachable
33: ; preds = %11
%34 = tail call i32 (...) @time()
%35 = tail call i32 @srand(i32 %34)
%36 = bitcast i8* %27 to i64*
%37 = icmp eq i32 %17, 0
br i1 %37, label %48, label %38
38: ; preds = %33
%39 = sext i32 %18 to i64
%40 = lshr exact i64 %39, 3
%41 = call i64 @llvm.umax.i64(i64 %40, i64 1)
br label %42
42: ; preds = %38, %42
%43 = phi i64 [ 0, %38 ], [ %46, %42 ]
%44 = tail call i64 (...) @rand()
%45 = getelementptr inbounds i64, i64* %36, i64 %43
store i64 %44, i64* %45
%46 = add nuw nsw i64 %43, 1
%47 = icmp eq i64 %46, %41
br i1 %47, label %48, label %42
48: ; preds = %42, %33
%49 = call i32 @memset(i8* %6, i32 0, i32 20)
%50 = call i32 @SHA1_Init(i32* %3)
%51 = call i32 @SHA1_Update(i32* %3, i8* %27, i32 %18)
%52 = call i32 @SHA1_Final(i8* %6, i32* %3)
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%54 = load i32, i32* @SHA_DIGEST_LENGTH
%55 = icmp sgt i32 %54, 0
br i1 %55, label %56, label %66
56: ; preds = %48, %56
%57 = phi i64 [ %62, %56 ], [ 0, %48 ]
%58 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 %57
%59 = load i8, i8* %58
%60 = zext i8 %59 to i32
%61 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %60)
%62 = add nuw nsw i64 %57, 1
%63 = load i32, i32* @SHA_DIGEST_LENGTH
%64 = sext i32 %63 to i64
%65 = icmp slt i64 %62, %64
br i1 %65, label %56, label %66
66: ; preds = %56, %48
%67 = call i32 @putchar(i32 10)
%68 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i8* %27)
%69 = call i32 @munmap(i8* %27, i32 %18)
%70 = call i32 @close(i32 %22)
ret i32 0
}