#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 scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ errno;
scalar_t__ read(int const, char *, size_t const);
ssize_t _fill_buffer(const int fd, char *buffer, const size_t buffer_size,
                     size_t *size_read) {
  ssize_t size;
  unsigned int retry = 0;
  *size_read = 0;
  do {
    size = read(fd, buffer + *size_read, buffer_size - *size_read);
    if (size < 0) {
      retry++;
      if (retry >= 10)
        break;
      if (errno == EINTR || errno == EAGAIN) {
        continue;
      } else
        break;
    }
    retry = 0;
    *size_read += (size_t)size;
  } while (size && *size_read < buffer_size);
  return size;
}


x86 assembly (clang, -O0):
<_fill_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
add    (%rax),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rax
sub    (%rax),%rdx
call   1030 <read@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    11bc <_fill_buffer+0xac>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0xa,-0x2c(%rbp)
jb     117e <_fill_buffer+0x6e>
jmp    11fd <_fill_buffer+0xed>
mov    0x2e5b(%rip),%rax
mov    (%rax),%rax
mov    0x2e49(%rip),%rcx
cmp    (%rcx),%rax
je     11b2 <_fill_buffer+0xa2>
mov    0x2e41(%rip),%rax
mov    (%rax),%rax
mov    0x2e1f(%rip),%rcx
cmp    (%rcx),%rax
jne    11b7 <_fill_buffer+0xa7>
jmp    11d1 <_fill_buffer+0xc1>
jmp    11fd <_fill_buffer+0xed>
movl   $0x0,-0x2c(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
xor    %eax,%eax
cmpq   $0x0,-0x28(%rbp)
mov    %al,-0x2d(%rbp)
je     11f2 <_fill_buffer+0xe2>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
cmp    -0x18(%rbp),%rax
setb   %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    1139 <_fill_buffer+0x29>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_fill_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %edi,%ebp
movq   $0x0,(%rcx)
xor    %ecx,%ecx
mov    0x2ea2(%rip),%r13
xor    %r12d,%r12d
jmp    1148 <_fill_buffer+0x38>
nopl   0x0(%rax,%rax,1)
mov    (%r14),%rcx
cmp    %rbx,%rcx
jae    11a6 <_fill_buffer+0x96>
lea    (%r15,%rcx,1),%rsi
mov    %rbx,%rdx
sub    %rcx,%rdx
mov    %ebp,%edi
call   1030 <read@plt>
test   %rax,%rax
js     1180 <_fill_buffer+0x70>
mov    (%r14),%rcx
add    %rax,%rcx
mov    %rcx,(%r14)
xor    %r12d,%r12d
test   %rax,%rax
jne    1143 <_fill_buffer+0x33>
jmp    11a6 <_fill_buffer+0x96>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%r12d
cmp    $0x9,%r12d
ja     11a6 <_fill_buffer+0x96>
mov    0x2e4f(%rip),%rcx
mov    (%rcx),%rcx
cmp    0x0(%r13),%rcx
je     1140 <_fill_buffer+0x30>
mov    0x2e27(%rip),%rdx
cmp    (%rdx),%rcx
je     1140 <_fill_buffer+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
define i64 @_fill_buffer(i32 %0, i8* %1, i64 %2, i64* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64*
%9 = alloca i64
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64* %3, i64** %8
store i32 0, i32* %10
%11 = load i64*, i64** %8
store i64 0, i64* %11
br label %12
12: ; preds = %54, %4
%13 = load i32, i32* %5
%14 = load i8*, i8** %6
%15 = load i64*, i64** %8
%16 = load i64, i64* %15
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i64, i64* %7
%19 = load i64*, i64** %8
%20 = load i64, i64* %19
%21 = sub i64 %18, %20
%22 = call i64 @read(i32 %13, i8* %17, i64 %21)
store i64 %22, i64* %9
%23 = load i64, i64* %9
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %41
25: ; preds = %12
%26 = load i32, i32* %10
%27 = add i32 %26, 1
store i32 %27, i32* %10
%28 = load i32, i32* %10
%29 = icmp uge i32 %28, 10
br i1 %29, label %30, label %31
30: ; preds = %25
br label %56
31: ; preds = %25
%32 = load i64, i64* @errno
%33 = load i64, i64* @EINTR
%34 = icmp eq i64 %32, %33
br i1 %34, label %39, label %35
35: ; preds = %31
%36 = load i64, i64* @errno
%37 = load i64, i64* @EAGAIN
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %35, %31
br label %46
40: ; preds = %35
br label %56
41: ; preds = %12
store i32 0, i32* %10
%42 = load i64, i64* %9
%43 = load i64*, i64** %8
%44 = load i64, i64* %43
%45 = add i64 %44, %42
store i64 %45, i64* %43
br label %46
46: ; preds = %41, %39
%47 = load i64, i64* %9
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %46
%50 = load i64*, i64** %8
%51 = load i64, i64* %50
%52 = load i64, i64* %7
%53 = icmp ult i64 %51, %52
br label %54
54: ; preds = %49, %46
%55 = phi i1 [ false, %46 ], [ %53, %49 ]
br i1 %55, label %12, label %56
56: ; preds = %54, %40, %30
%57 = load i64, i64* %9
ret i64 %57
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
define i64 @_fill_buffer(i32 %0, i8* nocapture %1, i64 %2, i64* nocapture %3) {
store i64 0, i64* %3
br label %5
5: ; preds = %28, %4
%6 = phi i64 [ 0, %4 ], [ %29, %28 ]
%7 = phi i32 [ 0, %4 ], [ %30, %28 ]
%8 = getelementptr inbounds i8, i8* %1, i64 %6
%9 = sub i64 %2, %6
%10 = tail call i64 @read(i32 %0, i8* %8, i64 %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %24
12: ; preds = %5
%13 = add i32 %7, 1
%14 = icmp ugt i32 %13, 9
br i1 %14, label %32, label %15
15: ; preds = %12
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINTR
%18 = icmp eq i64 %16, %17
%19 = load i64, i64* @EAGAIN
%20 = icmp eq i64 %16, %19
%21 = select i1 %18, i1 true, i1 %20
br i1 %21, label %22, label %32
22: ; preds = %15
%23 = load i64, i64* %3
br label %28
24: ; preds = %5
%25 = load i64, i64* %3
%26 = add i64 %25, %10
store i64 %26, i64* %3
%27 = icmp eq i64 %10, 0
br i1 %27, label %32, label %28
28: ; preds = %22, %24
%29 = phi i64 [ %26, %24 ], [ %23, %22 ]
%30 = phi i32 [ 0, %24 ], [ %13, %22 ]
%31 = icmp ult i64 %29, %2
br i1 %31, label %5, label %32
32: ; preds = %15, %24, %12, %28
ret i64 %10
}