// File: /workspace/github_c_makefile_star2_O2/icebert[P]pblat/lib/net.c
// Function: netParseUrl
void netParseUrl(char *url, struct netParsedUrl *parsed)
/* Parse a URL into components.   A full URL is made up as so:
 *   http://user:password@hostName:port/file;byterange=0-499
 * User and password may be cgi-encoded.
 * This is set up so that the http:// and the port are optional. 
 */
{
char *s, *t, *u, *v, *w;
char buf[MAXURLSIZE];

/* Make local copy of URL. */
if (strlen(url) >= sizeof(buf))
    errAbort("Url too long: '%s'", url);
strcpy(buf, url);
url = buf;

/* Find out protocol - default to http. */
s = trimSpaces(url);
s = stringIn("://", url);
if (s == NULL)
    {
    strcpy(parsed->protocol, "http");
    s = url;
    }
else
    {
    *s = 0;
    tolowers(url);
    safecpy(parsed->protocol, sizeof(parsed->protocol), url);
    s += 3;
    }

/* Split off file part. */
parsed->byteRangeStart = -1;  /* default to no byte range specified */
parsed->byteRangeEnd = -1;
u = strchr(s, '/');
if (u == NULL)
    strcpy(parsed->file, "/");
else
    {

    parseByteRange(u, &parsed->byteRangeStart, &parsed->byteRangeEnd, TRUE);

    if (sameWord(parsed->protocol,"http") ||
        sameWord(parsed->protocol,"https"))
	{
	// http servers expect the URL request to be URL-encoded already.
	/* need to encode spaces, but not ! other characters */
	char *t=replaceChars(u," ","%20");
	safecpy(parsed->file, sizeof(parsed->file), t);
	freeMem(t);
	}

    *u = 0; // terminate the host:port string

    if (sameWord(parsed->protocol,"ftp"))
	{
	++u; // that first slash is not considered part of the ftp path 
	// decode now because the FTP server does NOT expect URL-encoding.
	cgiDecodeFull(u,parsed->file,strlen(u));  // decodes %FF but not +
	}

    }

/* Split off user part */
v = strchr(s, '@');
if (v == NULL)
    {
    if (sameWord(parsed->protocol,"http") ||
        sameWord(parsed->protocol,"https"))
	{
	strcpy(parsed->user, "");
	strcpy(parsed->password, "");
	}
    if (sameWord(parsed->protocol,"ftp"))
	{
	strcpy(parsed->user, "anonymous");
	strcpy(parsed->password, "x@genome.ucsc.edu");
	}
    }
else
    {
    *v = 0;
    /* split off password part */
    w = strchr(s, ':');
    if (w == NULL)
	{
	safecpy(parsed->user, sizeof(parsed->user), s);
	strcpy(parsed->password, "");
	}
    else
	{
	*w = 0;
	safecpy(parsed->user, sizeof(parsed->user), s);
	safecpy(parsed->password, sizeof(parsed->password), w+1);
	}
    
    cgiDecode(parsed->user,parsed->user,strlen(parsed->user));
    cgiDecode(parsed->password,parsed->password,strlen(parsed->password));
    s = v+1;
    }


/* Save port if it's there.  If not default to 80. */
t = strchr(s, ':');
if (t == NULL)
    {
    if (sameWord(parsed->protocol,"http"))
	strcpy(parsed->port, "80");
    if (sameWord(parsed->protocol,"https"))
	strcpy(parsed->port, "443");
    if (sameWord(parsed->protocol,"ftp"))
	strcpy(parsed->port, "21");
    }
else
    {
    *t++ = 0;
    if (!isdigit(t[0]))
	errAbort("Non-numeric port name %s", t);
    safecpy(parsed->port, sizeof(parsed->port), t);
    }

/* What's left is the host. */
safecpy(parsed->host, sizeof(parsed->host), s);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x1008, %rsp           # imm = 0x1008
movq %rsi, %rbx
movq %rdi, %r15
callq 0x62d0
cmpq $0x1000, %rax           # imm = 0x1000
jb 0x2986b
leaq 0x5761e(%rip), %rdi     # 0x80e7f
movq %r15, %rsi
xorl %eax, %eax
callq 0x223fb
movq %rsp, %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x6680
movq %r14, %rdi
callq 0x1e897
leaq 0x56d34(%rip), %rsi     # 0x805bc
movq %r14, %rdi
callq 0x6180
testq %rax, %rax
je 0x298bd
movq %rax, %r15
movb $0x0, (%rax)
movq %rsp, %r14
movq %r14, %rdi
callq 0x1e270
pushq $0x10
popq %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x1d6d3
addq $0x3, %r15
movq %r15, %r14
jmp 0x298c7
movb $0x0, 0x4(%rbx)
movl $0x70747468, (%rbx)     # imm = 0x70747468
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, 0x2820(%rbx)
pushq $0x2f
popq %rsi
movq %r14, %rdi
callq 0x6270
testq %rax, %rax
je 0x29996
movq %rax, %r15
leaq 0x2820(%rbx), %rsi
leaq 0x2828(%rbx), %rdx
pushq $0x1
popq %rcx
movq %rax, %rdi
callq 0x29ba5
leaq 0x57588(%rip), %rsi     # 0x80e92
movq %rbx, %rdi
callq 0x1cd23
testl %eax, %eax
je 0x29929
leaq 0x5757a(%rip), %rsi     # 0x80e97
movq %rbx, %rdi
callq 0x1cd23
testl %eax, %eax
jne 0x2995e
leaq 0x53aa2(%rip), %rsi     # 0x7d3d2
leaq 0x57566(%rip), %rdx     # 0x80e9d
movq %r15, %rdi
callq 0x1e06b
movq %rax, %r12
leaq 0x1820(%rbx), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rax, %rdx
callq 0x1d6d3
movq %r12, %rdi
callq 0x28c2d
movb $0x0, (%r15)
leaq 0x5a5d4(%rip), %rsi     # 0x83f3d
movq %rbx, %rdi
callq 0x1cd23
testl %eax, %eax
jne 0x2999f
incq %r15
leaq 0x1820(%rbx), %r12
movq %r15, %rdi
callq 0x62d0
movq %r15, %rdi
movq %r12, %rsi
movl %eax, %edx
callq 0x3eb2c
jmp 0x2999f
movw $0x2f, 0x1820(%rbx)
pushq $0x40
popq %rsi
movq %r14, %rdi
callq 0x6270
testq %rax, %rax
je 0x299fc
movq %rax, %r15
movb $0x0, (%rax)
pushq $0x3a
popq %rsi
movq %r14, %rdi
callq 0x6270
testq %rax, %rax
je 0x29a73
movq %rax, %r12
movb $0x0, (%rax)
leaq 0x10(%rbx), %rdi
movl $0x800, %esi            # imm = 0x800
movq %r14, %rdx
callq 0x1d6d3
leaq 0x810(%rbx), %rdi
incq %r12
movl $0x800, %esi            # imm = 0x800
movq %r12, %rdx
callq 0x1d6d3
jmp 0x29a8b
leaq 0x5748f(%rip), %rsi     # 0x80e92
movq %rbx, %rdi
callq 0x1cd23
testl %eax, %eax
je 0x29a22
leaq 0x57481(%rip), %rsi     # 0x80e97
movq %rbx, %rdi
callq 0x1cd23
testl %eax, %eax
jne 0x29a2d
xorl %eax, %eax
movb %al, 0x10(%rbx)
movb %al, 0x810(%rbx)
leaq 0x5a509(%rip), %rsi     # 0x83f3d
movq %rbx, %rdi
callq 0x1cd23
testl %eax, %eax
jne 0x29ac6
movabsq $0x756f6d796e6f6e61, %rax # imm = 0x756F6D796E6F6E61
movq %rax, 0x10(%rbx)
movw $0x73, 0x18(%rbx)
movdqu 0x5744b(%rip), %xmm0    # 0x80eab
movdqu %xmm0, 0x810(%rbx)
movw $0x75, 0x820(%rbx)
jmp 0x29ac6
leaq 0x10(%rbx), %rdi
movl $0x800, %esi            # imm = 0x800
movq %r14, %rdx
callq 0x1d6d3
movb $0x0, 0x810(%rbx)
leaq 0x10(%rbx), %r14
movq %r14, %rdi
callq 0x62d0
movq %r14, %rdi
movq %r14, %rsi
movl %eax, %edx
callq 0x3e6ea
leaq 0x810(%rbx), %r14
movq %r14, %rdi
callq 0x62d0
movq %r14, %rdi
movq %r14, %rsi
movl %eax, %edx
callq 0x3e6ea
incq %r15
movq %r15, %r14
pushq $0x3a
popq %rsi
movq %r14, %rdi
callq 0x6270
testq %rax, %rax
je 0x29b1c
movq %rax, %r15
movq %rax, %r12
incq %r15
movb $0x0, (%rax)
callq 0x6790
movq (%rax), %rax
movsbq 0x1(%r12), %rcx
testb $0x8, 0x1(%rax,%rcx,2)
jne 0x29b08
leaq 0x573c9(%rip), %rdi     # 0x80ec7
movq %r15, %rsi
xorl %eax, %eax
callq 0x223fb
leaq 0x1810(%rbx), %rdi
pushq $0x10
popq %rsi
movq %r15, %rdx
callq 0x1d6d3
jmp 0x29b7f
leaq 0x5736f(%rip), %rsi     # 0x80e92
movq %rbx, %rdi
callq 0x1cd23
testl %eax, %eax
jne 0x29b3f
movb $0x0, 0x1812(%rbx)
movw $0x3038, 0x1810(%rbx)   # imm = 0x3038
leaq 0x57351(%rip), %rsi     # 0x80e97
movq %rbx, %rdi
callq 0x1cd23
testl %eax, %eax
jne 0x29b5c
movl $0x333434, 0x1810(%rbx) # imm = 0x333434
leaq 0x5a3da(%rip), %rsi     # 0x83f3d
movq %rbx, %rdi
callq 0x1cd23
testl %eax, %eax
jne 0x29b7f
movb $0x0, 0x1812(%rbx)
movw $0x3132, 0x1810(%rbx)   # imm = 0x3132
addq $0x1010, %rbx           # imm = 0x1010
movl $0x800, %esi            # imm = 0x800
movq %rbx, %rdi
movq %r14, %rdx
callq 0x1d6d3
addq $0x1008, %rsp           # imm = 0x1008
popq %rbx
popq %r12
popq %r14
popq %r15
retq