#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 unix_read(int, char *, size_t);
int proc_read(const char *filename, char *buff, size_t buffsize) {
  int len = 0;
  int fd = open(filename, O_RDONLY);
  if (fd >= 0) {
    len = unix_read(fd, buff, buffsize - 1);
    close(fd);
  }
  buff[len > 0 ? len : 0] = 0;
  return len;
}


x86 assembly (clang, -O0):
<proc_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jl     1189 <proc_read+0x59>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
call   1030 <unix_read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11a6 <proc_read+0x76>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    11b0 <proc_read+0x80>
xor    %eax,%eax
mov    %eax,-0x2c(%rbp)
jmp    11b0 <proc_read+0x80>
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proc_read>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    0x2ea7(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     1171 <proc_read+0x41>
mov    %eax,%ebp
add    $0xffffffffffffffff,%rbx
mov    %eax,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <unix_read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
test   %ebx,%ebx
cmovns %ebx,%eax
jmp    1175 <proc_read+0x45>
xor    %ebx,%ebx
xor    %eax,%eax
movb   $0x0,(%r14,%rax,1)
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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