// File: roberty65[P]bServer/common/src/xbs_io.cpp
// Function: beyondy::XbsWriteVN(int, iovec const*, int, long)
ssize_t XbsWriteVN(int fd, const struct iovec *iov, int iovcnt, long msTimedout)
{
	TimedoutCountdown toCountdown(msTimedout);
	size_t sed;
	ssize_t slen;

	sed = XbsWriteV(fd, iov, iovcnt, toCountdown.Update());
	if (sed < 0) return -1;

	int i = 0;
	size_t left = sed;
	while (i < iovcnt) {
		if (iov[i].iov_len > left) {
			break;
		}
		else {
			left -= iov[i].iov_len;
		}

		++i;
	}

	for (const struct iovec *next = &iov[i]; next != &iov[iovcnt]; ++next) {
		slen = XbsWriteN(fd, (char *)next->iov_base + left, next->iov_len - left, toCountdown.Update());
		if (slen < 0) break;
		if (static_cast<size_t>(slen) < (next->iov_len - left)) break;
		sed += static_cast<size_t>(slen);
		left = 0;
	}

	return (sed > 0) ? static_cast<ssize_t>(sed) : -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movl %edi, -0xc(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x28(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x111a0
movl -0xc(%rbp), %eax
movl %eax, -0x7c(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x78(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x6c(%rbp)
leaq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x111d0
movl -0x7c(%rbp), %edi
movq -0x78(%rbp), %rsi
movl -0x6c(%rbp), %edx
movq %rax, %rcx
callq 0x10e10
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jae 0x10ed1
movq $-0x1, -0x8(%rbp)
jmp 0x1102a
movl $0x0, -0x54(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x60(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x10f2c
movq -0x18(%rbp), %rax
movslq -0x54(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
cmpq -0x60(%rbp), %rax
jbe 0x10f03
jmp 0x10f2c
movq -0x18(%rbp), %rax
movslq -0x54(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
movq -0x60(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x60(%rbp)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x10ee0
movq -0x18(%rbp), %rax
movslq -0x54(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq -0x1c(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
je 0x10ffb
movl -0xc(%rbp), %eax
movl %eax, -0x94(%rbp)
movq -0x68(%rbp), %rax
movq (%rax), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x68(%rbp), %rax
movq 0x8(%rax), %rax
subq -0x60(%rbp), %rax
movq %rax, -0x88(%rbp)
leaq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x111d0
movl -0x94(%rbp), %edi
movq -0x90(%rbp), %rsi
movq -0x88(%rbp), %rdx
movq %rax, %rcx
callq 0x10d30
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jge 0x10fbf
jmp 0x10ffb
movq -0x50(%rbp), %rax
movq -0x68(%rbp), %rcx
movq 0x8(%rcx), %rcx
subq -0x60(%rbp), %rcx
cmpq %rcx, %rax
jae 0x10fd6
jmp 0x10ffb
movq -0x50(%rbp), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x68(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x68(%rbp)
jmp 0x10f3f
cmpq $0x0, -0x48(%rbp)
jbe 0x1100f
movq -0x48(%rbp), %rax
movq %rax, -0xa0(%rbp)
jmp 0x1101f
movq $-0x1, %rax
movq %rax, -0xa0(%rbp)
jmp 0x1101f
movq -0xa0(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
nopw (%rax,%rax)