#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 int /*<<< orphan*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
void *mmap(void *, size_t, int, int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
void *mmap_anon(void *addr, size_t len, int prot, int flags, off_t offset) {
  int fd;
  void *result;
  fd = open("/dev/zero", O_RDWR);
  if (fd < 0) {
    perror("Cannot open /dev/zero for READ+WRITE. Check permissions! error: ");
    return NULL;
  }
  result = mmap(addr, len, prot, flags, fd, offset);
  close(fd);
  return result;
}


x86 assembly (clang, -O0):
<mmap_anon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    0x2e87(%rip),%rax
mov    (%rax),%esi
lea    0xe96(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1195 <mmap_anon+0x55>
lea    0xe87(%rip),%rdi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    11c4 <mmap_anon+0x84>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    -0x28(%rbp),%r8d
mov    -0x24(%rbp),%r9d
call   1030 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmap_anon>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r14d
mov    %ecx,%r15d
mov    %edx,%r12d
mov    %rsi,%r13
mov    %rdi,%rbp
mov    0x2e87(%rip),%rax
mov    (%rax),%esi
lea    0xe96(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     11a8 <mmap_anon+0x68>
mov    %eax,%ebx
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %r12d,%edx
mov    %r15d,%ecx
mov    %eax,%r8d
mov    %r14d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe5b(%rip),%rdi
call   1060 <perror@plt>
xor    %eax,%eax
jmp    1199 <mmap_anon+0x59>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/zero\00"
@O_RDWR = global i32 0
@.str.1 = [65 x i8] c"Cannot open /dev/zero for READ+WRITE. Check permissions! error: \00"
define i8* @mmap_anon(i8* %0, i64 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
store i8* %0, i8** %7
store i64 %1, i64* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%14 = load i32, i32* @O_RDWR
%15 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* %12
%16 = load i32, i32* %12
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %5
%19 = call i32 @perror(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %6
br label %31
20: ; preds = %5
%21 = load i8*, i8** %7
%22 = load i64, i64* %8
%23 = load i32, i32* %9
%24 = load i32, i32* %10
%25 = load i32, i32* %12
%26 = load i32, i32* %11
%27 = call i8* @mmap(i8* %21, i64 %22, i32 %23, i32 %24, i32 %25, i32 %26)
store i8* %27, i8** %13
%28 = load i32, i32* %12
%29 = call i32 @close(i32 %28)
%30 = load i8*, i8** %13
store i8* %30, i8** %6
br label %31
31: ; preds = %20, %18
%32 = load i8*, i8** %6
ret i8* %32
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/zero\00"
@O_RDWR = global i32 0
@.str.1 = [65 x i8] c"Cannot open /dev/zero for READ+WRITE. Check permissions! error: \00"
define i8* @mmap_anon(i8* %0, i64 %1, i32 %2, i32 %3, i32 %4) {
%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)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = tail call i32 @perror(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.1, i64 0, i64 0))
br label %14
11: ; preds = %5
%12 = tail call i8* @mmap(i8* %0, i64 %1, i32 %2, i32 %3, i32 %7, i32 %4)
%13 = tail call i32 @close(i32 %7)
br label %14
14: ; preds = %11, %9
%15 = phi i8* [ null, %9 ], [ %12, %11 ]
ret i8* %15
}