// File: /workspace/github_c_makefile_star2_O1/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, %r15
movq %rcx, 0x2850(%rsp)
movq %rdx, 0x2848(%rsp)
movq %rsi, 0x2840(%rsp)
movq %rdi, %r14
movl $0x200, %edi            # imm = 0x200
callq 0x261d0
movq %rax, 0x2830(%rsp)
leaq 0x2858(%rsp), %rbx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2ea40
leaq 0x3868(%rsp), %rdi
callq 0x2f654
movl %eax, %ebp
leaq 0x62856(%rip), %rdi     # 0x91f82
callq 0x68f0
movl $0x70747468, %r13d      # imm = 0x70747468
movl (%rbx), %ecx
xorl %r13d, %ecx
movzwl 0x4(%rbx), %edx
xorl $0x73, %edx
xorl %ebx, %ebx
orl %ecx, %edx
cmovneq %rax, %rbx
testl %ebp, %ebp
setne %al
testq %rbx, %rbx
sete %r12b
orb %al, %r12b
je 0x2f780
leaq 0x2858(%rsp), %rsi
movl $0x506, %ecx            # imm = 0x506
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
movq %r14, %rdi
movl %ebp, %esi
callq 0x2f52d
movl %eax, %ebp
jmp 0x2f820
movq %r15, 0x2838(%rsp)
leaq 0x5088(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2ea40
xorl (%r15), %r13d
movzbl 0x4(%r15), %eax
orl %r13d, %eax
je 0x2f7c1
leaq 0x627de(%rip), %rdi     # 0x91f8d
leaq 0x5088(%rsp), %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x26b6f
leaq 0x5088(%rsp), %rsi
movl $0x506, %ecx            # imm = 0x506
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
movq %r14, %rdi
movl %ebp, %esi
callq 0x2f52d
movl %eax, %ebp
leaq 0x627c7(%rip), %rdi     # 0x91fae
callq 0x68f0
leaq 0x5df9b(%rip), %rsi     # 0x8d78e
movq %rax, %rdi
callq 0x21769
testl %eax, %eax
jne 0x2f818
leaq 0x631cf(%rip), %rsi     # 0x929d5
movl $0x1, %edi
movq %r14, %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x4246e
movq 0x2838(%rsp), %r15
testl %ebp, %ebp
js 0x2f82d
testb %r12b, %r12b
je 0x2f837
xorl %ebx, %ebx
jmp 0x2f86e
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x2fa5d
movq %r14, %rdi
callq 0x1f9f6
movq %rax, %rbx
movq %rax, %rdi
movl $0x3b, %esi
callq 0x67c0
testq %rax, %rax
je 0x2f86e
movq %rax, %r14
leaq 0x6275a(%rip), %rdi     # 0x91fb8
movq %rax, %rsi
callq 0x217c8
testl %eax, %eax
je 0x2f86e
movb $0x0, (%r14)
movq 0x2830(%rsp), %rdi
leaq 0x4078(%rsp), %r14
testb %r12b, %r12b
movq %rbx, %rcx
cmovneq %r14, %rcx
leaq 0x62735(%rip), %rsi     # 0x91fc4
movq 0x2840(%rsp), %rdx
movq 0x2848(%rsp), %r8
xorl %eax, %eax
callq 0x2656a
movq %rbx, %rdi
callq 0x2dd41
movq 0x2830(%rsp), %rdi
leaq 0x62712(%rip), %rsi     # 0x91fcf
movq 0x2850(%rsp), %rdx
xorl %eax, %eax
callq 0x2656a
movl $0x70747468, %eax       # imm = 0x70747468
movl -0x1820(%r14), %ecx
xorl %eax, %ecx
movzbl -0x181c(%r14), %edx
orl %ecx, %edx
jne 0x2f8ff
movl $0x3038, %ecx           # imm = 0x3038
xorl 0x4068(%rsp), %ecx
movzbl 0x406a(%rsp), %edx
orw %cx, %dx
je 0x2f922
xorl 0x2858(%rsp), %eax
movzwl 0x285c(%rsp), %ecx
xorl $0x73, %ecx
orl %eax, %ecx
jne 0x2f942
cmpl $0x333434, 0x4068(%rsp) # imm = 0x333434
jne 0x2f942
movq 0x2830(%rsp), %rdi
leaq 0x6bd81(%rip), %rsi     # 0x9b6b2
leaq 0x3868(%rsp), %rdx
xorl %eax, %eax
callq 0x2656a
jmp 0x2f968
leaq 0x4068(%rsp), %rcx
movq 0x2830(%rsp), %rdi
leaq 0x62687(%rip), %rsi     # 0x91fe0
leaq 0x3868(%rsp), %rdx
xorl %eax, %eax
callq 0x2656a
movq 0x2830(%rsp), %rax
leaq 0x2858(%rsp), %rsi
movl $0x506, %ecx            # imm = 0x506
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x6266a(%rip), %rdi     # 0x91ff4
movq %rax, %rsi
callq 0x2f5b4
testb %r12b, %r12b
jne 0x2f9c1
movq 0x2830(%rsp), %rax
leaq 0x5088(%rsp), %rsi
movl $0x506, %ecx            # imm = 0x506
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x62635(%rip), %rdi     # 0x91fee
movq %rax, %rsi
callq 0x2f5b4
movq 0x2830(%rsp), %rdi
leaq 0x62632(%rip), %rsi     # 0x92002
callq 0x26425
movq 0x5078(%rsp), %rdx
cmpq $-0x1, %rdx
je 0x2fa17
movq 0x5080(%rsp), %rcx
movq 0x2830(%rsp), %rdi
cmpq $-0x1, %rcx
je 0x2fa09
leaq 0x62610(%rip), %rsi     # 0x92010
xorl %eax, %eax
callq 0x2656a
jmp 0x2fa17
leaq 0x62619(%rip), %rsi     # 0x92029
xorl %eax, %eax
callq 0x2656a
testq %r15, %r15
je 0x2fa2c
movq 0x2830(%rsp), %rdi
movq %r15, %rsi
callq 0x26425
leaq 0x2830(%rsp), %rbx
movq (%rbx), %rdi
leaq 0x681f5(%rip), %rsi     # 0x97c33
callq 0x26425
movq (%rbx), %rax
movq 0x8(%rax), %rsi
movslq 0x14(%rax), %rdx
movl %ebp, %edi
callq 0x231f9
movq %rbx, %rdi
callq 0x2620d
movl %ebp, %eax
addq $0x78b8, %rsp           # imm = 0x78B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq