#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 /*<<< orphan*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
char *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ createFile(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/,
                int /*<<< orphan*/, int, int /*<<< orphan*/);
int munmap(char *, size_t);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
void readExample() {
  createFile("hello world");
  int fd = open("mmap.temp", O_RDONLY);
  size_t mmapLen = 15;
  off_t offset = 0;
  char *ptr = (char *)mmap(NULL, mmapLen, PROT_READ, MAP_PRIVATE, fd, offset);
  if (ptr == MAP_FAILED) {
    perror("mmap");
    exit(EXIT_FAILURE);
  }
  printf("Should say hello world: %s\n", ptr);
  for (unsigned int i = 0; i < mmapLen; i++)
    printf("ptr[%u] is: '%c' (or %d)\n", i, ptr[i], ptr[i]);
  if (munmap(ptr, mmapLen) == -1) {
    perror("munmap");
    exit(EXIT_FAILURE);
  }
  close(fd);
}


x86 assembly (clang, -O0):
<readExample>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe71(%rip),%rdi
call   1060 <createFile@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
lea    0xe68(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x4(%rbp)
movq   $0xf,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rsi
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
mov    0x2df9(%rip),%rax
mov    (%rax),%ecx
mov    -0x4(%rbp),%r8d
mov    -0x14(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2dcf(%rip),%rcx
cmp    (%rcx),%rax
jne    1214 <readExample+0x94>
lea    0xe15(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rsi
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x10(%rbp),%rax
jae    126f <readExample+0xef>
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
movsbl (%rax,%rcx,1),%edx
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
movsbl (%rax,%rcx,1),%ecx
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    122d <readExample+0xad>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <munmap@plt>
cmp    $0xffffffff,%eax
jne    129f <readExample+0x11f>
lea    0xdc5(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readExample>:
push   %rbp
push   %r14
push   %rbx
lea    0xe75(%rip),%rdi
call   1060 <createFile@plt>
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
lea    0xe6c(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%r14d
mov    0x2e49(%rip),%rax
mov    (%rax),%edx
mov    0x2e10(%rip),%rax
mov    (%rax),%ecx
mov    $0xf,%esi
xor    %edi,%edi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2dea(%rip),%rax
cmp    %rbx,(%rax)
je     134c <readExample+0x1cc>
lea    0xe35(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
movsbl (%rbx),%ecx
lea    0xe3d(%rip),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    %ecx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
movsbl 0x1(%rbx),%ecx
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %ecx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
movsbl 0x2(%rbx),%ecx
mov    %rbp,%rdi
mov    $0x2,%esi
mov    %ecx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
movsbl 0x3(%rbx),%ecx
mov    %rbp,%rdi
mov    $0x3,%esi
mov    %ecx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
movsbl 0x4(%rbx),%ecx
mov    %rbp,%rdi
mov    $0x4,%esi
mov    %ecx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
movsbl 0x5(%rbx),%ecx
mov    %rbp,%rdi
mov    $0x5,%esi
mov    %ecx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
movsbl 0x6(%rbx),%ecx
mov    %rbp,%rdi
mov    $0x6,%esi
mov    %ecx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
movsbl 0x7(%rbx),%ecx
mov    %rbp,%rdi
mov    $0x7,%esi
mov    %ecx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
movsbl 0x8(%rbx),%ecx
mov    %rbp,%rdi
mov    $0x8,%esi
mov    %ecx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
movsbl 0x9(%rbx),%ecx
mov    %rbp,%rdi
mov    $0x9,%esi
mov    %ecx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
movsbl 0xa(%rbx),%ecx
mov    %rbp,%rdi
mov    $0xa,%esi
mov    %ecx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
movsbl 0xb(%rbx),%ecx
mov    %rbp,%rdi
mov    $0xb,%esi
mov    %ecx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
movsbl 0xc(%rbx),%ecx
mov    %rbp,%rdi
mov    $0xc,%esi
mov    %ecx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
movsbl 0xd(%rbx),%ecx
mov    %rbp,%rdi
mov    $0xd,%esi
mov    %ecx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
movsbl 0xe(%rbx),%ecx
mov    %rbp,%rdi
mov    $0xe,%esi
mov    %ecx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xf,%esi
mov    %rbx,%rdi
call   1070 <munmap@plt>
cmp    $0xffffffff,%eax
je     1355 <readExample+0x1d5>
mov    %r14d,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <close@plt>
lea    0xcc3(%rip),%rdi
jmp    135c <readExample+0x1dc>
lea    0xcf5(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c68(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"hello world\00"
@.str.1 = [10 x i8] c"mmap.temp\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [5 x i8] c"mmap\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [28 x i8] c"Should say hello world: %s\0A\00"
@.str.4 = [26 x i8] c"ptr[%u] is: '%c' (or %d)\0A\00"
@.str.5 = [7 x i8] c"munmap\00"
define void @readExample() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = call i32 @createFile(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %7)
store i32 %8, i32* %1
store i64 15, i64* %2
store i32 0, i32* %3
%9 = load i64, i64* %2
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @MAP_PRIVATE
%12 = load i32, i32* %1
%13 = load i32, i32* %3
%14 = call i64 @mmap(i32* null, i64 %9, i32 %10, i32 %11, i32 %12, i32 %13)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %4
%16 = load i8*, i8** %4
%17 = load i8*, i8** @MAP_FAILED
%18 = icmp eq i8* %16, %17
br i1 %18, label %19, label %23
19: ; preds = %0
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %0
%24 = load i8*, i8** %4
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %24)
store i32 0, i32* %5
br label %26
26: ; preds = %46, %23
%27 = load i32, i32* %5
%28 = zext i32 %27 to i64
%29 = load i64, i64* %2
%30 = icmp ult i64 %28, %29
br i1 %30, label %31, label %49
31: ; preds = %26
%32 = load i32, i32* %5
%33 = load i8*, i8** %4
%34 = load i32, i32* %5
%35 = zext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = load i8*, i8** %4
%40 = load i32, i32* %5
%41 = zext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %32, i32 %38, i32 %44)
br label %46
46: ; preds = %31
%47 = load i32, i32* %5
%48 = add i32 %47, 1
store i32 %48, i32* %5
br label %26
49: ; preds = %26
%50 = load i8*, i8** %4
%51 = load i64, i64* %2
%52 = call i32 @munmap(i8* %50, i64 %51)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %58
54: ; preds = %49
%55 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%56 = load i32, i32* @EXIT_FAILURE
%57 = call i32 @exit(i32 %56)
unreachable
58: ; preds = %49
%59 = load i32, i32* %1
%60 = call i32 @close(i32 %59)
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"hello world\00"
@.str.1 = [10 x i8] c"mmap.temp\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [5 x i8] c"mmap\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [28 x i8] c"Should say hello world: %s\0A\00"
@.str.4 = [26 x i8] c"ptr[%u] is: '%c' (or %d)\0A\00"
@.str.5 = [7 x i8] c"munmap\00"
define void @readExample() {
%1 = tail call i32 @createFile(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %2)
%4 = load i32, i32* @PROT_READ
%5 = load i32, i32* @MAP_PRIVATE
%6 = tail call i64 @mmap(i32* null, i64 15, i32 %4, i32 %5, i32 %3, i32 0)
%7 = inttoptr i64 %6 to i8*
%8 = load i8*, i8** @MAP_FAILED
%9 = icmp eq i8* %8, %7
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %0
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %7)
%16 = load i8, i8* %7
%17 = sext i8 %16 to i32
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 0, i32 %17, i32 %17)
%19 = getelementptr inbounds i8, i8* %7, i64 1
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 1, i32 %21, i32 %21)
%23 = getelementptr inbounds i8, i8* %7, i64 2
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 2, i32 %25, i32 %25)
%27 = getelementptr inbounds i8, i8* %7, i64 3
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 3, i32 %29, i32 %29)
%31 = getelementptr inbounds i8, i8* %7, i64 4
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 4, i32 %33, i32 %33)
%35 = getelementptr inbounds i8, i8* %7, i64 5
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 5, i32 %37, i32 %37)
%39 = getelementptr inbounds i8, i8* %7, i64 6
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 6, i32 %41, i32 %41)
%43 = getelementptr inbounds i8, i8* %7, i64 7
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 7, i32 %45, i32 %45)
%47 = getelementptr inbounds i8, i8* %7, i64 8
%48 = load i8, i8* %47
%49 = sext i8 %48 to i32
%50 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 8, i32 %49, i32 %49)
%51 = getelementptr inbounds i8, i8* %7, i64 9
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 9, i32 %53, i32 %53)
%55 = getelementptr inbounds i8, i8* %7, i64 10
%56 = load i8, i8* %55
%57 = sext i8 %56 to i32
%58 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 10, i32 %57, i32 %57)
%59 = getelementptr inbounds i8, i8* %7, i64 11
%60 = load i8, i8* %59
%61 = sext i8 %60 to i32
%62 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 11, i32 %61, i32 %61)
%63 = getelementptr inbounds i8, i8* %7, i64 12
%64 = load i8, i8* %63
%65 = sext i8 %64 to i32
%66 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 12, i32 %65, i32 %65)
%67 = getelementptr inbounds i8, i8* %7, i64 13
%68 = load i8, i8* %67
%69 = sext i8 %68 to i32
%70 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 13, i32 %69, i32 %69)
%71 = getelementptr inbounds i8, i8* %7, i64 14
%72 = load i8, i8* %71
%73 = sext i8 %72 to i32
%74 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 14, i32 %73, i32 %73)
%75 = tail call i32 @munmap(i8* %7, i64 15)
%76 = icmp eq i32 %75, -1
br i1 %76, label %77, label %81
77: ; preds = %14
%78 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%79 = load i32, i32* @EXIT_FAILURE
%80 = tail call i32 @exit(i32 %79)
unreachable
81: ; preds = %14
%82 = tail call i32 @close(i32 %3)
ret void
}