// File: /workspace/github_cpp_makefile_star2_O3/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
movq $0x0, 0x38(%rsp)
movl $0x1, 0x20(%rsp)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpl $0x0, 0x28(%rsi)
jne 0xf90d
movl %r8d, %r12d
movq %rsi, %r15
leal -0x3(%r12), %eax
cmpl $-0x3, %eax
ja 0xf69e
addq $0x88, %r15
leaq 0x41d5(%rip), %rsi      # 0x13868
movq %r15, %rdi
movl %r12d, %edx
jmp 0xf906
movl %ecx, %r14d
movl %ecx, %eax
andl $-0x9, %eax
cmpl $0x41, %eax
jne 0xf8f2
movq %r9, %rbp
movq %rdi, %r13
movq %rdx, 0x28(%rsp)
leaq 0x24(%rsp), %rdx
movl $0x10, (%rdx)
movl 0x10(%r15), %edi
leaq 0x10(%rsp), %rsi
callq 0x4370
testl %eax, %eax
js 0xf950
movl $0x2, %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x40c0
cmpl $-0x1, %eax
je 0xf959
movq %rbp, 0x30(%rsp)
leaq 0x20(%rsp), %rcx
movl %eax, %ebp
movl %eax, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x4700
cmpl $-0x1, %eax
je 0xf921
leaq 0x38(%rsp), %rcx
movl %ebp, %edi
movl $0x1, %esi
movl $0xd, %edx
movl $0x8, %r8d
callq 0x4700
cmpl $-0x1, %eax
je 0xf921
leaq 0x10(%rsp), %rsi
movw $0x0, 0x2(%rsi)
movl %ebp, %edi
movl $0x10, %edx
callq 0x4090
cmpl $-0x1, %eax
je 0xf967
movl %ebp, %edi
movl $0x1, %esi
callq 0x4430
testl %eax, %eax
js 0xf980
leaq 0x10(%rsp), %rsi
leaq 0x24(%rsp), %rdx
movl %ebp, 0xc(%rsp)
movl %ebp, %edi
callq 0x4370
testl %eax, %eax
js 0xf94c
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 0x40e4(%rip), %rsi      # 0x138a7
leaq 0x140(%rsp), %rbp
movq %rbp, %rdi
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x40a0
addq $0x10, %rsp
movq %r13, %rdi
movq %rbp, %rsi
movl $0x32, %edx
movq %r15, %rcx
callq 0xeb90
testl %eax, %eax
je 0xf99c
movq (%r13), %rax
movq 0x1b0(%rax), %rdx
testq %rdx, %rdx
movq 0x30(%rsp), %rbp
je 0xf83d
leaq 0x3e77(%rip), %rsi      # 0x1368b
leaq 0x40(%rsp), %rdi
xorl %eax, %eax
callq 0x40a0
movq %r13, %rdi
leaq 0x40(%rsp), %rsi
movl $0x33, %edx
movq %r15, %rcx
callq 0xeb90
testl %eax, %eax
je 0xf943
movl $0x1, %edi
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x4240
testq %rax, %rax
je 0xf990
cmpl $0x41, %r14d
movq %rax, %r14
jne 0xf875
movl $0x400, %edi            # imm = 0x400
callq 0x4500
movq %rax, 0x20(%r14)
testq %rax, %rax
je 0xf9aa
movq %r13, %rdi
movq %rbp, %rsi
movl $0x31, %edx
movq %r15, %rcx
callq 0xeb90
testl %eax, %eax
je 0xf92a
movl 0xc(%rsp), %eax
movl %eax, 0x10(%r14)
movl %r12d, 0x28(%r14)
xorl %eax, %eax
cmpl $0x1, 0x38(%r15)
cmoveq %r15, %rax
movq %rax, 0x30(%r14)
movups 0x40(%r15), %xmm0
movups %xmm0, 0x40(%r14)
movq 0x68(%r15), %rax
movq %rax, 0x68(%r14)
movq 0x78(%r15), %rax
movq %rax, 0x78(%r14)
cmpq $0x0, 0x40(%r14)
movq 0x28(%rsp), %rdx
jne 0xf8d8
cmpq $0x0, 0x48(%r14)
je 0xf8e0
movq 0x58(%r15), %rcx
movq %rcx, 0x58(%r14)
testq %rax, %rax
je 0xf970
movq 0x50(%r15), %rax
jmp 0xf972
addq $0x88, %r15
leaq 0x3f7e(%rip), %rsi      # 0x1387e
movq %r15, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0x40a0
movl %ebx, %eax
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3e33(%rip), %rdi      # 0x1375b
jmp 0xf987
movq 0x28(%rsp), %r14
movq (%r14), %rsi
movq %r13, %rdi
callq 0xf9cc
movq $0x0, (%r14)
jmp 0xf90d
movl 0xc(%rsp), %edi
callq 0x4760
xorl %ebx, %ebx
jmp 0xf90d
leaq 0x3f38(%rip), %rdi      # 0x1388f
jmp 0xf960
leaq 0x3df4(%rip), %rdi      # 0x13754
callq 0x4310
jmp 0xf90d
leaq 0x3f2d(%rip), %rdi      # 0x1389b
jmp 0xf987
xorl %eax, %eax
movq %rax, 0x50(%r14)
movq %r14, (%rdx)
movl $0x1, %ebx
jmp 0xf90d
leaq 0x3f19(%rip), %rdi      # 0x138a0
callq 0x4310
movl %ebp, %edi
jmp 0xf9a0
leaq 0x3ced(%rip), %rdi      # 0x13684
callq 0x4310
movl 0xc(%rsp), %edi
callq 0x4760
jmp 0xf90d
leaq 0x3cd3(%rip), %rdi      # 0x13684
callq 0x4310
movl 0xc(%rsp), %edi
callq 0x4760
movq %r14, %rdi
callq 0x44a0
jmp 0xf90d