// File: lianxings[P]ftp_benchmark/ftplib.cc
// Function: ftplib::writeline(char*, int, ftphandle*)
int ftplib::writeline(char *buf, int len, ftphandle *nData)
{
	int x, nb=0, w;
	char *ubp = buf, *nbp;
	char lc=0;

	if (nData->dir != FTPLIB_WRITE)
	return -1;
	nbp = nData->buf;
	for (x=0; x < len; x++) 
	{
		if ((*ubp == '\n') && (lc != '\r'))
		{
			if (nb == FTPLIB_BUFSIZ)
			{
				if (!socket_wait(nData)) 
                    return x;
#ifndef NOSSL
				if (nData->tlsctrl) 
                    w = SSL_write(nData->ssl, nbp, FTPLIB_BUFSIZ);
				else 
                    w = net_write(nData->handle, nbp, FTPLIB_BUFSIZ);
#else
				w = net_write(nData->handle, nbp, FTPLIB_BUFSIZ);
#endif
				if (w != FTPLIB_BUFSIZ) 	{
					printf("write(1) returned %d, errno = %d\n", w, errno);
					return(-1);
				}
				nb = 0;
			}
			nbp[nb++] = '\r';
		}
		if (nb == FTPLIB_BUFSIZ)
		{
			if (!socket_wait(nData))
			    return x;
#ifndef NOSSL
			if (nData->tlsctrl) 
                w = SSL_write(nData->ssl, nbp, FTPLIB_BUFSIZ);
			else 
                w = net_write(nData->handle, nbp, FTPLIB_BUFSIZ);
#else
			w = net_write(nData->handle, nbp, FTPLIB_BUFSIZ);
#endif	
			if (w != FTPLIB_BUFSIZ) 			{
				fprintf(stderr, "write(2) returned %d, errno = %d\n", w, errno);
				return(-1);
			}
			nb = 0;
		}
		nbp[nb++] = lc = *ubp++;
	}
	if (nb)
	{
		if (!socket_wait(nData)) return x;
#ifndef NOSSL
		if (nData->tlsctrl) 
            w = SSL_write(nData->ssl, nbp, nb);
		else 
            w = net_write(nData->handle, nbp, nb);
#else
		w = net_write(nData->handle, nbp, nb);	
#endif
		if (w != nb) 		{
			fprintf(stderr, "write(3) returned %d, errno = %d\n", w, errno);
			return(-1);
		}
	}
	return len;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
movl $0x0, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x40(%rbp)
movb $0x0, -0x49(%rbp)
movq -0x28(%rbp), %rax
cmpl $0x2, 0x28(%rax)
je 0x16a9b
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x16caa
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x48(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x16c2a
movq -0x40(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
jne 0x16b65
movsbl -0x49(%rbp), %eax
cmpl $0xd, %eax
je 0x16b65
cmpl $0x400, -0x30(%rbp)     # imm = 0x400
jne 0x16b4f
movq -0x58(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x16530
cmpl $0x0, %eax
jne 0x16afd
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x16caa
movq -0x28(%rbp), %rax
movl 0x10(%rax), %edi
movq -0x48(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x4160
movl %eax, -0x34(%rbp)
cmpl $0x400, -0x34(%rbp)     # imm = 0x400
je 0x16b48
movl -0x34(%rbp), %eax
movl %eax, -0x5c(%rbp)
callq 0x4060
movl -0x5c(%rbp), %esi
movl (%rax), %edx
leaq 0x6c1d(%rip), %rdi      # 0x1d752
movb $0x0, %al
callq 0x4070
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x16caa
movl $0x0, -0x30(%rbp)
movq -0x48(%rbp), %rax
movl -0x30(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x30(%rbp)
movslq %ecx, %rcx
movb $0xd, (%rax,%rcx)
cmpl $0x400, -0x30(%rbp)     # imm = 0x400
jne 0x16bf3
movq -0x58(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x16530
cmpl $0x0, %eax
jne 0x16b8f
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x16caa
movq -0x28(%rbp), %rax
movl 0x10(%rax), %edi
movq -0x48(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x4160
movl %eax, -0x34(%rbp)
cmpl $0x400, -0x34(%rbp)     # imm = 0x400
je 0x16bec
movq 0xf411(%rip), %rax      # 0x25fc8
movq (%rax), %rax
movq %rax, -0x68(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x60(%rbp)
callq 0x4060
movq -0x68(%rbp), %rdi
movl -0x60(%rbp), %edx
movl (%rax), %ecx
leaq 0x6b9b(%rip), %rsi      # 0x1d774
movb $0x0, %al
callq 0x4680
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x16caa
movl $0x0, -0x30(%rbp)
movq -0x40(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x40(%rbp)
movb (%rax), %dl
movb %dl, -0x49(%rbp)
movq -0x48(%rbp), %rax
movl -0x30(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x30(%rbp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x16aae
cmpl $0x0, -0x30(%rbp)
je 0x16ca4
movq -0x58(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x16530
cmpl $0x0, %eax
jne 0x16c4a
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x16caa
movq -0x28(%rbp), %rax
movl 0x10(%rax), %edi
movq -0x48(%rbp), %rsi
movslq -0x30(%rbp), %rdx
callq 0x4160
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x30(%rbp), %eax
je 0x16ca2
movq 0xf358(%rip), %rax      # 0x25fc8
movq (%rax), %rax
movq %rax, -0x78(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x6c(%rbp)
callq 0x4060
movq -0x78(%rbp), %rdi
movl -0x6c(%rbp), %edx
movl (%rax), %ecx
leaq 0x6b04(%rip), %rsi      # 0x1d796
movb $0x0, %al
callq 0x4680
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x16caa
jmp 0x16ca4
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)