#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 /*<<< orphan*/ close(int);
int open(char const *, int /*<<< orphan*/);
int read(int, char *const, size_t const);
int read_buf(const char *path, char *const buffer, const size_t sz) {
  int fd;
  ssize_t ret;
  if ((fd = open(path, O_RDONLY)) < 0)
    return -1;
  ret = read(fd, buffer, sz);
  (void)close(fd);
  if ((ret < 1) || (ret > (ssize_t)sz))
    return -1;
  buffer[ret - 1] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<read_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    116e <read_buf+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c6 <read_buf+0x96>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
cmpl   $0x1,-0x28(%rbp)
jl     11a2 <read_buf+0x72>
mov    -0x28(%rbp),%eax
mov    -0x20(%rbp),%rcx
cmp    %ecx,%eax
jle    11ae <read_buf+0x7e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c6 <read_buf+0x96>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_buf>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    0x2ea3(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    $0xffffffff,%r12d
test   %eax,%eax
js     1182 <read_buf+0x52>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
test   %ebx,%ebx
jle    1182 <read_buf+0x52>
cmp    %r15d,%ebx
jg     1182 <read_buf+0x52>
add    $0xffffffff,%ebx
movb   $0x0,(%r14,%rbx,1)
xor    %r12d,%r12d
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i32 @read_buf(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
%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 slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %36
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* %8
%21 = call i32 @close(i32 %20)
%22 = load i32, i32* %9
%23 = icmp slt i32 %22, 1
br i1 %23, label %29, label %24
24: ; preds = %15
%25 = load i32, i32* %9
%26 = load i64, i64* %7
%27 = trunc i64 %26 to i32
%28 = icmp sgt i32 %25, %27
br i1 %28, label %29, label %30
29: ; preds = %24, %15
store i32 -1, i32* %4
br label %36
30: ; preds = %24
%31 = load i8*, i8** %6
%32 = load i32, i32* %9
%33 = sub nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %31, i64 %34
store i8 0, i8* %35
store i32 0, i32* %4
br label %36
36: ; preds = %30, %29, %14
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i32 @read_buf(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 slt i32 %5, 0
br i1 %6, label %18, label %7
7: ; preds = %3
%8 = tail call i32 @read(i32 %5, i8* %1, i64 %2)
%9 = tail call i32 @close(i32 %5)
%10 = icmp slt i32 %8, 1
%11 = trunc i64 %2 to i32
%12 = icmp sgt i32 %8, %11
%13 = or i1 %10, %12
br i1 %13, label %18, label %14
14: ; preds = %7
%15 = add nsw i32 %8, -1
%16 = zext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %1, i64 %16
store i8 0, i8* %17
br label %18
18: ; preds = %7, %3, %14
%19 = phi i32 [ 0, %14 ], [ -1, %3 ], [ -1, %7 ]
ret i32 %19
}