#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 */
struct iovec {
  int dummy;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
size_t get_iov_size(struct iovec const *, int);
int /*<<< orphan*/ shift_iovec(struct iovec const **, int *, scalar_t__,
                               scalar_t__ *, size_t *, int /*<<< orphan*/ *);
scalar_t__ xreadv(int, struct iovec const *, int);
ssize_t readv_in_full(int fd, const struct iovec *iov, int iovcnt) {
  ssize_t total = 0;
  size_t count = get_iov_size(iov, iovcnt);
  while (count > 0) {
    ssize_t nr;
    nr = xreadv(fd, iov, iovcnt);
    if (nr <= 0) {
      if (total > 0)
        return total;
      return -1;
    }
    shift_iovec(&iov, &iovcnt, nr, &total, &count, NULL);
  }
  return total;
}


x86 assembly (clang, -O0):
<readv_in_full>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1030 <get_iov_size@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jbe    11cb <readv_in_full+0x9b>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <xreadv@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jg     11a8 <readv_in_full+0x78>
cmpq   $0x0,-0x28(%rbp)
jle    119b <readv_in_full+0x6b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d3 <readv_in_full+0xa3>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11d3 <readv_in_full+0xa3>
mov    -0x38(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
lea    -0x28(%rbp),%rcx
lea    -0x30(%rbp),%r8
xor    %eax,%eax
mov    %eax,%r9d
call   1050 <shift_iovec@plt>
jmp    115a <readv_in_full+0x2a>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readv_in_full>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %edi,%ebx
mov    %rsi,0x20(%rsp)
mov    %edx,0xc(%rsp)
movq   $0x0,0x10(%rsp)
mov    %rsi,%rdi
mov    %edx,%esi
call   1030 <get_iov_size@plt>
mov    %rax,0x18(%rsp)
test   %rax,%rax
je     11e9 <readv_in_full+0xb9>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1040 <xreadv@plt>
test   %rax,%rax
jle    11d4 <readv_in_full+0xa4>
lea    0x20(%rsp),%r14
lea    0xc(%rsp),%r15
lea    0x10(%rsp),%r12
lea    0x18(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
mov    %r12,%rcx
mov    %r13,%r8
xor    %r9d,%r9d
call   1050 <shift_iovec@plt>
cmpq   $0x0,0x18(%rsp)
je     11ed <readv_in_full+0xbd>
mov    0x20(%rsp),%rsi
mov    0xc(%rsp),%edx
mov    %ebx,%edi
call   1040 <xreadv@plt>
test   %rax,%rax
jg     11a0 <readv_in_full+0x70>
mov    0x10(%rsp),%rcx
test   %rcx,%rcx
mov    $0xffffffffffffffff,%rax
cmovg  %rcx,%rax
jmp    11f2 <readv_in_full+0xc2>
xor    %eax,%eax
jmp    11f2 <readv_in_full+0xc2>
mov    0x10(%rsp),%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i32 }
define i64 @readv_in_full(i32 %0, %struct.iovec* %1, i32 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca %struct.iovec*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
store i32 %0, i32* %5
store %struct.iovec* %1, %struct.iovec** %6
store i32 %2, i32* %7
store i64 0, i64* %8
%11 = load %struct.iovec*, %struct.iovec** %6
%12 = load i32, i32* %7
%13 = call i64 @get_iov_size(%struct.iovec* %11, i32 %12)
store i64 %13, i64* %9
br label %14
14: ; preds = %30, %3
%15 = load i64, i64* %9
%16 = icmp ugt i64 %15, 0
br i1 %16, label %17, label %33
17: ; preds = %14
%18 = load i32, i32* %5
%19 = load %struct.iovec*, %struct.iovec** %6
%20 = load i32, i32* %7
%21 = call i64 @xreadv(i32 %18, %struct.iovec* %19, i32 %20)
store i64 %21, i64* %10
%22 = load i64, i64* %10
%23 = icmp sle i64 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %17
%25 = load i64, i64* %8
%26 = icmp sgt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = load i64, i64* %8
store i64 %28, i64* %4
br label %35
29: ; preds = %24
store i64 -1, i64* %4
br label %35
30: ; preds = %17
%31 = load i64, i64* %10
%32 = call i32 @shift_iovec(%struct.iovec** %6, i32* %7, i64 %31, i64* %8, i64* %9, i32* null)
br label %14
33: ; preds = %14
%34 = load i64, i64* %8
store i64 %34, i64* %4
br label %35
35: ; preds = %33, %29, %27
%36 = load i64, i64* %4
ret i64 %36
}

LLVM IR (-O2):
%struct.iovec = type { i32 }
define i64 @readv_in_full(i32 %0, %struct.iovec* %1, i32 %2) {
%4 = alloca %struct.iovec*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
store %struct.iovec* %1, %struct.iovec** %4
store i32 %2, i32* %5
%8 = bitcast i64* %6 to i8*
store i64 0, i64* %6
%9 = bitcast i64* %7 to i8*
%10 = tail call i64 @get_iov_size(%struct.iovec* %1, i32 %2)
store i64 %10, i64* %7
%11 = icmp eq i64 %10, 0
br i1 %11, label %31, label %12
12: ; preds = %3
%13 = call i64 @xreadv(i32 %0, %struct.iovec* %1, i32 %2)
%14 = icmp slt i64 %13, 1
br i1 %14, label %25, label %15
15: ; preds = %12, %20
%16 = phi i64 [ %23, %20 ], [ %13, %12 ]
%17 = call i32 @shift_iovec(%struct.iovec** %4, i32* %5, i64 %16, i64* %6, i64* %7, i32* null)
%18 = load i64, i64* %7
%19 = icmp eq i64 %18, 0
br i1 %19, label %29, label %20
20: ; preds = %15
%21 = load %struct.iovec*, %struct.iovec** %4
%22 = load i32, i32* %5
%23 = call i64 @xreadv(i32 %0, %struct.iovec* %21, i32 %22)
%24 = icmp slt i64 %23, 1
br i1 %24, label %25, label %15
25: ; preds = %20, %12
%26 = load i64, i64* %6
%27 = icmp sgt i64 %26, 0
%28 = select i1 %27, i64 %26, i64 -1
br label %31
29: ; preds = %15
%30 = load i64, i64* %6
br label %31
31: ; preds = %3, %29, %25
%32 = phi i64 [ %28, %25 ], [ %30, %29 ], [ 0, %3 ]
ret i64 %32
}