// File: /workspace/github_cpp_makefile_star2_O1/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, 0x8(%rsp)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpl $0x1, 0x28(%rcx)
ja 0xe5c3
movl %edx, %r12d
testl %edx, %edx
je 0xe58e
movq %rcx, %r14
xorl %ebx, %ebx
movq %rsi, 0x10(%rsp)
movq %rsi, %rbp
xorl %r13d, %r13d
movl 0x14(%r14), %eax
testl %eax, %eax
jle 0xe4d8
leal -0x1(%r12), %ecx
cmpl %eax, %r12d
cmovgel %eax, %ecx
movq 0x8(%r14), %rsi
movslq %ecx, %r15
movq %rbp, %rdi
movl $0xa, %edx
movq %r15, %rcx
callq 0x4220
movl %eax, %ecx
subl %ebp, %ecx
testq %rax, %rax
cmovnel %ecx, %r15d
addl %r15d, %ebx
movslq %r15d, %rcx
movb $0x0, (%rbp,%rcx)
addq %rcx, %rbp
addq %rcx, 0x8(%r14)
subl %r15d, 0x14(%r14)
testq %rax, %rax
jne 0xe5a7
subl %r15d, %r12d
cmpl $0x1, %r12d
je 0xe592
movq (%r14), %rax
cmpq 0x8(%r14), %rax
jne 0xe504
movq 0x20(%r14), %rax
movq %rax, 0x8(%r14)
movq %rax, (%r14)
movabsq $0x40000000000, %rax    # imm = 0x40000000000
movq %rax, 0x14(%r14)
testl %r13d, %r13d
jne 0xe59c
movq %r14, %rsi
callq 0xe346
testl %eax, %eax
je 0xe5c3
movl 0x10(%r14), %edi
movq (%r14), %rsi
movslq 0x18(%r14), %rdx
callq 0x4550
movq %rax, %r15
cmpl $-0x1, %r15d
je 0xe5d4
cmpl $0x0, 0x28(%r14)
jne 0xe571
movq 0x8(%rsp), %rax
movq (%rax), %rax
cmpq $0x0, 0x60(%rax)
je 0xe571
movq (%r14), %rax
movslq %r15d, %rcx
movb $0x0, (%rax,%rcx)
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq (%r14), %rdi
movq 0x68(%rax), %rsi
movl $0x1, %edx
callq *0x60(%rax)
xorl %r13d, %r13d
testl %r15d, %r15d
sete %r13b
subl %r15d, 0x18(%r14)
addl %r15d, 0x14(%r14)
movslq %r15d, %rax
addq %rax, (%r14)
jmp 0xe481
xorl %ebx, %ebx
jmp 0xe5c3
movq 0x10(%rsp), %rax
movb $0x0, (%rax)
jmp 0xe5c3
xorl %eax, %eax
cmpl $0x1, %ebx
sbbl %eax, %eax
orl %eax, %ebx
jmp 0xe5c3
leaq -0x2(%rbp), %rdi
leaq 0x5213(%rip), %rsi      # 0x137c5
callq 0x4520
testl %eax, %eax
jne 0xe5c3
movw $0xa, -0x2(%rbp)
decl %ebx
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x4bfb
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0xe5c3
nop