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

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/, unsigned long, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
void *grab_fd(int fd, unsigned long *size) {
  struct stat st;
  void *map;
  fstat(fd, &st);
  *size = st.st_size;
  map = mmap(0, *size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  if (map == MAP_FAILED)
    map = NULL;
  return map;
}


x86 assembly (clang, -O0):
<grab_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1040 <fstat@plt>
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    0x2ea4(%rip),%rax
mov    (%rax),%edx
mov    0x2e7b(%rip),%rax
or     (%rax),%edx
mov    0x2e6a(%rip),%rax
mov    (%rax),%ecx
mov    -0x4(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e42(%rip),%rcx
cmp    (%rcx),%rax
jne    1197 <grab_fd+0x77>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<grab_fd>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsp,%rsi
call   1040 <fstat@plt>
mov    (%rsp),%rsi
mov    %rsi,(%rbx)
mov    0x2eba(%rip),%rax
mov    0x2e93(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e80(%rip),%rax
mov    (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2e60(%rip),%rcx
cmp    (%rcx),%rax
cmove  %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
define i8* @grab_fd(i32 %0, i64* %1) {
%3 = alloca i32
%4 = alloca i64*
%5 = alloca %struct.stat
%6 = alloca i8*
store i32 %0, i32* %3
store i64* %1, i64** %4
%7 = load i32, i32* %3
%8 = call i32 @fstat(i32 %7, %struct.stat* %5)
%9 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%10 = load i64, i64* %9
%11 = load i64*, i64** %4
store i64 %10, i64* %11
%12 = load i64*, i64** %4
%13 = load i64, i64* %12
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @PROT_WRITE
%16 = or i32 %14, %15
%17 = load i32, i32* @MAP_PRIVATE
%18 = load i32, i32* %3
%19 = call i8* @mmap(i32 0, i64 %13, i32 %16, i32 %17, i32 %18, i32 0)
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = load i8*, i8** @MAP_FAILED
%22 = icmp eq i8* %20, %21
br i1 %22, label %23, label %24
23: ; preds = %2
store i8* null, i8** %6
br label %24
24: ; preds = %23, %2
%25 = load i8*, i8** %6
ret i8* %25
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
define i8* @grab_fd(i32 %0, i64* nocapture writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i32 @fstat(i32 %0, %struct.stat* %3)
%6 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%7 = load i64, i64* %6
store i64 %7, i64* %1
%8 = load i32, i32* @PROT_READ
%9 = load i32, i32* @PROT_WRITE
%10 = or i32 %9, %8
%11 = load i32, i32* @MAP_PRIVATE
%12 = tail call i8* @mmap(i32 0, i64 %7, i32 %10, i32 %11, i32 %0, i32 0)
%13 = load i8*, i8** @MAP_FAILED
%14 = icmp eq i8* %12, %13
%15 = select i1 %14, i8* null, i8* %12
ret i8* %15
}