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

/* Variables and functions */
unsigned char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int);
scalar_t__ mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
                int /*<<< orphan*/);
int /*<<< orphan*/ munmap(unsigned char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  int fd;
  int i;
  unsigned char *p_map;
  fd = open("/dev/mymap", O_RDWR);
  if (fd < 0) {
    printf("open fail\n");
    exit(1);
  }
  p_map =
      (unsigned char *)mmap(0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (p_map == MAP_FAILED) {
    printf("mmap fail\n");
    goto here;
  }
  for (i = 0; i < 10; i++)
    printf("%d\n", p_map[i]);
here:
  munmap(p_map, 4096);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
lea    0xe8a(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11a0 <main+0x50>
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%edx
mov    0x2e18(%rip),%rax
or     (%rax),%edx
mov    0x2e2f(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    $0x1000,%esi
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2de2(%rip),%rcx
cmp    (%rcx),%rax
jne    11fa <main+0xaa>
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1238 <main+0xe8>
movl   $0x0,-0x18(%rbp)
cmpl   $0xa,-0x18(%rbp)
jge    1233 <main+0xe3>
mov    -0x20(%rbp),%rax
movslq -0x18(%rbp),%rcx
movzbl (%rax,%rcx,1),%esi
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1201 <main+0xb1>
jmp    1238 <main+0xe8>
mov    -0x20(%rbp),%rdi
mov    $0x1000,%esi
call   1050 <munmap@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
lea    0xe8c(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1278 <main+0x118>
mov    0x2e70(%rip),%rcx
mov    0x2e39(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e4e(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    %eax,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2e08(%rip),%rax
cmp    %rbx,(%rax)
je     1255 <main+0xf5>
movzbl (%rbx),%esi
lea    0xe40(%rip),%r14
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl 0x1(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl 0x2(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl 0x3(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl 0x4(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl 0x5(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl 0x6(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl 0x7(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl 0x8(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl 0x9(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1261 <main+0x101>
lea    0xdb3(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1060 <munmap@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd9a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/mymap\00"
@O_RDWR = global i32 0
@.str.1 = [11 x i8] c"open fail\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [11 x i8] c"mmap fail\0A\00"
@.str.3 = [4 x i8] c"%d\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*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i32, i32* @PROT_READ
%18 = load i32, i32* @PROT_WRITE
%19 = or i32 %17, %18
%20 = load i32, i32* @MAP_SHARED
%21 = load i32, i32* %6
%22 = call i64 @mmap(i32 0, i32 4096, i32 %19, i32 %20, i32 %21, i32 0)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %8
%24 = load i8*, i8** %8
%25 = load i8*, i8** @MAP_FAILED
%26 = icmp eq i8* %24, %25
br i1 %26, label %27, label %29
27: ; preds = %16
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %45
29: ; preds = %16
store i32 0, i32* %7
br label %30
30: ; preds = %41, %29
%31 = load i32, i32* %7
%32 = icmp slt i32 %31, 10
br i1 %32, label %33, label %44
33: ; preds = %30
%34 = load i8*, i8** %8
%35 = load i32, i32* %7
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = load i8, i8* %37
%39 = zext i8 %38 to i32
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %33
%42 = load i32, i32* %7
%43 = add nsw i32 %42, 1
store i32 %43, i32* %7
br label %30
44: ; preds = %30
br label %45
45: ; preds = %44, %27
%46 = load i8*, i8** %8
%47 = call i32 @munmap(i8* %46, i32 4096)
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/mymap\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [4 x i8] c"%d\0A\00"
@str = [10 x i8] c"mmap fail\00"
@str.4 = [10 x i8] c"open fail\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.4, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %11, %10
%13 = load i32, i32* @MAP_SHARED
%14 = tail call i64 @mmap(i32 0, i32 4096, i32 %12, i32 %13, i32 %4, i32 0)
%15 = inttoptr i64 %14 to i8*
%16 = load i8*, i8** @MAP_FAILED
%17 = icmp eq i8* %16, %15
br i1 %17, label %58, label %18
18: ; preds = %9
%19 = load i8, i8* %15
%20 = zext i8 %19 to i32
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = getelementptr inbounds i8, i8* %15, i64 1
%23 = load i8, i8* %22
%24 = zext i8 %23 to i32
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = getelementptr inbounds i8, i8* %15, i64 2
%27 = load i8, i8* %26
%28 = zext i8 %27 to i32
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%30 = getelementptr inbounds i8, i8* %15, i64 3
%31 = load i8, i8* %30
%32 = zext i8 %31 to i32
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %32)
%34 = getelementptr inbounds i8, i8* %15, i64 4
%35 = load i8, i8* %34
%36 = zext i8 %35 to i32
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%38 = getelementptr inbounds i8, i8* %15, i64 5
%39 = load i8, i8* %38
%40 = zext i8 %39 to i32
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %40)
%42 = getelementptr inbounds i8, i8* %15, i64 6
%43 = load i8, i8* %42
%44 = zext i8 %43 to i32
%45 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %44)
%46 = getelementptr inbounds i8, i8* %15, i64 7
%47 = load i8, i8* %46
%48 = zext i8 %47 to i32
%49 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %48)
%50 = getelementptr inbounds i8, i8* %15, i64 8
%51 = load i8, i8* %50
%52 = zext i8 %51 to i32
%53 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %52)
%54 = getelementptr inbounds i8, i8* %15, i64 9
%55 = load i8, i8* %54
%56 = zext i8 %55 to i32
%57 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %56)
br label %60
58: ; preds = %9
%59 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
br label %60
60: ; preds = %18, %58
%61 = tail call i32 @munmap(i8* %15, i32 4096)
ret i32 0
}