#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 */
int /*<<< orphan*/ err(int, char *);
scalar_t__ read(int /*<<< orphan*/, char *, size_t);
void read_all(char *buf, size_t sz) {
  while (sz) {
    ssize_t cnt = read(0, buf, sz);
    if (cnt <= 0) {
      err(1, "read");
    }
    sz -= cnt;
    buf += cnt;
  }
}


x86 assembly (clang, -O0):
<read_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     118a <read_all+0x6a>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
xor    %edi,%edi
call   1030 <read@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jg     116a <read_all+0x4a>
mov    $0x1,%edi
lea    0xe9b(%rip),%rsi
call   1040 <err@plt>
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1130 <read_all+0x10>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_all>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
je     116c <read_all+0x4c>
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xec6(%rip),%r15
jmp    1148 <read_all+0x28>
nopl   0x0(%rax)
add    %r12,%rbx
sub    %r12,%r14
je     116c <read_all+0x4c>
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <read@plt>
mov    %rax,%r12
test   %rax,%rax
jg     1140 <read_all+0x20>
mov    $0x1,%edi
mov    %r15,%rsi
call   1040 <err@plt>
jmp    1140 <read_all+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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