#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(int, void *, int);
int read_to_buf(const char *filename, void *buf) {
  int fd;
  int ret = -1;
  fd = open(filename, O_RDONLY);
  if (fd >= 0) {
    ret = read(fd, buf, 1024 - 1);
    close(fd);
  }
  ((char *)buf)[ret > 0 ? ret : 0] = '\0';
  return ret;
}


x86 assembly (clang, -O0):
<read_to_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1182 <read_to_buf+0x52>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x3ff,%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    119f <read_to_buf+0x6f>
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    11a9 <read_to_buf+0x79>
xor    %eax,%eax
mov    %eax,-0x24(%rbp)
jmp    11a9 <read_to_buf+0x79>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_to_buf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    0x2ea7(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
xor    %r15d,%r15d
test   %eax,%eax
js     1171 <read_to_buf+0x41>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x3ff,%edx
call   1040 <read@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1030 <close@plt>
test   %ebp,%ebp
cmovns %ebp,%r15d
jmp    1176 <read_to_buf+0x46>
mov    $0xffffffff,%ebp
movb   $0x0,(%r14,%r15,1)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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