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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ mmap(int /*<<< orphan*/, size_t, int, int, int,
                        int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
caddr_t anon_mmap(caddr_t addr, size_t len, int prot, int flags) {
  static int fd = -1;
  if (fd == -1) {
    fd = open("/dev/zero", O_RDWR);
    if (fd == -1) {
      perror("Can't open /dev/zero");
      exit(2);
    }
  }
  return mmap(addr, len, prot, flags, fd, 0);
}


x86 assembly (clang, -O0):
<anon_mmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
cmpl   $0xffffffff,0x2ee4(%rip)
jne    11a5 <anon_mmap+0x65>
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
lea    0xe99(%rip),%rdi
call   1040 <open@plt>
mov    %eax,0x2ec3(%rip)
cmpl   $0xffffffff,0x2ebc(%rip)
jne    11a0 <anon_mmap+0x60>
lea    0xe6f(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%edi
call   1060 <exit@plt>
jmp    11a5 <anon_mmap+0x65>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    0x2e87(%rip),%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<anon_mmap>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2ee7(%rip),%r8d
cmp    $0xffffffff,%r8d
jne    1182 <anon_mmap+0x42>
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
lea    0xe9c(%rip),%rdi
call   1040 <open@plt>
mov    %eax,%r8d
mov    %eax,0x2ec3(%rip)
cmp    $0xffffffff,%eax
je     119f <anon_mmap+0x5f>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
mov    %r14d,%ecx
xor    %r9d,%r9d
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <mmap@plt>
lea    0xe5a(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@anon_mmap.fd = internal global i32 -1
@.str = [10 x i8] c"/dev/zero\00"
@O_RDWR = global i32 0
@.str.1 = [21 x i8] c"Can't open /dev/zero\00"
define i32 @anon_mmap(i32 %0, i64 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i64 %1, i64* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%9 = load i32, i32* @anon_mmap.fd
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %20
11: ; preds = %4
%12 = load i32, i32* @O_RDWR
%13 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* @anon_mmap.fd
%14 = load i32, i32* @anon_mmap.fd
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 2)
unreachable
19: ; preds = %11
br label %20
20: ; preds = %19, %4
%21 = load i32, i32* %5
%22 = load i64, i64* %6
%23 = load i32, i32* %7
%24 = load i32, i32* %8
%25 = load i32, i32* @anon_mmap.fd
%26 = call i32 @mmap(i32 %21, i64 %22, i32 %23, i32 %24, i32 %25, i32 0)
ret i32 %26
}

LLVM IR (-O2):
@anon_mmap.fd = internal unnamed_addr global i32 -1
@.str = [10 x i8] c"/dev/zero\00"
@O_RDWR = global i32 0
@.str.1 = [21 x i8] c"Can't open /dev/zero\00"
define i32 @anon_mmap(i32 %0, i64 %1, i32 %2, i32 %3) {
%5 = load i32, i32* @anon_mmap.fd
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %14
7: ; preds = %4
%8 = load i32, i32* @O_RDWR
%9 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* @anon_mmap.fd
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @exit(i32 2)
unreachable
14: ; preds = %7, %4
%15 = phi i32 [ %9, %7 ], [ %5, %4 ]
%16 = tail call i32 @mmap(i32 %0, i64 %1, i32 %2, i32 %3, i32 %15, i32 0)
ret i32 %16
}