// File: /workspace/github_cpp_makefile_star2_O2/lianxings[P]ftp_benchmark/ftplib.cc
// Function: ftplib::FtpOpenPort(ftphandle*, ftphandle**, ftplib::transfermode, int, char*)
int ftplib::FtpOpenPort(ftphandle *nControl, ftphandle **nData, transfermode mode, int dir, char *cmd)
{
	int sData;
	union {
	struct sockaddr sa;
	struct sockaddr_in in;
	} sin;
	struct linger lng = { 0, 0 };
	socklen_t l;
	int on=1;
	ftphandle *ctrl;
	char buf[256]; 

	if (nControl->dir != FTPLIB_CONTROL) 
        return -1;
	if ((dir != FTPLIB_READ) && (dir != FTPLIB_WRITE))	{
		sprintf(nControl->response, "Invalid direction %d\n", dir);
		return -1;
	}
	if ((mode != ftplib::ascii) && (mode != ftplib::image)) {
		sprintf(nControl->response, "Invalid mode %c\n", mode);
		return -1;
	}
	l = sizeof(sin);

	if (getsockname(nControl->handle, &sin.sa, &l) < 0) {
		perror("getsockname");
		return -1;
	}

	sData = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
	if (sData == -1) 	{
		perror("socket");
		return -1;
	}
	if (setsockopt(sData,SOL_SOCKET,SO_REUSEADDR, SETSOCKOPT_OPTVAL_TYPE &on,sizeof(on)) == -1) 	{
		perror("setsockopt");
		net_close(sData);
		return -1;
	}
	if (setsockopt(sData,SOL_SOCKET,SO_LINGER, SETSOCKOPT_OPTVAL_TYPE &lng,sizeof(lng)) == -1) 	{
		perror("setsockopt");
		net_close(sData);
		return -1;
	}

	sin.in.sin_port = 0;
	if (bind(sData, &sin.sa, sizeof(sin)) == -1) 	{
		perror("bind");
		net_close(sData);
		return -1;
	}
	if (listen(sData, 1) < 0) 	{
		perror("listen");
		net_close(sData);
		return -1;
	}
	if (getsockname(sData, &sin.sa, &l) < 0) 
        return 0;
    
	sprintf(buf, "PORT %hhu,%hhu,%hhu,%hhu,%hhu,%hhu",
		(unsigned char) sin.sa.sa_data[2],
		(unsigned char) sin.sa.sa_data[3],
		(unsigned char) sin.sa.sa_data[4],
		(unsigned char) sin.sa.sa_data[5],
		(unsigned char) sin.sa.sa_data[0],
		(unsigned char) sin.sa.sa_data[1]);

    if (!FtpSendCmd(buf,'2',nControl)) 	{
		net_close(sData);
		return -1;
	}

	if (mp_ftphandle->offset != 0)	{
    	char buf[256];
        sprint_rest(buf, mp_ftphandle->offset);
    	if (!FtpSendCmd(buf,'3',nControl)) 	{
    		net_close(sData);
    		return 0;
    	}
	}

	ctrl = static_cast<ftphandle*>(calloc(1,sizeof(ftphandle)));
	if (ctrl == NULL) 	{
		perror("calloc");
		net_close(sData);
		return -1;
	}
	if ((mode == 'A') && ((ctrl->buf = static_cast<char*>(malloc(FTPLIB_BUFSIZ))) == NULL)) 	{
		perror("calloc");
		net_close(sData);
		free(ctrl);
		return -1;
	}

	if (!FtpSendCmd(cmd, '1', nControl)) 	{
		FtpClose(*nData);
		*nData = NULL;
		return -1;
	}

	ctrl->handle = sData;
	ctrl->dir = dir;
	ctrl->ctrl = (nControl->cmode == ftplib::pasv) ? nControl : NULL;
	ctrl->idletime = nControl->idletime;
	ctrl->cbarg = nControl->cbarg;
	ctrl->xfered = 0;
	ctrl->xfered1 = 0;
	ctrl->cbbytes = nControl->cbbytes;
	if (ctrl->idletime.tv_sec || ctrl->idletime.tv_usec) ctrl->idlecb = nControl->idlecb;
	else ctrl->idlecb = NULL;
	if (ctrl->cbbytes ) ctrl->xfercb = nControl->xfercb;
	else ctrl->xfercb = NULL;
	*nData = ctrl;

	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x248, %rsp            # imm = 0x248
andq $0x0, 0x38(%rsp)
movl $0x1, 0x20(%rsp)
pushq $-0x1
popq %rbx
cmpl $0x0, 0x28(%rsi)
jne 0xd67b
movl %r8d, %r15d
movq %rsi, %r14
leal -0x3(%r15), %eax
cmpl $-0x3, %eax
ja 0xd42a
addq $0x88, %r14
leaq 0x43e9(%rip), %rsi      # 0x11808
movq %r14, %rdi
movl %r15d, %edx
jmp 0xd674
movl %ecx, %ebp
movl %ecx, %eax
andl $-0x9, %eax
cmpl $0x41, %eax
jne 0xd661
movq %r9, %r13
movq %rdi, %r12
movq %rdx, 0x28(%rsp)
leaq 0x24(%rsp), %rdx
movl $0x10, (%rdx)
movl 0x10(%r14), %edi
leaq 0x10(%rsp), %rsi
callq 0x4360
testl %eax, %eax
js 0xd6bb
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
pushq $0x6
popq %rdx
callq 0x40b0
cmpl $-0x1, %eax
je 0xd6c4
movq %r13, 0x30(%rsp)
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
leaq 0x20(%rsp), %rcx
pushq $0x4
popq %r8
movl %eax, %r13d
movl %eax, %edi
callq 0x46c0
cmpl $-0x1, %eax
je 0xd68f
pushq $0x1
popq %rsi
pushq $0xd
popq %rdx
leaq 0x38(%rsp), %rcx
pushq $0x8
popq %r8
movl %r13d, %edi
callq 0x46c0
cmpl $-0x1, %eax
je 0xd68f
leaq 0x10(%rsp), %rsi
andw $0x0, 0x2(%rsi)
pushq $0x10
popq %rdx
movl %r13d, %edi
callq 0x4080
cmpl $-0x1, %eax
je 0xd6d2
pushq $0x1
popq %rbx
movl %r13d, %edi
movl %ebx, %esi
callq 0x4430
testl %eax, %eax
js 0xd6f3
leaq 0x10(%rsp), %rsi
leaq 0x24(%rsp), %rdx
movl %r13d, 0xc(%rsp)
movl %r13d, %edi
callq 0x4360
testl %eax, %eax
js 0xd6b7
movzbl 0x14(%rsp), %edx
movzbl 0x15(%rsp), %ecx
movzbl 0x16(%rsp), %r8d
movzbl 0x17(%rsp), %r9d
movzbl 0x12(%rsp), %r10d
movzbl 0x13(%rsp), %r11d
leaq 0x4308(%rip), %rsi      # 0x11847
leaq 0x140(%rsp), %r13
movq %r13, %rdi
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x4090
popq %rcx
popq %rdx
pushq $0x32
popq %rdx
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rcx
callq 0xc9ae
testl %eax, %eax
je 0xd710
movq (%r12), %rax
movq 0x1b0(%rax), %rdx
testq %rdx, %rdx
movq 0x30(%rsp), %r13
je 0xd5b3
leaq 0x409f(%rip), %rsi      # 0x1162b
leaq 0x40(%rsp), %rdi
xorl %eax, %eax
callq 0x4090
pushq $0x33
popq %rdx
movq %r12, %rdi
leaq 0x40(%rsp), %rsi
movq %r14, %rcx
callq 0xc9ae
testl %eax, %eax
je 0xd6ae
pushq $0x1
popq %rdi
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x4240
testq %rax, %rax
je 0xd704
cmpl $0x41, %ebp
movq %rax, %rbp
jne 0xd5e8
movl $0x400, %edi            # imm = 0x400
callq 0x44f0
movq %rax, 0x20(%rbp)
testq %rax, %rax
je 0xd721
pushq $0x31
popq %rdx
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rcx
callq 0xc9ae
testl %eax, %eax
je 0xd698
movl 0xc(%rsp), %eax
movl %eax, 0x10(%rbp)
movl %r15d, 0x28(%rbp)
xorl %eax, %eax
cmpl $0x1, 0x38(%r14)
cmoveq %r14, %rax
movq %rax, 0x30(%rbp)
movups 0x40(%r14), %xmm0
movups %xmm0, 0x40(%rbp)
movq 0x68(%r14), %rax
movq %rax, 0x68(%rbp)
movq 0x78(%r14), %rax
movq %rax, 0x78(%rbp)
cmpq $0x0, 0x40(%rbp)
movq 0x28(%rsp), %rdx
jne 0xd647
cmpq $0x0, 0x48(%rbp)
je 0xd64f
movq 0x58(%r14), %rcx
movq %rcx, 0x58(%rbp)
testq %rax, %rax
je 0xd6e8
movq 0x50(%r14), %rax
jmp 0xd6ea
addq $0x88, %r14
leaq 0x41af(%rip), %rsi      # 0x1181e
movq %r14, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x4090
movl %ebx, %eax
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4065(%rip), %rdi      # 0x116fb
jmp 0xd6d9
movq 0x28(%rsp), %rbx
movq (%rbx), %rsi
movq %r12, %rdi
callq 0xd740
andq $0x0, (%rbx)
jmp 0xd719
movl 0xc(%rsp), %edi
callq 0x4740
xorl %ebx, %ebx
jmp 0xd67b
leaq 0x416d(%rip), %rdi      # 0x1182f
jmp 0xd6cb
leaq 0x4029(%rip), %rdi      # 0x116f4
callq 0x4300
jmp 0xd67b
leaq 0x4162(%rip), %rdi      # 0x1183b
callq 0x4300
movl %r13d, %edi
callq 0x4740
jmp 0xd67b
xorl %eax, %eax
movq %rax, 0x50(%rbp)
movq %rbp, (%rdx)
jmp 0xd67b
leaq 0x4146(%rip), %rdi      # 0x11840
callq 0x4300
movl %r13d, %edi
jmp 0xd714
leaq 0x3f19(%rip), %rdi      # 0x11624
callq 0x4300
movl 0xc(%rsp), %edi
callq 0x4740
pushq $-0x1
popq %rbx
jmp 0xd67b
leaq 0x3efc(%rip), %rdi      # 0x11624
callq 0x4300
movl 0xc(%rsp), %edi
callq 0x4740
movq %rbp, %rdi
callq 0x4480
jmp 0xd719