// File: /workspace/github_c_makefile_star2_O3/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 0xad14
movq %rax, %rbx
movl $0x0, 0x14(%rax)
movq 0x8(%rax), %rax
movb $0x0, (%rax)
testl %r15d, %r15d
je 0xe630
leaq 0x14(%rsp), %r15
movl $0x0, (%r15)
leaq 0x8(%rsp), %r12
leaq 0x4(%rsp), %rbp
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0xd043
testl %eax, %eax
je 0xe6dc
movq %r12, %rdi
callq 0x7292
movq %rax, %rdi
leaq 0xc92d(%rip), %rsi      # 0x1ae95
movq %r15, %rdx
xorl %eax, %eax
callq 0x3530
testl %eax, %eax
jle 0xe690
cmpl $0x0, 0x14(%rsp)
je 0xe6a4
xorl %r13d, %r13d
movq %r14, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0xd043
testl %eax, %eax
je 0xe5ca
movq 0x8(%rsp), %rsi
movl 0x4(%rsp), %edx
decl %edx
movq %rbx, %rdi
callq 0xae1e
movq %rbx, %rdi
movl $0xa, %esi
callq 0xaeab
addl 0x4(%rsp), %r13d
movl 0x14(%rsp), %eax
cmpl %eax, %r13d
jb 0xe588
jmp 0xe5ce
movl 0x14(%rsp), %eax
movl %eax, %ecx
subl %r13d, %ecx
jae 0xe5e8
addl 0x14(%rbx), %ecx
movl %ecx, 0x14(%rbx)
movq 0x8(%rbx), %rax
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
jmp 0xe61f
jne 0xe623
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0xd043
movq 0x8(%rsp), %rdx
testq %rdx, %rdx
je 0xe60d
movzbl (%rdx), %eax
testl %eax, %eax
je 0xe61f
cmpl $0xd, %eax
je 0xe61f
movq 0x8(%r14), %rsi
leaq 0xbd14(%rip), %rdi      # 0x1a32c
xorl %eax, %eax
callq 0xb41e
movl 0x14(%rsp), %eax
testl %eax, %eax
jne 0xe541
jmp 0xe6dc
testl %ebp, %ebp
js 0xe783
je 0xe7d5
xorl %r13d, %r13d
leaq 0x8(%rsp), %r15
leaq 0x4(%rsp), %r12
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xd043
testl %eax, %eax
je 0xe7d5
movq 0x8(%rsp), %rsi
movl 0x4(%rsp), %edx
decl %edx
movq %rbx, %rdi
callq 0xae1e
movq %rbx, %rdi
movl $0xa, %esi
callq 0xaeab
addl 0x4(%rsp), %r13d
cmpl %ebp, %r13d
jl 0xe64b
jmp 0xe7d5
movq 0x8(%r14), %rsi
leaq 0xbc5a(%rip), %rdi      # 0x1a2f5
xorl %eax, %eax
callq 0xb41e
jmp 0xe6dc
leaq 0x8(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0xd043
movq (%r15), %rdx
testq %rdx, %rdx
je 0xe6ca
movzbl (%rdx), %eax
testl %eax, %eax
je 0xe6dc
cmpl $0xd, %eax
je 0xe6dc
movq 0x8(%r14), %rsi
leaq 0xbc57(%rip), %rdi      # 0x1a32c
xorl %eax, %eax
callq 0xb41e
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0xd043
testl %eax, %eax
je 0xe7d5
movq 0x8(%rsp), %rsi
leaq 0xbb56(%rip), %rdi      # 0x1a255
callq 0x8038
testl %eax, %eax
je 0xe712
movb $0x1, 0x3c(%r14)
jmp 0xe7d5
movq 0x8(%r14), %rsi
leaq 0x8(%rsp), %r15
movq (%r15), %rdx
leaq 0xbc43(%rip), %rdi      # 0x1a368
xorl %eax, %eax
callq 0xb41e
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0xd043
testl %eax, %eax
je 0xe7d5
leaq 0xbc5e(%rip), %r15      # 0x1a3a6
leaq 0x8(%rsp), %r12
movq 0x8(%rsp), %rsi
movzbl (%rsi), %eax
cmpl $0xd, %eax
je 0xe760
testl %eax, %eax
jne 0xe766
jmp 0xe7d5
cmpb $0x0, 0x1(%rsi)
je 0xe7d5
movq %r15, %rdi
xorl %eax, %eax
callq 0xb41e
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0xd043
testl %eax, %eax
jne 0xe74d
jmp 0xe7d5
leaq 0x8(%rsp), %rsi
leaq 0x4(%rsp), %rdx
movq %r14, %rdi
callq 0xd043
testl %eax, %eax
je 0xe7d5
leaq 0x8(%rsp), %r15
leaq 0x4(%rsp), %r12
movq 0x8(%rsp), %rsi
movl 0x4(%rsp), %edx
decl %edx
movq %rbx, %rdi
callq 0xae1e
movq %rbx, %rdi
movl $0xa, %esi
callq 0xaeab
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xd043
testl %eax, %eax
jne 0xe7a3
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq