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


x86 assembly (clang, -O0):
<writev_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   1040 <get_iov_size@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jbe    11dd <writev_in_full+0xad>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <xwritev@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    1190 <writev_in_full+0x60>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11e5 <writev_in_full+0xb5>
cmpq   $0x0,-0x38(%rbp)
jne    11ba <writev_in_full+0x8a>
mov    0x2e36(%rip),%rax
mov    (%rax),%ecx
mov    0x2e35(%rip),%rax
mov    %ecx,(%rax)
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11e5 <writev_in_full+0xb5>
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 <writev_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):
<writev_in_full>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r14d
mov    %rsi,0x20(%rsp)
mov    %edx,0xc(%rsp)
movq   $0x0,0x18(%rsp)
mov    %rsi,%rdi
mov    %edx,%esi
call   1040 <get_iov_size@plt>
mov    %rax,0x10(%rsp)
test   %rax,%rax
je     11dc <writev_in_full+0xac>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <xwritev@plt>
mov    $0xffffffffffffffff,%rbx
test   %rax,%rax
js     11f9 <writev_in_full+0xc9>
lea    0x20(%rsp),%r15
lea    0xc(%rsp),%r12
lea    0x18(%rsp),%r13
lea    0x10(%rsp),%rbp
nopl   0x0(%rax)
test   %rax,%rax
je     11e0 <writev_in_full+0xb0>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rax,%rdx
mov    %r13,%rcx
mov    %rbp,%r8
xor    %r9d,%r9d
call   1050 <shift_iovec@plt>
cmpq   $0x0,0x10(%rsp)
je     11f4 <writev_in_full+0xc4>
mov    0x20(%rsp),%rsi
mov    0xc(%rsp),%edx
mov    %r14d,%edi
call   1030 <xwritev@plt>
test   %rax,%rax
jns    11a0 <writev_in_full+0x70>
jmp    11f9 <writev_in_full+0xc9>
xor    %ebx,%ebx
jmp    11f9 <writev_in_full+0xc9>
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    0x2df0(%rip),%rcx
mov    %eax,(%rcx)
jmp    11f9 <writev_in_full+0xc9>
mov    0x18(%rsp),%rbx
mov    %rbx,%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 @writev_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 @xwritev(i32 %18, %struct.iovec* %19, i32 %20)
store i64 %21, i64* %10
%22 = load i64, i64* %10
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %17
store i64 -1, i64* %4
br label %35
25: ; preds = %17
%26 = load i64, i64* %10
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = load i32, i32* @ENOSPC
store i32 %29, i32* @errno
store i64 -1, i64* %4
br label %35
30: ; preds = %25
%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, %28, %24
%36 = load i64, i64* %4
ret i64 %36
}

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