#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 */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(char *, float, int);
char *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int main(void) {
  int fd;
  char *addr = NULL;
  fd = open("/dev/mmap", O_RDWR);
  if (fd < 0) {
    perror("open");
    exit(1);
  }
  addr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (addr == MAP_FAILED) {
    perror("mmap");
    exit(1);
  }
  printf("%s\n", addr);
  memset(addr, 'f', 100);
  addr[0] = 'p';
  printf("%s\n", addr);
  munmap(addr, 4096);
  addr = NULL;
  close(fd);
  fd = open("/dev/mmap", O_RDWR);
  if (fd < 0) {
    perror("open");
    exit(1);
  }
  addr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (addr == MAP_FAILED) {
    perror("mmap");
    exit(1);
  }
  printf("%s\n", addr);
  munmap(addr, 4096);
  close(fd);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
lea    0xe5d(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11cf <main+0x4f>
lea    0xe4e(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
mov    0x2de9(%rip),%rax
or     (%rax),%edx
mov    0x2e00(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1000,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2db2(%rip),%rcx
cmp    (%rcx),%rax
jne    122d <main+0xad>
lea    0xdeb(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rsi
lea    0xddb(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
movss  0xdb7(%rip),%xmm0
mov    $0x64,%esi
call   1050 <memset@plt>
mov    -0x10(%rbp),%rax
movb   $0x70,(%rax)
mov    -0x10(%rbp),%rsi
lea    0xdae(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    $0x1000,%esi
call   1070 <munmap@plt>
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    0x2d49(%rip),%rax
mov    (%rax),%esi
lea    0xd6c(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    12c0 <main+0x140>
lea    0xd5d(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d31(%rip),%rax
mov    (%rax),%edx
mov    0x2cf8(%rip),%rax
or     (%rax),%edx
mov    0x2d0f(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1000,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2cc1(%rip),%rcx
cmp    (%rcx),%rax
jne    131e <main+0x19e>
lea    0xcfa(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rsi
lea    0xcea(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    $0x1000,%esi
call   1070 <munmap@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
lea    0xe69(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     12b1 <main+0x131>
mov    %eax,%ebp
mov    0x2e47(%rip),%rax
mov    0x2e10(%rip),%r13
mov    0x0(%r13),%edx
or     (%rax),%edx
mov    0x2e23(%rip),%r15
mov    (%r15),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2ddf(%rip),%r12
cmp    (%r12),%rax
je     12ba <main+0x13a>
mov    %rax,%rbx
lea    0xe1e(%rip),%r14
mov    %r14,%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
movss  0xdf8(%rip),%xmm0
mov    %rbx,%rdi
mov    $0x64,%esi
call   1050 <memset@plt>
movb   $0x70,(%rbx)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1070 <munmap@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
lea    0xdbd(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     12b1 <main+0x131>
mov    %eax,%ebx
mov    0x0(%r13),%edx
mov    0x2d9b(%rip),%rax
or     (%rax),%edx
mov    (%r15),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmp    (%r12),%rax
je     12ba <main+0x13a>
mov    %rax,%rbp
lea    0xd8f(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %rbp,%rdi
mov    $0x1000,%esi
call   1070 <munmap@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd56(%rip),%rdi
jmp    12c1 <main+0x141>
lea    0xd48(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

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

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/mmap\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [5 x i8] c"mmap\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %0
%8 = load i32, i32* @PROT_READ
%9 = load i32, i32* @PROT_WRITE
%10 = or i32 %9, %8
%11 = load i32, i32* @MAP_SHARED
%12 = tail call i8* @mmap(i32* null, i32 4096, i32 %10, i32 %11, i32 %2, i32 0)
%13 = load i8*, i8** @MAP_FAILED
%14 = icmp eq i8* %12, %13
br i1 %14, label %15, label %18
15: ; preds = %7
%16 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %7
%19 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %12)
%20 = tail call i32 @memset(i8* %12, float 1.020000e+02, i32 100)
store i8 112, i8* %12
%21 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %12)
%22 = tail call i32 @munmap(i8* %12, i32 4096)
%23 = tail call i32 @close(i32 %2)
%24 = load i32, i32* @O_RDWR
%25 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %18
%28 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %18
%31 = load i32, i32* @PROT_READ
%32 = load i32, i32* @PROT_WRITE
%33 = or i32 %32, %31
%34 = load i32, i32* @MAP_SHARED
%35 = tail call i8* @mmap(i32* null, i32 4096, i32 %33, i32 %34, i32 %25, i32 0)
%36 = load i8*, i8** @MAP_FAILED
%37 = icmp eq i8* %35, %36
br i1 %37, label %38, label %41
38: ; preds = %30
%39 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%40 = tail call i32 @exit(i32 1)
unreachable
41: ; preds = %30
%42 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %35)
%43 = tail call i32 @munmap(i8* %35, i32 4096)
%44 = tail call i32 @close(i32 %25)
ret i32 0
}