#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 uint64_t;
struct stat {
  scalar_t__ st_size;
};

/* Variables and functions */
char const *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_WRITE;
int /*<<< orphan*/ fstat(int, struct stat *);
int /*<<< orphan*/ handle_error(char *);
char *mmap(int /*<<< orphan*/ *, scalar_t__, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  const char *memblock;
  int fd;
  struct stat sb;
  fd = open(argv[1], O_RDONLY);
  fstat(fd, &sb);
  printf("Size: %llu\n", (uint64_t)sb.st_size);
  memblock = mmap(NULL, sb.st_size, PROT_WRITE, MAP_PRIVATE, fd, 0);
  if (memblock == MAP_FAILED)
    handle_error("mmap");
  for (uint64_t i = 0; i < 10; i++) {
    printf("[%llu]=%X\n", i, memblock[i]);
  }
  printf("\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1060 <fstat@plt>
mov    -0x28(%rbp),%rax
mov    %eax,%esi
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rsi
mov    0x2e2e(%rip),%rax
mov    (%rax),%edx
mov    0x2e1d(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2df4(%rip),%rcx
cmp    (%rcx),%rax
jne    11e9 <main+0x99>
lea    0xe28(%rip),%rdi
call   1070 <handle_error@plt>
movl   $0x0,-0x2c(%rbp)
cmpl   $0xa,-0x2c(%rbp)
jge    1225 <main+0xd5>
mov    -0x2c(%rbp),%esi
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsbl (%rax,%rcx,1),%edx
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11f0 <main+0xa0>
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x8(%rsi),%rdi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1070 <fstat@plt>
mov    0x8(%rsp),%rbx
lea    0xe6c(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%edx
mov    0x2e23(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2dff(%rip),%rax
cmp    (%rax),%rbx
jne    11da <main+0x7a>
lea    0xe37(%rip),%rdi
call   1080 <handle_error@plt>
movsbl (%rbx),%edx
lea    0xe2d(%rip),%r14
mov    %r14,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x1(%rbx),%edx
mov    %r14,%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x2(%rbx),%edx
mov    %r14,%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x3(%rbx),%edx
mov    %r14,%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x4(%rbx),%edx
mov    %r14,%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x5(%rbx),%edx
mov    %r14,%rdi
mov    $0x5,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x6(%rbx),%edx
mov    %r14,%rdi
mov    $0x6,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x7(%rbx),%edx
mov    %r14,%rdi
mov    $0x7,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x8(%rbx),%edx
mov    %r14,%rdi
mov    $0x8,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x9(%rbx),%edx
mov    %r14,%rdi
mov    $0x9,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [12 x i8] c"Size: %llu\0A\00"
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
@.str.2 = [11 x i8] c"[%llu]=%X\0A\00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = call i32 @fstat(i32 %15, %struct.stat* %8)
%17 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%18 = load i64, i64* %17
%19 = trunc i64 %18 to i32
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%22 = load i64, i64* %21
%23 = load i32, i32* @PROT_WRITE
%24 = load i32, i32* @MAP_PRIVATE
%25 = load i32, i32* %7
%26 = call i8* @mmap(i32* null, i64 %22, i32 %23, i32 %24, i32 %25, i32 0)
store i8* %26, i8** %6
%27 = load i8*, i8** %6
%28 = load i8*, i8** @MAP_FAILED
%29 = icmp eq i8* %27, %28
br i1 %29, label %30, label %32
30: ; preds = %2
%31 = call i32 @handle_error(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %30, %2
store i32 0, i32* %9
br label %33
33: ; preds = %45, %32
%34 = load i32, i32* %9
%35 = icmp slt i32 %34, 10
br i1 %35, label %36, label %48
36: ; preds = %33
%37 = load i32, i32* %9
%38 = load i8*, i8** %6
%39 = load i32, i32* %9
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %37, i32 %43)
br label %45
45: ; preds = %36
%46 = load i32, i32* %9
%47 = add nsw i32 %46, 1
store i32 %47, i32* %9
br label %33
48: ; preds = %33
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [12 x i8] c"Size: %llu\0A\00"
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
@.str.2 = [11 x i8] c"[%llu]=%X\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* %6, i32 %7)
%9 = call i32 @fstat(i32 %8, %struct.stat* %3)
%10 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%11 = load i64, i64* %10
%12 = trunc i64 %11 to i32
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = load i32, i32* @PROT_WRITE
%15 = load i32, i32* @MAP_PRIVATE
%16 = tail call i8* @mmap(i32* null, i64 %11, i32 %14, i32 %15, i32 %8, i32 0)
%17 = load i8*, i8** @MAP_FAILED
%18 = icmp eq i8* %16, %17
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = tail call i32 @handle_error(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %2
%22 = load i8, i8* %16
%23 = sext i8 %22 to i32
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 0, i32 %23)
%25 = getelementptr inbounds i8, i8* %16, i64 1
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 1, i32 %27)
%29 = getelementptr inbounds i8, i8* %16, i64 2
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 2, i32 %31)
%33 = getelementptr inbounds i8, i8* %16, i64 3
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 3, i32 %35)
%37 = getelementptr inbounds i8, i8* %16, i64 4
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 4, i32 %39)
%41 = getelementptr inbounds i8, i8* %16, i64 5
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 5, i32 %43)
%45 = getelementptr inbounds i8, i8* %16, i64 6
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 6, i32 %47)
%49 = getelementptr inbounds i8, i8* %16, i64 7
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 7, i32 %51)
%53 = getelementptr inbounds i8, i8* %16, i64 8
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 8, i32 %55)
%57 = getelementptr inbounds i8, i8* %16, i64 9
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 9, i32 %59)
%61 = tail call i32 @putchar(i32 10)
ret i32 0
}