#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;
typedef int /*<<< orphan*/ off_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__ xpreadv(int, struct iovec const *, int, int /*<<< orphan*/);
ssize_t preadv_in_full(int fd, const struct iovec *iov, int iovcnt,
                       off_t offset) {
  ssize_t total = 0;
  size_t count = get_iov_size(iov, iovcnt);
  while (count > 0) {
    ssize_t nr;
    nr = xpreadv(fd, iov, iovcnt, offset);
    if (nr <= 0) {
      if (total > 0)
        return total;
      return -1;
    }
    shift_iovec(&iov, &iovcnt, nr, &total, &count, &offset);
  }
  return total;
}


x86 assembly (clang, -O0):
<preadv_in_full>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%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    11d0 <preadv_in_full+0xa0>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
call   1040 <xpreadv@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jg     11ae <preadv_in_full+0x7e>
cmpq   $0x0,-0x28(%rbp)
jle    11a1 <preadv_in_full+0x71>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d8 <preadv_in_full+0xa8>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11d8 <preadv_in_full+0xa8>
mov    -0x38(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
lea    -0x28(%rbp),%rcx
lea    -0x30(%rbp),%r8
lea    -0x20(%rbp),%r9
call   1050 <shift_iovec@plt>
jmp    115d <preadv_in_full+0x2d>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<preadv_in_full>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %edi,%ebx
mov    %rsi,0x20(%rsp)
mov    %edx,0xc(%rsp)
mov    %ecx,0x8(%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     11fd <preadv_in_full+0xcd>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r15d,%edx
mov    %r14d,%ecx
call   1040 <xpreadv@plt>
test   %rax,%rax
jle    11e8 <preadv_in_full+0xb8>
lea    0x20(%rsp),%r14
lea    0xc(%rsp),%r15
lea    0x10(%rsp),%r12
lea    0x18(%rsp),%r13
lea    0x8(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
mov    %r12,%rcx
mov    %r13,%r8
mov    %rbp,%r9
call   1050 <shift_iovec@plt>
cmpq   $0x0,0x18(%rsp)
je     1201 <preadv_in_full+0xd1>
mov    0x20(%rsp),%rsi
mov    0xc(%rsp),%edx
mov    0x8(%rsp),%ecx
mov    %ebx,%edi
call   1040 <xpreadv@plt>
test   %rax,%rax
jg     11b0 <preadv_in_full+0x80>
mov    0x10(%rsp),%rcx
test   %rcx,%rcx
mov    $0xffffffffffffffff,%rax
cmovg  %rcx,%rax
jmp    1206 <preadv_in_full+0xd6>
xor    %eax,%eax
jmp    1206 <preadv_in_full+0xd6>
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 @preadv_in_full(i32 %0, %struct.iovec* %1, i32 %2, i32 %3) {
%5 = alloca i64
%6 = alloca i32
%7 = alloca %struct.iovec*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
store i32 %0, i32* %6
store %struct.iovec* %1, %struct.iovec** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i64 0, i64* %10
%13 = load %struct.iovec*, %struct.iovec** %7
%14 = load i32, i32* %8
%15 = call i64 @get_iov_size(%struct.iovec* %13, i32 %14)
store i64 %15, i64* %11
br label %16
16: ; preds = %33, %4
%17 = load i64, i64* %11
%18 = icmp ugt i64 %17, 0
br i1 %18, label %19, label %36
19: ; preds = %16
%20 = load i32, i32* %6
%21 = load %struct.iovec*, %struct.iovec** %7
%22 = load i32, i32* %8
%23 = load i32, i32* %9
%24 = call i64 @xpreadv(i32 %20, %struct.iovec* %21, i32 %22, i32 %23)
store i64 %24, i64* %12
%25 = load i64, i64* %12
%26 = icmp sle i64 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %19
%28 = load i64, i64* %10
%29 = icmp sgt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = load i64, i64* %10
store i64 %31, i64* %5
br label %38
32: ; preds = %27
store i64 -1, i64* %5
br label %38
33: ; preds = %19
%34 = load i64, i64* %12
%35 = call i32 @shift_iovec(%struct.iovec** %7, i32* %8, i64 %34, i64* %10, i64* %11, i32* %9)
br label %16
36: ; preds = %16
%37 = load i64, i64* %10
store i64 %37, i64* %5
br label %38
38: ; preds = %36, %32, %30
%39 = load i64, i64* %5
ret i64 %39
}

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