// File: /workspace/github_c_makefile_star2_O2/wtarreau[P]httpterm/httpterm.c
// Function: event_cli_read
int event_cli_read(int fd) {
    struct task *t = fdtab[fd].owner;
    struct session *s = t->context;
    struct buffer *b = s->req;
    int ret, max;

    if (fdtab[fd].state != FD_STERROR) {
#ifdef FILL_BUFFERS
	while (1)
#else
	do
#endif
	{
	    if (b->l == 0) { /* let's realign the buffer to optimize I/O */
		b->r = b->w = b->h = b->lr  = b->data;
		max = BUFSIZE;
	    }
	    else if (b->r > b->w) {
		max = b->data + BUFSIZE - b->r;
	    }
	    else {
		max = b->w - b->r;
	    }
	    
	    if (max == 0) {  /* not anymore room to store data */
		my_fd_clr(fd, StaticReadEvent);
		break;
	    }
	    
#ifndef MSG_NOSIGNAL
	    {
		int skerr;
		socklen_t lskerr = sizeof(skerr);
		
		getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
		if (skerr)
		    ret = -1;
		else
		    ret = recv(fd, b->r, max, 0);
	    }
#else
	    ret = recv(fd, b->r, max, MSG_NOSIGNAL);
#endif
	    if (ret > 0) {
		b->r += ret;
		b->l += ret;
		s->res_cr = RES_DATA;
		
		if (b->r == b->data + BUFSIZE) {
		    b->r = b->data; /* wrap around the buffer */
		}

		if (s->cli_state >= CL_STWAIT) {
		    /* drain body data */
		    if (s->req_body) {
			if (s->req_body > (long long)b->l) {
			    s->req_body -= b->l;
			    b->r = b->w = b->lr = b->data;
			    b->l = 0;
			} else {
			    b->l -= s->req_body;
			    b->w += s->req_body;
			    if (b->w >= b->data + BUFSIZE)
				b->w -= BUFSIZE;
			}
		    }
		}

		b->total += ret;
		/* we hope to read more data or to get a close on next round */
		continue;
	    }
	    else if (ret == 0) {
		s->res_cr = RES_NULL;
		break;
	    }
	    else if (errno == EAGAIN) {/* ignore EAGAIN */
		break;
	    }
	    else {
		s->res_cr = RES_ERROR;
		fdtab[fd].state = FD_STERROR;
		break;
	    }
	} /* while(1) */
#ifndef FILL_BUFFERS
	while (0);
#endif
    }
    else {
	s->res_cr = RES_ERROR;
	fdtab[fd].state = FD_STERROR;
    }

    if (s->res_cr != RES_SILENT) {
	if (s->proxy->clitimeout && my_fd_isset(fd, StaticReadEvent))
	    tv_delayfrom(&s->crexpire, &now, s->proxy->clitimeout);
	else
	    tv_eternity(&s->crexpire);
	
	task_wakeup(&rq, t);
    }

    return (s->res_cr == RES_SILENT) ? -1 : 0;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl %edi, %ebx
movq 0xb0af(%rip), %rax      # 0xe380
movslq %edi, %r13
shlq $0x5, %r13
movq 0x10(%rax,%r13), %r15
movq 0x40(%r15), %r14
cmpl $0x4, 0x18(%rax,%r13)
jne 0x32fc
movb $0x3, 0x38(%r14)
movl $0x4, 0x18(%rax,%r13)
jmp 0x3439
movq 0x58(%r14), %r12
cmpl $0x0, (%r12)
je 0x3345
movq 0x8(%r12), %rsi
movq 0x10(%r12), %rax
cmpq %rax, %rsi
jbe 0x3320
movl $0x1000, %eax           # imm = 0x1000
addq 0x30(%r12), %rax
subq %rsi, %rax
testl %eax, %eax
jne 0x3367
pushq $-0x2
popq %rax
movl %ebx, %ecx
roll %cl, %eax
movq 0xb0c3(%rip), %rcx      # 0xe3f8
movl %ebx, %edx
sarl $0x5, %edx
movslq %edx, %rdx
andl %eax, (%rcx,%rdx,4)
jmp 0x3439
movq 0x30(%r12), %rsi
movq %rsi, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movdqu %xmm0, 0x18(%r12)
movdqu %xmm0, 0x8(%r12)
movl $0x1000, %eax           # imm = 0x1000
movslq %eax, %rdx
movl %ebx, %edi
movl $0x4000, %ecx           # imm = 0x4000
callq 0x2060
testl %eax, %eax
jle 0x33ef
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq 0x8(%r12), %rdi
addq %rcx, %rdi
movq %rdi, 0x8(%r12)
movl (%r12), %edx
addl %eax, %edx
movl %edx, (%r12)
movb $0x1, 0x38(%r14)
movq 0x30(%r12), %rsi
leaq 0x1000(%rsi), %rax
cmpq %rax, %rdi
jne 0x33b4
movq %rsi, 0x8(%r12)
cmpl $0x2, 0x4c(%r14)
jl 0x3434
movq 0x130(%r14), %rdi
testq %rdi, %rdi
je 0x3434
movl %edx, %r9d
movq %rdi, %r8
subq %r9, %r8
jle 0x3413
movq %r8, 0x130(%r14)
movq %rsi, 0x20(%r12)
movq %rsi, 0x10(%r12)
movq %rsi, 0x8(%r12)
andl $0x0, (%r12)
jmp 0x3434
je 0x340c
callq 0x2080
cmpl $0xb, (%rax)
je 0x3439
movb $0x3, 0x38(%r14)
movq 0xaf79(%rip), %rax      # 0xe380
jmp 0x32ee
movb $0x2, 0x38(%r14)
jmp 0x3439
subl %edi, %edx
movl %edx, (%r12)
addq 0x10(%r12), %rdi
movq %rdi, 0x10(%r12)
cmpq %rax, %rdi
jb 0x3434
addq $-0x1000, %rdi          # imm = 0xF000
movq %rdi, 0x10(%r12)
addq %rcx, 0x28(%r12)
cmpb $0x0, 0x38(%r14)
je 0x3498
movq 0x40(%r14), %rax
movl 0x30(%rax), %esi
testl %esi, %esi
je 0x346d
movq 0xafa6(%rip), %rax      # 0xe3f8
movl %ebx, %ecx
sarl $0x5, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
btl %ebx, %eax
jae 0x346d
leaq 0x8(%r14), %rdi
callq 0x34ab
jmp 0x3477
pxor %xmm0, %xmm0
movdqu %xmm0, 0x8(%r14)
cmpl $0x1, 0x20(%r15)
je 0x3498
movq 0xaf03(%rip), %rax      # 0xe388
movq %rax, 0x10(%r15)
movl $0x1, 0x20(%r15)
movq %r15, 0xaef0(%rip)      # 0xe388
xorl %eax, %eax
cmpb $0x1, 0x38(%r14)
sbbl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq