#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int open(char const *, int /*<<< orphan*/);
int read(int, char *, size_t);
ssize_t fconf_load2buf(const char *filename, char *buf, size_t len) {
  int fd = 0;
  ssize_t bytes_read = 0;
  if ((fd = open(filename, O_RDONLY)) == -1) {
    return -1;
  }
  bytes_read = read(fd, buf, len);
  if (bytes_read == -1)
    return -1;
  else if (bytes_read > 0) {
    buf[bytes_read] = '\0';
  }
  return bytes_read;
}


x86 assembly (clang, -O0):
<fconf_load2buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    116c <fconf_load2buf+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b6 <fconf_load2buf+0x96>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1195 <fconf_load2buf+0x75>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b6 <fconf_load2buf+0x96>
cmpl   $0x0,-0x28(%rbp)
jle    11ab <fconf_load2buf+0x8b>
mov    -0x18(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11b0 <fconf_load2buf+0x90>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fconf_load2buf>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    0x2eb7(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     1161 <fconf_load2buf+0x41>
mov    %eax,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
je     1161 <fconf_load2buf+0x41>
test   %eax,%eax
jle    115f <fconf_load2buf+0x3f>
mov    %eax,%ecx
movb   $0x0,(%r14,%rcx,1)
mov    %eax,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i32 @fconf_load2buf(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* %8
store i32 0, i32* %9
%10 = load i8*, i8** %5
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %8
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %34
15: ; preds = %3
%16 = load i32, i32* %8
%17 = load i8*, i8** %6
%18 = load i64, i64* %7
%19 = call i32 @read(i32 %16, i8* %17, i64 %18)
store i32 %19, i32* %9
%20 = load i32, i32* %9
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %15
store i32 -1, i32* %4
br label %34
23: ; preds = %15
%24 = load i32, i32* %9
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %23
%27 = load i8*, i8** %6
%28 = load i32, i32* %9
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 0, i8* %30
br label %31
31: ; preds = %26, %23
br label %32
32: ; preds = %31
%33 = load i32, i32* %9
store i32 %33, i32* %4
br label %34
34: ; preds = %32, %22, %14
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i32 @fconf_load2buf(i8* nocapture %0, i8* nocapture %1, i64 %2) {
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %15, label %7
7: ; preds = %3
%8 = tail call i32 @read(i32 %5, i8* %1, i64 %2)
%9 = icmp eq i32 %8, -1
br i1 %9, label %15, label %10
10: ; preds = %7
%11 = icmp sgt i32 %8, 0
br i1 %11, label %12, label %15
12: ; preds = %10
%13 = zext i32 %8 to i64
%14 = getelementptr inbounds i8, i8* %1, i64 %13
store i8 0, i8* %14
br label %15
15: ; preds = %12, %10, %7, %3
%16 = phi i32 [ -1, %3 ], [ -1, %7 ], [ %8, %10 ], [ %8, %12 ]
ret i32 %16
}