#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 read(int, unsigned char *, int);
int fill_buf(int fd, unsigned char *buf, int record_size) {
  int num_read = 1;
  int total_read = 0;
  while (total_read < record_size && num_read > 0) {
    num_read = read(fd, buf + total_read, 4096);
    total_read = total_read + num_read;
  }
  return (total_read);
}


x86 assembly (clang, -O0):
<fill_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%ecx
xor    %eax,%eax
cmp    -0x14(%rbp),%ecx
mov    %al,-0x1d(%rbp)
jge    114b <fill_buf+0x3b>
cmpl   $0x0,-0x18(%rbp)
setg   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    115b <fill_buf+0x4b>
jmp    1184 <fill_buf+0x74>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    $0x1000,%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1130 <fill_buf+0x20>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fill_buf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    1150 <fill_buf+0x40>
mov    %edx,%r15d
mov    %rsi,%r14
mov    %edi,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
movslq %ebx,%rbx
lea    (%r14,%rbx,1),%rsi
mov    %ebp,%edi
mov    $0x1000,%edx
call   1030 <read@plt>
add    %eax,%ebx
test   %eax,%eax
jle    1152 <fill_buf+0x42>
cmp    %r15d,%ebx
jl     1130 <fill_buf+0x20>
jmp    1152 <fill_buf+0x42>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @fill_buf(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 1, i32* %7
store i32 0, i32* %8
br label %9
9: ; preds = %18, %3
%10 = load i32, i32* %8
%11 = load i32, i32* %6
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i32, i32* %7
%15 = icmp sgt i32 %14, 0
br label %16
16: ; preds = %13, %9
%17 = phi i1 [ false, %9 ], [ %15, %13 ]
br i1 %17, label %18, label %28
18: ; preds = %16
%19 = load i32, i32* %4
%20 = load i8*, i8** %5
%21 = load i32, i32* %8
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = call i32 @read(i32 %19, i8* %23, i32 4096)
store i32 %24, i32* %7
%25 = load i32, i32* %8
%26 = load i32, i32* %7
%27 = add nsw i32 %25, %26
store i32 %27, i32* %8
br label %9
28: ; preds = %16
%29 = load i32, i32* %8
ret i32 %29
}

LLVM IR (-O2):
define i32 @fill_buf(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %14
5: ; preds = %3, %5
%6 = phi i32 [ %10, %5 ], [ 0, %3 ]
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %1, i64 %7
%9 = tail call i32 @read(i32 %0, i8* %8, i32 4096)
%10 = add nsw i32 %9, %6
%11 = icmp slt i32 %10, %2
%12 = icmp sgt i32 %9, 0
%13 = and i1 %12, %11
br i1 %13, label %5, label %14
14: ; preds = %5, %3
%15 = phi i32 [ 0, %3 ], [ %10, %5 ]
ret i32 %15
}