#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*/ MADV_SEQUENTIAL;
int /*<<< orphan*/ MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
scalar_t__ madvise(void *, size_t, int /*<<< orphan*/);
void *mmap(int /*<<< orphan*/, size_t, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, size_t);
int mmap_helper(void **ptr, size_t size, int fd) {
  void *ptr0 = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (!ptr0) {
    return -9;
  }
  if (madvise(ptr0, size, MADV_SEQUENTIAL) != 0) {
    munmap(ptr0, size);
    return -10;
  }
  *ptr = ptr0;
  return 0;
}


x86 assembly (clang, -O0):
<mmap_helper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rsi
mov    0x2eaa(%rip),%rax
mov    (%rax),%edx
mov    0x2e79(%rip),%rax
or     (%rax),%edx
mov    0x2e88(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118c <mmap_helper+0x5c>
movl   $0xfffffff7,-0x4(%rbp)
jmp    11d7 <mmap_helper+0xa7>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
call   1050 <madvise@plt>
cmp    $0x0,%rax
je     11c5 <mmap_helper+0x95>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <munmap@plt>
movl   $0xfffffff6,-0x4(%rbp)
jmp    11d7 <mmap_helper+0xa7>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmap_helper>:
push   %r15
push   %r14
push   %rbx
mov    %edx,%r8d
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2eb3(%rip),%rax
mov    0x2e84(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e91(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
test   %rax,%rax
je     1196 <mmap_helper+0x66>
mov    %rax,%rbx
mov    0x2e4e(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <madvise@plt>
test   %rax,%rax
je     119d <mmap_helper+0x6d>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <munmap@plt>
mov    $0xfffffff6,%eax
jmp    11a2 <mmap_helper+0x72>
mov    $0xfffffff7,%eax
jmp    11a2 <mmap_helper+0x72>
mov    %rbx,(%r14)
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MADV_SEQUENTIAL = global i32 0
define i32 @mmap_helper(i8** %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
store i8** %0, i8*** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
%9 = load i64, i64* %6
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %10, %11
%13 = load i32, i32* @MAP_SHARED
%14 = load i32, i32* %7
%15 = call i8* @mmap(i32 0, i64 %9, i32 %12, i32 %13, i32 %14, i32 0)
store i8* %15, i8** %8
%16 = load i8*, i8** %8
%17 = icmp ne i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %3
store i32 -9, i32* %4
br label %32
19: ; preds = %3
%20 = load i8*, i8** %8
%21 = load i64, i64* %6
%22 = load i32, i32* @MADV_SEQUENTIAL
%23 = call i64 @madvise(i8* %20, i64 %21, i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = load i8*, i8** %8
%27 = load i64, i64* %6
%28 = call i32 @munmap(i8* %26, i64 %27)
store i32 -10, i32* %4
br label %32
29: ; preds = %19
%30 = load i8*, i8** %8
%31 = load i8**, i8*** %5
store i8* %30, i8** %31
store i32 0, i32* %4
br label %32
32: ; preds = %29, %25, %18
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MADV_SEQUENTIAL = global i32 0
define i32 @mmap_helper(i8** nocapture writeonly %0, i64 %1, i32 %2) {
%4 = load i32, i32* @PROT_READ
%5 = load i32, i32* @PROT_WRITE
%6 = or i32 %5, %4
%7 = load i32, i32* @MAP_SHARED
%8 = tail call i8* @mmap(i32 0, i64 %1, i32 %6, i32 %7, i32 %2, i32 0)
%9 = icmp eq i8* %8, null
br i1 %9, label %17, label %10
10: ; preds = %3
%11 = load i32, i32* @MADV_SEQUENTIAL
%12 = tail call i64 @madvise(i8* %8, i64 %1, i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = tail call i32 @munmap(i8* %8, i64 %1)
br label %17
16: ; preds = %10
store i8* %8, i8** %0
br label %17
17: ; preds = %3, %16, %14
%18 = phi i32 [ -10, %14 ], [ 0, %16 ], [ -9, %3 ]
ret i32 %18
}