#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*/ O_RDONLY;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ close(int);
int dup(int /*<<< orphan*/);
void *grab_fd(int, size_t *);
int open(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
void *grab_file(const char *filename, size_t *size) {
  int fd;
  void *buffer;
  if (!filename)
    fd = dup(STDIN_FILENO);
  else
    fd = open(filename, O_RDONLY, 0);
  if (fd < 0)
    return NULL;
  buffer = grab_fd(fd, size);
  close(fd);
  return buffer;
}


x86 assembly (clang, -O0):
<grab_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1171 <grab_file+0x31>
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
call   1030 <dup@plt>
mov    %eax,-0x1c(%rbp)
jmp    1188 <grab_file+0x48>
mov    -0x10(%rbp),%rdi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    119f <grab_file+0x5f>
movq   $0x0,-0x8(%rbp)
jmp    11bf <grab_file+0x7f>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1050 <grab_fd@plt>
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<grab_file>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
test   %rdi,%rdi
je     115d <grab_file+0x1d>
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <open@plt>
jmp    116b <grab_file+0x2b>
mov    0x2e74(%rip),%rax
mov    (%rax),%edi
call   1030 <dup@plt>
mov    %eax,%ebp
test   %eax,%eax
js     118a <grab_file+0x4a>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1050 <grab_fd@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %rbx,%rax
jmp    118c <grab_file+0x4c>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@O_RDONLY = global i32 0
define i8* @grab_file(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i64* %1, i64** %5
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i32, i32* @STDIN_FILENO
%12 = call i32 @dup(i32 %11)
store i32 %12, i32* %6
br label %17
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* %14, i32 %15, i32 0)
store i32 %16, i32* %6
br label %17
17: ; preds = %13, %10
%18 = load i32, i32* %6
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %17
store i8* null, i8** %3
br label %28
21: ; preds = %17
%22 = load i32, i32* %6
%23 = load i64*, i64** %5
%24 = call i8* @grab_fd(i32 %22, i64* %23)
store i8* %24, i8** %7
%25 = load i32, i32* %6
%26 = call i32 @close(i32 %25)
%27 = load i8*, i8** %7
store i8* %27, i8** %3
br label %28
28: ; preds = %21, %20
%29 = load i8*, i8** %3
ret i8* %29
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@O_RDONLY = global i32 0
define i8* @grab_file(i8* %0, i64* %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i32, i32* @STDIN_FILENO
%6 = tail call i32 @dup(i32 %5)
br label %10
7: ; preds = %2
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @open(i8* %0, i32 %8, i32 0)
br label %10
10: ; preds = %7, %4
%11 = phi i32 [ %9, %7 ], [ %6, %4 ]
%12 = icmp slt i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = tail call i8* @grab_fd(i32 %11, i64* %1)
%15 = tail call i32 @close(i32 %11)
br label %16
16: ; preds = %10, %13
%17 = phi i8* [ %14, %13 ], [ null, %10 ]
ret i8* %17
}