// File: /workspace/github_c_makefile_star2_O2/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, %r14
movl %edi, %ebp
movq %rsp, %r15
xorl %r13d, %r13d
andq $0x0, (%rsp)
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2aebc
movl %eax, %r12d
testl %eax, %eax
sete %al
movq (%rsp), %rbx
testq %rbx, %rbx
setne %cl
orb %al, %cl
je 0x2b632
movl %ebp, %edi
callq 0x6e80
testl %r12d, %r12d
je 0x2b5a6
leal 0x1(%r13), %r12d
cmpl $0x5, %r13d
jl 0x2b5ab
leaq 0x55d95(%rip), %rdi     # 0x8132c
movq %rbx, %rsi
xorl %eax, %eax
callq 0x22186
movb $0x1, %r13b
jmp 0x2b61a
movl %r13d, %r12d
jmp 0x2b611
leaq 0x54ff6(%rip), %rdi     # 0x805a8
movq %rbx, %rsi
callq 0x1db41
testl %eax, %eax
jne 0x2b5d1
leaq 0x54feb(%rip), %rdi     # 0x805b0
movq %rbx, %rsi
callq 0x1db41
testl %eax, %eax
je 0x2b600
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2b481
movq %rax, %rbx
movq %rax, %rdi
callq 0x2ae26
movl %eax, %ebp
testl %eax, %eax
js 0x2b5f7
testl %r13d, %r13d
jle 0x2b627
xorl %r13d, %r13d
jmp 0x2b61a
leaq 0x55d82(%rip), %rdi     # 0x81380
jmp 0x2b607
leaq 0x55d5b(%rip), %rdi     # 0x81362
movq %rbx, %rsi
xorl %eax, %eax
callq 0x22186
movb $0x1, %r13b
cmpl $0x1, %r12d
jle 0x2b655
movq %r14, %rdi
callq 0x28c2d
testb %r13b, %r13b
jne 0x2b65d
movq %rbx, %r14
movl %r12d, %r13d
jmp 0x2b54e
xorl %eax, %eax
testl %r13d, %r13d
pushq $-0x1
popq %rcx
cmovgl %ebp, %ecx
cmovgq %r14, %rax
movq 0x8(%rsp), %rdx
movl %ecx, (%rdx)
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
pushq $0x1
popq %rax
jmp 0x2b667
xorl %eax, %eax
cmpl $0x1, %r12d
jne 0x2b667
movq %rbx, %rdi
callq 0x28c2d
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq