// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
testq %rdx, %rdx
movq %rsi, -0x48(%rbp)
je 0xd051
movq %rsp, %rax
movl %r8d, %r15d
movq %rcx, %rbx
movq %rdx, %r13
movq %rsp, -0x40(%rbp)
addq $-0x40, %rax
movq %rax, -0x38(%rbp)
movq %rax, %rsp
xorl %r14d, %r14d
pushq $0x3
popq %rsi
xorl %edx, %edx
xorl %eax, %eax
movl %edi, %r12d
callq 0x2450
cmpl $-0x1, %eax
cmovel %r14d, %eax
btl $0xb, %eax
jb 0xce49
orl $0x800, %eax            # imm = 0x800
pushq $0x4
popq %rsi
movl %eax, %edx
movl %r12d, %edi
xorl %eax, %eax
callq 0x2450
cmpl $-0x1, %eax
je 0xd0de
movl %r12d, -0x30(%rbp)
movslq %r15d, %r14
callq 0xd4ef
addq %r14, %rax
movq %rbx, %r15
cmpq %rbx, %rax
cmovbq %rax, %r15
testq %rbx, %rbx
cmoveq %rax, %r15
callq 0xd4ef
pushq $0x1
popq %rdi
callq 0x2520
movl %eax, %r12d
testl %eax, %eax
js 0xd059
leaq -0x54(%rbp), %rcx
movl -0x30(%rbp), %edx
andl $0x0, 0x8(%rcx)
movl %edx, 0x4(%rcx)
movl $0x4, (%rcx)
pushq $0x1
popq %rsi
movl %r12d, %edi
callq 0x2320
testl %eax, %eax
js 0xd090
movl %r12d, -0x2c(%rbp)
xorl %r12d, %r12d
callq 0xd4ef
cmpq %r15, %rax
ja 0xd0e7
movq %rsp, %rbx
movq %rsp, %r14
addq $-0x10, %rbx
movq %rbx, %rsp
movl -0x2c(%rbp), %edi
movq %rbx, %rsi
pushq $0x1
popq %rcx
movl %ecx, %edx
callq 0x2110
testl %eax, %eax
js 0xcf13
cmpl $0x1, %eax
je 0xcf87
testl %eax, %eax
jne 0xcf59
leaq 0x83d6(%rip), %rax      # 0x152c8
movl (%rax), %eax
cmpl $0x3, %eax
jl 0xd01f
leaq 0x83cc(%rip), %rax      # 0x152d0
movq (%rax), %rcx
pushq $0x3
popq %rdi
leaq 0x2a49(%rip), %rsi      # 0xf95a
jmp 0xcf49
callq 0x2040
movl (%rax), %ebx
leaq 0x83a7(%rip), %rax      # 0x152c8
movl (%rax), %eax
cmpl $0x4, %ebx
jne 0xd0f4
cmpl $0x3, %eax
jl 0xd01f
leaq 0x8394(%rip), %rax      # 0x152d0
movq (%rax), %rcx
pushq $0x3
popq %rdi
leaq 0x2998(%rip), %rsi      # 0xf8e1
leaq 0x2bf8(%rip), %rdx      # 0xfb48
xorl %eax, %eax
callq *%rcx
jmp 0xd01f
leaq 0x8368(%rip), %rcx      # 0x152c8
cmpl $0x0, (%rcx)
jle 0xcf87
leaq 0x8364(%rip), %rcx      # 0x152d0
movq (%rcx), %r8
pushq $0x1
popq %rdi
movl %eax, %ecx
leaq 0x2a10(%rip), %rsi      # 0xf98b
leaq 0x2bc6(%rip), %rdx      # 0xfb48
xorl %eax, %eax
callq *%r8
movl 0x4(%rbx), %ecx
cmpl -0x30(%rbp), %ecx
jne 0xcff3
movl (%rbx), %ecx
testb $0x4, %cl
je 0xd132
leaq 0x8327(%rip), %rax      # 0x152c8
movl (%rax), %eax
cmpl $0x3, %eax
jl 0xcfc7
leaq 0x8321(%rip), %rax      # 0x152d0
movq (%rax), %rcx
pushq $0x3
popq %rdi
leaq 0x2ba6(%rip), %rsi      # 0xfb62
leaq 0x2b85(%rip), %rdx      # 0xfb48
xorl %eax, %eax
callq *%rcx
movq -0x48(%rbp), %rax
movl -0x30(%rbp), %edi
movq %r13, %rdx
subq %r12, %rdx
leaq (%rax,%r12), %rsi
callq 0x20c0
testl %eax, %eax
jle 0xd030
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq %rax, %r12
cmpq %r13, %r12
jb 0xd01f
jmp 0xd16c
leaq 0x82ce(%rip), %rax      # 0x152c8
cmpl $0x0, (%rax)
jle 0xd01f
leaq 0x82ca(%rip), %rax      # 0x152d0
movq (%rax), %r8
pushq $0x1
popq %rdi
leaq 0x2a8e(%rip), %rsi      # 0xfaa1
leaq 0x2b2e(%rip), %rdx      # 0xfb48
xorl %eax, %eax
callq *%r8
movq %r14, %rsp
cmpq %r13, %r12
jb 0xceaf
jmp 0xd16f
je 0xd29f
callq 0x2040
movl (%rax), %eax
cmpl $0xb, %eax
je 0xd01f
cmpl $0x6e, %eax
je 0xd01f
cmpl $0x73, %eax
je 0xd01f
jmp 0xd2a2
xorl %r13d, %r13d
jmp 0xd28d
callq 0x2040
leaq 0x8263(%rip), %rcx      # 0x152c8
movl (%rax), %r13d
cmpl $0x0, (%rcx)
jle 0xd1d6
leaq 0x8258(%rip), %rax      # 0x152d0
movq (%rax), %rbx
movq -0x38(%rbp), %r14
movq %r14, %rdi
callq 0xcdb1
leaq 0x27db(%rip), %rsi      # 0xf869
jmp 0xd0c5
callq 0x2040
leaq 0x822c(%rip), %rcx      # 0x152c8
movl (%rax), %r13d
cmpl $0x0, (%rcx)
jle 0xd1d6
leaq 0x8221(%rip), %rax      # 0x152d0
movq (%rax), %rbx
movq -0x38(%rbp), %r14
movq %r14, %rdi
callq 0xcdb1
leaq 0x27db(%rip), %rsi      # 0xf8a0
leaq 0x2a7c(%rip), %rdx      # 0xfb48
pushq $0x1
popq %rdi
movl %r13d, %ecx
movq %r14, %r8
xorl %eax, %eax
callq *%rbx
jmp 0xd1d6
pushq $0x2
popq %r13
jmp 0xd289
movl -0x2c(%rbp), %r12d
pushq $0x6e
popq %r13
jmp 0xd1d6
testl %eax, %eax
jle 0xd129
leaq 0x81d1(%rip), %rax      # 0x152d0
movq (%rax), %r15
movq -0x38(%rbp), %r12
movq %r12, %rdi
callq 0xcdb1
leaq 0x2806(%rip), %rsi      # 0xf91b
leaq 0x2a2c(%rip), %rdx      # 0xfb48
pushq $0x1
popq %rdi
movl %ebx, %ecx
movq %r12, %r8
xorl %eax, %eax
callq *%r15
movl -0x2c(%rbp), %r12d
jmp 0xd1d0
leaq 0x818f(%rip), %rax      # 0x152c8
testb $0x18, %cl
movl (%rax), %eax
pushq $0x9
popq %rbx
je 0xd1a8
movl -0x2c(%rbp), %r12d
cmpl $0x3, %eax
jl 0xd1d0
leaq 0x8179(%rip), %rax      # 0x152d0
leaq 0x28bb(%rip), %rsi      # 0xfa19
leaq 0x29e3(%rip), %rdx      # 0xfb48
movq (%rax), %r8
pushq $0x3
jmp 0xd1ca
movq %r14, %rsp
pushq $0x2
popq %rsi
movl -0x2c(%rbp), %r12d
movl -0x30(%rbp), %edx
leaq -0x54(%rbp), %rcx
movl %r12d, %edi
callq 0x2320
xorl %r13d, %r13d
testl %eax, %eax
jns 0xd281
leaq 0x8131(%rip), %rax      # 0x152c8
xorl %r13d, %r13d
cmpl $0x0, (%rax)
jg 0xd249
jmp 0xd281
movl -0x2c(%rbp), %r12d
testl %eax, %eax
jle 0xd1d0
leaq 0x8119(%rip), %rax      # 0x152d0
leaq 0x29e7(%rip), %rsi      # 0xfba5
leaq 0x2983(%rip), %rdx      # 0xfb48
movq (%rax), %r8
pushq $0x1
popq %rdi
xorl %eax, %eax
callq *%r8
movq %r14, %rsp
movl %ebx, %r13d
leaq 0x80eb(%rip), %rbx      # 0x152c8
movl (%rbx), %eax
cmpl $0x3, %eax
jl 0xd229
leaq 0x80e5(%rip), %rax      # 0x152d0
movq (%rax), %r15
callq 0x2040
movl %r13d, %r14d
movq -0x38(%rbp), %r13
movl (%rax), %ebx
movq %r13, %rdi
callq 0xcdb1
leaq 0x29e0(%rip), %rsi      # 0xfbeb
leaq 0x2936(%rip), %rdx      # 0xfb48
pushq $0x3
popq %rdi
movl %ebx, %ecx
leaq 0x80aa(%rip), %rbx      # 0x152c8
movq %r13, %r8
xorl %eax, %eax
movl %r14d, %r13d
callq *%r15
testl %r12d, %r12d
jle 0xd289
pushq $0x2
popq %rsi
movl -0x30(%rbp), %edx
leaq -0x54(%rbp), %rcx
movl %r12d, %edi
callq 0x2320
testl %eax, %eax
jns 0xd281
cmpl $0x0, (%rbx)
jle 0xd281
leaq 0x8080(%rip), %rax      # 0x152d0
movq (%rax), %r14
callq 0x2040
movq -0x38(%rbp), %r15
movl (%rax), %ebx
movq %r15, %rdi
callq 0xcdb1
leaq 0x2880(%rip), %rsi      # 0xfaed
leaq 0x28d4(%rip), %rdx      # 0xfb48
pushq $0x1
popq %rdi
movl %ebx, %ecx
movq %r15, %r8
xorl %eax, %eax
callq *%r14
movl %r12d, %edi
callq 0x2550
movq -0x40(%rbp), %rsp
movl %r13d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x9
popq %rax
movq %r14, %rsp
movl -0x2c(%rbp), %r12d
movl %eax, %r13d
jmp 0xd1d6