#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 */
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fread(char *, int, size_t, int /*<<< orphan*/ *);
size_t slurp_file(char *fname, char *buf, size_t len) {
  FILE *fh = fopen(fname, "r");
  if (!fh)
    return -1;
  ssize_t rlen = fread(buf, 1, len, fh);
  if (rlen < 0) {
    fclose(fh);
    return -1;
  }
  fclose(fh);
  return rlen;
}


x86 assembly (clang, -O0):
<slurp_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb1(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1170 <slurp_file+0x40>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11bc <slurp_file+0x8c>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    11ab <slurp_file+0x7b>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11bc <slurp_file+0x8c>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<slurp_file>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
lea    0xebe(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     117d <slurp_file+0x4d>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %r14,%r14
mov    $0xffffffffffffffff,%rax
cmovns %r14,%rax
jmp    1184 <slurp_file+0x54>
mov    $0xffffffffffffffff,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i64 @slurp_file(i8* %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = icmp ne i32* %12, null
br i1 %13, label %15, label %14
14: ; preds = %3
store i64 -1, i64* %4
br label %29
15: ; preds = %3
%16 = load i8*, i8** %6
%17 = load i64, i64* %7
%18 = load i32*, i32** %8
%19 = call i64 @fread(i8* %16, i32 1, i64 %17, i32* %18)
store i64 %19, i64* %9
%20 = load i64, i64* %9
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = load i32*, i32** %8
%24 = call i32 @fclose(i32* %23)
store i64 -1, i64* %4
br label %29
25: ; preds = %15
%26 = load i32*, i32** %8
%27 = call i32 @fclose(i32* %26)
%28 = load i64, i64* %9
store i64 %28, i64* %4
br label %29
29: ; preds = %25, %22, %14
%30 = load i64, i64* %4
ret i64 %30
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i64 @slurp_file(i8* nocapture %0, i8* nocapture %1, i64 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %11, label %6
6: ; preds = %3
%7 = tail call i64 @fread(i8* %1, i32 1, i64 %2, i32* %4)
%8 = tail call i32 @fclose(i32* %4)
%9 = icmp sgt i64 %7, -1
%10 = select i1 %9, i64 %7, i64 -1
br label %11
11: ; preds = %6, %3
%12 = phi i64 [ -1, %3 ], [ %10, %6 ]
ret i64 %12
}