// File: /workspace/github_c_makefile_star2_O3/icebert[P]pblat/lib/net.c
// Function: netHttpGetMultiple
int netHttpGetMultiple(char *url, struct slName *queries, void *userData,
		       void (*responseCB)(void *userData, char *req,
					  char *hdr, struct dyString *body))
/* Given an URL which is the base of all requests to be made, and a 
 * linked list of queries to be appended to that base and sent in as 
 * requests, send the requests as a batch and read the HTTP response 
 * headers and bodies.  If not all the requests get responses (i.e. if 
 * the server is ignoring Keep-Alive or is imposing a limit), try again 
 * until we can't connect or until all requests have been served. 
 * For each HTTP response, do a callback. */
{
  struct slName *qStart;
  struct slName *qPtr;
  struct lineFile *lf;
  struct netParsedUrl *npu;
  struct dyString *dyQ    = newDyString(512);
  struct dyString *body;
  char *base;
  char *hdr;
  int qCount;
  int qTotal;
  int numParseFailures;
  int contentLength;
  boolean chunked;
  boolean done;
  boolean keepAlive;

  /* Find out how many queries we'll need to do so we know how many times 
   * it's OK to run into end of file in case server ignores Keep-Alive. */
  qTotal = 0;
  for (qPtr = queries;  qPtr != NULL;  qPtr = qPtr->next)
    {
      qTotal++;
    }

  done = FALSE;
  qCount = 0;
  numParseFailures = 0;
  qStart = queries;
  while ((! done) && (qStart != NULL))
    {
      lf = netHttpLineFileMayOpen(url, &npu);
      if (lf == NULL)
	{
	  done = TRUE;
	  break;
	}
      base = cloneString(npu->file);
      /* Send all remaining requests with keep-alive. */
      for (qPtr = qStart;  qPtr != NULL;  qPtr = qPtr->next)
	{
	  dyStringClear(dyQ);
	  dyStringAppend(dyQ, base);
	  dyStringAppend(dyQ, qPtr->name);
	  strcpy(npu->file, dyQ->string);
	  keepAlive = (qPtr->next == NULL) ? FALSE : TRUE;
	  netHttpGet(lf, npu, keepAlive);
	}
      /* Get as many responses as we can; call responseCB() and 
       * advance qStart for each. */
      for (qPtr = qStart;  qPtr != NULL;  qPtr = qPtr->next)
        {
	  if (lineFileParseHttpHeader(lf, &hdr, &chunked, &contentLength))
	    {
	      body = lineFileSlurpHttpBody(lf, chunked, contentLength);
	      dyStringClear(dyQ);
	      dyStringAppend(dyQ, base);
	      dyStringAppend(dyQ, qPtr->name);
	      responseCB(userData, dyQ->string, hdr, body);
	      qStart = qStart->next;
	      qCount++;
	    }
	  else
	    {
	      if (numParseFailures++ > qTotal) {
		done = TRUE;
	      }
	      break;
	    }
	}
    }

  return qCount;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rcx, 0x40(%rsp)
movq %rdx, 0x38(%rsp)
movq %rsi, %rbx
movq %rdi, 0x28(%rsp)
movl $0x200, %edi            # imm = 0x200
callq 0x264e0
testq %rbx, %rbx
je 0x3157e
movq %rax, %r13
xorl %ecx, %ecx
movq %rbx, %rax
incl %ecx
movq (%rax), %rax
testq %rax, %rax
jne 0x31421
testq %rbx, %rbx
je 0x3157e
movl %ecx, 0xc(%rsp)
movq $0x0, 0x18(%rsp)
xorl %ebp, %ebp
movq 0x28(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x31211
testq %rax, %rax
je 0x31580
movq %rax, %r12
movq 0x20(%rsp), %rdi
movl $0x1820, %eax           # imm = 0x1820
addq %rax, %rdi
callq 0x1feba
movq %rbx, %r15
movq %rax, %rbx
movq %r15, 0x30(%rsp)
movl $0x0, 0x14(%r13)
movq 0x8(%r13), %rax
movb $0x0, (%rax)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x2674f
leaq 0x8(%r15), %rsi
movq %r13, %rdi
callq 0x2674f
movq 0x20(%rsp), %r14
leaq 0x1820(%r14), %rdi
movq 0x8(%r13), %rsi
callq 0x6680
xorl %edx, %edx
cmpq $0x0, (%r15)
setne %dl
movq %r12, %rdi
movq %r14, %rsi
callq 0x3129e
movq (%r15), %r15
testq %r15, %r15
jne 0x3147b
movq %rbx, 0x48(%rsp)
movq 0x30(%rsp), %rbx
movq %rbx, %r15
movq %r12, %rdi
leaq 0x50(%rsp), %rsi
leaq 0x10(%rsp), %rdx
leaq 0x14(%rsp), %rcx
callq 0x2bc04
testl %eax, %eax
je 0x3155b
movl 0x10(%rsp), %esi
movl 0x14(%rsp), %edx
movq %r12, %rdi
callq 0x2be80
movq %rax, %r14
movl $0x0, 0x14(%r13)
movq 0x8(%r13), %rax
movb $0x0, (%rax)
movq %r13, %rdi
movq 0x48(%rsp), %rsi
callq 0x2674f
leaq 0x8(%r15), %rsi
movq %r13, %rdi
callq 0x2674f
movq 0x8(%r13), %rsi
movq 0x50(%rsp), %rdx
movq 0x38(%rsp), %rdi
movq %r14, %rcx
callq *0x40(%rsp)
movq (%rbx), %rbx
incl %ebp
movq (%r15), %r15
testq %r15, %r15
jne 0x314df
movb $0x1, %al
jmp 0x3156f
movq 0x18(%rsp), %rax
cmpl 0xc(%rsp), %eax
leal 0x1(%rax), %ecx
setle %al
movq %rcx, 0x18(%rsp)
testb %al, %al
je 0x31580
testq %rbx, %rbx
jne 0x31443
jmp 0x31580
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq