// File: /workspace/github_c_makefile_star2_O0/icebert[P]pblat/lib/net.c
// Function: netGetOpenFtpSockets
static int netGetOpenFtpSockets(char *url, int *retCtrlSd)
/* Return a socket descriptor for url data (url can end in ";byterange:start-end)",
 * or -1 if error.
 * If retCtrlSd is non-null, keep the control socket alive and set *retCtrlSd.
 * Otherwise, create a pipe and fork to keep control socket alive in the child 
 * process until we are done fetching data. */
{
char cmd[256];

/* Parse the URL and connect. */
struct netParsedUrl npu;
struct netParsedUrl pxy;
netParseUrl(url, &npu);
if (!sameString(npu.protocol, "ftp"))
    errAbort("netGetOpenFtpSockets: url (%s) is not for ftp.", url);

boolean noProxy = checkNoProxy(npu.host);
char *proxyUrl = getenv("ftp_proxy");
if (noProxy)
    proxyUrl = NULL;

int sd = -1;
if (proxyUrl)
    {
    netParseUrl(proxyUrl, &pxy);
    if (!sameString(pxy.protocol, "ftp"))
        errAbort("Unknown proxy protocol %s in %s. Should be ftp.", pxy.protocol, proxyUrl);
    char proxyUser[4096];
    safef(proxyUser, sizeof proxyUser, "%s@%s:%s", npu.user, npu.host, npu.port);
    sd = openFtpControlSocket(pxy.host, atoi(pxy.port), proxyUser, npu.password);
    char *logProxy = getenv("log_proxy");
    if (sameOk(logProxy,"on"))
	verbose(1, "%s as %s via proxy %s\n", url, proxyUser, proxyUrl);
    }
else
    {
    sd = openFtpControlSocket(npu.host, atoi(npu.port), npu.user, npu.password);
    }

if (sd == -1)
    return -1;

struct dyString *rs = NULL;
if (!sendFtpCommand(sd, "PASV\r\n", &rs, NULL))
    {
    close(sd);
    return -1;
    }
/* 227 Entering Passive Mode (128,231,210,81,222,250) */

if (npu.byteRangeStart != -1)
    {
    safef(cmd,sizeof(cmd),"REST %lld\r\n", (long long) npu.byteRangeStart);
    if (!sendFtpCommand(sd, cmd, NULL, NULL))
	{
	close(sd);
	return -1;
	}
    }

/* RETR for files, LIST for directories ending in / */
safef(cmd,sizeof(cmd),"%s %s\r\n",((npu.file[strlen(npu.file)-1]) == '/') ? "LIST" : "RETR", npu.file);
sendFtpCommandOnly(sd, cmd);

int sdata = -1;
if (proxyUrl)
    sdata = netConnect(pxy.host, parsePasvPort(rs->string));
else
    sdata = netConnect(npu.host, parsePasvPort(rs->string));
dyStringFree(&rs);
if (sdata < 0)
    {
    close(sd);
    return -1;
    }

int secondsWaited = 0;
while (TRUE)
    {
    if (secondsWaited >= 10)
	{
	warn("ftp server error on cmd=[%s] timed-out waiting for data or error", cmd);
	close(sd);
	close(sdata);
	return -1;
	}
    if (readReadyWait(sdata, NET_FTP_TIMEOUT))
	break;   // we have some data
    if (readReadyWait(sd, 0)) /* wait in microsec */
	{
	// this can see an error like bad filename
	if (!receiveFtpReply(sd, cmd, NULL, NULL))
	    {
	    close(sd);
	    close(sdata);
	    return -1;
	    }
	}
    ++secondsWaited;
    }

if (retCtrlSd != NULL)
    {
    *retCtrlSd = sd;
    return sdata;
    }
else
    {
    /* Because some FTP servers will kill the data connection
     * as soon as the control connection closes,
     * we have to develop a workaround using a partner thread. */
    fflush(stdin);
    fflush(stdout);
    fflush(stderr);

    struct netConnectFtpParams *params;
    AllocVar(params);
    params->sd = sd;
    params->sdata = sdata;
    params->npu = npu;
    /* make a pipe (fds go in pipefd[0] and pipefd[1])  */
    if (pipe(params->pipefd) != 0)
	errAbort("netGetOpenFtpSockets: failed to create pipe: %s", strerror(errno));
    int rc;
    rc = pthread_create(&params->thread, NULL, sendFtpDataToPipeThread, (void *)params);
    if (rc)
	{
	errAbort("Unexpected error %d from pthread_create(): %s",rc,strerror(rc));
	}

    return params->pipefd[0];
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x6200, %rsp           # imm = 0x6200
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x2950(%rbp), %rsi
callq 0x42160
leaq -0x2950(%rbp), %rdi
leaq 0x9b12f(%rip), %rsi     # 0xdee30
callq 0x6a40
cmpl $0x0, %eax
je 0x43d1d
movq -0x10(%rbp), %rsi
leaq 0x98aef(%rip), %rdi     # 0xdc805
movb $0x0, %al
callq 0x35d40
leaq -0x2950(%rbp), %rdi
addq $0x1010, %rdi           # imm = 0x1010
callq 0x43430
movl %eax, -0x5184(%rbp)
leaq 0x98af7(%rip), %rdi     # 0xdc834
callq 0x68d0
movq %rax, -0x5190(%rbp)
cmpl $0x0, -0x5184(%rbp)
je 0x43d5d
movq $0x0, -0x5190(%rbp)
movl $0xffffffff, -0x5194(%rbp) # imm = 0xFFFFFFFF
cmpq $0x0, -0x5190(%rbp)
je 0x43e9f
movq -0x5190(%rbp), %rdi
leaq -0x5180(%rbp), %rsi
callq 0x42160
leaq -0x5180(%rbp), %rdi
leaq 0x9b09a(%rip), %rsi     # 0xdee30
callq 0x6a40
cmpl $0x0, %eax
je 0x43dbc
leaq -0x5180(%rbp), %rsi
movq -0x5190(%rbp), %rdx
leaq 0x98a89(%rip), %rdi     # 0xdc83e
movb $0x0, %al
callq 0x35d40
leaq -0x61a0(%rbp), %rdi
leaq -0x2950(%rbp), %rcx
addq $0x10, %rcx
leaq -0x2950(%rbp), %r8
addq $0x1010, %r8            # imm = 0x1010
leaq -0x2950(%rbp), %r9
addq $0x1810, %r9            # imm = 0x1810
movl $0x1000, %esi           # imm = 0x1000
leaq 0x98a78(%rip), %rdx     # 0xdc86e
movb $0x0, %al
callq 0x31090
leaq -0x5180(%rbp), %rax
addq $0x1010, %rax           # imm = 0x1010
movq %rax, -0x61d0(%rbp)
leaq -0x5180(%rbp), %rdi
addq $0x1810, %rdi           # imm = 0x1810
callq 0x6bd0
movq -0x61d0(%rbp), %rdi
movl %eax, %esi
leaq -0x61a0(%rbp), %rdx
leaq -0x2950(%rbp), %rcx
addq $0x810, %rcx            # imm = 0x810
callq 0x42d20
movl %eax, -0x5194(%rbp)
leaq 0x98002(%rip), %rdi     # 0xdbe56
callq 0x68d0
movq %rax, -0x61a8(%rbp)
movq -0x61a8(%rbp), %rdi
leaq 0x9394a(%rip), %rsi     # 0xd77b8
callq 0x2d310
cmpl $0x0, %eax
jne 0x43e9d
movq -0x10(%rbp), %rdx
leaq -0x61a0(%rbp), %rcx
movq -0x5190(%rbp), %r8
movl $0x1, %edi
leaq 0x989e1(%rip), %rsi     # 0xdc877
movb $0x0, %al
callq 0x60040
jmp 0x43ef3
leaq -0x2950(%rbp), %rax
addq $0x1010, %rax           # imm = 0x1010
movq %rax, -0x61d8(%rbp)
leaq -0x2950(%rbp), %rdi
addq $0x1810, %rdi           # imm = 0x1810
callq 0x6bd0
movq -0x61d8(%rbp), %rdi
movl %eax, %esi
leaq -0x2950(%rbp), %rdx
addq $0x10, %rdx
leaq -0x2950(%rbp), %rcx
addq $0x810, %rcx            # imm = 0x810
callq 0x42d20
movl %eax, -0x5194(%rbp)
cmpl $-0x1, -0x5194(%rbp)
jne 0x43f08
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x442da
movq $0x0, -0x61b0(%rbp)
movl -0x5194(%rbp), %edi
leaq 0x9896e(%rip), %rsi     # 0xdc88e
leaq -0x61b0(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x42e80
cmpl $0x0, %eax
jne 0x43f4c
movl -0x5194(%rbp), %edi
callq 0x6e90
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x442da
cmpq $-0x1, -0x130(%rbp)
je 0x43fae
leaq -0x120(%rbp), %rdi
movq -0x130(%rbp), %rcx
movl $0x100, %esi            # imm = 0x100
leaq 0x98925(%rip), %rdx     # 0xdc895
movb $0x0, %al
callq 0x31090
movl -0x5194(%rbp), %edi
leaq -0x120(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x42e80
cmpl $0x0, %eax
jne 0x43fac
movl -0x5194(%rbp), %edi
callq 0x6e90
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x442da
jmp 0x43fae
leaq -0x120(%rbp), %rax
movq %rax, -0x61e0(%rbp)
leaq -0x2950(%rbp), %rdi
addq $0x1820, %rdi           # imm = 0x1820
callq 0x62c0
movq -0x61e0(%rbp), %rdi
subq $0x1, %rax
movsbl -0x1130(%rbp,%rax), %edx
leaq 0x988bd(%rip), %rcx     # 0xdc8a6
leaq 0x988b1(%rip), %rax     # 0xdc8a1
cmpl $0x2f, %edx
cmoveq %rax, %rcx
leaq -0x2950(%rbp), %r8
addq $0x1820, %r8            # imm = 0x1820
movl $0x100, %esi            # imm = 0x100
leaq 0x97e5e(%rip), %rdx     # 0xdbe6f
movb $0x0, %al
callq 0x31090
movl -0x5194(%rbp), %edi
leaq -0x120(%rbp), %rsi
callq 0x45e30
movl $0xffffffff, -0x61b4(%rbp) # imm = 0xFFFFFFFF
cmpq $0x0, -0x5190(%rbp)
je 0x44078
leaq -0x5180(%rbp), %rax
addq $0x1010, %rax           # imm = 0x1010
movq %rax, -0x61e8(%rbp)
movq -0x61b0(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x460f0
movq -0x61e8(%rbp), %rdi
movl %eax, %esi
callq 0x41710
movl %eax, -0x61b4(%rbp)
jmp 0x440b0
leaq -0x2950(%rbp), %rax
addq $0x1010, %rax           # imm = 0x1010
movq %rax, -0x61f0(%rbp)
movq -0x61b0(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x460f0
movq -0x61f0(%rbp), %rdi
movl %eax, %esi
callq 0x41710
movl %eax, -0x61b4(%rbp)
leaq -0x61b0(%rbp), %rdi
callq 0x35080
cmpl $0x0, -0x61b4(%rbp)
jge 0x440dc
movl -0x5194(%rbp), %edi
callq 0x6e90
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x442da
movl $0x0, -0x61b8(%rbp)
cmpl $0xa, -0x61b8(%rbp)
jl 0x44126
leaq -0x120(%rbp), %rsi
leaq 0x987ae(%rip), %rdi     # 0xdc8ab
movb $0x0, %al
callq 0x358e0
movl -0x5194(%rbp), %edi
callq 0x6e90
movl -0x61b4(%rbp), %edi
callq 0x6e90
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x442da
movl -0x61b4(%rbp), %edi
movl $0xf4240, %esi          # imm = 0xF4240
callq 0x45e00
cmpl $0x0, %eax
je 0x4413d
jmp 0x441a5
movl -0x5194(%rbp), %edi
xorl %esi, %esi
callq 0x45e00
cmpl $0x0, %eax
je 0x44191
movl -0x5194(%rbp), %edi
leaq -0x120(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x45e70
cmpl $0x0, %eax
jne 0x4418f
movl -0x5194(%rbp), %edi
callq 0x6e90
movl -0x61b4(%rbp), %edi
callq 0x6e90
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x442da
jmp 0x44191
movl -0x61b8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x61b8(%rbp)
jmp 0x440e6
cmpq $0x0, -0x18(%rbp)
je 0x441c6
movl -0x5194(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl -0x61b4(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x442da
movq 0xc0dfb(%rip), %rax     # 0x104fc8
movq (%rax), %rdi
callq 0x6850
movq 0xc0dd4(%rip), %rax     # 0x104fb0
movq (%rax), %rdi
callq 0x6850
movq 0xc0de5(%rip), %rax     # 0x104fd0
movq (%rax), %rdi
callq 0x6850
movl $0x2848, %edi           # imm = 0x2848
callq 0x40c50
movq %rax, -0x61c0(%rbp)
movl -0x5194(%rbp), %ecx
movq -0x61c0(%rbp), %rax
movl %ecx, 0x10(%rax)
movl -0x61b4(%rbp), %ecx
movq -0x61c0(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x61c0(%rbp), %rdi
addq $0x18, %rdi
leaq -0x2950(%rbp), %rsi
movl $0x2830, %edx           # imm = 0x2830
callq 0x6570
movq -0x61c0(%rbp), %rdi
addq $0x8, %rdi
callq 0x69e0
cmpl $0x0, %eax
je 0x44272
callq 0x6060
movl (%rax), %edi
callq 0x6220
movq %rax, %rsi
leaq 0x98681(%rip), %rdi     # 0xdc8ec
movb $0x0, %al
callq 0x35d40
movq -0x61c0(%rbp), %rdi
movq -0x61c0(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
leaq 0x1f15(%rip), %rdx      # 0x461a0
callq 0x6dc0
movl %eax, -0x61c4(%rbp)
cmpl $0x0, -0x61c4(%rbp)
je 0x442cd
movl -0x61c4(%rbp), %eax
movl %eax, -0x61f4(%rbp)
movl -0x61c4(%rbp), %edi
callq 0x6220
movl -0x61f4(%rbp), %esi
movq %rax, %rdx
leaq 0x98656(%rip), %rdi     # 0xdc91c
movb $0x0, %al
callq 0x35d40
movq -0x61c0(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x6200, %rsp           # imm = 0x6200
popq %rbp
retq
nopw %cs:(%rax,%rax)