// File: /workspace/github_c_makefile_star2_O0/ToniWestbrook[P]paladin/kopen.c
// Function: ftp_open
static int ftp_open(const char *fn)
{
	char *p, *host = 0, *port = 0, *retr = 0;
	char host2[80], port2[10];
	int v[6], l, fd = -1, ret, pasv_port, pasv_ip[4];
	ftpaux_t aux;
	
	/* parse URL */
	if (strstr(fn, "ftp://") != fn) return 0;
	for (p = (char*)fn + 6; *p && *p != '/'; ++p);
	if (*p != '/') return 0;
	l = p - fn - 6;
	port = strdup("21");
	host = calloc(l + 1, 1);
	strncpy(host, fn + 6, l);
	retr = calloc(strlen(p) + 8, 1);
	sprintf(retr, "RETR %s\r\n", p);
	
	/* connect to ctrl */
	memset(&aux, 0, sizeof(ftpaux_t));
	aux.ctrl_fd = socket_connect(host, port);
	if (aux.ctrl_fd == -1) goto ftp_open_end; /* fail to connect ctrl */

	/* connect to the data stream */
	kftp_get_response(&aux);
	kftp_send_cmd(&aux, "USER anonymous\r\n", 1);
	kftp_send_cmd(&aux, "PASS kopen@\r\n", 1);
	kftp_send_cmd(&aux, "TYPE I\r\n", 1);
	kftp_send_cmd(&aux, "PASV\r\n", 1);
	for (p = aux.response; *p && *p != '('; ++p);
	if (*p != '(') goto ftp_open_end;
	++p;
	sscanf(p, "%d,%d,%d,%d,%d,%d", &v[0], &v[1], &v[2], &v[3], &v[4], &v[5]);
	memcpy(pasv_ip, v, 4 * sizeof(int));
	pasv_port = (v[4]<<8&0xff00) + v[5];
	kftp_send_cmd(&aux, retr, 0);
	sprintf(host2, "%d.%d.%d.%d", pasv_ip[0], pasv_ip[1], pasv_ip[2], pasv_ip[3]);
	sprintf(port2, "%d", pasv_port);
	fd = socket_connect(host2, port2);
	if (fd == -1) goto ftp_open_end;
	ret = kftp_get_response(&aux);
	if (ret != 150) {
		close(fd);
		fd = -1;
	}
	close(aux.ctrl_fd);

ftp_open_end:
	free(host); free(port); free(retr); free(aux.response);
	return fd;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x10(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movl $0xffffffff, -0xb8(%rbp) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rdi
leaq 0x30bd1(%rip), %rsi     # 0x3769d
callq 0x3080
cmpq -0x10(%rbp), %rax
je 0x6ae3
movl $0x0, -0x4(%rbp)
jmp 0x6e90
movq -0x10(%rbp), %rax
addq $0x6, %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0xe1(%rbp)
je 0x6b16
movq -0x18(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2f, %eax
setne %al
movb %al, -0xe1(%rbp)
movb -0xe1(%rbp), %al
testb $0x1, %al
jne 0x6b22
jmp 0x6b32
jmp 0x6b24
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x6aef
movq -0x18(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2f, %eax
je 0x6b4a
movl $0x0, -0x4(%rbp)
jmp 0x6e90
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
subq %rcx, %rax
subq $0x6, %rax
movl %eax, -0xb4(%rbp)
leaq 0x30ba5(%rip), %rdi     # 0x3770b
leaq 0x30b22(%rip), %rsi     # 0x3768f
movl $0xbf, %edx
leaq 0x30b95(%rip), %rcx     # 0x3770e
callq 0x31df0
movq %rax, -0x28(%rbp)
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdi
movl $0x1, %esi
leaq 0x30af5(%rip), %rdx     # 0x3768f
movl $0xc0, %ecx
leaq 0x30b68(%rip), %r8      # 0x3770e
callq 0x31bf0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x6, %rsi
movslq -0xb4(%rbp), %rdx
callq 0x3120
movq -0x18(%rbp), %rdi
callq 0x30f0
movq %rax, %rdi
addq $0x8, %rdi
movl $0x1, %esi
leaq 0x30aac(%rip), %rdx     # 0x3768f
movl $0xc2, %ecx
leaq 0x30b1f(%rip), %r8      # 0x3770e
callq 0x31bf0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x30b10(%rip), %rsi     # 0x37717
movb $0x0, %al
callq 0x3070
leaq -0xe0(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x3130
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x7190
movl %eax, -0xdc(%rbp)
cmpl $-0x1, -0xdc(%rbp)
jne 0x6c42
jmp 0x6e60
leaq -0xe0(%rbp), %rdi
callq 0x73d0
leaq -0xe0(%rbp), %rdi
leaq 0x30ac5(%rip), %rsi     # 0x37721
movl $0x1, %edx
callq 0x75a0
leaq -0xe0(%rbp), %rdi
leaq 0x30abe(%rip), %rsi     # 0x37732
movl $0x1, %edx
callq 0x75a0
leaq -0xe0(%rbp), %rdi
leaq 0x30ab4(%rip), %rsi     # 0x37740
movl $0x1, %edx
callq 0x75a0
leaq -0xe0(%rbp), %rdi
leaq 0x30aa5(%rip), %rsi     # 0x37749
movl $0x1, %edx
callq 0x75a0
movq -0xd8(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0xe2(%rbp)
je 0x6ce0
movq -0x18(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x28, %eax
setne %al
movb %al, -0xe2(%rbp)
movb -0xe2(%rbp), %al
testb $0x1, %al
jne 0x6cec
jmp 0x6cfc
jmp 0x6cee
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x6cb9
movq -0x18(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x28, %eax
je 0x6d0d
jmp 0x6e60
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
leaq -0xb0(%rbp), %rdx
leaq -0xb0(%rbp), %rcx
addq $0x4, %rcx
leaq -0xb0(%rbp), %r8
addq $0x8, %r8
leaq -0xb0(%rbp), %r9
addq $0xc, %r9
leaq -0xb0(%rbp), %r10
addq $0x10, %r10
leaq -0xb0(%rbp), %rax
addq $0x14, %rax
leaq 0x309ee(%rip), %rsi     # 0x37750
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x32d0
movq -0xb0(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0xc8(%rbp)
movl -0xa0(%rbp), %eax
shll $0x8, %eax
andl $0xff00, %eax           # imm = 0xFF00
addl -0x9c(%rbp), %eax
movl %eax, -0xc0(%rbp)
movq -0x30(%rbp), %rsi
leaq -0xe0(%rbp), %rdi
xorl %edx, %edx
callq 0x75a0
leaq -0x80(%rbp), %rdi
movl -0xd0(%rbp), %edx
movl -0xcc(%rbp), %ecx
movl -0xc8(%rbp), %r8d
movl -0xc4(%rbp), %r9d
leaq 0x30983(%rip), %rsi     # 0x37762
movb $0x0, %al
callq 0x3070
leaq -0x8a(%rbp), %rdi
movl -0xc0(%rbp), %edx
leaq 0x30965(%rip), %rsi     # 0x3775f
movb $0x0, %al
callq 0x3070
leaq -0x80(%rbp), %rdi
leaq -0x8a(%rbp), %rsi
callq 0x7190
movl %eax, -0xb8(%rbp)
cmpl $-0x1, -0xb8(%rbp)
jne 0x6e22
jmp 0x6e60
leaq -0xe0(%rbp), %rdi
callq 0x73d0
movl %eax, -0xbc(%rbp)
cmpl $0x96, -0xbc(%rbp)
je 0x6e55
movl -0xb8(%rbp), %edi
callq 0x3780
movl $0xffffffff, -0xb8(%rbp) # imm = 0xFFFFFFFF
movl -0xdc(%rbp), %edi
callq 0x3780
movq -0x20(%rbp), %rdi
callq 0x3380
movq -0x28(%rbp), %rdi
callq 0x3380
movq -0x30(%rbp), %rdi
callq 0x3380
movq -0xd8(%rbp), %rdi
callq 0x3380
movl -0xb8(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopl (%rax)