#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 */
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ PROT_WRITE;
char *mmap(int /*<<< orphan*/, int, int /*<<< orphan*/, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, char *);
int strlen(char const *);
int /*<<< orphan*/ write(int, char const *, int);
int main() {
  int fd;
  const char str[] = "Hello world!";
  int len = strlen(str);
  ;
  fd = open("/tmp/file", O_CREAT | O_RDWR, 0600);
  write(fd, str, len);
  char *addr;
  addr = mmap(0, len, PROT_READ, MAP_PRIVATE, fd, 0);
  printf("%s\n\n", addr);
  munmap(addr, len);
  addr = mmap(0, len, PROT_READ, MAP_SHARED, fd, 0);
  printf("%s", addr);
  munmap(addr, len);
  addr = mmap(0, len, PROT_WRITE, MAP_SHARED, fd, 0);
  addr[0] = 'h';
  munmap(addr, len);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe8a(%rip),%rax
mov    %rax,-0x15(%rbp)
mov    0xe88(%rip),%eax
mov    %eax,-0xd(%rbp)
mov    0xe83(%rip),%al
mov    %al,-0x9(%rbp)
lea    -0x15(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
mov    0x2e28(%rip),%rax
or     (%rax),%esi
lea    0xe5c(%rip),%rdi
mov    $0x180,%edx
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    -0x1c(%rbp),%esi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
mov    0x2dd8(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1050 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe17(%rip),%rdi
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1070 <munmap@plt>
mov    -0x1c(%rbp),%esi
mov    0x2dd5(%rip),%rax
mov    (%rax),%edx
mov    0x2dbc(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1050 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xdd8(%rip),%rdi
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1070 <munmap@plt>
mov    -0x1c(%rbp),%esi
mov    0x2d61(%rip),%rax
mov    (%rax),%edx
mov    0x2d78(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1050 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movb   $0x68,(%rax)
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1070 <munmap@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
movabs $0x21646c726f7720,%rax
mov    %rax,0xd(%rsp)
movabs $0x6f77206f6c6c6548,%rax
20 77 6f
mov    %rax,0x8(%rsp)
lea    0x8(%rsp),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    0x2e5a(%rip),%rax
mov    0x2e2b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe5d(%rip),%rdi
mov    $0x180,%edx
call   1080 <open@plt>
mov    %eax,%r15d
mov    %eax,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
mov    0x2e20(%rip),%r14
mov    (%r14),%edx
mov    0x2dde(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%esi
mov    %r15d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbp
lea    0xe22(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
call   1070 <munmap@plt>
mov    (%r14),%edx
mov    0x2dcf(%rip),%r14
mov    (%r14),%ecx
xor    %edi,%edi
mov    %ebx,%esi
mov    %r15d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbp
lea    0xdef(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
call   1070 <munmap@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edx
mov    (%r14),%ecx
xor    %edi,%edi
mov    %ebx,%esi
mov    %r15d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
movb   $0x68,(%rax)
mov    %rax,%rdi
mov    %ebx,%esi
call   1070 <munmap@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.str = [13 x i8] c"Hello world!\00"
@.str = [10 x i8] c"/tmp/file\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.1 = [5 x i8] c"%s\0A\0A\00"
@MAP_SHARED = global i32 0
@.str.2 = [3 x i8] c"%s\00"
@PROT_WRITE = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [13 x i8]
%4 = alloca i32
%5 = alloca i8*
store i32 0, i32* %1
%6 = bitcast [13 x i8]* %3 to i8*
%7 = getelementptr inbounds [13 x i8], [13 x i8]* %3, i64 0, i64 0
%8 = call i32 @strlen(i8* %7)
store i32 %8, i32* %4
%9 = load i32, i32* @O_CREAT
%10 = load i32, i32* @O_RDWR
%11 = or i32 %9, %10
%12 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %11, i32 384)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = getelementptr inbounds [13 x i8], [13 x i8]* %3, i64 0, i64 0
%15 = load i32, i32* %4
%16 = call i32 @write(i32 %13, i8* %14, i32 %15)
%17 = load i32, i32* %4
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @MAP_PRIVATE
%20 = load i32, i32* %2
%21 = call i8* @mmap(i32 0, i32 %17, i32 %18, i32 %19, i32 %20, i32 0)
store i8* %21, i8** %5
%22 = load i8*, i8** %5
%23 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = load i8*, i8** %5
%25 = load i32, i32* %4
%26 = call i32 @munmap(i8* %24, i32 %25)
%27 = load i32, i32* %4
%28 = load i32, i32* @PROT_READ
%29 = load i32, i32* @MAP_SHARED
%30 = load i32, i32* %2
%31 = call i8* @mmap(i32 0, i32 %27, i32 %28, i32 %29, i32 %30, i32 0)
store i8* %31, i8** %5
%32 = load i8*, i8** %5
%33 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %32)
%34 = load i8*, i8** %5
%35 = load i32, i32* %4
%36 = call i32 @munmap(i8* %34, i32 %35)
%37 = load i32, i32* %4
%38 = load i32, i32* @PROT_WRITE
%39 = load i32, i32* @MAP_SHARED
%40 = load i32, i32* %2
%41 = call i8* @mmap(i32 0, i32 %37, i32 %38, i32 %39, i32 %40, i32 0)
store i8* %41, i8** %5
%42 = load i8*, i8** %5
%43 = getelementptr inbounds i8, i8* %42, i64 0
store i8 104, i8* %43
%44 = load i8*, i8** %5
%45 = load i32, i32* %4
%46 = call i32 @munmap(i8* %44, i32 %45)
ret i32 0
}

LLVM IR (-O2):
@__const.main.str = [13 x i8] c"Hello world!\00"
@.str = [10 x i8] c"/tmp/file\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.1 = [5 x i8] c"%s\0A\0A\00"
@MAP_SHARED = global i32 0
@.str.2 = [3 x i8] c"%s\00"
@PROT_WRITE = global i32 0
define i32 @main() {
%1 = alloca [13 x i8]
%2 = getelementptr inbounds [13 x i8], [13 x i8]* %1, i64 0, i64 0
%3 = call i32 @strlen(i8* %2)
%4 = load i32, i32* @O_CREAT
%5 = load i32, i32* @O_RDWR
%6 = or i32 %5, %4
%7 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %6, i32 384)
%8 = call i32 @write(i32 %7, i8* %2, i32 %3)
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @MAP_PRIVATE
%11 = call i8* @mmap(i32 0, i32 %3, i32 %9, i32 %10, i32 %7, i32 0)
%12 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = call i32 @munmap(i8* %11, i32 %3)
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @MAP_SHARED
%16 = call i8* @mmap(i32 0, i32 %3, i32 %14, i32 %15, i32 %7, i32 0)
%17 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%18 = call i32 @munmap(i8* %16, i32 %3)
%19 = load i32, i32* @PROT_WRITE
%20 = load i32, i32* @MAP_SHARED
%21 = call i8* @mmap(i32 0, i32 %3, i32 %19, i32 %20, i32 %7, i32 0)
store i8 104, i8* %21
%22 = call i32 @munmap(i8* %21, i32 %3)
ret i32 0
}