#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 O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int S_IRGRP;
int S_IROTH;
int S_IRWXU;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ ftruncate(int, int);
int /*<<< orphan*/ memset(char *, char, int);
scalar_t__ mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
                int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcpy(char *, char *);
int main(void) {
  int fd;
  char *addr;
  if ((fd = open("test.txt", O_CREAT | O_RDWR, S_IRWXU | S_IRGRP | S_IROTH)) <
      0) {
    printf("open test.txt error!\n");
    return -1;
  }
  ftruncate(fd, 30);
  if ((addr = (char *)mmap(NULL, 30, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
                           0)) == MAP_FAILED) {
    printf("mmap error!\n");
    return -1;
  }
  memset(addr, '\0', 30);
  strcpy(addr, "hello");
  printf("addr:%s\n", addr);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
mov    0x2e39(%rip),%rax
or     (%rax),%esi
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
mov    0x2e17(%rip),%rax
or     (%rax),%edx
mov    0x2e06(%rip),%rax
or     (%rax),%edx
lea    0xe4d(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11de <main+0x6e>
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128a <main+0x11a>
mov    -0x8(%rbp),%edi
mov    $0x1e,%esi
call   1060 <ftruncate@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edx
mov    0x2dad(%rip),%rax
or     (%rax),%edx
mov    0x2ddc(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1e,%esi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    0x2d7a(%rip),%rcx
cmp    (%rcx),%rax
jne    1249 <main+0xd9>
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128a <main+0x11a>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x1e,%edx
call   1070 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    0xdc8(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rsi
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e6d(%rip),%rax
mov    0x2e36(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e33(%rip),%rax
mov    0x2e14(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e01(%rip),%rax
or     (%rax),%edx
lea    0xe48(%rip),%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     124a <main+0xca>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x1e,%esi
call   1070 <ftruncate@plt>
mov    0x2e16(%rip),%rax
mov    0x2dc7(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2df4(%rip),%rax
mov    (%rax),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    $0x1e,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
mov    0x2d93(%rip),%rax
cmp    %rbx,(%rax)
je     1253 <main+0xd3>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1e,%edx
call   1080 <memset@plt>
lea    0xde1(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0xdd8(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
call   1090 <close@plt>
jmp    1265 <main+0xe5>
lea    0xdd3(%rip),%rdi
jmp    125a <main+0xda>
lea    0xdbe(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [22 x i8] c"open test.txt error!\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [13 x i8] c"mmap error!\0A\00"
@.str.3 = [6 x i8] c"hello\00"
@.str.4 = [9 x i8] c"addr:%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
store i32 0, i32* %1
%4 = load i32, i32* @O_CREAT
%5 = load i32, i32* @O_RDWR
%6 = or i32 %4, %5
%7 = load i32, i32* @S_IRWXU
%8 = load i32, i32* @S_IRGRP
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IROTH
%11 = or i32 %9, %10
%12 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %11)
store i32 %12, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %39
16: ; preds = %0
%17 = load i32, i32* %2
%18 = call i32 @ftruncate(i32 %17, i32 30)
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @PROT_WRITE
%21 = or i32 %19, %20
%22 = load i32, i32* @MAP_SHARED
%23 = load i32, i32* %2
%24 = call i64 @mmap(i32* null, i32 30, i32 %21, i32 %22, i32 %23, i32 0)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %3
%26 = load i8*, i8** @MAP_FAILED
%27 = icmp eq i8* %25, %26
br i1 %27, label %28, label %30
28: ; preds = %16
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %39
30: ; preds = %16
%31 = load i8*, i8** %3
%32 = call i32 @memset(i8* %31, i8 signext 0, i32 30)
%33 = load i8*, i8** %3
%34 = call i32 @strcpy(i8* %33, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%35 = load i8*, i8** %3
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %35)
%37 = load i32, i32* %2
%38 = call i32 @close(i32 %37)
store i32 0, i32* %1
br label %39
39: ; preds = %30, %28, %14
%40 = load i32, i32* %1
ret i32 %40
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = 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 = [6 x i8] c"hello\00"
@.str.4 = [9 x i8] c"addr:%s\0A\00"
@str = [12 x i8] c"mmap error!\00"
@str.5 = [21 x i8] c"open test.txt error!\00"
define i32 @main() {
%1 = load i32, i32* @O_CREAT
%2 = load i32, i32* @O_RDWR
%3 = or i32 %2, %1
%4 = load i32, i32* @S_IRWXU
%5 = load i32, i32* @S_IRGRP
%6 = or i32 %5, %4
%7 = load i32, i32* @S_IROTH
%8 = or i32 %6, %7
%9 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.5, i64 0, i64 0))
br label %30
13: ; preds = %0
%14 = tail call i32 @ftruncate(i32 %9, i32 30)
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @PROT_WRITE
%17 = or i32 %16, %15
%18 = load i32, i32* @MAP_SHARED
%19 = tail call i64 @mmap(i32* null, i32 30, i32 %17, i32 %18, i32 %9, i32 0)
%20 = inttoptr i64 %19 to i8*
%21 = load i8*, i8** @MAP_FAILED
%22 = icmp eq i8* %21, %20
br i1 %22, label %23, label %25
23: ; preds = %13
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %30
25: ; preds = %13
%26 = tail call i32 @memset(i8* %20, i8 signext 0, i32 30)
%27 = tail call i32 @strcpy(i8* %20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %20)
%29 = tail call i32 @close(i32 %9)
br label %30
30: ; preds = %25, %23, %11
%31 = phi i32 [ -1, %11 ], [ -1, %23 ], [ 0, %25 ]
ret i32 %31
}