// File: /workspace/github_c_makefile_star2_O2/JimKent[P]linearSat/lib/linefile.c
// Function: lineFileSlurpHttpBody
struct dyString *lineFileSlurpHttpBody(struct lineFile *lf,
				       boolean chunked, int contentLength)
/* Return a dyString that contains the http response body in lf.  Handle
 * chunk-encoding and content-length. */
{
  struct dyString *body = newDyString(64*1024);
  char *line;
  int lineSize;

  dyStringClear(body);
  if (chunked)
    {
      /* Handle "Transfer-Encoding: chunked" body */
      /* Procedure from RFC2068 section 19.4.6 */
      char *csword;
      unsigned chunkSize = 0;
      unsigned size;
      do
	{
	  /* Read line that has chunk size (in hex) as first word. */
	  if (lineFileNext(lf, &line, NULL))
	    csword = nextWord(&line);
	  else break;
	  if (sscanf(csword, "%x", &chunkSize) < 1)
	    {
	      warn("%s: chunked transfer-encoding chunk size parse error.\n",
		   lf->fileName);
	      break;
	    }
	  /* If chunk size is 0, read in a blank line & then we're done. */
	  if (chunkSize == 0)
	    {
	      lineFileNext(lf, &line, NULL);
	      if (line == NULL || (line[0] != '\r' && line[0] != 0))
		warn("%s: chunked transfer-encoding: expected blank line, got %s\n",
		     lf->fileName, line);

	      break;
	    }
	  /* Read (and save) lines until we have read in chunk. */
	  for (size = 0;  size < chunkSize;  size += lineSize)
	    {
	      if (! lineFileNext(lf, &line, &lineSize))
		break;
	      dyStringAppendN(body, line, lineSize-1);
	      dyStringAppendC(body, '\n');
	    }
	  /* Read blank line - or extra CRLF inserted in the middle of the
	   * current line, in which case we need to trim it. */
	  if (size > chunkSize)
	    {
	      body->stringSize -= (size - chunkSize);
	      body->string[body->stringSize] = 0;
	    }
	  else if (size == chunkSize)
	    {
	      lineFileNext(lf, &line, NULL);
	      if (line == NULL || (line[0] != '\r' && line[0] != 0))
		warn("%s: chunked transfer-encoding: expected blank line, got %s\n",
		     lf->fileName, line);
	    }
	} while (chunkSize > 0);
      /* Try to read in next line.  If it's an HTTP header, put it back. */
      /* If there is a next line but it's not an HTTP header, it's a footer. */
      if (lineFileNext(lf, &line, NULL))
	{
	  if (startsWith("HTTP/", line))
	    lineFileReuse(lf);
	  else
	    {
	      /* Got a footer -- keep reading until blank line */
	      warn("%s: chunked transfer-encoding: got footer %s, discarding it.\n",
		   lf->fileName, line);
	      while (lineFileNext(lf, &line, NULL))
		{
		  if ((line[0] == '\r' && line[1] == 0) || line[0] == 0)
		    break;
		  warn("discarding footer line: %s\n", line);
		}
	    }
	}
    }
  else if (contentLength >= 0)
    {
      /* Read in known length */
      int size;
      for (size = 0;  size < contentLength;  size += lineSize)
	{
	  if (! lineFileNext(lf, &line, &lineSize))
	    break;
	  dyStringAppendN(body, line, lineSize-1);
	  dyStringAppendC(body, '\n');
	}
    }
  else
    {
      /* Read in to end of file (assume it's not a persistent connection) */
      while (lineFileNext(lf, &line, &lineSize))
	{
	  dyStringAppendN(body, line, lineSize-1);
	  dyStringAppendC(body, '\n');
	}
    }

  return(body);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, %ebp
movl %esi, %r15d
movq %rdi, %r14
movl $0x10000, %edi          # imm = 0x10000
callq 0x9df0
movq %rax, %rbx
andl $0x0, 0x14(%rax)
movq 0x8(%rax), %rax
movb $0x0, (%rax)
testl %r15d, %r15d
je 0xd114
leaq 0x14(%rsp), %rax
andl $0x0, (%rax)
leaq 0x8(%rsp), %r12
leaq 0x4(%rsp), %rbp
pushq $0xa
popq %r13
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0xbcea
testl %eax, %eax
je 0xd1ba
movq %r12, %rdi
callq 0x6b8e
movq %rax, %rdi
leaq 0xbe4a(%rip), %rsi      # 0x18e95
leaq 0x14(%rsp), %rdx
xorl %eax, %eax
callq 0x3530
testl %eax, %eax
jle 0xd16e
movl 0x14(%rsp), %eax
testl %eax, %eax
je 0xd182
xorl %r15d, %r15d
cmpl %eax, %r15d
jae 0xd0b2
movq %r14, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0xbcea
testl %eax, %eax
je 0xd0ae
movq 0x8(%rsp), %rsi
movl 0x4(%rsp), %edx
decl %edx
movq %rbx, %rdi
callq 0x9eec
movq %rbx, %rdi
movl %r13d, %esi
callq 0x9f5e
addl 0x4(%rsp), %r15d
movl 0x14(%rsp), %eax
jmp 0xd06e
movl 0x14(%rsp), %eax
movl %eax, %ecx
subl %r15d, %ecx
jae 0xd0cc
addl 0x14(%rbx), %ecx
movl %ecx, 0x14(%rbx)
movq 0x8(%rbx), %rax
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
jmp 0xd103
jne 0xd107
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0xbcea
movq 0x8(%rsp), %rdx
testq %rdx, %rdx
je 0xd0f1
movzbl (%rdx), %eax
testl %eax, %eax
je 0xd103
cmpl $0xd, %eax
je 0xd103
movq 0x8(%r14), %rsi
leaq 0xb230(%rip), %rdi      # 0x1832c
xorl %eax, %eax
callq 0xa3aa
movl 0x14(%rsp), %eax
testl %eax, %eax
jne 0xd024
jmp 0xd1ba
testl %ebp, %ebp
js 0xd250
xorl %r15d, %r15d
leaq 0x4(%rsp), %r12
pushq $0xa
popq %r13
cmpl %ebp, %r15d
jge 0xd1e7
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
movq %r12, %rdx
callq 0xbcea
testl %eax, %eax
je 0xd1e7
movq 0x8(%rsp), %rsi
movl 0x4(%rsp), %edx
decl %edx
movq %rbx, %rdi
callq 0x9eec
movq %rbx, %rdi
movl %r13d, %esi
callq 0x9f5e
addl 0x4(%rsp), %r15d
jmp 0xd128
movq 0x8(%r14), %rsi
leaq 0xb17c(%rip), %rdi      # 0x182f5
xorl %eax, %eax
callq 0xa3aa
jmp 0xd1ba
leaq 0x8(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0xbcea
movq (%r15), %rdx
testq %rdx, %rdx
je 0xd1a8
movzbl (%rdx), %eax
testl %eax, %eax
je 0xd1ba
cmpl $0xd, %eax
je 0xd1ba
movq 0x8(%r14), %rsi
leaq 0xb179(%rip), %rdi      # 0x1832c
xorl %eax, %eax
callq 0xa3aa
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0xbcea
testl %eax, %eax
je 0xd1e7
movq 0x8(%rsp), %rsi
leaq 0xb07c(%rip), %rdi      # 0x18255
callq 0x76a5
testl %eax, %eax
je 0xd1f9
movb $0x1, 0x3c(%r14)
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%r14), %rsi
leaq 0x8(%rsp), %r15
movq (%r15), %rdx
leaq 0xb15c(%rip), %rdi      # 0x18368
xorl %eax, %eax
callq 0xa3aa
leaq 0xb18c(%rip), %r12      # 0x183a6
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0xbcea
testl %eax, %eax
je 0xd1e7
movq 0x8(%rsp), %rsi
movzbl (%rsi), %eax
cmpl $0xd, %eax
je 0xd23e
testl %eax, %eax
jne 0xd244
jmp 0xd1e7
cmpb $0x0, 0x1(%rsi)
je 0xd1e7
movq %r12, %rdi
xorl %eax, %eax
callq 0xa3aa
jmp 0xd21a
leaq 0x8(%rsp), %r15
leaq 0x4(%rsp), %r12
pushq $0xa
popq %rbp
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xbcea
testl %eax, %eax
je 0xd1e7
movq 0x8(%rsp), %rsi
movl 0x4(%rsp), %edx
decl %edx
movq %rbx, %rdi
callq 0x9eec
movq %rbx, %rdi
movl %ebp, %esi
callq 0x9f5e
jmp 0xd25d