#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 /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
void *mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
void *os_alloc_pages(size_t len) {
  void *r;
  static int fd;
  static int fd_created;
  if (!fd_created) {
    fd_created = 1;
    fd = open("/dev/zero", O_RDWR);
  }
  r = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  if (r == (void *)-1)
    return NULL;
  return r;
}


x86 assembly (clang, -O0):
<os_alloc_pages>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpl   $0x0,0x2f05(%rip)
jne    115e <os_alloc_pages+0x3e>
movl   $0x1,0x2ef5(%rip)
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
lea    0xead(%rip),%rdi
call   1040 <open@plt>
mov    %eax,0x2ed6(%rip)
mov    -0x10(%rbp),%rsi
mov    0x2e8f(%rip),%rax
mov    (%rax),%edx
mov    0x2e5e(%rip),%rax
or     (%rax),%edx
mov    0x2e4d(%rip),%rax
mov    (%rax),%ecx
mov    0x2eb0(%rip),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x18(%rbp)
jne    11b2 <os_alloc_pages+0x92>
movq   $0x0,-0x8(%rbp)
jmp    11ba <os_alloc_pages+0x9a>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_alloc_pages>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
cmpb   $0x1,0x2f0a(%rip)
jne    1139 <os_alloc_pages+0x19>
mov    0x2efd(%rip),%r8d
jmp    115e <os_alloc_pages+0x3e>
movb   $0x1,0x2ef8(%rip)
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
lea    0xeb0(%rip),%rdi
call   1040 <open@plt>
mov    %eax,%r8d
mov    %eax,0x2ed6(%rip)
mov    0x2e93(%rip),%rax
mov    0x2e64(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e51(%rip),%rax
mov    (%rax),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    %rbx,%rsi
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
cmove  %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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