#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 {
  int /*<<< orphan*/ st_size;
};
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ assert(int);
int fstat(int, struct stat *);
char *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
char *do_mmap(char *path, int *fd, off_t *size) {
  int err;
  struct stat st;
  char *p;
  *fd = open(path, O_RDWR);
  assert(*fd >= 0);
  err = fstat(*fd, &st);
  assert(!err);
  *size = st.st_size;
  p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, *fd, 0);
  assert(p != MAP_FAILED);
  return p;
}


x86 assembly (clang, -O0):
<do_mmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1060 <fstat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%esi
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
mov    0x2e25(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    (%rax),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2de1(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_mmap>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    0x2e87(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,(%rbx)
not    %eax
shr    $0x1f,%eax
mov    %eax,%edi
call   1040 <assert@plt>
mov    (%rbx),%edi
mov    %rsp,%rsi
call   1060 <fstat@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1040 <assert@plt>
mov    (%rsp),%esi
mov    %esi,(%r14)
mov    0x2e6e(%rip),%rax
mov    (%rax),%edx
mov    0x2e55(%rip),%rax
mov    (%rax),%ecx
mov    (%rbx),%r8d
xor    %edi,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e1c(%rip),%rax
xor    %edi,%edi
cmp    (%rax),%rbx
setne  %dil
call   1040 <assert@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
define i8* @do_mmap(i8* %0, i32* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca i8*
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32* %2, i32** %6
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* %10, i32 %11)
%13 = load i32*, i32** %5
store i32 %12, i32* %13
%14 = load i32*, i32** %5
%15 = load i32, i32* %14
%16 = icmp sge i32 %15, 0
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = load i32*, i32** %5
%20 = load i32, i32* %19
%21 = call i32 @fstat(i32 %20, %struct.stat* %8)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp ne i32 %22, 0
%24 = xor i1 %23, true
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
%27 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%28 = load i32, i32* %27
%29 = load i32*, i32** %6
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%31 = load i32, i32* %30
%32 = load i32, i32* @PROT_READ
%33 = load i32, i32* @MAP_SHARED
%34 = load i32*, i32** %5
%35 = load i32, i32* %34
%36 = call i8* @mmap(i32* null, i32 %31, i32 %32, i32 %33, i32 %35, i32 0)
store i8* %36, i8** %9
%37 = load i8*, i8** %9
%38 = load i8*, i8** @MAP_FAILED
%39 = icmp ne i8* %37, %38
%40 = zext i1 %39 to i32
%41 = call i32 @assert(i32 %40)
%42 = load i8*, i8** %9
ret i8* %42
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
define i8* @do_mmap(i8* nocapture %0, i32* nocapture %1, i32* nocapture writeonly %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = load i32, i32* @O_RDWR
%7 = tail call i32 @open(i8* %0, i32 %6)
store i32 %7, i32* %1
%8 = xor i32 %7, -1
%9 = lshr i32 %8, 31
%10 = tail call i32 @assert(i32 %9)
%11 = load i32, i32* %1
%12 = call i32 @fstat(i32 %11, %struct.stat* %4)
%13 = icmp eq i32 %12, 0
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%17 = load i32, i32* %16
store i32 %17, i32* %2
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @MAP_SHARED
%20 = load i32, i32* %1
%21 = tail call i8* @mmap(i32* null, i32 %17, i32 %18, i32 %19, i32 %20, i32 0)
%22 = load i8*, i8** @MAP_FAILED
%23 = icmp ne i8* %21, %22
%24 = zext i1 %23 to i32
%25 = tail call i32 @assert(i32 %24)
ret i8* %21
}