#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int read(int, unsigned char *, int);
int /*<<< orphan*/ stderr;
int bot_fileutils_read_fully(int fd, void *b, int len) {
  int cnt = 0;
  int thiscnt;
  unsigned char *bb = (unsigned char *)b;
  while (cnt < len) {
    thiscnt = read(fd, &bb[cnt], len - cnt);
    if (thiscnt < 0) {
      perror("read_fully");
      return -1;
    }
    if (thiscnt == 0) {
      fprintf(stderr, "read_fully end of file\n");
      return -1;
    }
    printf("%d\n", thiscnt);
    cnt += thiscnt;
  }
  return cnt;
}


x86 assembly (clang, -O0):
<bot_fileutils_read_fully>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11f3 <bot_fileutils_read_fully+0xb3>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11ab <bot_fileutils_read_fully+0x6b>
lea    0xe66(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f9 <bot_fileutils_read_fully+0xb9>
cmpl   $0x0,-0x1c(%rbp)
jne    11d6 <bot_fileutils_read_fully+0x96>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe46(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f9 <bot_fileutils_read_fully+0xb9>
mov    -0x1c(%rbp),%esi
lea    0xe43(%rip),%rdi
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1161 <bot_fileutils_read_fully+0x21>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bot_fileutils_read_fully>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    119f <bot_fileutils_read_fully+0x5f>
mov    %edx,%r13d
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebx,%ebx
lea    0xec2(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movslq %ebx,%rax
lea    (%r14,%rax,1),%rsi
mov    %r13d,%edx
sub    %eax,%edx
mov    %r15d,%edi
call   1040 <read@plt>
test   %eax,%eax
js     11ce <bot_fileutils_read_fully+0x8e>
je     11a3 <bot_fileutils_read_fully+0x63>
mov    %eax,%ebp
mov    %r12,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
add    %ebp,%ebx
cmp    %r13d,%ebx
jl     1170 <bot_fileutils_read_fully+0x30>
jmp    11bd <bot_fileutils_read_fully+0x7d>
xor    %ebx,%ebx
jmp    11bd <bot_fileutils_read_fully+0x7d>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
lea    0xe58(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe2b(%rip),%rdi
call   1060 <perror@plt>
jmp    11b8 <bot_fileutils_read_fully+0x78>

LLVM IR (-O0):
@.str = [11 x i8] c"read_fully\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"read_fully end of file\0A\00"
@.str.2 = [4 x i8] c"%d\0A\00"
define i32 @bot_fileutils_read_fully(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
%11 = load i8*, i8** %6
store i8* %11, i8** %10
br label %12
12: ; preds = %36, %3
%13 = load i32, i32* %8
%14 = load i32, i32* %7
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %42
16: ; preds = %12
%17 = load i32, i32* %5
%18 = load i8*, i8** %10
%19 = load i32, i32* %8
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i32, i32* %7
%23 = load i32, i32* %8
%24 = sub nsw i32 %22, %23
%25 = call i32 @read(i32 %17, i8* %21, i32 %24)
store i32 %25, i32* %9
%26 = load i32, i32* %9
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %16
%29 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %44
30: ; preds = %16
%31 = load i32, i32* %9
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %44
36: ; preds = %30
%37 = load i32, i32* %9
%38 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %37)
%39 = load i32, i32* %9
%40 = load i32, i32* %8
%41 = add nsw i32 %40, %39
store i32 %41, i32* %8
br label %12
42: ; preds = %12
%43 = load i32, i32* %8
store i32 %43, i32* %4
br label %44
44: ; preds = %42, %33, %28
%45 = load i32, i32* %4
ret i32 %45
}

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