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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char const *, int /*<<< orphan*/);
size_t read(int, void *, int);
int read_to_buf(const char *filename, void *buf) {
  int fd;
  ssize_t 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)
movq   $0xffffffffffffffff,-0x20(%rbp)
ff
mov    -0x8(%rbp),%rdi
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1184 <read_to_buf+0x54>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x3ff,%edx
call   1040 <read@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    11a4 <read_to_buf+0x74>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11af <read_to_buf+0x7f>
xor    %eax,%eax
mov    %rax,-0x30(%rbp)
jmp    11af <read_to_buf+0x7f>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_to_buf>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    0x2eaa(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     1166 <read_to_buf+0x36>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x3ff,%edx
call   1040 <read@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1030 <close@plt>
jmp    116d <read_to_buf+0x3d>
mov    $0xffffffffffffffff,%rbx
movb   $0x0,(%r14,%rbx,1)
mov    %ebx,%eax
pop    %rbx
pop    %r14
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 i64
store i8* %0, i8** %3
store i8* %1, i8** %4
store i64 -1, i64* %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 i64 @read(i32 %13, i8* %14, i32 1023)
store i64 %15, i64* %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 i64, i64* %6
%21 = icmp ugt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = load i64, i64* %6
br label %25
24: ; preds = %18
br label %25
25: ; preds = %24, %22
%26 = phi i64 [ %23, %22 ], [ 0, %24 ]
%27 = getelementptr inbounds i8, i8* %19, i64 %26
store i8 0, i8* %27
%28 = load i64, i64* %6
%29 = trunc i64 %28 to i32
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 %9
6: ; preds = %2
%7 = tail call i64 @read(i32 %4, i8* %1, i32 1023)
%8 = tail call i32 @close(i32 %4)
br label %9
9: ; preds = %6, %2
%10 = phi i64 [ %7, %6 ], [ -1, %2 ]
%11 = getelementptr inbounds i8, i8* %1, i64 %10
store i8 0, i8* %11
%12 = trunc i64 %10 to i32
ret i32 %12
}