// File: /workspace/github_c_makefile_star2_O3/sharpyfox[P]citrusleaf-client/src/cf_socket.c
// Function: cf_socket_write_timeout
int
cf_socket_write_timeout(int fd, uint8_t *buf, size_t buf_len, uint64_t trans_deadline, int attempt_ms)
{
	if (!buf_len) {
		return 0;
	}

	char *ctx = "cf_socket_write_timeout()";
	int errbuf_len = 64;
	char errbuf[errbuf_len];

	int flags;
	if (-1 == (flags = fcntl(fd, F_GETFL, 0))) {
		flags = 0;
	}
	if (!(flags & O_NONBLOCK)) {
		if (-1 == fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
			return(ENOENT);
		}
	}

	// between the transaction deadline and the attempt_ms, find the lesser
	// and create a deadline for this attempt
	uint64_t deadline = attempt_ms + cf_getms();
	if ((trans_deadline != 0) && (trans_deadline < deadline)) {
		deadline = trans_deadline;
	}

	int rv = 0, epoll_fd = -1, busy = 0, try = 0;
	size_t pos = 0;
	uint64_t start = cf_getms();

	if (0 > (epoll_fd = epoll_create(1))) {
		rv = errno;
		cf_warn("%s: epoll_create() failed (errno %d: \"%s\") ~~ Failing!", ctx, errno, my_strerror_r(errno, errbuf, errbuf_len));
		goto Fail;
	}

	struct epoll_event event;
	memset(&event, 0, sizeof(struct epoll_event));
	event.data.fd = fd;
	event.events = EPOLLOUT;

	if (0 > epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &event)) {
		rv = errno;
		cf_warn("%s: epoll_ctl(ADD) of socket failed (errno %d: \"%s\") ~~ Failing!", ctx, errno, my_strerror_r(errno, errbuf, errbuf_len));
		goto Fail;
	}

	do {
		uint64_t now = cf_getms();
		if (now > deadline) {
#ifdef DEBUG_TIME
			debug_time_printf("socket write timeout 1", try, busy, start, now, deadline);
#endif
			rv = ETIMEDOUT;
			goto Fail;
		}

		uint64_t ms_left = deadline - now;

		int nevents = 0;
		int max_events = 1;
		int wait_ms = 1;
		struct epoll_event events[max_events];

		if (0 > (nevents = epoll_wait(epoll_fd, events, max_events, wait_ms))) {
			if ((rv = errno) == EINTR) {
				cf_debug("%s: epoll_wait() on socket encountered EINTR ~~ Retrying!", ctx);
				busy++;
				goto Retry;
			} else {
				cf_warn("%s: epoll_wait() on socket failed (errno %d: \"%s\") ~~ Failing!", ctx, errno, my_strerror_r(errno, errbuf, errbuf_len));
			}
			goto Fail;
		} else {
			if (nevents == 0) {
				cf_debug("%s: epoll_wait() returned no events ~~ Retrying!", ctx);
				busy++;
				goto Retry;
			}
			if (nevents != 1) {
				cf_warn("%s: epoll_wait() returned %d events ~~ only 1 expected, so ignoring others!", ctx, nevents);
			}
			if (events[0].data.fd == fd) {
				if (events[0].events & EPOLLOUT) {
					cf_debug("%s: epoll_wait() on socket ready for write detected ~~ Succeeding!", ctx);
				} else {
					// (Note:  ERR and HUP events are automatically waited for as well.)
					if (events[0].events & (EPOLLERR | EPOLLHUP)) {
						cf_debug("%s: epoll_wait() on socket detected failure event 0x%x ~~ Failing!", ctx, events[0].events);
					} else {
						cf_warn("%s: epoll_wait() on socket detected non-write events 0x%x ~~ Failing!", ctx, events[0].events);
					}
					rv = EBADF;
					goto Fail;
				}
			} else {
				cf_warn("%s: epoll_wait() on socket returned event on unknown socket %d ~~ Retrying!", ctx, events[0].data.fd);
				goto Retry;
			}

			int r_bytes = write(fd, buf + pos, buf_len - pos);

			if (r_bytes > 0) {
				pos += r_bytes;
				if (pos >= buf_len)	{
					goto Success;
				}
			} else if (r_bytes == 0) {
				// It's likely the socket has been closed on the remote side.
				rv = EBADF;
				goto Fail;
			} else if ((errno != ETIMEDOUT) && (errno != EWOULDBLOCK) && (errno != EINPROGRESS) && (errno != EAGAIN)) {
#ifdef DEBUG_TIME
				debug_time_printf("socket write timeout 2", try, busy, start, now, deadline);
#endif
				rv = errno;
				goto Fail;
			}
		}

Retry:
		try++;

	} while (pos < buf_len);

Success:
	if (0 > epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, &event)) {
		cf_warn("%s: epoll_ctl(DEL) on socket failed (errno %d: \"%s\")", ctx, errno, my_strerror_r(errno, errbuf, errbuf_len));
	}
	close(epoll_fd);

	rv = 0;
	goto Done;

Fail:
	cf_debug("%s: socket write timeout fail: %d (%s)", ctx, errno, my_strerror_r(errno, errbuf, errbuf_len));

	if (epoll_fd > 0) {
		if (0 > epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, &event)) {
			cf_warn("%s: epoll_ctl(DEL) on socket failed (errno %d: \"%s\")", ctx, errno, my_strerror_r(errno, errbuf, errbuf_len));
		}
		close(epoll_fd);
	}

Done:
	return(rv);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
testq %rdx, %rdx
movq %rsi, -0x68(%rbp)
je 0xdde4
movq %rsp, %rax
movl %r8d, %r13d
movq %rcx, %r14
movq %rsp, %r12
movq %rdx, -0x60(%rbp)
addq $-0x40, %rax
movq %rax, -0x48(%rbp)
movq %rax, %rsp
movl $0x3, %esi
xorl %edx, %edx
xorl %eax, %eax
xorl %ebx, %ebx
movl %edi, %r15d
callq 0x2450
cmpl $-0x1, %eax
cmovel %ebx, %eax
btl $0xb, %eax
jb 0xdb60
orl $0x800, %eax            # imm = 0x800
movl %r15d, %edi
movl $0x4, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x2450
cmpl $-0x1, %eax
je 0xde92
movslq %r13d, %rbx
leaq -0x58(%rbp), %r13
movl $0x1, %edi
movl %r15d, -0x3c(%rbp)
movq %r12, -0x38(%rbp)
movq %r13, %rsi
callq 0x2230
movabsq $0x431bde82d7b634db, %rax # imm = 0x431BDE82D7B634DB
movq %r14, %r15
leaq -0x58(%rbp), %rsi
movl $0x1, %edi
mulq 0x8(%r13)
imulq $0x3e8, (%r13), %rax    # imm = 0x3E8
shrq $0x12, %rdx
addq %rbx, %rax
addq %rdx, %rax
cmpq %r14, %rax
cmovbq %rax, %r15
testq %r14, %r14
cmoveq %rax, %r15
callq 0x2230
movl $0x1, %edi
callq 0x2520
movl %eax, %r14d
testl %eax, %eax
js 0xddec
movl -0x3c(%rbp), %edx
leaq -0x74(%rbp), %rcx
movl %r14d, %edi
movl $0x1, %esi
movl $0x0, 0x8(%rcx)
movl %edx, 0x4(%rcx)
movl $0x4, (%rcx)
callq 0x2320
testl %eax, %eax
js 0xde31
movq -0x60(%rbp), %r12
movl %r14d, -0x2c(%rbp)
xorl %r14d, %r14d
movl $0x1, %edi
leaq -0x58(%rbp), %rsi
callq 0x2230
movabsq $0x431bde82d7b634db, %rax # imm = 0x431BDE82D7B634DB
mulq -0x50(%rbp)
imulq $0x3e8, -0x58(%rbp), %rax # imm = 0x3E8
shrq $0x12, %rdx
addq %rdx, %rax
cmpq %r15, %rax
ja 0xdea0
movq %rsp, %r13
movq %rsp, %rbx
addq $-0x10, %r13
movq %r13, %rsp
movl -0x2c(%rbp), %edi
movq %r13, %rsi
movl $0x1, %edx
movl $0x1, %ecx
callq 0x2110
testl %eax, %eax
js 0xdc95
cmpl $0x1, %eax
je 0xdd0f
testl %eax, %eax
jne 0xdcdf
leaq 0x8653(%rip), %rax      # 0x162c8
movl (%rax), %eax
cmpl $0x3, %eax
jl 0xddae
leaq 0x8649(%rip), %rax      # 0x162d0
movl $0x3, %edi
leaq 0x2cb7(%rip), %rsi      # 0x1094a
jmp 0xdccc
callq 0x2040
leaq 0x8627(%rip), %rcx      # 0x162c8
movl (%rax), %r13d
movl (%rcx), %ecx
cmpl $0x4, %r13d
jne 0xdeb3
cmpl $0x3, %ecx
jl 0xddae
leaq 0x8610(%rip), %rax      # 0x162d0
movl $0x3, %edi
leaq 0x2c05(%rip), %rsi      # 0x108d1
movq (%rax), %rcx
leaq 0x2e62(%rip), %rdx      # 0x10b38
xorl %eax, %eax
callq *%rcx
jmp 0xddae
leaq 0x85e2(%rip), %rcx      # 0x162c8
cmpl $0x0, (%rcx)
jle 0xdd0f
leaq 0x85de(%rip), %rcx      # 0x162d0
movl $0x1, %edi
leaq 0x2c7d(%rip), %rsi      # 0x1097b
leaq 0x2e33(%rip), %rdx      # 0x10b38
movq (%rcx), %r8
movl %eax, %ecx
xorl %eax, %eax
callq *%r8
movl 0x4(%r13), %ecx
cmpl -0x3c(%rbp), %ecx
jne 0xdd80
movl (%r13), %ecx
testb $0x4, %cl
je 0xdf03
leaq 0x859c(%rip), %rax      # 0x162c8
movl (%rax), %eax
cmpl $0x3, %eax
jl 0xdd54
leaq 0x8596(%rip), %rax      # 0x162d0
movl $0x3, %edi
leaq 0x2e0c(%rip), %rsi      # 0x10b52
leaq 0x2deb(%rip), %rdx      # 0x10b38
movq (%rax), %rcx
xorl %eax, %eax
callq *%rcx
movq -0x68(%rbp), %rax
movl -0x3c(%rbp), %edi
movq %r12, %rdx
subq %r14, %rdx
leaq (%rax,%r14), %rsi
callq 0x20c0
testl %eax, %eax
jle 0xddbf
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq %rax, %r14
cmpq %r12, %r14
jb 0xddae
jmp 0xdf4b
leaq 0x8541(%rip), %rax      # 0x162c8
cmpl $0x0, (%rax)
jle 0xddae
leaq 0x853d(%rip), %rax      # 0x162d0
movl $0x1, %edi
leaq 0x2cf2(%rip), %rsi      # 0x10a91
leaq 0x2d92(%rip), %rdx      # 0x10b38
movq (%rax), %r8
xorl %eax, %eax
callq *%r8
movq %rbx, %rsp
cmpq %r12, %r14
jb 0xdc07
jmp 0xdf4e
je 0xe0ff
callq 0x2040
movl (%rax), %r13d
cmpl $0xb, %r13d
je 0xddae
cmpl $0x6e, %r13d
je 0xddae
cmpl $0x73, %r13d
je 0xddae
jmp 0xe105
xorl %r13d, %r13d
jmp 0xe0ed
callq 0x2040
leaq 0x84d0(%rip), %rcx      # 0x162c8
movl (%rax), %r13d
cmpl $0x0, (%rcx)
jle 0xde89
leaq 0x84c5(%rip), %rcx      # 0x162d0
movl %r14d, %r15d
movl $0x40, %edx
movq (%rcx), %rbx
movq -0x48(%rbp), %r14
movb $0x0, (%r14)
movq %r14, %rsi
movl (%rax), %edi
callq 0x2100
leaq 0x2a2a(%rip), %rsi      # 0x10859
jmp 0xde70
callq 0x2040
leaq 0x848b(%rip), %rcx      # 0x162c8
movl (%rax), %r13d
cmpl $0x0, (%rcx)
jle 0xde89
leaq 0x8484(%rip), %rcx      # 0x162d0
movl %r14d, %r15d
movl $0x40, %edx
movq (%rcx), %rbx
movq -0x48(%rbp), %r14
movb $0x0, (%r14)
movq %r14, %rsi
movl (%rax), %edi
callq 0x2100
leaq 0x2a20(%rip), %rsi      # 0x10890
movq %r14, %r8
leaq 0x2cbe(%rip), %rdx      # 0x10b38
movl $0x1, %edi
movl %r13d, %ecx
xorl %eax, %eax
movl %r15d, %r14d
callq *%rbx
movq -0x38(%rbp), %r15
jmp 0xe00d
movl $0x2, %r13d
movq %r12, %r15
jmp 0xe0ea
movl $0x6e, %r13d
movq -0x38(%rbp), %r15
movl -0x2c(%rbp), %r14d
jmp 0xe00d
testl %ecx, %ecx
jle 0xdef6
leaq 0x8412(%rip), %rcx      # 0x162d0
movl $0x40, %edx
movq (%rcx), %r14
movq -0x48(%rbp), %r15
movb $0x0, (%r15)
movq %r15, %rsi
movl (%rax), %edi
callq 0x2100
leaq 0x2a2c(%rip), %rsi      # 0x1090b
leaq 0x2c52(%rip), %rdx      # 0x10b38
movl $0x1, %edi
movl %r13d, %ecx
movq %r15, %r8
xorl %eax, %eax
callq *%r14
movq -0x38(%rbp), %r15
movl -0x2c(%rbp), %r14d
jmp 0xe00a
leaq 0x83be(%rip), %rax      # 0x162c8
testb $0x18, %cl
movl $0x9, %r13d
movl (%rax), %eax
je 0xdfdc
movq -0x38(%rbp), %r15
movl -0x2c(%rbp), %r14d
cmpl $0x3, %eax
jl 0xe00a
leaq 0x839d(%rip), %rax      # 0x162d0
leaq 0x2acf(%rip), %rsi      # 0x10a09
leaq 0x2bf7(%rip), %rdx      # 0x10b38
movl $0x3, %edi
jmp 0xe002
movq %rbx, %rsp
movl -0x2c(%rbp), %r14d
movl -0x3c(%rbp), %edx
leaq -0x74(%rbp), %rcx
movl $0x2, %esi
movl %r14d, %edi
callq 0x2320
xorl %r13d, %r13d
testl %eax, %eax
js 0xdf76
movq -0x38(%rbp), %r15
jmp 0xe0e2
leaq 0x834b(%rip), %rax      # 0x162c8
cmpl $0x0, (%rax)
movq -0x38(%rbp), %r15
jle 0xe0e2
leaq 0x833f(%rip), %rax      # 0x162d0
movq (%rax), %rbx
callq 0x2040
movq -0x48(%rbp), %r14
movl (%rax), %r15d
movl $0x40, %edx
movb $0x0, (%r14)
movq %r14, %rsi
movl (%rax), %edi
callq 0x2100
movl %r15d, %ecx
movq %r14, %r8
movq -0x38(%rbp), %r15
movl -0x2c(%rbp), %r14d
leaq 0x2b15(%rip), %rsi      # 0x10add
leaq 0x2b69(%rip), %rdx      # 0x10b38
xorl %r13d, %r13d
movl $0x1, %edi
jmp 0xe0de
movq -0x38(%rbp), %r15
movl -0x2c(%rbp), %r14d
testl %eax, %eax
jle 0xe00a
leaq 0x82e1(%rip), %rax      # 0x162d0
leaq 0x2b9f(%rip), %rsi      # 0x10b95
leaq 0x2b3b(%rip), %rdx      # 0x10b38
movl $0x1, %edi
movq (%rax), %r8
xorl %eax, %eax
callq *%r8
movq %rbx, %rsp
leaq 0x82b4(%rip), %rbx      # 0x162c8
movl (%rbx), %eax
cmpl $0x3, %eax
jl 0xe070
leaq 0x82ae(%rip), %rax      # 0x162d0
movl %r14d, -0x2c(%rbp)
movq (%rax), %r14
callq 0x2040
movq -0x48(%rbp), %rbx
movl (%rax), %r12d
movl $0x40, %edx
movb $0x0, (%rbx)
movq %rbx, %rsi
movl (%rax), %edi
callq 0x2100
movq %rbx, %r8
leaq 0x8277(%rip), %rbx      # 0x162c8
leaq 0x2b83(%rip), %rsi      # 0x10bdb
leaq 0x2ad9(%rip), %rdx      # 0x10b38
movl $0x3, %edi
movl %r12d, %ecx
xorl %eax, %eax
callq *%r14
movl -0x2c(%rbp), %r14d
testl %r14d, %r14d
jle 0xe0ea
movl -0x3c(%rbp), %edx
leaq -0x74(%rbp), %rcx
movl %r14d, %edi
movl $0x2, %esi
callq 0x2320
testl %eax, %eax
jns 0xe0e2
cmpl $0x0, (%rbx)
jle 0xe0e2
leaq 0x8237(%rip), %rax      # 0x162d0
movq (%rax), %rbx
callq 0x2040
movl %r14d, %r12d
movq -0x48(%rbp), %r14
movl (%rax), %r15d
movl $0x40, %edx
movb $0x0, (%r14)
movq %r14, %rsi
movl (%rax), %edi
callq 0x2100
movl %r15d, %ecx
movq -0x38(%rbp), %r15
movq %r14, %r8
leaq 0x2a0e(%rip), %rsi      # 0x10add
leaq 0x2a62(%rip), %rdx      # 0x10b38
movl $0x1, %edi
movl %r12d, %r14d
xorl %eax, %eax
callq *%rbx
movl %r14d, %edi
callq 0x2550
movq %r15, %rsp
movl %r13d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x9, %r13d
movq %rbx, %rsp
jmp 0xdea6