#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 read(int, char *, int);
int fullRead(int fd, char *buf, int len) {
  int cc;
  int total;
  total = 0;
  while (len > 0) {
    cc = read(fd, buf, len);
    if (cc < 0)
      return -1;
    if (cc == 0)
      break;
    buf += cc;
    total += cc;
    len -= cc;
  }
  return total;
}


x86 assembly (clang, -O0):
<fullRead>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1194 <fullRead+0x84>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    115b <fullRead+0x4b>
movl   $0xffffffff,-0x4(%rbp)
jmp    119a <fullRead+0x8a>
cmpl   $0x0,-0x18(%rbp)
jne    116a <fullRead+0x5a>
jmp    1194 <fullRead+0x84>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    1129 <fullRead+0x19>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fullRead>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    1151 <fullRead+0x41>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <read@plt>
test   %eax,%eax
js     1156 <fullRead+0x46>
je     115c <fullRead+0x4c>
mov    %eax,%ecx
add    %rcx,%rbx
add    %eax,%r14d
sub    %eax,%ebp
jg     1130 <fullRead+0x20>
jmp    115c <fullRead+0x4c>
xor    %r14d,%r14d
jmp    115c <fullRead+0x4c>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @fullRead(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %9
br label %10
10: ; preds = %25, %3
%11 = load i32, i32* %7
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %36
13: ; preds = %10
%14 = load i32, i32* %5
%15 = load i8*, i8** %6
%16 = load i32, i32* %7
%17 = call i32 @read(i32 %14, i8* %15, i32 %16)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %13
store i32 -1, i32* %4
br label %38
21: ; preds = %13
%22 = load i32, i32* %8
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %21
br label %36
25: ; preds = %21
%26 = load i32, i32* %8
%27 = load i8*, i8** %6
%28 = sext i32 %26 to i64
%29 = getelementptr inbounds i8, i8* %27, i64 %28
store i8* %29, i8** %6
%30 = load i32, i32* %8
%31 = load i32, i32* %9
%32 = add nsw i32 %31, %30
store i32 %32, i32* %9
%33 = load i32, i32* %8
%34 = load i32, i32* %7
%35 = sub nsw i32 %34, %33
store i32 %35, i32* %7
br label %10
36: ; preds = %24, %10
%37 = load i32, i32* %9
store i32 %37, i32* %4
br label %38
38: ; preds = %36, %20
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
define i32 @fullRead(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %19
5: ; preds = %3, %13
%6 = phi i32 [ %16, %13 ], [ 0, %3 ]
%7 = phi i32 [ %17, %13 ], [ %2, %3 ]
%8 = phi i8* [ %15, %13 ], [ %1, %3 ]
%9 = tail call i32 @read(i32 %0, i8* %8, i32 %7)
%10 = icmp slt i32 %9, 0
br i1 %10, label %19, label %11
11: ; preds = %5
%12 = icmp eq i32 %9, 0
br i1 %12, label %19, label %13
13: ; preds = %11
%14 = zext i32 %9 to i64
%15 = getelementptr inbounds i8, i8* %8, i64 %14
%16 = add nsw i32 %9, %6
%17 = sub nsw i32 %7, %9
%18 = icmp sgt i32 %17, 0
br i1 %18, label %5, label %19
19: ; preds = %5, %11, %13, %3
%20 = phi i32 [ 0, %3 ], [ %16, %13 ], [ %6, %11 ], [ -1, %5 ]
ret i32 %20
}