// File: /workspace/github_c_makefile_star2_O1/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 $0x68, %rsp
testq %rdx, %rdx
je 0xe35d
movl %r8d, %r13d
movq %rcx, %r14
movq %rdx, %r12
movl %edi, %r15d
movq %rsi, -0x70(%rbp)
movq %rsp, -0x78(%rbp)
movq %rsp, %rax
addq $-0x40, %rax
movq %rax, -0x48(%rbp)
movq %rax, %rsp
xorl %ebx, %ebx
movl $0x3, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2450
cmpl $-0x1, %eax
cmovel %ebx, %eax
btl $0xb, %eax
jb 0xdf8b
orl $0x800, %eax            # imm = 0x800
movl %r15d, %edi
movl $0x4, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x2450
cmpl $-0x1, %eax
je 0xe54e
movl %r15d, -0x2c(%rbp)
movslq %r13d, %rbx
leaq -0x60(%rbp), %r13
movl $0x1, %edi
movq %r13, %rsi
callq 0x2230
movabsq $0x431bde82d7b634db, %rax # imm = 0x431BDE82D7B634DB
mulq 0x8(%r13)
shrq $0x12, %rdx
imulq $0x3e8, (%r13), %rax    # imm = 0x3E8
addq %rbx, %rax
addq %rdx, %rax
cmpq %r14, %rax
movq %r14, %r15
cmovbq %rax, %r15
testq %r14, %r14
cmoveq %rax, %r15
leaq -0x60(%rbp), %rsi
movl $0x1, %edi
callq 0x2230
movl $0x1, %edi
callq 0x2520
testl %eax, %eax
movl %eax, -0x3c(%rbp)
js 0xe365
leaq -0x88(%rbp), %rcx
movq $0x0, (%rcx)
movl $0x0, 0x8(%rcx)
movl -0x2c(%rbp), %edx
movl %edx, 0x4(%rcx)
movl $0x4, (%rcx)
movl %eax, %edi
movl $0x1, %esi
callq 0x2320
testl %eax, %eax
js 0xe42e
movq %r12, -0x50(%rbp)
xorl %r12d, %r12d
xorl %r14d, %r14d
movl $0x1, %edi
movq %r13, %rsi
callq 0x2230
movabsq $0x431bde82d7b634db, %rax # imm = 0x431BDE82D7B634DB
mulq -0x58(%rbp)
shrq $0x12, %rdx
imulq $0x3e8, -0x60(%rbp), %rax # imm = 0x3E8
addq %rdx, %rax
cmpq %r15, %rax
jbe 0xe075
movl $0x2, %ebx
movl $0x6e, %r14d
jmp 0xe253
movq %rsp, -0x38(%rbp)
movq %rsp, %r13
addq $-0x10, %r13
movq %r13, %rsp
movl -0x3c(%rbp), %edi
movq %r13, %rsi
movl $0x1, %edx
movl $0x1, %ecx
callq 0x2110
testl %eax, %eax
js 0xe0e7
cmpl $0x1, %eax
je 0xe169
testl %eax, %eax
jne 0xe139
leaq 0x9214(%rip), %rax      # 0x172c8
movl (%rax), %eax
cmpl $0x3, %eax
jl 0xe240
leaq 0x920a(%rip), %rax      # 0x172d0
movq (%rax), %rcx
xorl %ebx, %ebx
movl $0x3, %edi
leaq 0x3853(%rip), %rsi      # 0x1192a
leaq 0x3a3a(%rip), %rdx      # 0x11b18
xorl %eax, %eax
callq *%rcx
jmp 0xe242
callq 0x2040
movl (%rax), %r13d
cmpl $0x4, %r13d
leaq 0x91ce(%rip), %rcx      # 0x172c8
movl (%rcx), %ecx
jne 0xe1f2
movl $0x4, %r13d
cmpl $0x3, %ecx
jl 0xe2da
leaq 0x91b8(%rip), %rax      # 0x172d0
movq (%rax), %rcx
xorl %ebx, %ebx
movl $0x3, %edi
leaq 0x3788(%rip), %rsi      # 0x118b1
leaq 0x39e8(%rip), %rdx      # 0x11b18
xorl %eax, %eax
callq *%rcx
jmp 0xe245
leaq 0x9188(%rip), %rcx      # 0x172c8
cmpl $0x0, (%rcx)
jle 0xe169
leaq 0x9184(%rip), %rcx      # 0x172d0
movq (%rcx), %r8
movl $0x1, %edi
leaq 0x3800(%rip), %rsi      # 0x1195b
leaq 0x39b6(%rip), %rdx      # 0x11b18
movl %eax, %ecx
xorl %eax, %eax
callq *%r8
movl 0x4(%r13), %ecx
cmpl -0x2c(%rbp), %ecx
jne 0xe1c0
movl (%r13), %ecx
testb $0x4, %cl
jne 0xe26a
testb $0x18, %cl
leaq 0x913f(%rip), %rax      # 0x172c8
movl (%rax), %eax
movl $0x2, %ebx
movl $0x9, %r13d
je 0xe2e1
cmpl $0x3, %eax
jl 0xe245
leaq 0x9124(%rip), %rax      # 0x172d0
movq (%rax), %r8
movl $0x3, %edi
leaq 0x382e(%rip), %rsi      # 0x119e9
jmp 0xe2ff
leaq 0x9101(%rip), %rax      # 0x172c8
cmpl $0x0, (%rax)
jle 0xe240
leaq 0x90fd(%rip), %rax      # 0x172d0
movq (%rax), %r8
xorl %ebx, %ebx
movl $0x1, %edi
leaq 0x388d(%rip), %rsi      # 0x11a71
leaq 0x392d(%rip), %rdx      # 0x11b18
xorl %eax, %eax
callq *%r8
jmp 0xe242
movl $0x2, %ebx
testl %ecx, %ecx
jle 0xe245
leaq 0x90ce(%rip), %rcx      # 0x172d0
movq (%rcx), %rcx
movq %rcx, -0x68(%rbp)
movq -0x48(%rbp), %r14
movb $0x0, (%r14)
movl (%rax), %edi
movl $0x40, %edx
movq %r14, %rsi
callq 0x2100
movl $0x1, %edi
leaq 0x36bf(%rip), %rsi      # 0x118eb
leaq 0x38e5(%rip), %rdx      # 0x11b18
movl %r13d, %ecx
movq %r14, %r8
xorl %eax, %eax
callq *-0x68(%rbp)
jmp 0xe245
xorl %ebx, %ebx
movl %r14d, %r13d
movq -0x38(%rbp), %rcx
movq %rcx, %rsp
movl %r13d, %r14d
leaq -0x60(%rbp), %r13
testl %ebx, %ebx
jne 0xe3aa
cmpq -0x50(%rbp), %r12
jb 0xe036
jmp 0xe3bc
leaq 0x9057(%rip), %rax      # 0x172c8
movl (%rax), %eax
cmpl $0x3, %eax
jl 0xe299
leaq 0x9051(%rip), %rax      # 0x172d0
movq (%rax), %rcx
movl $0x3, %edi
leaq 0x38a4(%rip), %rsi      # 0x11b32
leaq 0x3883(%rip), %rdx      # 0x11b18
xorl %eax, %eax
callq *%rcx
movq -0x70(%rbp), %rax
leaq (%rax,%r12), %rsi
movq -0x50(%rbp), %r13
movq %r13, %rdx
subq %r12, %rdx
movl -0x2c(%rbp), %edi
callq 0x20c0
testl %eax, %eax
jle 0xe310
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq %r12, %rax
movl $0x6, %ebx
cmpq %r13, %rax
movq -0x38(%rbp), %rcx
jae 0xe2cf
xorl %ebx, %ebx
movq %rax, %r12
movl %r14d, %r13d
jmp 0xe249
xorl %ebx, %ebx
jmp 0xe245
testl %eax, %eax
jle 0xe245
leaq 0x8fe0(%rip), %rax      # 0x172d0
movq (%rax), %r8
movl $0x1, %edi
leaq 0x3876(%rip), %rsi      # 0x11b75
leaq 0x3812(%rip), %rdx      # 0x11b18
xorl %eax, %eax
callq *%r8
jmp 0xe245
movl $0x2, %ebx
je 0xe33e
callq 0x2040
movl (%rax), %r13d
cmpl $0xb, %r13d
je 0xe351
cmpl $0x73, %r13d
movq -0x38(%rbp), %rcx
je 0xe349
cmpl $0x6e, %r13d
movq %r12, %rax
jne 0xe249
jmp 0xe2cd
movl $0x9, %r13d
jmp 0xe245
movq %r12, %rax
jmp 0xe2cd
movq %r12, %rax
movq -0x38(%rbp), %rcx
jmp 0xe2cd
xorl %r14d, %r14d
jmp 0xe55a
callq 0x2040
movl (%rax), %r14d
leaq 0x8f54(%rip), %rcx      # 0x172c8
cmpl $0x0, (%rcx)
jle 0xe480
leaq 0x8f4c(%rip), %rcx      # 0x172d0
movq (%rcx), %rbx
movq -0x48(%rbp), %r15
movb $0x0, (%r15)
movl (%rax), %edi
movl $0x40, %edx
movq %r15, %rsi
callq 0x2100
leaq 0x3494(%rip), %rsi      # 0x11839
jmp 0xe46a
cmpl $0x2, %ebx
je 0xe480
cmpl $0x6, %ebx
jne 0xe556
leaq -0x88(%rbp), %rcx
movl -0x3c(%rbp), %edi
movl $0x2, %esi
movl -0x2c(%rbp), %edx
callq 0x2320
xorl %r14d, %r14d
testl %eax, %eax
jns 0xe544
leaq 0x8ee3(%rip), %rax      # 0x172c8
cmpl $0x0, (%rax)
jle 0xe544
leaq 0x8edb(%rip), %rax      # 0x172d0
movq (%rax), %rbx
callq 0x2040
movl (%rax), %r15d
movq -0x48(%rbp), %r12
movb $0x0, (%r12)
movl (%rax), %edi
movl $0x40, %edx
movq %r12, %rsi
callq 0x2100
leaq 0x369e(%rip), %rsi      # 0x11abd
leaq 0x36f2(%rip), %rdx      # 0x11b18
xorl %r14d, %r14d
jmp 0xe535
callq 0x2040
movl (%rax), %r14d
leaq 0x8e8b(%rip), %rcx      # 0x172c8
cmpl $0x0, (%rcx)
jle 0xe480
leaq 0x8e87(%rip), %rcx      # 0x172d0
movq (%rcx), %rbx
movq -0x48(%rbp), %r15
movb $0x0, (%r15)
movl (%rax), %edi
movl $0x40, %edx
movq %r15, %rsi
callq 0x2100
leaq 0x3406(%rip), %rsi      # 0x11870
leaq 0x36a7(%rip), %rdx      # 0x11b18
movl $0x1, %edi
movl %r14d, %ecx
movq %r15, %r8
xorl %eax, %eax
callq *%rbx
movl -0x2c(%rbp), %edx
leaq 0x8e3e(%rip), %rbx      # 0x172c8
movl (%rbx), %eax
cmpl $0x3, %eax
jl 0xe4dc
leaq 0x8e38(%rip), %rax      # 0x172d0
movq (%rax), %r15
callq 0x2040
movl (%rax), %r12d
movq -0x48(%rbp), %r13
movb $0x0, (%r13)
movl (%rax), %edi
movl $0x40, %edx
movq %r13, %rsi
callq 0x2100
leaq 0x36f9(%rip), %rsi      # 0x11bbb
leaq 0x364f(%rip), %rdx      # 0x11b18
movl $0x3, %edi
movl %r12d, %ecx
movq %r13, %r8
xorl %eax, %eax
callq *%r15
movl -0x2c(%rbp), %edx
movl -0x3c(%rbp), %edi
testl %edi, %edi
jle 0xe556
leaq -0x88(%rbp), %rcx
movl $0x2, %esi
callq 0x2320
testl %eax, %eax
jns 0xe544
cmpl $0x0, (%rbx)
jle 0xe544
leaq 0x8dcc(%rip), %rax      # 0x172d0
movq (%rax), %rbx
callq 0x2040
movl (%rax), %r15d
movq -0x48(%rbp), %r12
movb $0x0, (%r12)
movl (%rax), %edi
movl $0x40, %edx
movq %r12, %rsi
callq 0x2100
leaq 0x358f(%rip), %rsi      # 0x11abd
leaq 0x35e3(%rip), %rdx      # 0x11b18
movl $0x1, %edi
movl %r15d, %ecx
movq %r12, %r8
xorl %eax, %eax
callq *%rbx
movl -0x3c(%rbp), %edi
callq 0x2550
jmp 0xe556
movl $0x2, %r14d
jmp 0xe556
movq -0x78(%rbp), %rsp
movl %r14d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq