#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 {
  unsigned long long st_size;
};

/* Variables and functions */
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
unsigned char *mmap(int /*<<< orphan*/ *, unsigned long long,
                    int /*<<< orphan*/, int /*<<< orphan*/, int,
                    int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int stat(char const *, struct stat *);
const unsigned char *gt4_mmap(const char *filename, unsigned long long *size) {
  struct stat st;
  int status, handle;
  const unsigned char *data;
  status = stat(filename, &st);
  if (status < 0) {
    perror("gt4_mmap (stat)");
    return NULL;
  }
  handle = open(filename, O_RDONLY);
  if (handle < 0) {
    perror("gt4_mmap (open)");
    return NULL;
  }
  data = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, handle, 0);
  if (data == (const unsigned char *)-1) {
    perror("gt4_mmap (mmap)");
    return NULL;
  } else {
    *size = st.st_size;
  }
  close(handle);
  return data;
}


x86 assembly (clang, -O0):
<gt4_mmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1193 <gt4_mmap+0x43>
lea    0xe7f(%rip),%rdi
call   1070 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    123a <gt4_mmap+0xea>
mov    -0x10(%rbp),%rdi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11cb <gt4_mmap+0x7b>
lea    0xe57(%rip),%rdi
call   1070 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    123a <gt4_mmap+0xea>
mov    -0x20(%rbp),%rsi
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
mov    0x2df1(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x30(%rbp)
jne    121f <gt4_mmap+0xcf>
lea    0xe13(%rip),%rdi
call   1070 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    123a <gt4_mmap+0xea>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
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):
<gt4_mmap>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1050 <stat@plt>
test   %eax,%eax
js     11c7 <gt4_mmap+0x77>
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11d0 <gt4_mmap+0x80>
mov    %eax,%ebp
mov    (%rsp),%r15
mov    0x2e6d(%rip),%rax
mov    (%rax),%edx
mov    0x2e3c(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     11d9 <gt4_mmap+0x89>
mov    %rax,%rbx
mov    %r15,(%r14)
mov    %ebp,%edi
call   1040 <close@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe32(%rip),%rdi
jmp    11e0 <gt4_mmap+0x90>
lea    0xe39(%rip),%rdi
jmp    11e0 <gt4_mmap+0x90>
lea    0xe40(%rip),%rdi
call   1070 <perror@plt>
xor    %ebx,%ebx
jmp    11b9 <gt4_mmap+0x69>

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [16 x i8] c"gt4_mmap (stat)\00"
@O_RDONLY = global i32 0
@.str.1 = [16 x i8] c"gt4_mmap (open)\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.2 = [16 x i8] c"gt4_mmap (mmap)\00"
define i8* @gt4_mmap(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %4
store i64* %1, i64** %5
%10 = load i8*, i8** %4
%11 = call i32 @stat(i8* %10, %struct.stat* %6)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %3
br label %43
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %3
br label %43
24: ; preds = %16
%25 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%26 = load i64, i64* %25
%27 = load i32, i32* @PROT_READ
%28 = load i32, i32* @MAP_PRIVATE
%29 = load i32, i32* %8
%30 = call i8* @mmap(i32* null, i64 %26, i32 %27, i32 %28, i32 %29, i32 0)
store i8* %30, i8** %9
%31 = load i8*, i8** %9
%32 = icmp eq i8* %31, inttoptr (i64 -1 to i8*)
br i1 %32, label %33, label %35
33: ; preds = %24
%34 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %3
br label %43
35: ; preds = %24
%36 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%37 = load i64, i64* %36
%38 = load i64*, i64** %5
store i64 %37, i64* %38
br label %39
39: ; preds = %35
%40 = load i32, i32* %8
%41 = call i32 @close(i32 %40)
%42 = load i8*, i8** %9
store i8* %42, i8** %3
br label %43
43: ; preds = %39, %33, %22, %14
%44 = load i8*, i8** %3
ret i8* %44
}

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