// File: /workspace/github_cpp_makefile_star2_O1/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, 0x28(%rsp)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpl $0x0, 0x28(%rsi)
jne 0xf937
movl %r8d, %r12d
movq %rsi, %r15
leal -0x3(%r12), %eax
cmpl $-0x3, %eax
ja 0xf6ba
addq $0x88, %r15
leaq 0x41a9(%rip), %rsi      # 0x13858
movq %r15, %rdi
movl %r12d, %edx
jmp 0xf930
movl %ecx, %r14d
movl %ecx, %eax
andl $-0x9, %eax
cmpl $0x41, %eax
jne 0xf91c
movq %r9, %rbp
movq %rdi, %r13
movq %rdx, 0x20(%rsp)
leaq 0x2c(%rsp), %rdx
movl $0x10, (%rdx)
movl 0x10(%r15), %edi
leaq 0x10(%rsp), %rsi
callq 0x4370
testl %eax, %eax
js 0xf97a
movl $0x2, %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x40c0
cmpl $-0x1, %eax
je 0xf983
movq %rbp, 0x30(%rsp)
leaq 0x28(%rsp), %rcx
movl %eax, %ebp
movl %eax, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x4710
cmpl $-0x1, %eax
je 0xf94b
leaq 0x38(%rsp), %rcx
movl %ebp, %edi
movl $0x1, %esi
movl $0xd, %edx
movl $0x8, %r8d
callq 0x4710
cmpl $-0x1, %eax
je 0xf94b
leaq 0x10(%rsp), %rsi
movw $0x0, 0x2(%rsi)
movl %ebp, %edi
movl $0x10, %edx
callq 0x4090
cmpl $-0x1, %eax
je 0xf991
movl %ebp, %edi
movl $0x1, %esi
callq 0x4430
testl %eax, %eax
js 0xf9af
leaq 0x10(%rsp), %rsi
leaq 0x2c(%rsp), %rdx
movl %ebp, 0xc(%rsp)
movl %ebp, %edi
callq 0x4370
testl %eax, %eax
js 0xf976
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 0x40b8(%rip), %rsi      # 0x13897
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 0xeba0
testl %eax, %eax
je 0xf9d2
movq (%r13), %rax
movq 0x1b0(%rax), %rdx
testq %rdx, %rdx
movq 0x30(%rsp), %rbp
je 0xf859
leaq 0x3e4b(%rip), %rsi      # 0x1367b
leaq 0x40(%rsp), %rdi
xorl %eax, %eax
callq 0x40a0
movq %r13, %rdi
leaq 0x40(%rsp), %rsi
movl $0x33, %edx
movq %r15, %rcx
callq 0xeba0
testl %eax, %eax
je 0xf96d
movl $0x1, %edi
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x4240
testq %rax, %rax
je 0xf9c6
cmpl $0x41, %r14d
movq %rax, %r14
jne 0xf891
movl $0x400, %edi            # imm = 0x400
callq 0x4500
movq %rax, 0x20(%r14)
testq %rax, %rax
je 0xf9e0
movq %r13, %rdi
movq %rbp, %rsi
movl $0x31, %edx
movq %r15, %rcx
callq 0xeba0
testl %eax, %eax
je 0xf954
movl 0xc(%rsp), %eax
movl %eax, 0x10(%r14)
movl %r12d, 0x28(%r14)
xorl %ecx, %ecx
cmpl $0x1, 0x38(%r15)
movl $0x0, %eax
cmoveq %r15, %rax
movq %rax, 0x30(%r14)
movups 0x40(%r15), %xmm0
movups %xmm0, 0x40(%r14)
movq 0x68(%r15), %rax
movq %rax, 0x68(%r14)
movq %rcx, 0x70(%r14)
movq %rcx, 0x80(%r14)
movq 0x78(%r15), %rax
movq %rax, 0x78(%r14)
cmpq %rcx, 0x40(%r14)
jne 0xf902
cmpq $0x0, 0x48(%r14)
je 0xf9bf
movq 0x58(%r15), %rcx
movq %rcx, 0x58(%r14)
testq %rax, %rax
je 0xf99a
movq 0x50(%r15), %rax
jmp 0xf99c
addq $0x88, %r15
leaq 0x3f44(%rip), %rsi      # 0x1386e
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 0x3df9(%rip), %rdi      # 0x1374b
jmp 0xf9b6
movq 0x20(%rsp), %r14
movq (%r14), %rsi
movq %r13, %rdi
callq 0xfa02
movq $0x0, (%r14)
jmp 0xf937
movl 0xc(%rsp), %edi
callq 0x4770
xorl %ebx, %ebx
jmp 0xf937
leaq 0x3efe(%rip), %rdi      # 0x1387f
jmp 0xf98a
leaq 0x3dba(%rip), %rdi      # 0x13744
callq 0x4310
jmp 0xf937
leaq 0x3ef3(%rip), %rdi      # 0x1388b
jmp 0xf9b6
xorl %eax, %eax
movq 0x20(%rsp), %rcx
movq %rax, 0x50(%r14)
movq %r14, (%rcx)
movl $0x1, %ebx
jmp 0xf937
leaq 0x3eda(%rip), %rdi      # 0x13890
callq 0x4310
movl %ebp, %edi
jmp 0xf9d6
xorl %ecx, %ecx
jmp 0xf906
leaq 0x3ca7(%rip), %rdi      # 0x13674
callq 0x4310
movl 0xc(%rsp), %edi
callq 0x4770
jmp 0xf937
leaq 0x3c8d(%rip), %rdi      # 0x13674
callq 0x4310
movl 0xc(%rsp), %edi
callq 0x4770
movq %r14, %rdi
callq 0x44a0
jmp 0xf937