// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movq %rcx, 0x20(%rsp)
movq %rdx, 0x18(%rsp)
movq %rsi, %r15
movl %edi, %r13d
leaq 0x8(%rsp), %r14
xorl %ebp, %ebp
movq $0x0, 0x8(%rsp)
movl %r13d, %edi
movq %r15, %rsi
movq %r14, %rdx
callq 0x30158
movl %eax, %ebx
testl %eax, %eax
sete %al
movq 0x8(%rsp), %r12
testq %r12, %r12
setne %cl
orb %al, %cl
je 0x30829
movl %r13d, %edi
callq 0x6e80
movb $0x1, %r14b
testl %ebx, %ebx
je 0x3085b
leal 0x1(%rbp), %ebx
cmpl $0x5, %ebp
jl 0x3085f
leaq 0x61b18(%rip), %rdi     # 0x9233c
jmp 0x308be
xorl %eax, %eax
testl %ebp, %ebp
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmovgl %r13d, %ecx
movl $0x0, %edx
cmovgq %r15, %rdx
movq 0x18(%rsp), %rsi
movl %ecx, (%rsi)
movq 0x20(%rsp), %rcx
movq %rdx, (%rcx)
movl $0x1, 0x14(%rsp)
jmp 0x30909
movl %ebp, %ebx
jmp 0x308c8
leaq 0x60d52(%rip), %rdi     # 0x915b8
movq %r12, %rsi
callq 0x217c8
testl %eax, %eax
jne 0x30885
leaq 0x60d47(%rip), %rdi     # 0x915c0
movq %r12, %rsi
callq 0x217c8
testl %eax, %eax
je 0x308b7
movq %r15, %rdi
movq %r12, %rsi
callq 0x3070a
movq %rax, %r12
movq %rax, 0x8(%rsp)
movq %rax, %rdi
xorl %esi, %esi
callq 0x2fc1b
movl %eax, %r13d
testl %eax, %eax
js 0x308ae
xorl %r14d, %r14d
jmp 0x308c8
leaq 0x61adb(%rip), %rdi     # 0x92390
jmp 0x308be
leaq 0x61ab4(%rip), %rdi     # 0x92372
movq %r12, %rsi
xorl %eax, %eax
callq 0x268ca
cmpl $0x2, %ebx
jl 0x308d5
movq %r15, %rdi
callq 0x2dd41
testb %r14b, %r14b
je 0x308fb
testl %ebx, %ebx
leaq 0x8(%rsp), %r14
jle 0x308ed
movq 0x8(%rsp), %rdi
callq 0x2dd41
movl $0x0, 0x14(%rsp)
movl %ebx, %ebp
xorl %eax, %eax
jmp 0x30909
movb $0x1, %al
movq 0x8(%rsp), %r15
movl %ebx, %ebp
leaq 0x8(%rsp), %r14
testb %al, %al
jne 0x307d9
movl 0x14(%rsp), %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq