#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int read(int, char *, size_t);
int /*<<< orphan*/ stderr;
int read_fully(int fd, void *buf, size_t count) {
  int i = 0;
  while (1) {
    int rd = read(fd, (char *)buf + i, count - i);
    if (rd == -1) {
      return -1;
    } else if (rd == 0) {
      return i;
    } else if (rd < -1 || rd + i > count) {
      fprintf(stderr, "Invalid read size\n");
      exit(EXIT_FAILURE);
    }
    i += rd;
    if (i >= count) {
      return i;
    }
  }
}


x86 assembly (clang, -O0):
<read_fully>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
movslq -0x1c(%rbp),%rax
sub    %rax,%rdx
call   1030 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1181 <read_fully+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    1206 <read_fully+0xd6>
cmpl   $0x0,-0x20(%rbp)
jne    1196 <read_fully+0x66>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1206 <read_fully+0xd6>
cmpl   $0xffffffff,-0x20(%rbp)
jl     11b2 <read_fully+0x82>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
cltq
cmp    -0x18(%rbp),%rax
jbe    11d5 <read_fully+0xa5>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
jmp    11da <read_fully+0xaa>
jmp    11df <read_fully+0xaf>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     1201 <read_fully+0xd1>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1206 <read_fully+0xd6>
jmp    114a <read_fully+0x1a>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_fully>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movslq %ebx,%rax
lea    (%r15,%rax,1),%rsi
mov    %r14,%rdx
sub    %rax,%rdx
mov    %ebp,%edi
call   1030 <read@plt>
cmp    $0xffffffff,%eax
je     1180 <read_fully+0x50>
test   %eax,%eax
je     1182 <read_fully+0x52>
cmp    $0xffffffff,%eax
jl     118f <read_fully+0x5f>
add    %eax,%ebx
movslq %ebx,%rax
cmp    %r14,%rax
ja     118f <read_fully+0x5f>
jb     1150 <read_fully+0x20>
jmp    1182 <read_fully+0x52>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
lea    0xe61(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [19 x i8] c"Invalid read size\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @read_fully(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* %8
br label %10
10: ; preds = %3, %56
%11 = load i32, i32* %5
%12 = load i8*, i8** %6
%13 = load i32, i32* %8
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = load i64, i64* %7
%17 = load i32, i32* %8
%18 = sext i32 %17 to i64
%19 = sub i64 %16, %18
%20 = call i32 @read(i32 %11, i8* %15, i64 %19)
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %10
store i32 -1, i32* %4
br label %57
24: ; preds = %10
%25 = load i32, i32* %9
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = load i32, i32* %8
store i32 %28, i32* %4
br label %57
29: ; preds = %24
%30 = load i32, i32* %9
%31 = icmp slt i32 %30, -1
br i1 %31, label %39, label %32
32: ; preds = %29
%33 = load i32, i32* %9
%34 = load i32, i32* %8
%35 = add nsw i32 %33, %34
%36 = sext i32 %35 to i64
%37 = load i64, i64* %7
%38 = icmp ugt i64 %36, %37
br i1 %38, label %39, label %44
39: ; preds = %32, %29
%40 = load i32, i32* @stderr
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %32
br label %45
45: ; preds = %44
br label %46
46: ; preds = %45
%47 = load i32, i32* %9
%48 = load i32, i32* %8
%49 = add nsw i32 %48, %47
store i32 %49, i32* %8
%50 = load i32, i32* %8
%51 = sext i32 %50 to i64
%52 = load i64, i64* %7
%53 = icmp uge i64 %51, %52
br i1 %53, label %54, label %56
54: ; preds = %46
%55 = load i32, i32* %8
store i32 %55, i32* %4
br label %57
56: ; preds = %46
br label %10
57: ; preds = %54, %27, %23
%58 = load i32, i32* %4
ret i32 %58
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [19 x i8] c"Invalid read size\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @read_fully(i32 %0, i8* nocapture %1, i64 %2) {
br label %4
4: ; preds = %21, %3
%5 = phi i32 [ 0, %3 ], [ %13, %21 ]
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds i8, i8* %1, i64 %6
%8 = sub i64 %2, %6
%9 = tail call i32 @read(i32 %0, i8* %7, i64 %8)
switch i32 %9, label %10 [
i32 -1, label %23
i32 0, label %25
]
10: ; preds = %4
%11 = icmp slt i32 %9, -1
br i1 %11, label %16, label %12
12: ; preds = %10
%13 = add nsw i32 %9, %5
%14 = sext i32 %13 to i64
%15 = icmp ugt i64 %14, %2
br i1 %15, label %16, label %21
16: ; preds = %12, %10
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %12
%22 = icmp ult i64 %14, %2
br i1 %22, label %4, label %23
23: ; preds = %21, %4
%24 = phi i32 [ %9, %4 ], [ %13, %21 ]
br label %25
25: ; preds = %4, %23
%26 = phi i32 [ %24, %23 ], [ %5, %4 ]
ret i32 %26
}