#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*/ perror(char *);
int read(int, char *, int);
ssize_t fill_buf(char *buf, size_t bufsize, int fd) {
  ssize_t this_read = 0, count = 0;
  do {
    this_read = read(fd, buf + count, bufsize - count);
    if (this_read == -1) {
      perror("read");
      return -1;
    }
    count += this_read;
  } while (this_read > 0);
  return count;
}


x86 assembly (clang, -O0):
<fill_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1186 <fill_buf+0x66>
lea    0xe8b(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    119f <fill_buf+0x7f>
mov    -0x20(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     1141 <fill_buf+0x21>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fill_buf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movslq %ebx,%rax
lea    (%rax,%rbp,1),%rsi
mov    %r15d,%edx
sub    %eax,%edx
mov    %r14d,%edi
call   1030 <read@plt>
cmp    $0xffffffff,%eax
je     116c <fill_buf+0x4c>
add    %eax,%ebx
test   %eax,%eax
jg     1140 <fill_buf+0x20>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe8d(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%ebx
jmp    115f <fill_buf+0x3f>

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

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