#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*/ close(int);
int open(char const *, int /*<<< orphan*/);
int read_file(int, char **);
int read_file_to_end(const char *name, char **buffer) {
  *buffer = NULL;
  int fd = open(name, O_RDONLY);
  if (fd < 0)
    return -1;
  int length = read_file(fd, buffer);
  if (length < 0)
    return -1;
  close(fd);
  return length;
}


x86 assembly (clang, -O0):
<read_file_to_end>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1176 <read_file_to_end+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a9 <read_file_to_end+0x79>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1040 <read_file@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    119b <read_file_to_end+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a9 <read_file_to_end+0x79>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_to_end>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
movq   $0x0,(%rsi)
mov    0x2ea3(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     1172 <read_file_to_end+0x42>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
call   1040 <read_file@plt>
test   %eax,%eax
js     1172 <read_file_to_end+0x42>
mov    %eax,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
mov    %ebx,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i32 @read_file_to_end(i8* nocapture %0, i8** %1) {
store i8* null, i8** %1
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = tail call i32 @read_file(i32 %4, i8** %1)
%8 = icmp slt i32 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call i32 @close(i32 %4)
br label %11
11: ; preds = %9, %6, %2
%12 = phi i32 [ -1, %2 ], [ %7, %9 ], [ -1, %6 ]
ret i32 %12
}