#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*/ error(char *);
int read(int, char *, size_t);
void read_all(int fd, char *buffer, size_t bytes) {
  size_t n = 0;
  while (n < bytes) {
    int next = read(fd, buffer + n, bytes - n);
    if (next <= 0)
      error("read failed");
    n += next;
  }
}


x86 assembly (clang, -O0):
<read_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    118b <read_all+0x6b>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     117a <read_all+0x5a>
lea    0xe8b(%rip),%rdi
call   1040 <error@plt>
movslq -0x24(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    113b <read_all+0x1b>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_all>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     117d <read_all+0x5d>
mov    %rdx,%r13
mov    %rsi,%r14
mov    %edi,%r12d
xor    %ebx,%ebx
lea    0xebe(%rip),%r15
jmp    115b <read_all+0x3b>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movslq %ebp,%rax
add    %rax,%rbx
cmp    %r13,%rbx
jae    117d <read_all+0x5d>
lea    (%r14,%rbx,1),%rsi
mov    %r13,%rdx
sub    %rbx,%rdx
mov    %r12d,%edi
call   1030 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     1150 <read_all+0x30>
mov    %r15,%rdi
call   1040 <error@plt>
jmp    1150 <read_all+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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