// File: /workspace/github_cpp_makefile_star2_O3/lianxings[P]ftp_benchmark/ftplib.cc
// Function: ftplib::readline(char*, int, ftphandle*)
int ftplib::readline(char *buf,int max,ftphandle *ctl)
{
    int errCode;
	int x,retval = 0;
	char *end,*bp=buf;
	int eof = 0;

	if ((ctl->dir != FTPLIB_CONTROL) && (ctl->dir != FTPLIB_READ))
	    return -1;
	if (max == 0)
	    return 0;
    
	do 	{
		if (ctl->cavail > 0) 		{
			x = (max >= ctl->cavail) ? ctl->cavail : max-1;
			end = static_cast<char*>(memccpy(bp,ctl->cget,'\n',x));
			if (end != NULL)
			    x = end - bp;
			retval += x;
			bp += x;
			*bp = '\0';
			max -= x;
			ctl->cget += x;
			ctl->cavail -= x;
			if (end != NULL) 			{
				bp -= 2;
				if (strcmp(bp,"\r\n") == 0) 				{
					*bp++ = '\n';
					*bp++ = '\0';
					--retval;
				}
				break;
			}
		}
		if (max == 1) 		{
			*buf = '\0';
			break;
		}
		if (ctl->cput == ctl->cget) 		{
			ctl->cput = ctl->cget = ctl->buf;
			ctl->cavail = 0;
			ctl->cleft = FTPLIB_BUFSIZ;
		}
		if (eof) 		{
			if (retval == 0)
			retval = -1;
			break;
		}

		if (!socket_wait(ctl)) 
            return retval;

#ifndef NOSSL
		if (ctl->tlsdata)
            x = SSL_read(ctl->ssl, ctl->cput, ctl->cleft);
		else 		{
			if (ctl->tlsctrl) x = SSL_read(ctl->ssl, ctl->cput, ctl->cleft);
			else x = net_read(ctl->handle,ctl->cput,ctl->cleft);
		}
#else
		x = net_read(ctl->handle,ctl->cput,ctl->cleft);		
#endif
		if ( x == -1) 		{
		    errCode = errno;
		    fprintf(stderr, "Thread:%-8ld  sock:%-4d   recv failed:%s\n", syscall(SYS_gettid), ctl->handle, strerror(errno));
			retval = -1;
			break;
		}

		// LOGGING FUNCTIONALITY!!!

		if ((ctl->dir == FTPLIB_CONTROL) && (mp_ftphandle->logcb != NULL)) 		{
			*((ctl->cput)+x) = '\0';
			mp_ftphandle->logcb(ctl->cput, mp_ftphandle->cbarg, true);
		}

		if (x == 0) 
            eof = 1;
		ctl->cleft -= x;
		ctl->cavail += x;
		ctl->cput += x;
	} while (1);
    
	return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, (%rsp)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpl $0x1, 0x28(%rcx)
ja 0xe5db
movl %edx, %r12d
testl %edx, %edx
je 0xe594
movq %rcx, %r14
leaq 0x10(%rcx), %rax
movq %rax, 0x8(%rsp)
movl 0x14(%rcx), %eax
xorl %ebx, %ebx
movq %rsi, 0x10(%rsp)
movq %rsi, %r15
xorl %r13d, %r13d
testl %eax, %eax
jle 0xe4dc
leal -0x1(%r12), %ecx
cmpl %eax, %r12d
cmovgel %eax, %ecx
movq 0x8(%r14), %rsi
movslq %ecx, %rbp
movq %r15, %rdi
movl $0xa, %edx
movq %rbp, %rcx
callq 0x4220
movl %eax, %ecx
subl %r15d, %ecx
testq %rax, %rax
cmovnel %ecx, %ebp
addl %ebp, %ebx
movslq %ebp, %rcx
movb $0x0, (%r15,%rcx)
addq %rcx, %r15
addq %rcx, 0x8(%r14)
subl %ebp, 0x14(%r14)
testq %rax, %rax
jne 0xe5ad
subl %ebp, %r12d
cmpl $0x1, %r12d
je 0xe598
movq (%r14), %rax
cmpq 0x8(%r14), %rax
jne 0xe508
movq 0x20(%r14), %rax
movq %rax, 0x8(%r14)
movq %rax, (%r14)
movabsq $0x40000000000, %rax    # imm = 0x40000000000
movq %rax, 0x14(%r14)
testl %r13d, %r13d
jne 0xe5a2
movq %r14, %rsi
callq 0xe344
testl %eax, %eax
je 0xe5db
movl 0x10(%r14), %edi
movq (%r14), %rsi
movslq 0x18(%r14), %rdx
callq 0x4540
movq %rax, %rbp
cmpl $-0x1, %ebp
je 0xe5cc
cmpl $0x0, 0x28(%r14)
jne 0xe572
movq (%rsp), %rax
movq (%rax), %rax
cmpq $0x0, 0x60(%rax)
je 0xe572
movq (%r14), %rax
movslq %ebp, %rcx
movb $0x0, (%rax,%rcx)
movq (%rsp), %rax
movq (%rax), %rax
movq (%r14), %rdi
movq 0x68(%rax), %rsi
movl $0x1, %edx
callq *0x60(%rax)
xorl %r13d, %r13d
testl %ebp, %ebp
sete %r13b
subl %ebp, 0x18(%r14)
movl 0x14(%r14), %eax
addl %ebp, %eax
movl %eax, 0x14(%r14)
movslq %ebp, %rcx
addq %rcx, (%r14)
jmp 0xe48a
xorl %ebx, %ebx
jmp 0xe5db
movq 0x10(%rsp), %rax
movb $0x0, (%rax)
jmp 0xe5db
xorl %eax, %eax
cmpl $0x1, %ebx
sbbl %eax, %eax
orl %eax, %ebx
jmp 0xe5db
cmpb $0xd, -0x2(%r15)
jne 0xe5db
cmpb $0xa, -0x1(%r15)
jne 0xe5db
cmpb $0x0, (%r15)
jne 0xe5db
movw $0xa, -0x2(%r15)
decl %ebx
jmp 0xe5db
movq 0x8(%rsp), %rdi
callq 0x4bd2
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq