// File: /workspace/github_cpp_makefile_star2_O2/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)
pushq $-0x1
popq %rbx
cmpl $0x1, 0x28(%rcx)
ja 0xc4a1
movl %edx, %r12d
testl %edx, %edx
je 0xc424
movq %rcx, %r14
movl 0x14(%rcx), %eax
xorl %ebx, %ebx
movq %rsi, 0x10(%rsp)
movq %rsi, %rbp
xorl %r13d, %r13d
testl %eax, %eax
jle 0xc369
leal -0x1(%r12), %ecx
cmpl %eax, %r12d
cmovgel %eax, %ecx
movq 0x8(%r14), %rsi
movslq %ecx, %r15
movq %rbp, %rdi
pushq $0xa
popq %rdx
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 0xc43d
subl %r15d, %r12d
cmpl $0x1, %r12d
je 0xc428
movq (%r14), %rax
cmpq 0x8(%r14), %rax
jne 0xc395
movq 0x20(%r14), %rax
movq %rax, 0x8(%r14)
movq %rax, (%r14)
movabsq $0x40000000000, %rax    # imm = 0x40000000000
movq %rax, 0x14(%r14)
testl %r13d, %r13d
jne 0xc432
movq %r14, %rsi
callq 0xc1f0
testl %eax, %eax
je 0xc4a1
movl 0x10(%r14), %edi
movq (%r14), %rsi
movslq 0x18(%r14), %rdx
callq 0x4540
movq %rax, %r15
cmpl $-0x1, %r15d
je 0xc459
cmpl $0x0, 0x28(%r14)
jne 0xc400
movq 0x8(%rsp), %rax
movq (%rax), %rax
cmpq $0x0, 0x60(%rax)
je 0xc400
movq (%r14), %rax
movslq %r15d, %rcx
movb $0x0, (%rax,%rcx)
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq (%r14), %rdi
movq 0x68(%rax), %rsi
pushq $0x1
popq %rdx
callq *0x60(%rax)
xorl %r13d, %r13d
testl %r15d, %r15d
sete %r13b
subl %r15d, 0x18(%r14)
movl 0x14(%r14), %eax
addl %r15d, %eax
movl %eax, 0x14(%r14)
movslq %r15d, %rcx
addq %rcx, (%r14)
jmp 0xc318
xorl %ebx, %ebx
jmp 0xc4a1
movq 0x10(%rsp), %rax
movb $0x0, (%rax)
jmp 0xc4a1
xorl %eax, %eax
cmpl $0x1, %ebx
sbbl %eax, %eax
orl %eax, %ebx
jmp 0xc4a1
cmpb $0xd, -0x2(%rbp)
jne 0xc4a1
cmpb $0xa, -0x1(%rbp)
jne 0xc4a1
cmpb $0x0, (%rbp)
jne 0xc4a1
movw $0xa, -0x2(%rbp)
decl %ebx
jmp 0xc4a1
callq 0x4040
movq %rax, %rbx
movq 0xab60(%rip), %rax      # 0x16fc8
movq (%rax), %r15
movl $0xba, %edi
xorl %eax, %eax
callq 0x4060
movq %rax, %r12
movl 0x10(%r14), %ebp
movl (%rbx), %edi
callq 0x4110
leaq 0x51a8(%rip), %rsi      # 0x11634
movq %r15, %rdi
movq %r12, %rdx
movl %ebp, %ecx
movq %rax, %r8
xorl %eax, %eax
callq 0x4530
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq