// File: /workspace/github_c_makefile_star2_O2/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 $0x48, %rsp
movq %rcx, 0x38(%rsp)
movq %rdx, 0x30(%rsp)
movq %rsi, %rbx
movq %rdi, 0x28(%rsp)
movl $0x200, %edi            # imm = 0x200
callq 0x21bcc
movq %rax, %r13
xorl %ecx, %ecx
movq %rbx, %rbp
testq %rbx, %rbx
je 0x2be1f
incl %ecx
movq (%rbx), %rbx
jmp 0x2be13
movl %ecx, 0xc(%rsp)
movl $0x0, 0x8(%rsp)
movq $0x0, 0x18(%rsp)
xorl %eax, %eax
testl %eax, %eax
sete %al
testb $0x1, %al
je 0x2bf72
testq %rbp, %rbp
je 0x2bf72
movq 0x28(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x2bc0e
testq %rax, %rax
je 0x2bf72
movq %rax, %r12
movq 0x20(%rsp), %rdi
movl $0x1820, %eax           # imm = 0x1820
addq %rax, %rdi
callq 0x1c306
movq %rax, %rbx
movq %rbp, %r15
testq %r15, %r15
je 0x2bed5
andl $0x0, 0x14(%r13)
movq 0x8(%r13), %rax
movb $0x0, (%rax)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x21dda
leaq 0x8(%r15), %rsi
movq %r13, %rdi
callq 0x21dda
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 0x2bc99
movq (%r15), %r15
jmp 0x2be7f
movq %rbp, %r15
testq %r15, %r15
je 0x2bf50
movq %r12, %rdi
leaq 0x40(%rsp), %rsi
leaq 0x10(%rsp), %rdx
leaq 0x14(%rsp), %rcx
callq 0x26953
testl %eax, %eax
je 0x2bf57
movl 0x10(%rsp), %esi
movl 0x14(%rsp), %edx
movq %r12, %rdi
callq 0x26bb3
andl $0x0, 0x14(%r13)
movq %rax, %r14
movq 0x8(%r13), %rax
movb $0x0, (%rax)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x21dda
leaq 0x8(%r15), %rsi
movq %r13, %rdi
callq 0x21dda
movq 0x8(%r13), %rsi
movq 0x40(%rsp), %rdx
movq 0x30(%rsp), %rdi
movq %r14, %rcx
callq *0x38(%rsp)
movq (%rbp), %rbp
incl 0x8(%rsp)
movq (%r15), %r15
jmp 0x2bed8
movb $0x1, %al
jmp 0x2be3b
xorl %eax, %eax
movq 0x18(%rsp), %rcx
cmpl 0xc(%rsp), %ecx
leal 0x1(%rcx), %ecx
seta %al
movq %rcx, 0x18(%rsp)
jmp 0x2be36
movl 0x8(%rsp), %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq