// File: /workspace/github_c_makefile_star2_O2/sharpyfox[P]citrusleaf-client/src/cf_socket.c
// Function: cf_socket_read_timeout
int
cf_socket_read_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_read_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 = EPOLLIN;

	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 read 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 & EPOLLIN) {
					cf_debug("%s: epoll_wait() on socket ready for read 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-read 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 = read(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 read 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 read 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 0xcb51
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 0xc949
orl $0x800, %eax            # imm = 0x800
pushq $0x4
popq %rsi
movl %eax, %edx
movl %r12d, %edi
xorl %eax, %eax
callq 0x2450
cmpl $-0x1, %eax
je 0xcbde
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 0xcb59
leaq -0x54(%rbp), %rcx
movl -0x30(%rbp), %edx
andl $0x0, 0x8(%rcx)
movl %edx, 0x4(%rcx)
movl $0x1, (%rcx)
pushq $0x1
popq %rsi
movl %r12d, %edi
callq 0x2320
testl %eax, %eax
js 0xcb90
movl %r12d, -0x2c(%rbp)
xorl %r12d, %r12d
callq 0xd4ef
cmpq %r15, %rax
ja 0xcbe7
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 0xca13
cmpl $0x1, %eax
je 0xca87
testl %eax, %eax
jne 0xca59
leaq 0x88d6(%rip), %rax      # 0x152c8
movl (%rax), %eax
cmpl $0x3, %eax
jl 0xcb1f
leaq 0x88cc(%rip), %rax      # 0x152d0
movq (%rax), %rcx
pushq $0x3
popq %rdi
leaq 0x2f49(%rip), %rsi      # 0xf95a
jmp 0xca49
callq 0x2040
movl (%rax), %ebx
leaq 0x88a7(%rip), %rax      # 0x152c8
movl (%rax), %eax
cmpl $0x4, %ebx
jne 0xcbf4
cmpl $0x3, %eax
jl 0xcb1f
leaq 0x8894(%rip), %rax      # 0x152d0
movq (%rax), %rcx
pushq $0x3
popq %rdi
leaq 0x2e98(%rip), %rsi      # 0xf8e1
leaq 0x2e00(%rip), %rdx      # 0xf850
xorl %eax, %eax
callq *%rcx
jmp 0xcb1f
leaq 0x8868(%rip), %rcx      # 0x152c8
cmpl $0x0, (%rcx)
jle 0xca87
leaq 0x8864(%rip), %rcx      # 0x152d0
movq (%rcx), %r8
pushq $0x1
popq %rdi
movl %eax, %ecx
leaq 0x2f10(%rip), %rsi      # 0xf98b
leaq 0x2dce(%rip), %rdx      # 0xf850
xorl %eax, %eax
callq *%r8
movl 0x4(%rbx), %ecx
cmpl -0x30(%rbp), %ecx
jne 0xcaf3
movl (%rbx), %ecx
testb $0x1, %cl
je 0xcc32
leaq 0x8827(%rip), %rax      # 0x152c8
movl (%rax), %eax
cmpl $0x3, %eax
jl 0xcac7
leaq 0x8821(%rip), %rax      # 0x152d0
movq (%rax), %rcx
pushq $0x3
popq %rdi
leaq 0x2f1b(%rip), %rsi      # 0xf9d7
leaq 0x2d8d(%rip), %rdx      # 0xf850
xorl %eax, %eax
callq *%rcx
movq -0x48(%rbp), %rax
movl -0x30(%rbp), %edi
movq %r13, %rdx
subq %r12, %rdx
leaq (%rax,%r12), %rsi
callq 0x2390
testl %eax, %eax
jle 0xcb30
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq %rax, %r12
cmpq %r13, %r12
jb 0xcb1f
jmp 0xcc6c
leaq 0x87ce(%rip), %rax      # 0x152c8
cmpl $0x0, (%rax)
jle 0xcb1f
leaq 0x87ca(%rip), %rax      # 0x152d0
movq (%rax), %r8
pushq $0x1
popq %rdi
leaq 0x2f8e(%rip), %rsi      # 0xfaa1
leaq 0x2d36(%rip), %rdx      # 0xf850
xorl %eax, %eax
callq *%r8
movq %r14, %rsp
cmpq %r13, %r12
jb 0xc9af
jmp 0xcc6f
je 0xcd9f
callq 0x2040
movl (%rax), %eax
cmpl $0xb, %eax
je 0xcb1f
cmpl $0x6e, %eax
je 0xcb1f
cmpl $0x73, %eax
je 0xcb1f
jmp 0xcda2
xorl %r13d, %r13d
jmp 0xcd8d
callq 0x2040
leaq 0x8763(%rip), %rcx      # 0x152c8
movl (%rax), %r13d
cmpl $0x0, (%rcx)
jle 0xccd6
leaq 0x8758(%rip), %rax      # 0x152d0
movq (%rax), %rbx
movq -0x38(%rbp), %r14
movq %r14, %rdi
callq 0xcdb1
leaq 0x2cdb(%rip), %rsi      # 0xf869
jmp 0xcbc5
callq 0x2040
leaq 0x872c(%rip), %rcx      # 0x152c8
movl (%rax), %r13d
cmpl $0x0, (%rcx)
jle 0xccd6
leaq 0x8721(%rip), %rax      # 0x152d0
movq (%rax), %rbx
movq -0x38(%rbp), %r14
movq %r14, %rdi
callq 0xcdb1
leaq 0x2cdb(%rip), %rsi      # 0xf8a0
leaq 0x2c84(%rip), %rdx      # 0xf850
pushq $0x1
popq %rdi
movl %r13d, %ecx
movq %r14, %r8
xorl %eax, %eax
callq *%rbx
jmp 0xccd6
pushq $0x2
popq %r13
jmp 0xcd89
movl -0x2c(%rbp), %r12d
pushq $0x6e
popq %r13
jmp 0xccd6
testl %eax, %eax
jle 0xcc29
leaq 0x86d1(%rip), %rax      # 0x152d0
movq (%rax), %r15
movq -0x38(%rbp), %r12
movq %r12, %rdi
callq 0xcdb1
leaq 0x2d06(%rip), %rsi      # 0xf91b
leaq 0x2c34(%rip), %rdx      # 0xf850
pushq $0x1
popq %rdi
movl %ebx, %ecx
movq %r12, %r8
xorl %eax, %eax
callq *%r15
movl -0x2c(%rbp), %r12d
jmp 0xccd0
leaq 0x868f(%rip), %rax      # 0x152c8
testb $0x18, %cl
movl (%rax), %eax
pushq $0x9
popq %rbx
je 0xcca8
movl -0x2c(%rbp), %r12d
cmpl $0x3, %eax
jl 0xccd0
leaq 0x8679(%rip), %rax      # 0x152d0
leaq 0x2dbb(%rip), %rsi      # 0xfa19
leaq 0x2beb(%rip), %rdx      # 0xf850
movq (%rax), %r8
pushq $0x3
jmp 0xccca
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 0xcd81
leaq 0x8631(%rip), %rax      # 0x152c8
xorl %r13d, %r13d
cmpl $0x0, (%rax)
jg 0xcd49
jmp 0xcd81
movl -0x2c(%rbp), %r12d
testl %eax, %eax
jle 0xccd0
leaq 0x8619(%rip), %rax      # 0x152d0
leaq 0x2d9e(%rip), %rsi      # 0xfa5c
leaq 0x2b8b(%rip), %rdx      # 0xf850
movq (%rax), %r8
pushq $0x1
popq %rdi
xorl %eax, %eax
callq *%r8
movq %r14, %rsp
movl %ebx, %r13d
leaq 0x85eb(%rip), %rbx      # 0x152c8
movl (%rbx), %eax
cmpl $0x3, %eax
jl 0xcd29
leaq 0x85e5(%rip), %rax      # 0x152d0
movq (%rax), %r15
callq 0x2040
movl %r13d, %r14d
movq -0x38(%rbp), %r13
movl (%rax), %ebx
movq %r13, %rdi
callq 0xcdb1
leaq 0x2e17(%rip), %rsi      # 0xfb22
leaq 0x2b3e(%rip), %rdx      # 0xf850
pushq $0x3
popq %rdi
movl %ebx, %ecx
leaq 0x85aa(%rip), %rbx      # 0x152c8
movq %r13, %r8
xorl %eax, %eax
movl %r14d, %r13d
callq *%r15
testl %r12d, %r12d
jle 0xcd89
pushq $0x2
popq %rsi
movl -0x30(%rbp), %edx
leaq -0x54(%rbp), %rcx
movl %r12d, %edi
callq 0x2320
testl %eax, %eax
jns 0xcd81
cmpl $0x0, (%rbx)
jle 0xcd81
leaq 0x8580(%rip), %rax      # 0x152d0
movq (%rax), %r14
callq 0x2040
movq -0x38(%rbp), %r15
movl (%rax), %ebx
movq %r15, %rdi
callq 0xcdb1
leaq 0x2d80(%rip), %rsi      # 0xfaed
leaq 0x2adc(%rip), %rdx      # 0xf850
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 0xccd6