#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_ANON;
int MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_NONE;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
void *mmap(int /*<<< orphan*/ *, long, int /*<<< orphan*/, int, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ mprotect(void *, long, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, long);
int open(char *, int /*<<< orphan*/);
int foo(long sz) {
  int fd;
  void *ptr;
  fd = open("/dev/zero", O_RDONLY);
  ptr = mmap(NULL, sz, PROT_READ, MAP_ANON | MAP_PRIVATE, fd, 0);
  mprotect(ptr, sz, PROT_NONE);
  munmap(ptr, sz);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<foo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
lea    0xe94(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rsi
mov    0x2e79(%rip),%rax
mov    (%rax),%edx
mov    0x2e40(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2f(%rip),%rax
or     (%rax),%ecx
mov    -0xc(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
call   1060 <mprotect@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <munmap@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<foo>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
lea    0xe99(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%r14d
mov    0x2e82(%rip),%rax
mov    (%rax),%edx
mov    0x2e49(%rip),%rax
mov    0x2e3a(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
mov    0x2e44(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1060 <mprotect@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1050 <munmap@plt>
mov    %r14d,%edi
call   1040 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/zero\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_ANON = global i32 0
@MAP_PRIVATE = global i32 0
@PROT_NONE = global i32 0
define i32 @foo(i64 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i8*
store i64 %0, i64* %2
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %3
%7 = load i64, i64* %2
%8 = load i32, i32* @PROT_READ
%9 = load i32, i32* @MAP_ANON
%10 = load i32, i32* @MAP_PRIVATE
%11 = or i32 %9, %10
%12 = load i32, i32* %3
%13 = call i8* @mmap(i32* null, i64 %7, i32 %8, i32 %11, i32 %12, i32 0)
store i8* %13, i8** %4
%14 = load i8*, i8** %4
%15 = load i64, i64* %2
%16 = load i32, i32* @PROT_NONE
%17 = call i32 @mprotect(i8* %14, i64 %15, i32 %16)
%18 = load i8*, i8** %4
%19 = load i64, i64* %2
%20 = call i32 @munmap(i8* %18, i64 %19)
%21 = load i32, i32* %3
%22 = call i32 @close(i32 %21)
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/zero\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_ANON = global i32 0
@MAP_PRIVATE = global i32 0
@PROT_NONE = global i32 0
define i32 @foo(i64 %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = load i32, i32* @PROT_READ
%5 = load i32, i32* @MAP_ANON
%6 = load i32, i32* @MAP_PRIVATE
%7 = or i32 %6, %5
%8 = tail call i8* @mmap(i32* null, i64 %0, i32 %4, i32 %7, i32 %3, i32 0)
%9 = load i32, i32* @PROT_NONE
%10 = tail call i32 @mprotect(i8* %8, i64 %0, i32 %9)
%11 = tail call i32 @munmap(i8* %8, i64 %0)
%12 = tail call i32 @close(i32 %3)
ret i32 0
}