// File: /workspace/github_c_makefile_star2_O3/icebert[P]pblat/lib/net.c
// Function: netSkipHttpHeaderLinesHandlingRedirect
boolean netSkipHttpHeaderLinesHandlingRedirect(int sd, char *url, int *redirectedSd, char **redirectedUrl)
/* Skip http headers lines, returning FALSE if there is a problem.  Generally called as
 *    netSkipHttpHeaderLine(sd, url, &sd, &url);
 * where sd is a socket (file) opened with netUrlOpen(url), and url is in dynamic memory.
 * If the http header indicates that the file has moved, then it will update the *redirectedSd and
 * *redirectedUrl with the new socket and URL, first closing sd.
 * If for some reason you want to detect whether the forwarding has occurred you could
 * call this as:
 *    char *newUrl = NULL;
 *    int newSd = 0;
 *    netSkipHttpHeaderLine(sd, url, &newSd, &newUrl);
 *    if (newUrl != NULL)
 *          // Update sd with newSd, free url if appropriate and replace it with newUrl, etc.
 *          //  free newUrl when finished.
 * This routine handles up to 5 steps of redirection.
 * The logic to this routine is also complicated a little to make it work in a pipe, which means we
 * can't attach a lineFile since filling the lineFile buffer reads in more than just the http header. */
{
int redirectCount = 0;
while (TRUE)
    {
    /* url needed for err msgs, and to return redirect location */
    char *newUrl = NULL;
    boolean success = netSkipHttpHeaderLinesWithRedirect(sd, url, &newUrl);
    if (success && !newUrl) /* success after 0 to 5 redirects */
        {
	if (redirectCount > 0)
	    {
	    *redirectedSd = sd;
	    *redirectedUrl = url;
	    }
	else
	    {
	    *redirectedSd = -1;
	    *redirectedUrl = NULL;
	    }
	return TRUE;
	}
    close(sd);
    if (success)
	{
	/* we have a new url to try */
	++redirectCount;
	if (redirectCount > 5)
	    {
	    warn("code 30x redirects: exceeded limit of 5 redirects, %s", newUrl);
	    success = FALSE;
	    }
	else if (!startsWith("http://",newUrl) 
              && !startsWith("https://",newUrl))
	    {
	    warn("redirected to non-http(s): %s", newUrl);
	    success = FALSE;
	    }
	else 
	    {
	    newUrl = transferParamsToRedirectedUrl(url, newUrl);		
	    sd = netUrlOpen(newUrl);
	    if (sd < 0)
		{
		warn("Couldn't open %s", newUrl);
		success = FALSE;
		}
	    }
	}
    if (redirectCount > 1)
	freeMem(url);
    if (!success)
	{  /* failure after 0 to 5 redirects */
	if (redirectCount > 0)
	    freeMem(newUrl);
	return FALSE;
	}
    url = newUrl;
    }
return FALSE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, 0x10(%rsp)
movq %rdx, 0x8(%rsp)
movq %rsi, %r15
movl %edi, %r14d
movq %rsp, %rbx
movq $0x0, (%rbx)
movq %rbx, %rdx
callq 0x30448
testl %eax, %eax
sete %r13b
movq (%rbx), %r12
testq %r12, %r12
setne %al
orb %r13b, %al
xorl %ebp, %ebp
cmpb $0x1, %al
jne 0x30bb6
movl %r14d, %edi
callq 0x6e80
testb $0x1, %r13b
jne 0x30c13
cmpl $0x5, %ebp
jae 0x30bd5
leaq 0x60b78(%rip), %rdi     # 0x916a8
movq %r12, %rsi
callq 0x21b23
testl %eax, %eax
jne 0x30b53
leaq 0x60b6d(%rip), %rdi     # 0x916b0
movq %r12, %rsi
callq 0x21b23
testl %eax, %eax
je 0x30c00
movq %r15, %rdi
movq %r12, %rsi
callq 0x30a18
movq %rax, %rbx
movq %rax, %rdi
xorl %esi, %esi
callq 0x2ff0b
testl %eax, %eax
js 0x30be8
movl %eax, %r14d
testl %ebp, %ebp
je 0x30b7e
movq %r15, %rdi
callq 0x2e0a0
movq $0x0, (%rsp)
movl %r14d, %edi
movq %rbx, %rsi
movq %rsp, %rdx
callq 0x30448
testl %eax, %eax
sete %r13b
movq (%rsp), %r12
testq %r12, %r12
setne %al
orb %r13b, %al
incl %ebp
movq %rbx, %r15
testb %al, %al
jne 0x30b0e
jmp 0x30bbe
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
xorl %ebx, %ebx
movq 0x8(%rsp), %rax
movl %r14d, (%rax)
movq 0x10(%rsp), %rax
movq %rbx, (%rax)
movl $0x1, %eax
jmp 0x30c33
leaq 0x61850(%rip), %rdi     # 0x9242c
movq %r12, %rsi
xorl %eax, %eax
callq 0x26bea
jmp 0x30c18
leaq 0x61891(%rip), %rdi     # 0x92480
movq %rbx, %rsi
xorl %eax, %eax
callq 0x26bea
incl %ebp
movq %rbx, %r12
jmp 0x30c13
leaq 0x6185b(%rip), %rdi     # 0x92462
movq %r12, %rsi
xorl %eax, %eax
callq 0x26bea
incl %ebp
cmpl $0x1, %ebp
jle 0x30c22
movq %r15, %rdi
callq 0x2e0a0
jmp 0x30c29
xorl %eax, %eax
cmpl $0x1, %ebp
jne 0x30c33
movq %r12, %rdi
callq 0x2e0a0
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq