#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*/ S_IRUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
char *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int main(int argc, char **argv) {
  char *m;
  int i;
  int fd = open("./hello.py", O_RDWR, S_IRUSR);
  if (fd == -1) {
    perror("hello.py failed\n");
    exit(-1);
  };
  m = mmap(0, 50, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (m == MAP_FAILED) {
    perror("mmap /dev/sda failed\n");
    exit(-1);
  }
  printf("%s\n", m);
  *m = 'a';
  *(m + 1) = 'b';
  m[2] = 'c';
  printf("%s\n", m);
  munmap(m, 50);
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
lea    0xe68(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11c0 <main+0x50>
lea    0xe5a(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
mov    0x2df8(%rip),%rax
or     (%rax),%edx
mov    0x2e0f(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%r8d
xor    %r9d,%r9d
mov    $0x32,%esi
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2dba(%rip),%rcx
cmp    (%rcx),%rax
jne    121d <main+0xad>
lea    0xe0e(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rsi
lea    0xe0a(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
movb   $0x61,(%rax)
mov    -0x18(%rbp),%rax
movb   $0x62,0x1(%rax)
mov    -0x18(%rbp),%rax
movb   $0x63,0x2(%rax)
mov    -0x18(%rbp),%rsi
lea    0xde3(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    $0x32,%esi
call   1060 <munmap@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
lea    0xe73(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1216 <main+0xa6>
mov    %eax,%ebp
mov    0x2e58(%rip),%rax
mov    0x2e21(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e36(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x32,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2de8(%rip),%rax
cmp    (%rax),%rbx
je     121f <main+0xaf>
lea    0xe56(%rip),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
movw   $0x6261,(%rbx)
movb   $0x63,0x2(%rbx)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x32,%esi
call   1060 <munmap@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdee(%rip),%rdi
jmp    1226 <main+0xb6>
lea    0xdf6(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"./hello.py\00"
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@.str.1 = [17 x i8] c"hello.py failed\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [22 x i8] c"mmap /dev/sda failed\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @S_IRUSR
%10 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %9)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 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* %7
%22 = call i8* @mmap(i32 0, i32 50, i32 %19, i32 %20, i32 %21, i32 0)
store i8* %22, i8** %5
%23 = load i8*, i8** %5
%24 = load i8*, i8** @MAP_FAILED
%25 = icmp eq i8* %23, %24
br i1 %25, label %26, label %29
26: ; preds = %16
%27 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %16
%30 = load i8*, i8** %5
%31 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %30)
%32 = load i8*, i8** %5
store i8 97, i8* %32
%33 = load i8*, i8** %5
%34 = getelementptr inbounds i8, i8* %33, i64 1
store i8 98, i8* %34
%35 = load i8*, i8** %5
%36 = getelementptr inbounds i8, i8* %35, i64 2
store i8 99, i8* %36
%37 = load i8*, i8** %5
%38 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %37)
%39 = load i8*, i8** %5
%40 = call i32 @munmap(i8* %39, i32 50)
%41 = load i32, i32* %7
%42 = call i32 @close(i32 %41)
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"./hello.py\00"
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@.str.1 = [17 x i8] c"hello.py failed\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [22 x i8] c"mmap /dev/sda failed\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @S_IRUSR
%5 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %2
%11 = load i32, i32* @PROT_READ
%12 = load i32, i32* @PROT_WRITE
%13 = or i32 %12, %11
%14 = load i32, i32* @MAP_SHARED
%15 = tail call i8* @mmap(i32 0, i32 50, i32 %13, i32 %14, i32 %5, i32 0)
%16 = load i8*, i8** @MAP_FAILED
%17 = icmp eq i8* %15, %16
br i1 %17, label %18, label %21
18: ; preds = %10
%19 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call i32 @exit(i32 -1)
unreachable
21: ; preds = %10
%22 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %15)
store i8 97, i8* %15
%23 = getelementptr inbounds i8, i8* %15, i64 1
store i8 98, i8* %23
%24 = getelementptr inbounds i8, i8* %15, i64 2
store i8 99, i8* %24
%25 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %15)
%26 = tail call i32 @munmap(i8* %15, i32 50)
%27 = tail call i32 @close(i32 %5)
ret i32 0
}