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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char *, unsigned long);
int read(int, char *, size_t);
ssize_t mb_readall(int fd, char *buf, size_t size, bool continue_on_error) {
  size_t sz = size;
  ssize_t ret;
  for (;;) {
    if ((ret = read(fd, buf, sz)) == -1) {
      printf("fd=%d, buf=%p, sz=%lu\n", fd, buf, (unsigned long)sz);
      perror("mb_readall:read");
      if (!continue_on_error)
        exit(EXIT_FAILURE);
    }
    if (ret < sz) {
      sz -= ret;
      buf += ret;
    } else {
      break;
    }
  }
  return size;
}


x86 assembly (clang, -O0):
<mb_readall>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    11ba <mb_readall+0x7a>
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xe74(%rip),%rdi
call   1030 <printf@plt>
lea    0xe7f(%rip),%rdi
call   1050 <perror@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    11b5 <mb_readall+0x75>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    11ba <mb_readall+0x7a>
movslq -0x2c(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    11ed <mb_readall+0xad>
movslq -0x2c(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    11f2 <mb_readall+0xb2>
jmp    11f7 <mb_readall+0xb7>
jmp    115e <mb_readall+0x1e>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mb_readall>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rdx,%r12
mov    %rsi,%rbp
mov    %edi,%ebx
mov    %rdx,%r15
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1040 <read@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
je     1182 <mb_readall+0x42>
movslq %r13d,%rax
sub    %rax,%r15
jbe    11b5 <mb_readall+0x75>
add    %rax,%rbp
jmp    1160 <mb_readall+0x20>
lea    0xe77(%rip),%rdi
mov    %ebx,%esi
mov    %rbp,%rdx
mov    %r15,%rcx
call   1030 <printf@plt>
lea    0xe7a(%rip),%rdi
call   1050 <perror@plt>
test   %r14d,%r14d
jne    1175 <mb_readall+0x35>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"fd=%d, buf=%p, sz=%lu\0A\00"
@.str.1 = [16 x i8] c"mb_readall:read\00"
@EXIT_FAILURE = global i32 0
define i32 @mb_readall(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 %3, i32* %8
%11 = load i64, i64* %7
store i64 %11, i64* %9
br label %12
12: ; preds = %45, %4
%13 = load i32, i32* %5
%14 = load i8*, i8** %6
%15 = load i64, i64* %9
%16 = call i32 @read(i32 %13, i8* %14, i64 %15)
store i32 %16, i32* %10
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %30
18: ; preds = %12
%19 = load i32, i32* %5
%20 = load i8*, i8** %6
%21 = load i64, i64* %9
%22 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %19, i8* %20, i64 %21)
%23 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* %8
%25 = icmp ne i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %18
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %18
br label %30
30: ; preds = %29, %12
%31 = load i32, i32* %10
%32 = sext i32 %31 to i64
%33 = load i64, i64* %9
%34 = icmp ult i64 %32, %33
br i1 %34, label %35, label %44
35: ; preds = %30
%36 = load i32, i32* %10
%37 = sext i32 %36 to i64
%38 = load i64, i64* %9
%39 = sub i64 %38, %37
store i64 %39, i64* %9
%40 = load i32, i32* %10
%41 = load i8*, i8** %6
%42 = sext i32 %40 to i64
%43 = getelementptr inbounds i8, i8* %41, i64 %42
store i8* %43, i8** %6
br label %45
44: ; preds = %30
br label %46
45: ; preds = %35
br label %12
46: ; preds = %44
%47 = load i64, i64* %7
%48 = trunc i64 %47 to i32
ret i32 %48
}

LLVM IR (-O2):
@.str = [23 x i8] c"fd=%d, buf=%p, sz=%lu\0A\00"
@.str.1 = [16 x i8] c"mb_readall:read\00"
@EXIT_FAILURE = global i32 0
define i32 @mb_readall(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = icmp eq i32 %3, 0
br label %6
6: ; preds = %20, %4
%7 = phi i64 [ %2, %4 ], [ %21, %20 ]
%8 = phi i8* [ %1, %4 ], [ %22, %20 ]
%9 = tail call i32 @read(i32 %0, i8* %8, i64 %7)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %17
11: ; preds = %6
%12 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %0, i8* %8, i64 %7)
%13 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br i1 %5, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %11, %6
%18 = sext i32 %9 to i64
%19 = icmp ugt i64 %7, %18
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = sub i64 %7, %18
%22 = getelementptr inbounds i8, i8* %8, i64 %18
br label %6
23: ; preds = %17
%24 = trunc i64 %2 to i32
ret i32 %24
}