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

/* Variables and functions */
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(void *, int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int read_file_to_buf(char *name, void *buf, ssize_t buflen) {
  FILE *f;
  size_t done;
  int ret = -1;
  f = fopen(name, "r");
  if (f == NULL)
    goto out;
  errno = 0;
  done = fread(buf, buflen, 1, f);
  if (done != 1)
    goto close;
  ret = 0;
close:
  fclose(f);
out:
  return ret;
}


x86 assembly (clang, -O0):
<read_file_to_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xeab(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    116e <read_file_to_buf+0x3e>
jmp    11b5 <read_file_to_buf+0x85>
mov    0x2e6b(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x1,-0x28(%rbp)
je     11a5 <read_file_to_buf+0x75>
jmp    11ac <read_file_to_buf+0x7c>
movl   $0x0,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_to_buf>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbp
lea    0xebf(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1185 <read_file_to_buf+0x55>
mov    %rax,%rbx
mov    0x2e8b(%rip),%rax
movq   $0x0,(%rax)
mov    %rbp,%rdi
mov    %r14d,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
xor    %ebp,%ebp
cmp    $0x1,%rax
setne  %bpl
neg    %ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    118a <read_file_to_buf+0x5a>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@errno = global i64 0
define i32 @read_file_to_buf(i8* nocapture %0, i8* nocapture %1, i32 %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
store i64 0, i64* @errno
%7 = tail call i64 @fread(i8* %1, i32 %2, i32 1, i32* %4)
%8 = icmp ne i64 %7, 1
%9 = sext i1 %8 to i32
%10 = tail call i32 @fclose(i32* %4)
br label %11
11: ; preds = %3, %6
%12 = phi i32 [ -1, %3 ], [ %9, %6 ]
ret i32 %12
}