#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 */
struct stat {
  size_t st_size;
};

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int open_and_mmap_file(const char *f, void **buf_ref, int *fd_ref,
                       size_t *sz_ref) {
  struct stat statbuf;
  int rc, fd = open(f, O_RDONLY);
  if (fd < 0)
    return -errno;
  if (fstat(fd, &statbuf) < 0)
    goto err_end;
  *buf_ref = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (*buf_ref == (void *)-1)
    goto err_end;
  *sz_ref = statbuf.st_size;
  *fd_ref = fd;
  return 0;
err_end:
  rc = -errno;
  close(fd);
  return rc;
}


x86 assembly (clang, -O0):
<open_and_mmap_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    118a <open_and_mmap_file+0x4a>
xor    %eax,%eax
mov    0x2e48(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    122a <open_and_mmap_file+0xea>
mov    -0x38(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0x0,%rax
jge    11a5 <open_and_mmap_file+0x65>
jmp    120e <open_and_mmap_file+0xce>
mov    -0x30(%rbp),%rsi
mov    0x2e48(%rip),%rax
mov    (%rax),%edx
mov    0x2e2f(%rip),%rax
mov    (%rax),%ecx
mov    -0x38(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,(%rax)
jne    11ee <open_and_mmap_file+0xae>
jmp    120e <open_and_mmap_file+0xce>
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    122a <open_and_mmap_file+0xea>
xor    %eax,%eax
mov    0x2db1(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x34(%rbp)
mov    -0x38(%rbp),%edi
call   1040 <close@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_and_mmap_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     11b1 <open_and_mmap_file+0x71>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1060 <fstat@plt>
test   %rax,%rax
js     11be <open_and_mmap_file+0x7e>
mov    (%rsp),%r13
mov    0x2e76(%rip),%rax
mov    (%rax),%edx
mov    0x2e5d(%rip),%rax
mov    (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    %r13,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,(%r12)
cmp    $0xffffffffffffffff,%rax
je     11be <open_and_mmap_file+0x7e>
mov    %r13,(%r14)
mov    %ebp,(%r15)
jmp    11d0 <open_and_mmap_file+0x90>
mov    0x2e10(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
jmp    11d0 <open_and_mmap_file+0x90>
mov    0x2e03(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@errno = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
define i32 @open_and_mmap_file(i8* %0, i8** %1, i32* %2, i64* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i32*
%9 = alloca i64*
%10 = alloca %struct.stat
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %6
store i8** %1, i8*** %7
store i32* %2, i32** %8
store i64* %3, i64** %9
%13 = load i8*, i8** %6
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %12
%16 = load i32, i32* %12
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %4
%19 = load i32, i32* @errno
%20 = sub nsw i32 0, %19
store i32 %20, i32* %5
br label %50
21: ; preds = %4
%22 = load i32, i32* %12
%23 = call i64 @fstat(i32 %22, %struct.stat* %10)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %21
br label %44
26: ; preds = %21
%27 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%28 = load i64, i64* %27
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @MAP_SHARED
%31 = load i32, i32* %12
%32 = call i8* @mmap(i32* null, i64 %28, i32 %29, i32 %30, i32 %31, i32 0)
%33 = load i8**, i8*** %7
store i8* %32, i8** %33
%34 = load i8**, i8*** %7
%35 = load i8*, i8** %34
%36 = icmp eq i8* %35, inttoptr (i64 -1 to i8*)
br i1 %36, label %37, label %38
37: ; preds = %26
br label %44
38: ; preds = %26
%39 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%40 = load i64, i64* %39
%41 = load i64*, i64** %9
store i64 %40, i64* %41
%42 = load i32, i32* %12
%43 = load i32*, i32** %8
store i32 %42, i32* %43
store i32 0, i32* %5
br label %50
44: ; preds = %37, %25
%45 = load i32, i32* @errno
%46 = sub nsw i32 0, %45
store i32 %46, i32* %11
%47 = load i32, i32* %12
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* %11
store i32 %49, i32* %5
br label %50
50: ; preds = %44, %38, %18
%51 = load i32, i32* %5
ret i32 %51
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@errno = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
define i32 @open_and_mmap_file(i8* nocapture %0, i8** nocapture writeonly %1, i32* nocapture writeonly %2, i64* nocapture writeonly %3) {
%5 = alloca %struct.stat
%6 = bitcast %struct.stat* %5 to i8*
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %4
%11 = load i32, i32* @errno
%12 = sub nsw i32 0, %11
br label %28
13: ; preds = %4
%14 = call i64 @fstat(i32 %8, %struct.stat* %5)
%15 = icmp slt i64 %14, 0
br i1 %15, label %24, label %16
16: ; preds = %13
%17 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%18 = load i64, i64* %17
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @MAP_SHARED
%21 = tail call i8* @mmap(i32* null, i64 %18, i32 %19, i32 %20, i32 %8, i32 0)
store i8* %21, i8** %1
%22 = icmp eq i8* %21, inttoptr (i64 -1 to i8*)
br i1 %22, label %24, label %23
23: ; preds = %16
store i64 %18, i64* %3
store i32 %8, i32* %2
br label %28
24: ; preds = %16, %13
%25 = load i32, i32* @errno
%26 = sub nsw i32 0, %25
%27 = tail call i32 @close(i32 %8)
br label %28
28: ; preds = %24, %23, %10
%29 = phi i32 [ %12, %10 ], [ %26, %24 ], [ 0, %23 ]
ret i32 %29
}