// File: /workspace/github_c_makefile_star2_O2/icebert[P]pblat/lib/net.c
// Function: netHttpConnect
int netHttpConnect(char *url, char *method, char *protocol, char *agent, char *optionalHeader)
/* Parse URL, connect to associated server on port, and send most of
 * the request to the server.  If specified in the url send user name
 * and password too.  Typically the "method" will be "GET" or "POST"
 * and the agent will be the name of your program or
 * library. optionalHeader may be NULL or contain additional header
 * lines such as cookie info. 
 * Proxy support via hg.conf httpProxy or env var http_proxy
 * Return data socket, or -1 if error.*/
{
struct netParsedUrl npu;
struct netParsedUrl pxy;
struct dyString *dy = newDyString(512);
int sd = -1;
/* Parse the URL and connect. */
netParseUrl(url, &npu);

boolean noProxy = checkNoProxy(npu.host);
char *proxyUrl = getenv("http_proxy");
if (sameString(npu.protocol, "https"))
    proxyUrl = NULL;
if (noProxy)
    proxyUrl = NULL;
if (proxyUrl)
    {
    netParseUrl(proxyUrl, &pxy);
    if (!sameString(pxy.protocol, "http"))
	errAbort("Unknown proxy protocol %s in %s.", pxy.protocol, proxyUrl);
    sd = connectNpu(pxy, url, noProxy);
    char *logProxy = getenv("log_proxy");
    if (sameOk(logProxy,"on"))
	verbose(1, "%s via proxy %s\n", url, proxyUrl);
    }
else
    {
    sd = connectNpu(npu, url, noProxy);
    }
if (sd < 0)
    return -1;

/* Ask remote server for a file. */
char *urlForProxy = NULL;
if (proxyUrl)
    {
    /* trim off the byterange part at the end of url because proxy does not understand it. */
    urlForProxy = cloneString(url);
    char *x = strrchr(urlForProxy, ';');
    if (x && startsWith(";byterange=", x))
	*x = 0;
    }
dyStringPrintf(dy, "%s %s %s\r\n", method, proxyUrl ? urlForProxy : npu.file, protocol);
freeMem(urlForProxy);
dyStringPrintf(dy, "User-Agent: %s\r\n", agent);
/* do not need the 80 since it is the default */
if ((sameString(npu.protocol, "http" ) && sameString("80", npu.port)) ||
    (sameString(npu.protocol, "https") && sameString("443",npu.port)))
    dyStringPrintf(dy, "Host: %s\r\n", npu.host);
else
    dyStringPrintf(dy, "Host: %s:%s\r\n", npu.host, npu.port);
setAuthorization(npu, "Authorization", dy);
if (proxyUrl)
    setAuthorization(pxy, "Proxy-Authorization", dy);
dyStringAppend(dy, "Accept: */*\r\n");
if (npu.byteRangeStart != -1)
    {
    if (npu.byteRangeEnd != -1)
	dyStringPrintf(dy, "Range: bytes=%lld-%lld\r\n"
		       , (long long)npu.byteRangeStart
		       , (long long)npu.byteRangeEnd);
    else
	dyStringPrintf(dy, "Range: bytes=%lld-\r\n"
		       , (long long)npu.byteRangeStart);
    }

if (optionalHeader)
    dyStringAppend(dy, optionalHeader);

/* finish off the header with final blank line */
dyStringAppend(dy, "\r\n");

mustWriteFd(sd, dy->string, dy->stringSize);

/* Clean up and return handle. */
dyStringFree(&dy);
return sd;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78b8, %rsp           # imm = 0x78B8
movq %r8, 0x2848(%rsp)
movq %rcx, 0x2840(%rsp)
movq %rdx, 0x2838(%rsp)
movq %rsi, 0x2830(%rsp)
movq %rdi, %r14
movl $0x200, %edi            # imm = 0x200
callq 0x21bcc
movq %rax, %rbx
movq %rax, 0x2850(%rsp)
leaq 0x2858(%rsp), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x29839
leaq 0x3868(%rsp), %rdi
callq 0x2a3e1
movl %eax, %ebp
leaq 0x56ab5(%rip), %rdi     # 0x80f72
callq 0x68f0
movq %rax, %r13
leaq 0x569cb(%rip), %rsi     # 0x80e97
pushq $0x6
popq %rdx
movq %r12, %rdi
callq 0x6710
xorl %r12d, %r12d
testl %eax, %eax
cmovneq %r13, %r12
testl %ebp, %ebp
setne %al
testq %r12, %r12
sete %r15b
orb %al, %r15b
je 0x2a520
leaq 0x2858(%rsp), %rsi
movl $0x506, %ecx            # imm = 0x506
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
movq %r14, %rdi
movl %ebp, %esi
callq 0x2a2b1
testl %eax, %eax
js 0x2a6bf
movl %eax, %ebp
xorl %r14d, %r14d
jmp 0x2a5f5
leaq 0x5088(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x29839
leaq 0x56958(%rip), %rsi     # 0x80e92
pushq $0x5
popq %rdx
movq %r13, %rdi
callq 0x6710
testl %eax, %eax
je 0x2a562
leaq 0x56a2d(%rip), %rdi     # 0x80f7d
leaq 0x5088(%rsp), %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x223fb
leaq 0x5088(%rsp), %rsi
movl $0x506, %ecx            # imm = 0x506
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
movq %r14, %rdi
xorl %esi, %esi
callq 0x2a2b1
movl %eax, %ebp
leaq 0x56a16(%rip), %rdi     # 0x80f9e
callq 0x68f0
leaq 0x521fa(%rip), %rsi     # 0x7c78e
movq %rax, %rdi
callq 0x1daf1
testl %eax, %eax
jne 0x2a5b7
leaq 0x5741e(%rip), %rsi     # 0x819c5
pushq $0x1
popq %rdi
movq %r14, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x3c521
testl %ebp, %ebp
js 0x2a6bf
movq %r14, %rdi
callq 0x1c306
movq %rax, %r14
pushq $0x3b
popq %rsi
movq %rax, %rdi
callq 0x67c0
testq %rax, %rax
je 0x2a5f5
movq %rax, %r12
leaq 0x569c4(%rip), %rdi     # 0x80fa8
movq %rax, %rsi
callq 0x1db41
testl %eax, %eax
je 0x2a5f5
movb $0x0, (%r12)
leaq 0x4078(%rsp), %rcx
testb %r15b, %r15b
cmoveq %r14, %rcx
leaq 0x569a9(%rip), %rsi     # 0x80fb4
movq %rbx, %rdi
movq 0x2830(%rsp), %rdx
movq 0x2838(%rsp), %r8
xorl %eax, %eax
callq 0x21ec7
movq %r14, %rdi
callq 0x28c2d
leaq 0x5698b(%rip), %rsi     # 0x80fbf
movq %rbx, %rdi
movq 0x2840(%rsp), %rdx
xorl %eax, %eax
callq 0x21ec7
leaq 0x56845(%rip), %rsi     # 0x80e92
leaq 0x2858(%rsp), %rdi
pushq $0x5
popq %rdx
callq 0x6710
testl %eax, %eax
jne 0x2a67c
leaq 0x4068(%rsp), %rsi
leaq 0x5684d(%rip), %rdi     # 0x80ebd
pushq $0x3
popq %rdx
callq 0x6710
testl %eax, %eax
je 0x2a6a4
leaq 0x56814(%rip), %rsi     # 0x80e97
leaq 0x2858(%rsp), %rdi
pushq $0x6
popq %rdx
callq 0x6710
testl %eax, %eax
jne 0x2a6c7
cmpl $0x333434, 0x4068(%rsp) # imm = 0x333434
jne 0x2a6c7
leaq 0x5ffb7(%rip), %rsi     # 0x8a662
movq %rbx, %rdi
leaq 0x3868(%rsp), %rdx
xorl %eax, %eax
callq 0x21ec7
jmp 0x2a6e8
pushq $-0x1
popq %rbp
jmp 0x2a7c0
leaq 0x4068(%rsp), %rcx
leaq 0x568fa(%rip), %rsi     # 0x80fd0
movq %rbx, %rdi
leaq 0x3868(%rsp), %rdx
xorl %eax, %eax
callq 0x21ec7
leaq 0x2858(%rsp), %rsi
movl $0x506, %ecx            # imm = 0x506
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x568e2(%rip), %rdi     # 0x80fe4
movq %rbx, %rsi
callq 0x2a34c
testb %r15b, %r15b
jne 0x2a731
leaq 0x5088(%rsp), %rsi
movl $0x506, %ecx            # imm = 0x506
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x568b5(%rip), %rdi     # 0x80fde
movq %rbx, %rsi
callq 0x2a34c
leaq 0x568ba(%rip), %rsi     # 0x80ff2
movq %rbx, %rdi
callq 0x21dda
movq 0x5078(%rsp), %rdx
cmpq $-0x1, %rdx
je 0x2a780
movq 0x5080(%rsp), %rcx
cmpq $-0x1, %rcx
je 0x2a76f
leaq 0x5689d(%rip), %rsi     # 0x81000
movq %rbx, %rdi
xorl %eax, %eax
callq 0x21ec7
jmp 0x2a780
leaq 0x568a3(%rip), %rsi     # 0x81019
movq %rbx, %rdi
xorl %eax, %eax
callq 0x21ec7
movq 0x2848(%rsp), %rsi
testq %rsi, %rsi
je 0x2a795
movq %rbx, %rdi
callq 0x21dda
leaq 0x5c453(%rip), %rsi     # 0x86bef
movq %rbx, %rdi
callq 0x21dda
movq 0x8(%rbx), %rsi
movslq 0x14(%rbx), %rdx
movl %ebp, %edi
callq 0x1f108
leaq 0x2850(%rsp), %rdi
callq 0x21c07
movl %ebp, %eax
addq $0x78b8, %rsp           # imm = 0x78B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq