// File: lianxings[P]ftp_benchmark/ftplib.cc
// Function: ftplib::FtpAccess(char const*, ftplib::accesstype, ftplib::transfermode, ftphandle*, ftphandle**)
int ftplib::FtpAccess(const char *path, accesstype type, transfermode mode, ftphandle *nControl, ftphandle **nData)
{
	char buf[256];
	int dir;

	if ((path == NULL) && ((type == ftplib::filewrite)
		|| (type == ftplib::fileread)
		|| (type == ftplib::filereadappend)
		|| (type == ftplib::filewriteappend)))
	{
		sprintf(nControl->response,"Missing path argument for file transfer\n");
		return 0;
	}
	sprintf(buf, "TYPE %c", mode);
	if (!FtpSendCmd(buf, '2', nControl)) 
        return 0;

	switch (type)
	{
	case ftplib::dir:
		strcpy(buf,"NLST");
		dir = FTPLIB_READ;
		break;
	case ftplib::dirverbose:
		strcpy(buf,"LIST -aL");
		dir = FTPLIB_READ;
		break;
	case ftplib::filereadappend:
	case ftplib::fileread:
		strcpy(buf,"RETR");
		dir = FTPLIB_READ;
		break;
	case ftplib::filewriteappend:
	case ftplib::filewrite:
		strcpy(buf,"STOR");
		dir = FTPLIB_WRITE;
		break;
	default:
		sprintf(nControl->response, "Invalid open type %d\n", type);
		return 0;
	}
	if (path != NULL)
	{
		int i = strlen(buf);
		buf[i++] = ' ';
		if ((strlen(path) + i) >= sizeof(buf)) return 0;
		strcpy(&buf[i],path);
	}

	if (nControl->cmode == ftplib::pasv)
	{
		if (FtpOpenPasv(nControl, nData, mode, dir, buf) == -1) 
            return 0;
	}

	if (nControl->cmode == ftplib::port)
	{
		if (FtpOpenPort(nControl, nData, mode, dir, buf) == -1) 
            return 0;
		if (!FtpAcceptConnection(*nData,nControl))
		{
			FtpClose(*nData);
			*nData = NULL;
			return 0;
		}
	}

#ifndef NOSSL
	if (nControl->tlsdata)
	{
		(*nData)->ssl = SSL_new(nControl->ctx);
		(*nData)->sbio = BIO_new_socket((*nData)->handle, BIO_NOCLOSE);
		SSL_set_bio((*nData)->ssl,(*nData)->sbio,(*nData)->sbio);
		int ret = SSL_connect((*nData)->ssl);
		if (ret != 1) return 0;
		(*nData)->tlsdata = 1;
	}
#endif
	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x140(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x17860
cmpl $0x4, -0x1c(%rbp)
je 0x1783b
cmpl $0x3, -0x1c(%rbp)
je 0x1783b
cmpl $0x5, -0x1c(%rbp)
je 0x1783b
cmpl $0x6, -0x1c(%rbp)
jne 0x17860
movq -0x28(%rbp), %rdi
addq $0x88, %rdi
leaq 0x604e(%rip), %rsi      # 0x1d89b
movb $0x0, %al
callq 0x40d0
movl $0x0, -0x4(%rbp)
jmp 0x17aba
leaq -0x130(%rbp), %rdi
movl -0x20(%rbp), %edx
leaq 0x6053(%rip), %rsi      # 0x1d8c4
movb $0x0, %al
callq 0x40d0
movq -0x140(%rbp), %rdi
leaq -0x130(%rbp), %rsi
movq -0x28(%rbp), %rcx
movl $0x32, %edx
callq 0x171c0
cmpl $0x0, %eax
jne 0x178a5
movl $0x0, -0x4(%rbp)
jmp 0x17aba
movl -0x1c(%rbp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x148(%rbp)
subl $0x5, %eax
ja 0x17952
movq -0x148(%rbp), %rax
leaq 0x5e36(%rip), %rcx      # 0x1d700
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq -0x130(%rbp), %rdi
leaq 0x5feb(%rip), %rsi      # 0x1d8cc
callq 0x4430
movl $0x1, -0x134(%rbp)
jmp 0x1797a
leaq -0x130(%rbp), %rdi
leaq 0x5fce(%rip), %rsi      # 0x1d8d1
callq 0x4430
movl $0x1, -0x134(%rbp)
jmp 0x1797a
leaq -0x130(%rbp), %rdi
leaq 0x5fb8(%rip), %rsi      # 0x1d8da
callq 0x4430
movl $0x1, -0x134(%rbp)
jmp 0x1797a
leaq -0x130(%rbp), %rdi
leaq 0x5f9e(%rip), %rsi      # 0x1d8df
callq 0x4430
movl $0x2, -0x134(%rbp)
jmp 0x1797a
movq -0x28(%rbp), %rdi
addq $0x88, %rdi
movl -0x1c(%rbp), %edx
leaq 0x5f7d(%rip), %rsi      # 0x1d8e4
movb $0x0, %al
callq 0x40d0
movl $0x0, -0x4(%rbp)
jmp 0x17aba
cmpq $0x0, -0x18(%rbp)
je 0x179ef
leaq -0x130(%rbp), %rdi
callq 0x41d0
movl %eax, -0x138(%rbp)
movl -0x138(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x138(%rbp)
cltq
movb $0x20, -0x130(%rbp,%rax)
movq -0x18(%rbp), %rdi
callq 0x41d0
movslq -0x138(%rbp), %rcx
addq %rcx, %rax
cmpq $0x100, %rax            # imm = 0x100
jb 0x179d5
movl $0x0, -0x4(%rbp)
jmp 0x17aba
movslq -0x138(%rbp), %rax
leaq -0x130(%rbp), %rdi
addq %rax, %rdi
movq -0x18(%rbp), %rsi
callq 0x4430
movq -0x28(%rbp), %rax
cmpl $0x1, 0x38(%rax)
jne 0x17a31
movq -0x140(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movl -0x20(%rbp), %ecx
movl -0x134(%rbp), %r8d
leaq -0x130(%rbp), %r9
callq 0x17ad0
cmpl $-0x1, %eax
jne 0x17a2f
movl $0x0, -0x4(%rbp)
jmp 0x17aba
jmp 0x17a31
movq -0x28(%rbp), %rax
cmpl $0x2, 0x38(%rax)
jne 0x17ab3
movq -0x140(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movl -0x20(%rbp), %ecx
movl -0x134(%rbp), %r8d
leaq -0x130(%rbp), %r9
callq 0x18190
cmpl $-0x1, %eax
jne 0x17a6e
movl $0x0, -0x4(%rbp)
jmp 0x17aba
movq -0x140(%rbp), %rdi
movq -0x30(%rbp), %rax
movq (%rax), %rsi
movq -0x28(%rbp), %rdx
callq 0x17480
cmpl $0x0, %eax
jne 0x17ab1
movq -0x140(%rbp), %rdi
movq -0x30(%rbp), %rax
movq (%rax), %rsi
callq 0x18690
movq -0x30(%rbp), %rax
movq $0x0, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x17aba
jmp 0x17ab3
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
nopw %cs:(%rax,%rax)