// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movq $0x0, -0x30(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq -0x30(%rbp), %rdx
callq 0x443d0
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x44db3
cmpq $0x0, -0x30(%rbp)
jne 0x44db3
cmpl $0x0, -0x24(%rbp)
jle 0x44d92
movl -0x8(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
jmp 0x44da7
movq -0x18(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq -0x20(%rbp), %rax
movq $0x0, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x44eb3
movl -0x8(%rbp), %edi
callq 0x6e90
cmpl $0x0, -0x34(%rbp)
je 0x44e79
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
cmpl $0x5, -0x24(%rbp)
jle 0x44df2
movq -0x30(%rbp), %rsi
leaq 0x97405(%rip), %rdi     # 0xdc1e4
movb $0x0, %al
callq 0x358e0
movl $0x0, -0x34(%rbp)
jmp 0x44e77
movq -0x30(%rbp), %rsi
leaq 0x96663(%rip), %rdi     # 0xdb460
callq 0x2d3d0
cmpl $0x0, %eax
jne 0x44e37
movq -0x30(%rbp), %rsi
leaq 0x96656(%rip), %rdi     # 0xdb468
callq 0x2d3d0
cmpl $0x0, %eax
jne 0x44e37
movq -0x30(%rbp), %rsi
leaq 0x973f3(%rip), %rdi     # 0xdc21a
movb $0x0, %al
callq 0x358e0
movl $0x0, -0x34(%rbp)
jmp 0x44e75
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x44c60
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x442f0
movl %eax, -0x8(%rbp)
cmpl $0x0, -0x8(%rbp)
jge 0x44e73
movq -0x30(%rbp), %rsi
leaq 0x973d3(%rip), %rdi     # 0xdc238
movb $0x0, %al
callq 0x358e0
movl $0x0, -0x34(%rbp)
jmp 0x44e75
jmp 0x44e77
jmp 0x44e79
cmpl $0x1, -0x24(%rbp)
jle 0x44e88
movq -0x10(%rbp), %rdi
callq 0x40d40
cmpl $0x0, -0x34(%rbp)
jne 0x44ea6
cmpl $0x0, -0x24(%rbp)
jle 0x44e9d
movq -0x30(%rbp), %rdi
callq 0x40d40
movl $0x0, -0x4(%rbp)
jmp 0x44eb3
movq -0x30(%rbp), %rax
movq %rax, -0x10(%rbp)
jmp 0x44d4e
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)