#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 */
scalar_t__ xread(int, char *, size_t);
ssize_t xread_in_full(int fd, void *buf, size_t len) {
  char *p = buf;
  ssize_t total = 0;
  while (len > 0) {
    ssize_t read = xread(fd, p, len);
    if (read < 0)
      return -1;
    if (read == 0)
      return total;
    len -= read;
    p += read;
    total += read;
  }
  return total;
}


x86 assembly (clang, -O0):
<xread_in_full>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    11ae <xread_in_full+0x9e>
mov    -0xc(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <xread@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    116a <xread_in_full+0x5a>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11b6 <xread_in_full+0xa6>
cmpq   $0x0,-0x38(%rbp)
jne    1182 <xread_in_full+0x72>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b6 <xread_in_full+0xa6>
mov    -0x38(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1133 <xread_in_full+0x23>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xread_in_full>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdx,%rdx
je     1151 <xread_in_full+0x41>
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %edi,%ebp
xor    %r14d,%r14d
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <xread@plt>
test   %rax,%rax
js     1156 <xread_in_full+0x46>
je     115d <xread_in_full+0x4d>
add    %rax,%r15
add    %rax,%r14
sub    %rax,%rbx
jne    1130 <xread_in_full+0x20>
jmp    115d <xread_in_full+0x4d>
xor    %r14d,%r14d
jmp    115d <xread_in_full+0x4d>
mov    $0xffffffffffffffff,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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