#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*/ perror(char *);
int read(int /*<<< orphan*/, char *, int);
int fill_buffer(char *buffer, int size) {
  int x;
  int n = 0;
  while (n < size) {
    x = read(0, buffer + n, size - n);
    if (x == 0) {
      return 0;
    }
    if (x < 0) {
      perror("read");
      return 0;
    }
    n += x;
  }
  return 1;
}


x86 assembly (clang, -O0):
<fill_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11a3 <fill_buffer+0x83>
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
xor    %edi,%edi
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1173 <fill_buffer+0x53>
movl   $0x0,-0x4(%rbp)
jmp    11aa <fill_buffer+0x8a>
cmpl   $0x0,-0x18(%rbp)
jge    1195 <fill_buffer+0x75>
lea    0xe7c(%rip),%rdi
call   1040 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11aa <fill_buffer+0x8a>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1136 <fill_buffer+0x16>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fill_buffer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %esi,%esi
jle    115e <fill_buffer+0x3e>
mov    %esi,%ebp
mov    %rdi,%r15
xor    %r14d,%r14d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
movslq %ebx,%rax
lea    (%r15,%rax,1),%rsi
mov    %ebp,%edx
sub    %eax,%edx
xor    %edi,%edi
call   1030 <read@plt>
test   %eax,%eax
je     1164 <fill_buffer+0x44>
js     1172 <fill_buffer+0x52>
add    %eax,%ebx
cmp    %ebp,%ebx
jl     1140 <fill_buffer+0x20>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe87(%rip),%rdi
call   1040 <perror@plt>
jmp    1164 <fill_buffer+0x44>

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

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