#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 */
int /*<<< orphan*/ ENOSPC;
int /*<<< orphan*/ errno;
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__ xpwritev(int, struct iovec const *, int, int /*<<< orphan*/);
ssize_t pwritev_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 = xpwritev(fd, iov, iovcnt, offset);
    if (nr < 0)
      return -1;
    if (nr == 0) {
      errno = ENOSPC;
      return -1;
    }
    shift_iovec(&iov, &iovcnt, nr, &total, &count, &offset);
  }
  return total;
}


x86 assembly (clang, -O0):
<pwritev_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    11e2 <pwritev_in_full+0xb2>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
call   1040 <xpwritev@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    1196 <pwritev_in_full+0x66>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11ea <pwritev_in_full+0xba>
cmpq   $0x0,-0x38(%rbp)
jne    11c0 <pwritev_in_full+0x90>
mov    0x2e30(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2f(%rip),%rax
mov    %ecx,(%rax)
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11ea <pwritev_in_full+0xba>
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 <pwritev_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):
<pwritev_in_full>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%r14d
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
mov    %rsi,0x20(%rsp)
mov    %edx,0xc(%rsp)
mov    %ecx,0x8(%rsp)
movq   $0x0,0x18(%rsp)
mov    %rsi,%rdi
mov    %edx,%esi
call   1030 <get_iov_size@plt>
mov    %rax,0x10(%rsp)
test   %rax,%rax
je     11f7 <pwritev_in_full+0xc7>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
mov    %r14d,%ecx
call   1040 <xpwritev@plt>
mov    $0xffffffffffffffff,%rcx
test   %rax,%rax
js     121b <pwritev_in_full+0xeb>
lea    0x20(%rsp),%rbx
lea    0xc(%rsp),%r12
lea    0x18(%rsp),%r13
lea    0x10(%rsp),%rbp
lea    0x8(%rsp),%r14
test   %rax,%rax
je     11fb <pwritev_in_full+0xcb>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %rax,%rdx
mov    %r13,%rcx
mov    %rbp,%r8
mov    %r14,%r9
call   1050 <shift_iovec@plt>
cmpq   $0x0,0x10(%rsp)
je     1216 <pwritev_in_full+0xe6>
mov    0x20(%rsp),%rsi
mov    0xc(%rsp),%edx
mov    0x8(%rsp),%ecx
mov    %r15d,%edi
call   1040 <xpwritev@plt>
test   %rax,%rax
jns    11b0 <pwritev_in_full+0x80>
mov    $0xffffffffffffffff,%rcx
jmp    121b <pwritev_in_full+0xeb>
xor    %ecx,%ecx
jmp    121b <pwritev_in_full+0xeb>
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    0x2dd5(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffffffffffff,%rcx
jmp    121b <pwritev_in_full+0xeb>
mov    0x18(%rsp),%rcx
mov    %rcx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i32 }
@ENOSPC = global i32 0
@errno = global i32 0
define i64 @pwritev_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 @xpwritev(i32 %20, %struct.iovec* %21, i32 %22, i32 %23)
store i64 %24, i64* %12
%25 = load i64, i64* %12
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %19
store i64 -1, i64* %5
br label %38
28: ; preds = %19
%29 = load i64, i64* %12
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = load i32, i32* @ENOSPC
store i32 %32, i32* @errno
store i64 -1, i64* %5
br label %38
33: ; preds = %28
%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, %31, %27
%39 = load i64, i64* %5
ret i64 %39
}

LLVM IR (-O2):
%struct.iovec = type { i32 }
@ENOSPC = global i32 0
@errno = global i32 0
define i64 @pwritev_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 @xpwritev(i32 %0, %struct.iovec* %1, i32 %2, i32 %3)
%16 = icmp slt i64 %15, 0
br i1 %16, label %34, label %17
17: ; preds = %14, %26
%18 = phi i64 [ %30, %26 ], [ %15, %14 ]
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = load i32, i32* @ENOSPC
store i32 %21, i32* @errno
br label %34
22: ; preds = %17
%23 = call i32 @shift_iovec(%struct.iovec** %5, i32* %6, i64 %18, i64* %8, i64* %9, i32* %7)
%24 = load i64, i64* %9
%25 = icmp eq i64 %24, 0
br i1 %25, label %32, label %26
26: ; preds = %22
%27 = load %struct.iovec*, %struct.iovec** %5
%28 = load i32, i32* %6
%29 = load i32, i32* %7
%30 = call i64 @xpwritev(i32 %0, %struct.iovec* %27, i32 %28, i32 %29)
%31 = icmp slt i64 %30, 0
br i1 %31, label %34, label %17
32: ; preds = %22
%33 = load i64, i64* %8
br label %34
34: ; preds = %26, %14, %4, %32, %20
%35 = phi i64 [ -1, %20 ], [ %33, %32 ], [ 0, %4 ], [ -1, %14 ], [ -1, %26 ]
ret i64 %35
}