// File: /workspace/github_c_makefile_star2_O3/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 $0x58, %rsp
testq %rdx, %rdx
movq %rsi, -0x68(%rbp)
je 0xd7c0
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 0xd53c
orl $0x800, %eax            # imm = 0x800
movl %r15d, %edi
movl $0x4, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x2450
cmpl $-0x1, %eax
je 0xd86e
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 0xd7c8
movl -0x3c(%rbp), %edx
leaq -0x74(%rbp), %rcx
movl %r14d, %edi
movl $0x1, %esi
movl $0x0, 0x8(%rcx)
movl %edx, 0x4(%rcx)
movl $0x1, (%rcx)
callq 0x2320
testl %eax, %eax
js 0xd80d
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 0xd87c
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 0xd671
cmpl $0x1, %eax
je 0xd6eb
testl %eax, %eax
jne 0xd6bb
leaq 0x8c77(%rip), %rax      # 0x162c8
movl (%rax), %eax
cmpl $0x3, %eax
jl 0xd78a
leaq 0x8c6d(%rip), %rax      # 0x162d0
movl $0x3, %edi
leaq 0x32db(%rip), %rsi      # 0x1094a
jmp 0xd6a8
callq 0x2040
leaq 0x8c4b(%rip), %rcx      # 0x162c8
movl (%rax), %r13d
movl (%rcx), %ecx
cmpl $0x4, %r13d
jne 0xd88f
cmpl $0x3, %ecx
jl 0xd78a
leaq 0x8c34(%rip), %rax      # 0x162d0
movl $0x3, %edi
leaq 0x3229(%rip), %rsi      # 0x108d1
movq (%rax), %rcx
leaq 0x318e(%rip), %rdx      # 0x10840
xorl %eax, %eax
callq *%rcx
jmp 0xd78a
leaq 0x8c06(%rip), %rcx      # 0x162c8
cmpl $0x0, (%rcx)
jle 0xd6eb
leaq 0x8c02(%rip), %rcx      # 0x162d0
movl $0x1, %edi
leaq 0x32a1(%rip), %rsi      # 0x1097b
leaq 0x315f(%rip), %rdx      # 0x10840
movq (%rcx), %r8
movl %eax, %ecx
xorl %eax, %eax
callq *%r8
movl 0x4(%r13), %ecx
cmpl -0x3c(%rbp), %ecx
jne 0xd75c
movl (%r13), %ecx
testb $0x1, %cl
je 0xd8df
leaq 0x8bc0(%rip), %rax      # 0x162c8
movl (%rax), %eax
cmpl $0x3, %eax
jl 0xd730
leaq 0x8bba(%rip), %rax      # 0x162d0
movl $0x3, %edi
leaq 0x32a5(%rip), %rsi      # 0x109c7
leaq 0x3117(%rip), %rdx      # 0x10840
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 0x2390
testl %eax, %eax
jle 0xd79b
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq %rax, %r14
cmpq %r12, %r14
jb 0xd78a
jmp 0xd927
leaq 0x8b65(%rip), %rax      # 0x162c8
cmpl $0x0, (%rax)
jle 0xd78a
leaq 0x8b61(%rip), %rax      # 0x162d0
movl $0x1, %edi
leaq 0x3316(%rip), %rsi      # 0x10a91
leaq 0x30be(%rip), %rdx      # 0x10840
movq (%rax), %r8
xorl %eax, %eax
callq *%r8
movq %rbx, %rsp
cmpq %r12, %r14
jb 0xd5e3
jmp 0xd92a
je 0xdadb
callq 0x2040
movl (%rax), %r13d
cmpl $0xb, %r13d
je 0xd78a
cmpl $0x6e, %r13d
je 0xd78a
cmpl $0x73, %r13d
je 0xd78a
jmp 0xdae1
xorl %r13d, %r13d
jmp 0xdac9
callq 0x2040
leaq 0x8af4(%rip), %rcx      # 0x162c8
movl (%rax), %r13d
cmpl $0x0, (%rcx)
jle 0xd865
leaq 0x8ae9(%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 0x304e(%rip), %rsi      # 0x10859
jmp 0xd84c
callq 0x2040
leaq 0x8aaf(%rip), %rcx      # 0x162c8
movl (%rax), %r13d
cmpl $0x0, (%rcx)
jle 0xd865
leaq 0x8aa8(%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 0x3044(%rip), %rsi      # 0x10890
movq %r14, %r8
leaq 0x2fea(%rip), %rdx      # 0x10840
movl $0x1, %edi
movl %r13d, %ecx
xorl %eax, %eax
movl %r15d, %r14d
callq *%rbx
movq -0x38(%rbp), %r15
jmp 0xd9e9
movl $0x2, %r13d
movq %r12, %r15
jmp 0xdac6
movl $0x6e, %r13d
movq -0x38(%rbp), %r15
movl -0x2c(%rbp), %r14d
jmp 0xd9e9
testl %ecx, %ecx
jle 0xd8d2
leaq 0x8a36(%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 0x3050(%rip), %rsi      # 0x1090b
leaq 0x2f7e(%rip), %rdx      # 0x10840
movl $0x1, %edi
movl %r13d, %ecx
movq %r15, %r8
xorl %eax, %eax
callq *%r14
movq -0x38(%rbp), %r15
movl -0x2c(%rbp), %r14d
jmp 0xd9e6
leaq 0x89e2(%rip), %rax      # 0x162c8
testb $0x18, %cl
movl $0x9, %r13d
movl (%rax), %eax
je 0xd9b8
movq -0x38(%rbp), %r15
movl -0x2c(%rbp), %r14d
cmpl $0x3, %eax
jl 0xd9e6
leaq 0x89c1(%rip), %rax      # 0x162d0
leaq 0x30f3(%rip), %rsi      # 0x10a09
leaq 0x2f23(%rip), %rdx      # 0x10840
movl $0x3, %edi
jmp 0xd9de
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 0xd952
movq -0x38(%rbp), %r15
jmp 0xdabe
leaq 0x896f(%rip), %rax      # 0x162c8
cmpl $0x0, (%rax)
movq -0x38(%rbp), %r15
jle 0xdabe
leaq 0x8963(%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 0x3139(%rip), %rsi      # 0x10add
leaq 0x2e95(%rip), %rdx      # 0x10840
xorl %r13d, %r13d
movl $0x1, %edi
jmp 0xdaba
movq -0x38(%rbp), %r15
movl -0x2c(%rbp), %r14d
testl %eax, %eax
jle 0xd9e6
leaq 0x8905(%rip), %rax      # 0x162d0
leaq 0x307a(%rip), %rsi      # 0x10a4c
leaq 0x2e67(%rip), %rdx      # 0x10840
movl $0x1, %edi
movq (%rax), %r8
xorl %eax, %eax
callq *%r8
movq %rbx, %rsp
leaq 0x88d8(%rip), %rbx      # 0x162c8
movl (%rbx), %eax
cmpl $0x3, %eax
jl 0xda4c
leaq 0x88d2(%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 0x889b(%rip), %rbx      # 0x162c8
leaq 0x30de(%rip), %rsi      # 0x10b12
leaq 0x2e05(%rip), %rdx      # 0x10840
movl $0x3, %edi
movl %r12d, %ecx
xorl %eax, %eax
callq *%r14
movl -0x2c(%rbp), %r14d
testl %r14d, %r14d
jle 0xdac6
movl -0x3c(%rbp), %edx
leaq -0x74(%rbp), %rcx
movl %r14d, %edi
movl $0x2, %esi
callq 0x2320
testl %eax, %eax
jns 0xdabe
cmpl $0x0, (%rbx)
jle 0xdabe
leaq 0x885b(%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 0x3032(%rip), %rsi      # 0x10add
leaq 0x2d8e(%rip), %rdx      # 0x10840
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 0xd882