#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 MAP_ANONYMOUS;
char *MAP_FAILED;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
char *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int munmap(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int main(void) {
  char *p;
  int ret;
  p = mmap(NULL, 512 * 1024, PROT_READ | PROT_WRITE,
           MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  if (p == MAP_FAILED)
    perror("mmap");
  strcpy(p, "HELLO");
  printf("%s\n", p);
  ret = munmap(p, 512 * 1024);
  if (ret)
    perror("munmap");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%edx
mov    0x2e61(%rip),%rax
or     (%rax),%edx
mov    0x2e68(%rip),%rax
mov    (%rax),%ecx
mov    0x2e47(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x80000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e17(%rip),%rcx
cmp    (%rcx),%rax
jne    11be <main+0x6e>
lea    0xe47(%rip),%rdi
call   1070 <perror@plt>
mov    -0x10(%rbp),%rdi
lea    0xe3c(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rsi
lea    0xe32(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    $0x80000,%esi
call   1060 <munmap@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1205 <main+0xb5>
lea    0xe0f(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2ea0(%rip),%rax
mov    0x2e71(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e76(%rip),%rax
mov    0x2e57(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0x80000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2e2c(%rip),%rax
cmp    (%rax),%rbx
je     11cc <main+0x7c>
lea    0xe65(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0xe5c(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x80000,%esi
call   1060 <munmap@plt>
test   %eax,%eax
jne    11da <main+0x8a>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe2d(%rip),%rdi
call   1070 <perror@plt>
jmp    1199 <main+0x49>
lea    0xe2e(%rip),%rdi
call   1070 <perror@plt>
jmp    11c8 <main+0x78>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str = [5 x i8] c"mmap\00"
@.str.1 = [6 x i8] c"HELLO\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [7 x i8] c"munmap\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32, i32* @PROT_READ
%5 = load i32, i32* @PROT_WRITE
%6 = or i32 %4, %5
%7 = load i32, i32* @MAP_ANONYMOUS
%8 = load i32, i32* @MAP_PRIVATE
%9 = or i32 %7, %8
%10 = call i8* @mmap(i32* null, i32 524288, i32 %6, i32 %9, i32 -1, i32 0)
store i8* %10, i8** %2
%11 = load i8*, i8** %2
%12 = load i8*, i8** @MAP_FAILED
%13 = icmp eq i8* %11, %12
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %0
%17 = load i8*, i8** %2
%18 = call i32 @strcpy(i8* %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%19 = load i8*, i8** %2
%20 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %19)
%21 = load i8*, i8** %2
%22 = call i32 @munmap(i8* %21, i32 524288)
store i32 %22, i32* %3
%23 = load i32, i32* %3
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %16
%26 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %27
27: ; preds = %25, %16
ret i32 0
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str = [5 x i8] c"mmap\00"
@.str.1 = [6 x i8] c"HELLO\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [7 x i8] c"munmap\00"
define i32 @main() {
%1 = load i32, i32* @PROT_READ
%2 = load i32, i32* @PROT_WRITE
%3 = or i32 %2, %1
%4 = load i32, i32* @MAP_ANONYMOUS
%5 = load i32, i32* @MAP_PRIVATE
%6 = or i32 %5, %4
%7 = tail call i8* @mmap(i32* null, i32 524288, i32 %3, i32 %6, i32 -1, i32 0)
%8 = load i8*, i8** @MAP_FAILED
%9 = icmp eq i8* %7, %8
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = tail call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %7)
%15 = tail call i32 @munmap(i8* %7, i32 524288)
%16 = icmp eq i32 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %12
%18 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %19
19: ; preds = %17, %12
ret i32 0
}