#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 *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int read_once(char *filename, char *buffer, int bufsize) {
  int fp = open(filename, O_RDONLY);
  ssize_t len = 0, r = 0;
  if (!fp) {
    perror("Unable to open file");
    return -1;
  }
  while ((r = read(fp, buffer, bufsize - len)) > 0)
    len += r;
  return len;
}


x86 assembly (clang, -O0):
<read_once>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e9a(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1188 <read_once+0x58>
lea    0xe89(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bf <read_once+0x8f>
jmp    118d <read_once+0x5d>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
sub    -0x24(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jle    11b9 <read_once+0x89>
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    118d <read_once+0x5d>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_once>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    0x2ea4(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
test   %eax,%eax
je     1185 <read_once+0x55>
mov    %eax,%ebp
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ecx,%ebx
mov    %r14d,%edx
sub    %ecx,%edx
mov    %ebp,%edi
mov    %r15,%rsi
call   1030 <read@plt>
lea    (%rax,%rbx,1),%ecx
test   %eax,%eax
jg     1160 <read_once+0x30>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe74(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1178 <read_once+0x48>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [20 x i8] c"Unable to open file\00"
define i32 @read_once(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i8*, i8** %5
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
%14 = load i32, i32* %8
%15 = icmp ne i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %3
%17 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %33
18: ; preds = %3
br label %19
19: ; preds = %27, %18
%20 = load i32, i32* %8
%21 = load i8*, i8** %6
%22 = load i32, i32* %7
%23 = load i32, i32* %9
%24 = sub nsw i32 %22, %23
%25 = call i32 @read(i32 %20, i8* %21, i32 %24)
store i32 %25, i32* %10
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %19
%28 = load i32, i32* %10
%29 = load i32, i32* %9
%30 = add nsw i32 %29, %28
store i32 %30, i32* %9
br label %19
31: ; preds = %19
%32 = load i32, i32* %9
store i32 %32, i32* %4
br label %33
33: ; preds = %31, %16
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [20 x i8] c"Unable to open file\00"
define i32 @read_once(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %15
9: ; preds = %3, %9
%10 = phi i32 [ %14, %9 ], [ 0, %3 ]
%11 = sub nsw i32 %2, %10
%12 = tail call i32 @read(i32 %5, i8* %1, i32 %11)
%13 = icmp sgt i32 %12, 0
%14 = add nsw i32 %12, %10
br i1 %13, label %9, label %15
15: ; preds = %9, %7
%16 = phi i32 [ -1, %7 ], [ %10, %9 ]
ret i32 %16
}