#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 timeval {
  int dummy;
};
struct pollfd {
  int fd;
  int events;
  int revents;
};
struct iovec {
  size_t iov_len;
  char *iov_base;
};
typedef int ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EBADF;
scalar_t__ EINTR;
scalar_t__ EINVAL;
scalar_t__ EIO;
scalar_t__ ENOMEM;
scalar_t__ ETIMEDOUT;
int POLLERR;
int POLLHUP;
int POLLNVAL;
int POLLOUT;
int _fd_get_poll_timeout(struct timeval const *);
int /*<<< orphan*/ assert(int);
scalar_t__ errno;
int /*<<< orphan*/ free(struct iovec *);
struct iovec *malloc(int);
int /*<<< orphan*/ memcpy(struct iovec *, struct iovec const *, int);
int poll(struct pollfd *, int, int);
int writev(int, struct iovec *, int);
ssize_t fd_timed_write_iov(int fd, const struct iovec *iov_orig, int iov_cnt,
                           const struct timeval *when, int do_skip_first_poll) {
  int iov_mem_len;
  struct iovec *iov;
  int i;
  size_t n, nleft, iov_len;
  struct pollfd pfd;
  int nfd;
  int msecs;
  ssize_t nwritten;
  if ((fd < 0) || (iov_orig == NULL) || (iov_cnt <= 0)) {
    errno = EINVAL;
    return (-1);
  }
  iov_mem_len = sizeof(struct iovec) * iov_cnt;
  iov = malloc(iov_mem_len);
  if (iov == NULL) {
    errno = ENOMEM;
    return (-1);
  }
  memcpy(iov, iov_orig, iov_mem_len);
  for (i = 0, n = 0; i < iov_cnt; i++) {
    n += iov[i].iov_len;
  }
  nleft = iov_len = n;
  pfd.fd = fd;
  pfd.events = POLLOUT;
  if (do_skip_first_poll && (nleft > 0)) {
    msecs = -1;
    goto writev_me;
  }
  while (nleft > 0) {
    msecs = _fd_get_poll_timeout(when);
    nfd = poll(&pfd, 1, msecs);
    if (nfd < 0) {
      if ((errno == EINTR) || (errno == EAGAIN))
        continue;
      else
        goto err;
    } else if (nfd == 0) {
      errno = ETIMEDOUT;
      break;
    } else if (pfd.revents & POLLHUP) {
      break;
    } else if (pfd.revents & POLLNVAL) {
      errno = EBADF;
      goto err;
    } else if (pfd.revents & POLLERR) {
      errno = EIO;
      goto err;
    }
    assert(pfd.revents & POLLOUT);
  writev_me:
    nwritten = writev(fd, iov, iov_cnt);
    if (nwritten < 0) {
      if ((errno == EINTR) || (errno == EAGAIN))
        continue;
      else
        goto err;
    }
    nleft -= nwritten;
    if (msecs == 0) {
      break;
    }
    for (i = 0; (i < iov_cnt) && (nwritten > 0); i++) {
      n = (nwritten > iov[i].iov_len) ? iov[i].iov_len : nwritten;
      if (n == 0)
        continue;
      nwritten -= n;
      iov[i].iov_len -= n;
      iov[i].iov_base = (char *)iov[i].iov_base + n;
    }
  }
  free(iov);
  return (iov_len - nleft);
err:
  free(iov);
  return (-1);
}


x86 assembly (clang, -O0):
<fd_timed_write_iov>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     11ac <fd_timed_write_iov+0x3c>
cmpq   $0x0,-0x10(%rbp)
je     11ac <fd_timed_write_iov+0x3c>
cmpl   $0x0,-0x14(%rbp)
jg     11cc <fd_timed_write_iov+0x5c>
mov    0x2de5(%rip),%rax
mov    (%rax),%rcx
mov    0x2df3(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1567 <fd_timed_write_iov+0x3f7>
movslq -0x14(%rbp),%rax
shl    $0x4,%rax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    120e <fd_timed_write_iov+0x9e>
mov    0x2dc3(%rip),%rax
mov    (%rax),%rcx
mov    0x2db1(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1567 <fd_timed_write_iov+0x3f7>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1060 <memcpy@plt>
movl   $0x0,-0x34(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1261 <fd_timed_write_iov+0xf1>
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%rax
add    -0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    122d <fd_timed_write_iov+0xbd>
mov    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x60(%rbp)
mov    0x2d7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     12a0 <fd_timed_write_iov+0x130>
cmpq   $0x0,-0x48(%rbp)
jbe    12a0 <fd_timed_write_iov+0x130>
movl   $0xffffffff,-0x68(%rbp)
jmp    13d6 <fd_timed_write_iov+0x266>
jmp    12a5 <fd_timed_write_iov+0x135>
cmpq   $0x0,-0x48(%rbp)
jbe    153e <fd_timed_write_iov+0x3ce>
mov    -0x20(%rbp),%rdi
call   1090 <_fd_get_poll_timeout@plt>
mov    %eax,-0x68(%rbp)
mov    -0x68(%rbp),%edx
lea    -0x60(%rbp),%rdi
mov    $0x1,%esi
call   1080 <poll@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
jge    1318 <fd_timed_write_iov+0x1a8>
mov    0x2ccf(%rip),%rax
mov    (%rax),%rax
mov    0x2cb5(%rip),%rcx
cmp    (%rcx),%rax
je     130e <fd_timed_write_iov+0x19e>
mov    0x2cb5(%rip),%rax
mov    (%rax),%rax
mov    0x2c7b(%rip),%rcx
cmp    (%rcx),%rax
jne    1313 <fd_timed_write_iov+0x1a3>
jmp    12a5 <fd_timed_write_iov+0x135>
jmp    1557 <fd_timed_write_iov+0x3e7>
cmpl   $0x0,-0x64(%rbp)
jne    133b <fd_timed_write_iov+0x1cb>
mov    0x2c7f(%rip),%rax
mov    (%rax),%rcx
mov    0x2c7d(%rip),%rax
mov    %rcx,(%rax)
jmp    153e <fd_timed_write_iov+0x3ce>
mov    -0x58(%rbp),%eax
mov    0x2c9b(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1355 <fd_timed_write_iov+0x1e5>
jmp    153e <fd_timed_write_iov+0x3ce>
mov    -0x58(%rbp),%eax
mov    0x2c71(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1383 <fd_timed_write_iov+0x213>
mov    0x2c1f(%rip),%rax
mov    (%rax),%rcx
mov    0x2c35(%rip),%rax
mov    %rcx,(%rax)
jmp    1557 <fd_timed_write_iov+0x3e7>
mov    -0x58(%rbp),%eax
mov    0x2c4b(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     13b1 <fd_timed_write_iov+0x241>
mov    0x2c21(%rip),%rax
mov    (%rax),%rcx
mov    0x2c07(%rip),%rax
mov    %rcx,(%rax)
jmp    1557 <fd_timed_write_iov+0x3e7>
jmp    13b6 <fd_timed_write_iov+0x246>
jmp    13bb <fd_timed_write_iov+0x24b>
jmp    13c0 <fd_timed_write_iov+0x250>
jmp    13c5 <fd_timed_write_iov+0x255>
mov    -0x58(%rbp),%edi
mov    0x2c29(%rip),%rax
and    (%rax),%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <writev@plt>
mov    %eax,-0x6c(%rbp)
cmpl   $0x0,-0x6c(%rbp)
jge    1430 <fd_timed_write_iov+0x2c0>
mov    0x2bb7(%rip),%rax
mov    (%rax),%rax
mov    0x2b9d(%rip),%rcx
cmp    (%rcx),%rax
je     1426 <fd_timed_write_iov+0x2b6>
mov    0x2b9d(%rip),%rax
mov    (%rax),%rax
mov    0x2b63(%rip),%rcx
cmp    (%rcx),%rax
jne    142b <fd_timed_write_iov+0x2bb>
jmp    12a5 <fd_timed_write_iov+0x135>
jmp    1557 <fd_timed_write_iov+0x3e7>
movslq -0x6c(%rbp),%rcx
mov    -0x48(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x48(%rbp)
cmpl   $0x0,-0x68(%rbp)
jne    144e <fd_timed_write_iov+0x2de>
jmp    153e <fd_timed_write_iov+0x3ce>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%ecx
xor    %eax,%eax
cmp    -0x14(%rbp),%ecx
mov    %al,-0x6d(%rbp)
jge    1470 <fd_timed_write_iov+0x300>
cmpl   $0x0,-0x6c(%rbp)
setg   %al
mov    %al,-0x6d(%rbp)
mov    -0x6d(%rbp),%al
test   $0x1,%al
jne    1480 <fd_timed_write_iov+0x310>
jmp    1539 <fd_timed_write_iov+0x3c9>
movslq -0x6c(%rbp),%rax
mov    -0x30(%rbp),%rcx
movslq -0x34(%rbp),%rdx
shl    $0x4,%rdx
add    %rdx,%rcx
cmp    (%rcx),%rax
jbe    14b7 <fd_timed_write_iov+0x347>
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%rax
mov    %rax,-0x78(%rbp)
jmp    14bf <fd_timed_write_iov+0x34f>
movslq -0x6c(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    14d7 <fd_timed_write_iov+0x367>
jmp    152b <fd_timed_write_iov+0x3bb>
mov    -0x40(%rbp),%rcx
movslq -0x6c(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x6c(%rbp)
mov    -0x40(%rbp),%rdx
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%rcx
sub    %rdx,%rcx
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%rcx
add    -0x40(%rbp),%rcx
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rdx
shl    $0x4,%rdx
add    %rdx,%rax
mov    %rcx,0x8(%rax)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1455 <fd_timed_write_iov+0x2e5>
jmp    12a5 <fd_timed_write_iov+0x135>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x50(%rbp),%rax
sub    -0x48(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    1567 <fd_timed_write_iov+0x3f7>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_timed_write_iov>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rcx,0x30(%rsp)
test   %edi,%edi
js     12b4 <fd_timed_write_iov+0x144>
mov    %rsi,%rbx
test   %rsi,%rsi
je     12b4 <fd_timed_write_iov+0x144>
mov    %edx,%r15d
test   %edx,%edx
jle    12b4 <fd_timed_write_iov+0x144>
mov    %r8d,%r13d
mov    %edi,%r12d
mov    %r15d,%ebp
shl    $0x4,%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     12bd <fd_timed_write_iov+0x14d>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1060 <memcpy@plt>
mov    %r15d,%eax
xor    %ecx,%ecx
mov    $0x0,%ebp
cmp    $0x5,%r15d
jb     124f <fd_timed_write_iov+0xdf>
mov    %eax,%ecx
and    $0x3,%ecx
test   %rcx,%rcx
mov    $0x4,%edx
cmovne %rcx,%rdx
mov    %rax,%rcx
sub    %rdx,%rcx
lea    0x20(%r14),%rdx
pxor   %xmm0,%xmm0
mov    %rcx,%rsi
pxor   %xmm1,%xmm1
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movdqu -0x20(%rdx),%xmm2
movdqu -0x10(%rdx),%xmm3
punpcklqdq %xmm3,%xmm2
paddq  %xmm2,%xmm0
movdqu (%rdx),%xmm2
movdqu 0x10(%rdx),%xmm3
punpcklqdq %xmm3,%xmm2
paddq  %xmm2,%xmm1
add    $0x40,%rdx
add    $0xfffffffffffffffc,%rsi
jne    1210 <fd_timed_write_iov+0xa0>
paddq  %xmm0,%xmm1
pshufd $0xee,%xmm1,%xmm0
paddq  %xmm1,%xmm0
movq   %xmm0,%rbp
mov    %r15d,0x1c(%rsp)
sub    %rcx,%rax
shl    $0x4,%rcx
mov    %r14,0x10(%rsp)
add    %r14,%rcx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    (%rcx),%rbp
add    $0x10,%rcx
add    $0xffffffffffffffff,%rax
jne    1270 <fd_timed_write_iov+0x100>
mov    %r12d,0x18(%rsp)
mov    %r12d,0x20(%rsp)
mov    0x2d6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    $0xffffffff,%r14d
mov    %rbp,0x8(%rsp)
test   %r13d,%r13d
je     12d3 <fd_timed_write_iov+0x163>
mov    %rbp,0x8(%rsp)
test   %rbp,%rbp
jne    13a5 <fd_timed_write_iov+0x235>
jmp    12d3 <fd_timed_write_iov+0x163>
mov    0x2cdd(%rip),%rax
jmp    12c4 <fd_timed_write_iov+0x154>
mov    0x2cf4(%rip),%rax
mov    (%rax),%rax
mov    0x2ce2(%rip),%rcx
mov    %rax,(%rcx)
jmp    133c <fd_timed_write_iov+0x1cc>
xor    %ebx,%ebx
cmpq   $0x0,0x8(%rsp)
je     147c <fd_timed_write_iov+0x30c>
lea    0x20(%rsp),%rbx
mov    0x2cc3(%rip),%r13
mov    0x2cac(%rip),%r12
mov    0x2c85(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    0x30(%rsp),%rdi
call   1090 <_fd_get_poll_timeout@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
call   1080 <poll@plt>
test   %eax,%eax
jns    1352 <fd_timed_write_iov+0x1e2>
mov    0x0(%r13),%rax
mov    (%r15),%rcx
cmp    (%r12),%rax
je     1300 <fd_timed_write_iov+0x190>
cmp    %rcx,%rax
je     1300 <fd_timed_write_iov+0x190>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
je     1469 <fd_timed_write_iov+0x2f9>
mov    0x28(%rsp),%edi
mov    0x2c7d(%rip),%rax
test   %edi,(%rax)
jne    1477 <fd_timed_write_iov+0x307>
mov    0x2c5e(%rip),%rcx
mov    0x2c17(%rip),%rax
test   %edi,(%rcx)
jne    145d <fd_timed_write_iov+0x2ed>
mov    0x2c50(%rip),%rcx
mov    0x2c31(%rip),%rax
test   %edi,(%rcx)
jne    145d <fd_timed_write_iov+0x2ed>
mov    0x2c5a(%rip),%rax
and    (%rax),%edi
call   1050 <assert@plt>
mov    0x18(%rsp),%edi
mov    0x10(%rsp),%r15
mov    %r15,%rsi
mov    0x1c(%rsp),%ebx
mov    %ebx,%edx
call   1040 <writev@plt>
test   %eax,%eax
js     142b <fd_timed_write_iov+0x2bb>
mov    %eax,%ecx
sub    %rcx,0x8(%rsp)
test   %r14d,%r14d
je     1477 <fd_timed_write_iov+0x307>
test   %eax,%eax
je     12d3 <fd_timed_write_iov+0x163>
movslq %ebx,%rcx
lea    0x8(%r15),%rdx
mov    $0x1,%esi
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %eax,%edi
mov    -0x8(%rdx),%rbx
cmp    %rdi,%rbx
cmovb  %rbx,%rdi
test   %rdi,%rdi
je     140e <fd_timed_write_iov+0x29e>
sub    %edi,%eax
sub    %rdi,%rbx
mov    %rbx,-0x8(%rdx)
add    %rdi,(%rdx)
add    $0x10,%rdx
lea    0x1(%rsi),%rdi
cmp    %rcx,%rsi
jge    12d3 <fd_timed_write_iov+0x163>
mov    %rdi,%rsi
test   %eax,%eax
jg     13f0 <fd_timed_write_iov+0x280>
jmp    12d3 <fd_timed_write_iov+0x163>
mov    0x2b7e(%rip),%rax
mov    (%rax),%rax
mov    0x2b64(%rip),%rdx
mov    0x2b3d(%rip),%rcx
mov    (%rcx),%rcx
cmp    (%rdx),%rax
je     12d3 <fd_timed_write_iov+0x163>
cmp    %rcx,%rax
je     12d3 <fd_timed_write_iov+0x163>
jmp    1332 <fd_timed_write_iov+0x1c2>
mov    (%rax),%rax
mov    %rax,0x0(%r13)
jmp    1332 <fd_timed_write_iov+0x1c2>
mov    0x2b38(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x0(%r13)
mov    0x8(%rsp),%rbx
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
sub    %ebx,%ebp
jmp    1341 <fd_timed_write_iov+0x1d1>

LLVM IR (-O0):
%struct.iovec = type { i64, i8* }
%struct.timeval = type { i32 }
%struct.pollfd = type { i32, i32, i32 }
@EINVAL = global i64 0
@errno = global i64 0
@ENOMEM = global i64 0
@POLLOUT = global i32 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@ETIMEDOUT = global i64 0
@POLLHUP = global i32 0
@POLLNVAL = global i32 0
@EBADF = global i64 0
@POLLERR = global i32 0
@EIO = global i64 0
define i32 @fd_timed_write_iov(i32 %0, %struct.iovec* %1, i32 %2, %struct.timeval* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.iovec*
%9 = alloca i32
%10 = alloca %struct.timeval*
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.iovec*
%14 = alloca i32
%15 = alloca i64
%16 = alloca i64
%17 = alloca i64
%18 = alloca %struct.pollfd
%19 = alloca i32
%20 = alloca i32
%21 = alloca i32
store i32 %0, i32* %7
store %struct.iovec* %1, %struct.iovec** %8
store i32 %2, i32* %9
store %struct.timeval* %3, %struct.timeval** %10
store i32 %4, i32* %11
%22 = load i32, i32* %7
%23 = icmp slt i32 %22, 0
br i1 %23, label %30, label %24
24: ; preds = %5
%25 = load %struct.iovec*, %struct.iovec** %8
%26 = icmp eq %struct.iovec* %25, null
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = load i32, i32* %9
%29 = icmp sle i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27, %24, %5
%31 = load i64, i64* @EINVAL
store i64 %31, i64* @errno
store i32 -1, i32* %6
br label %236
32: ; preds = %27
%33 = load i32, i32* %9
%34 = sext i32 %33 to i64
%35 = mul i64 16, %34
%36 = trunc i64 %35 to i32
store i32 %36, i32* %12
%37 = load i32, i32* %12
%38 = call %struct.iovec* @malloc(i32 %37)
store %struct.iovec* %38, %struct.iovec** %13
%39 = load %struct.iovec*, %struct.iovec** %13
%40 = icmp eq %struct.iovec* %39, null
br i1 %40, label %41, label %43
41: ; preds = %32
%42 = load i64, i64* @ENOMEM
store i64 %42, i64* @errno
store i32 -1, i32* %6
br label %236
43: ; preds = %32
%44 = load %struct.iovec*, %struct.iovec** %13
%45 = load %struct.iovec*, %struct.iovec** %8
%46 = load i32, i32* %12
%47 = call i32 @memcpy(%struct.iovec* %44, %struct.iovec* %45, i32 %46)
store i32 0, i32* %14
store i64 0, i64* %15
br label %48
48: ; preds = %61, %43
%49 = load i32, i32* %14
%50 = load i32, i32* %9
%51 = icmp slt i32 %49, %50
br i1 %51, label %52, label %64
52: ; preds = %48
%53 = load %struct.iovec*, %struct.iovec** %13
%54 = load i32, i32* %14
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds %struct.iovec, %struct.iovec* %53, i64 %55
%57 = getelementptr inbounds %struct.iovec, %struct.iovec* %56, i32 0, i32 0
%58 = load i64, i64* %57
%59 = load i64, i64* %15
%60 = add i64 %59, %58
store i64 %60, i64* %15
br label %61
61: ; preds = %52
%62 = load i32, i32* %14
%63 = add nsw i32 %62, 1
store i32 %63, i32* %14
br label %48
64: ; preds = %48
%65 = load i64, i64* %15
store i64 %65, i64* %17
store i64 %65, i64* %16
%66 = load i32, i32* %7
%67 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %18, i32 0, i32 0
store i32 %66, i32* %67
%68 = load i32, i32* @POLLOUT
%69 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %18, i32 0, i32 1
store i32 %68, i32* %69
%70 = load i32, i32* %11
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %76
72: ; preds = %64
%73 = load i64, i64* %16
%74 = icmp ugt i64 %73, 0
br i1 %74, label %75, label %76
75: ; preds = %72
store i32 -1, i32* %20
br label %135
76: ; preds = %72, %64
br label %77
77: ; preds = %225, %150, %95, %76
%78 = load i64, i64* %16
%79 = icmp ugt i64 %78, 0
br i1 %79, label %80, label %226
80: ; preds = %77
%81 = load %struct.timeval*, %struct.timeval** %10
%82 = call i32 @_fd_get_poll_timeout(%struct.timeval* %81)
store i32 %82, i32* %20
%83 = load i32, i32* %20
%84 = call i32 @poll(%struct.pollfd* %18, i32 1, i32 %83)
store i32 %84, i32* %19
%85 = load i32, i32* %19
%86 = icmp slt i32 %85, 0
br i1 %86, label %87, label %97
87: ; preds = %80
%88 = load i64, i64* @errno
%89 = load i64, i64* @EINTR
%90 = icmp eq i64 %88, %89
br i1 %90, label %95, label %91
91: ; preds = %87
%92 = load i64, i64* @errno
%93 = load i64, i64* @EAGAIN
%94 = icmp eq i64 %92, %93
br i1 %94, label %95, label %96
95: ; preds = %91, %87
br label %77
96: ; preds = %91
br label %233
97: ; preds = %80
%98 = load i32, i32* %19
%99 = icmp eq i32 %98, 0
br i1 %99, label %100, label %102
100: ; preds = %97
%101 = load i64, i64* @ETIMEDOUT
store i64 %101, i64* @errno
br label %226
102: ; preds = %97
%103 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %18, i32 0, i32 2
%104 = load i32, i32* %103
%105 = load i32, i32* @POLLHUP
%106 = and i32 %104, %105
%107 = icmp ne i32 %106, 0
br i1 %107, label %108, label %109
108: ; preds = %102
br label %226
109: ; preds = %102
%110 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %18, i32 0, i32 2
%111 = load i32, i32* %110
%112 = load i32, i32* @POLLNVAL
%113 = and i32 %111, %112
%114 = icmp ne i32 %113, 0
br i1 %114, label %115, label %117
115: ; preds = %109
%116 = load i64, i64* @EBADF
store i64 %116, i64* @errno
br label %233
117: ; preds = %109
%118 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %18, i32 0, i32 2
%119 = load i32, i32* %118
%120 = load i32, i32* @POLLERR
%121 = and i32 %119, %120
%122 = icmp ne i32 %121, 0
br i1 %122, label %123, label %125
123: ; preds = %117
%124 = load i64, i64* @EIO
store i64 %124, i64* @errno
br label %233
125: ; preds = %117
br label %126
126: ; preds = %125
br label %127
127: ; preds = %126
br label %128
128: ; preds = %127
br label %129
129: ; preds = %128
%130 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %18, i32 0, i32 2
%131 = load i32, i32* %130
%132 = load i32, i32* @POLLOUT
%133 = and i32 %131, %132
%134 = call i32 @assert(i32 %133)
br label %135
135: ; preds = %129, %75
%136 = load i32, i32* %7
%137 = load %struct.iovec*, %struct.iovec** %13
%138 = load i32, i32* %9
%139 = call i32 @writev(i32 %136, %struct.iovec* %137, i32 %138)
store i32 %139, i32* %21
%140 = load i32, i32* %21
%141 = icmp slt i32 %140, 0
br i1 %141, label %142, label %152
142: ; preds = %135
%143 = load i64, i64* @errno
%144 = load i64, i64* @EINTR
%145 = icmp eq i64 %143, %144
br i1 %145, label %150, label %146
146: ; preds = %142
%147 = load i64, i64* @errno
%148 = load i64, i64* @EAGAIN
%149 = icmp eq i64 %147, %148
br i1 %149, label %150, label %151
150: ; preds = %146, %142
br label %77
151: ; preds = %146
br label %233
152: ; preds = %135
%153 = load i32, i32* %21
%154 = sext i32 %153 to i64
%155 = load i64, i64* %16
%156 = sub i64 %155, %154
store i64 %156, i64* %16
%157 = load i32, i32* %20
%158 = icmp eq i32 %157, 0
br i1 %158, label %159, label %160
159: ; preds = %152
br label %226
160: ; preds = %152
store i32 0, i32* %14
br label %161
161: ; preds = %222, %160
%162 = load i32, i32* %14
%163 = load i32, i32* %9
%164 = icmp slt i32 %162, %163
br i1 %164, label %165, label %168
165: ; preds = %161
%166 = load i32, i32* %21
%167 = icmp sgt i32 %166, 0
br label %168
168: ; preds = %165, %161
%169 = phi i1 [ false, %161 ], [ %167, %165 ]
br i1 %169, label %170, label %225
170: ; preds = %168
%171 = load i32, i32* %21
%172 = sext i32 %171 to i64
%173 = load %struct.iovec*, %struct.iovec** %13
%174 = load i32, i32* %14
%175 = sext i32 %174 to i64
%176 = getelementptr inbounds %struct.iovec, %struct.iovec* %173, i64 %175
%177 = getelementptr inbounds %struct.iovec, %struct.iovec* %176, i32 0, i32 0
%178 = load i64, i64* %177
%179 = icmp ugt i64 %172, %178
br i1 %179, label %180, label %187
180: ; preds = %170
%181 = load %struct.iovec*, %struct.iovec** %13
%182 = load i32, i32* %14
%183 = sext i32 %182 to i64
%184 = getelementptr inbounds %struct.iovec, %struct.iovec* %181, i64 %183
%185 = getelementptr inbounds %struct.iovec, %struct.iovec* %184, i32 0, i32 0
%186 = load i64, i64* %185
br label %190
187: ; preds = %170
%188 = load i32, i32* %21
%189 = sext i32 %188 to i64
br label %190
190: ; preds = %187, %180
%191 = phi i64 [ %186, %180 ], [ %189, %187 ]
store i64 %191, i64* %15
%192 = load i64, i64* %15
%193 = icmp eq i64 %192, 0
br i1 %193, label %194, label %195
194: ; preds = %190
br label %222
195: ; preds = %190
%196 = load i64, i64* %15
%197 = load i32, i32* %21
%198 = sext i32 %197 to i64
%199 = sub i64 %198, %196
%200 = trunc i64 %199 to i32
store i32 %200, i32* %21
%201 = load i64, i64* %15
%202 = load %struct.iovec*, %struct.iovec** %13
%203 = load i32, i32* %14
%204 = sext i32 %203 to i64
%205 = getelementptr inbounds %struct.iovec, %struct.iovec* %202, i64 %204
%206 = getelementptr inbounds %struct.iovec, %struct.iovec* %205, i32 0, i32 0
%207 = load i64, i64* %206
%208 = sub i64 %207, %201
store i64 %208, i64* %206
%209 = load %struct.iovec*, %struct.iovec** %13
%210 = load i32, i32* %14
%211 = sext i32 %210 to i64
%212 = getelementptr inbounds %struct.iovec, %struct.iovec* %209, i64 %211
%213 = getelementptr inbounds %struct.iovec, %struct.iovec* %212, i32 0, i32 1
%214 = load i8*, i8** %213
%215 = load i64, i64* %15
%216 = getelementptr inbounds i8, i8* %214, i64 %215
%217 = load %struct.iovec*, %struct.iovec** %13
%218 = load i32, i32* %14
%219 = sext i32 %218 to i64
%220 = getelementptr inbounds %struct.iovec, %struct.iovec* %217, i64 %219
%221 = getelementptr inbounds %struct.iovec, %struct.iovec* %220, i32 0, i32 1
store i8* %216, i8** %221
br label %222
222: ; preds = %195, %194
%223 = load i32, i32* %14
%224 = add nsw i32 %223, 1
store i32 %224, i32* %14
br label %161
225: ; preds = %168
br label %77
226: ; preds = %159, %108, %100, %77
%227 = load %struct.iovec*, %struct.iovec** %13
%228 = call i32 @free(%struct.iovec* %227)
%229 = load i64, i64* %17
%230 = load i64, i64* %16
%231 = sub i64 %229, %230
%232 = trunc i64 %231 to i32
store i32 %232, i32* %6
br label %236
233: ; preds = %151, %123, %115, %96
%234 = load %struct.iovec*, %struct.iovec** %13
%235 = call i32 @free(%struct.iovec* %234)
store i32 -1, i32* %6
br label %236
236: ; preds = %233, %226, %41, %30
%237 = load i32, i32* %6
ret i32 %237
}

LLVM IR (-O2):
%struct.iovec = type { i64, i8* }
%struct.timeval = type { i32 }
%struct.pollfd = type { i32, i32, i32 }
@EINVAL = global i64 0
@errno = global i64 0
@ENOMEM = global i64 0
@POLLOUT = global i32 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@ETIMEDOUT = global i64 0
@POLLHUP = global i32 0
@POLLNVAL = global i32 0
@EBADF = global i64 0
@POLLERR = global i32 0
@EIO = global i64 0
define i32 @fd_timed_write_iov(i32 %0, %struct.iovec* %1, i32 %2, %struct.timeval* %3, i32 %4) {
%6 = alloca %struct.pollfd
%7 = bitcast %struct.pollfd* %6 to i8*
%8 = icmp slt i32 %0, 0
%9 = icmp eq %struct.iovec* %1, null
%10 = or i1 %8, %9
%11 = icmp slt i32 %2, 1
%12 = or i1 %10, %11
br i1 %12, label %13, label %15
13: ; preds = %5
%14 = load i64, i64* @EINVAL
store i64 %14, i64* @errno
br label %156
15: ; preds = %5
%16 = shl i32 %2, 4
%17 = tail call %struct.iovec* @malloc(i32 %16)
%18 = icmp eq %struct.iovec* %17, null
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = load i64, i64* @ENOMEM
store i64 %20, i64* @errno
br label %156
21: ; preds = %15
%22 = tail call i32 @memcpy(%struct.iovec* %17, %struct.iovec* %1, i32 %16)
%23 = zext i32 %2 to i64
%24 = icmp ult i32 %2, 5
br i1 %24, label %50, label %25
25: ; preds = %21
%26 = and i64 %23, 3
%27 = icmp eq i64 %26, 0
%28 = select i1 %27, i64 4, i64 %26
%29 = sub nsw i64 %23, %28
br label %30
30: ; preds = %30, %25
%31 = phi i64 [ 0, %25 ], [ %45, %30 ]
%32 = phi <2 x i64> [ zeroinitializer, %25 ], [ %43, %30 ]
%33 = phi <2 x i64> [ zeroinitializer, %25 ], [ %44, %30 ]
%34 = or i64 %31, 2
%35 = getelementptr inbounds %struct.iovec, %struct.iovec* %17, i64 %31, i32 0
%36 = getelementptr inbounds %struct.iovec, %struct.iovec* %17, i64 %34, i32 0
%37 = bitcast i64* %35 to <4 x i64>*
%38 = bitcast i64* %36 to <4 x i64>*
%39 = load <4 x i64>, <4 x i64>* %37
%40 = load <4 x i64>, <4 x i64>* %38
%41 = shufflevector <4 x i64> %39, <4 x i64> poison, <2 x i32> <i32 0, i32 2>
%42 = shufflevector <4 x i64> %40, <4 x i64> poison, <2 x i32> <i32 0, i32 2>
%43 = add <2 x i64> %41, %32
%44 = add <2 x i64> %42, %33
%45 = add nuw i64 %31, 4
%46 = icmp eq i64 %45, %29
br i1 %46, label %47, label %30
47: ; preds = %30
%48 = add <2 x i64> %44, %43
%49 = call i64 @llvm.vector.reduce.add.v2i64(<2 x i64> %48)
br label %50
50: ; preds = %21, %47
%51 = phi i64 [ 0, %21 ], [ %29, %47 ]
%52 = phi i64 [ 0, %21 ], [ %49, %47 ]
br label %53
53: ; preds = %50, %53
%54 = phi i64 [ %59, %53 ], [ %51, %50 ]
%55 = phi i64 [ %58, %53 ], [ %52, %50 ]
%56 = getelementptr inbounds %struct.iovec, %struct.iovec* %17, i64 %54, i32 0
%57 = load i64, i64* %56
%58 = add i64 %57, %55
%59 = add nuw nsw i64 %54, 1
%60 = icmp eq i64 %59, %23
br i1 %60, label %61, label %53
61: ; preds = %53
%62 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i64 0, i32 0
store i32 %0, i32* %62
%63 = load i32, i32* @POLLOUT
%64 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i64 0, i32 1
store i32 %63, i32* %64
%65 = icmp ne i32 %4, 0
%66 = icmp ne i64 %58, 0
%67 = select i1 %65, i1 %66, i1 false
br i1 %67, label %104, label %68
68: ; preds = %140, %120, %61, %109
%69 = phi i64 [ %105, %109 ], [ %58, %61 ], [ %118, %120 ], [ %118, %140 ]
%70 = icmp eq i64 %69, 0
br i1 %70, label %146, label %71
71: ; preds = %68, %75
%72 = call i32 @_fd_get_poll_timeout(%struct.timeval* %3)
%73 = call i32 @poll(%struct.pollfd* %6, i32 1, i32 %72)
%74 = icmp slt i32 %73, 0
br i1 %74, label %75, label %82
75: ; preds = %71
%76 = load i64, i64* @errno
%77 = load i64, i64* @EINTR
%78 = icmp eq i64 %76, %77
%79 = load i64, i64* @EAGAIN
%80 = icmp eq i64 %76, %79
%81 = select i1 %78, i1 true, i1 %80
br i1 %81, label %71, label %154
82: ; preds = %71
%83 = icmp eq i32 %73, 0
br i1 %83, label %84, label %86
84: ; preds = %82
%85 = load i64, i64* @ETIMEDOUT
store i64 %85, i64* @errno
br label %146
86: ; preds = %82
%87 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i64 0, i32 2
%88 = load i32, i32* %87
%89 = load i32, i32* @POLLHUP
%90 = and i32 %89, %88
%91 = icmp eq i32 %90, 0
br i1 %91, label %92, label %146
92: ; preds = %86
%93 = load i32, i32* @POLLNVAL
%94 = and i32 %93, %88
%95 = icmp eq i32 %94, 0
br i1 %95, label %96, label %151
96: ; preds = %92
%97 = load i32, i32* @POLLERR
%98 = and i32 %97, %88
%99 = icmp eq i32 %98, 0
br i1 %99, label %100, label %151
100: ; preds = %96
%101 = load i32, i32* @POLLOUT
%102 = and i32 %101, %88
%103 = call i32 @assert(i32 %102)
br label %104
104: ; preds = %61, %100
%105 = phi i64 [ %69, %100 ], [ %58, %61 ]
%106 = phi i32 [ %72, %100 ], [ -1, %61 ]
%107 = call i32 @writev(i32 %0, %struct.iovec* %17, i32 %2)
%108 = icmp slt i32 %107, 0
br i1 %108, label %109, label %116
109: ; preds = %104
%110 = load i64, i64* @errno
%111 = load i64, i64* @EINTR
%112 = icmp eq i64 %110, %111
%113 = load i64, i64* @EAGAIN
%114 = icmp eq i64 %110, %113
%115 = select i1 %112, i1 true, i1 %114
br i1 %115, label %68, label %154
116: ; preds = %104
%117 = zext i32 %107 to i64
%118 = sub i64 %105, %117
%119 = icmp eq i32 %106, 0
br i1 %119, label %146, label %120
120: ; preds = %116
%121 = icmp eq i32 %107, 0
br i1 %121, label %68, label %122
122: ; preds = %120
%123 = sext i32 %2 to i64
br label %124
124: ; preds = %122, %140
%125 = phi i64 [ 0, %122 ], [ %142, %140 ]
%126 = phi i32 [ %107, %122 ], [ %141, %140 ]
%127 = zext i32 %126 to i64
%128 = getelementptr inbounds %struct.iovec, %struct.iovec* %17, i64 %125, i32 0
%129 = load i64, i64* %128
%130 = icmp ult i64 %129, %127
%131 = select i1 %130, i64 %129, i64 %127
%132 = icmp eq i64 %131, 0
br i1 %132, label %140, label %133
133: ; preds = %124
%134 = trunc i64 %131 to i32
%135 = sub nsw i32 %126, %134
%136 = sub i64 %129, %131
store i64 %136, i64* %128
%137 = getelementptr inbounds %struct.iovec, %struct.iovec* %17, i64 %125, i32 1
%138 = load i8*, i8** %137
%139 = getelementptr inbounds i8, i8* %138, i64 %131
store i8* %139, i8** %137
br label %140
140: ; preds = %124, %133
%141 = phi i32 [ %126, %124 ], [ %135, %133 ]
%142 = add nuw nsw i64 %125, 1
%143 = icmp slt i64 %142, %123
%144 = icmp sgt i32 %141, 0
%145 = select i1 %143, i1 %144, i1 false
br i1 %145, label %124, label %68
146: ; preds = %68, %116, %86, %84
%147 = phi i64 [ %69, %84 ], [ %69, %86 ], [ %118, %116 ], [ 0, %68 ]
%148 = call i32 @free(%struct.iovec* %17)
%149 = sub i64 %58, %147
%150 = trunc i64 %149 to i32
br label %156
151: ; preds = %96, %92
%152 = phi i64* [ @EBADF, %92 ], [ @EIO, %96 ]
%153 = load i64, i64* %152
store i64 %153, i64* @errno
br label %154
154: ; preds = %75, %151, %109
%155 = call i32 @free(%struct.iovec* %17)
br label %156
156: ; preds = %154, %146, %19, %13
%157 = phi i32 [ -1, %13 ], [ -1, %19 ], [ -1, %154 ], [ %150, %146 ]
ret i32 %157
}