#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 */
typedef scalar_t__ caddr_t;

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
scalar_t__ mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
                int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
void *memmap(int size) {
  caddr_t ptr;
  int fd = -1;
  fd = open("/dev/zero", O_RDWR);
  if (fd < 0)
    return NULL;
  ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  close(fd);
  if (ptr == ((caddr_t)-1))
    return NULL;
  return ptr;
}


x86 assembly (clang, -O0):
<memmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
lea    0xeae(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1171 <memmap+0x41>
movq   $0x0,-0x8(%rbp)
jmp    11cb <memmap+0x9b>
mov    -0xc(%rbp),%esi
mov    0x2e7d(%rip),%rax
mov    (%rax),%edx
mov    0x2e44(%rip),%rax
or     (%rax),%edx
mov    0x2e5b(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    11c3 <memmap+0x93>
movq   $0x0,-0x8(%rbp)
jmp    11cb <memmap+0x9b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memmap>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebx
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
lea    0xeba(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     1192 <memmap+0x62>
mov    %eax,%ebp
mov    0x2ea0(%rip),%rax
mov    0x2e69(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e7e(%rip),%rax
mov    (%rax),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    %ebx,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1040 <close@plt>
cmp    $0xffffffffffffffff,%rbx
cmove  %r14,%rbx
jmp    1194 <memmap+0x64>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/zero\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
define i8* @memmap(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
store i32 %0, i32* %3
store i32 -1, i32* %5
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
store i8* null, i8** %2
br label %27
11: ; preds = %1
%12 = load i32, i32* %3
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @PROT_WRITE
%15 = or i32 %13, %14
%16 = load i32, i32* @MAP_SHARED
%17 = load i32, i32* %5
%18 = call i64 @mmap(i32* null, i32 %12, i32 %15, i32 %16, i32 %17, i32 0)
store i64 %18, i64* %4
%19 = load i32, i32* %5
%20 = call i32 @close(i32 %19)
%21 = load i64, i64* %4
%22 = icmp eq i64 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %11
store i8* null, i8** %2
br label %27
24: ; preds = %11
%25 = load i64, i64* %4
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %2
br label %27
27: ; preds = %24, %23, %10
%28 = load i8*, i8** %2
ret i8* %28
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/zero\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
define i8* @memmap(i32 %0) {
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %15, label %5
5: ; preds = %1
%6 = load i32, i32* @PROT_READ
%7 = load i32, i32* @PROT_WRITE
%8 = or i32 %7, %6
%9 = load i32, i32* @MAP_SHARED
%10 = tail call i64 @mmap(i32* null, i32 %0, i32 %8, i32 %9, i32 %3, i32 0)
%11 = tail call i32 @close(i32 %3)
%12 = icmp eq i64 %10, -1
%13 = inttoptr i64 %10 to i8*
%14 = select i1 %12, i8* null, i8* %13
br label %15
15: ; preds = %5, %1
%16 = phi i8* [ null, %1 ], [ %14, %5 ]
ret i8* %16
}