// File: /workspace/github_c_makefile_star2_O1/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 0xaf38
movq %rax, %rbx
movl $0x0, 0x14(%rax)
movq 0x8(%rax), %rax
movb $0x0, (%rax)
testl %r15d, %r15d
je 0xe880
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 0xd274
testl %eax, %eax
je 0xe92c
movq %r12, %rdi
callq 0x7371
movq %rax, %rdi
leaq 0xc6d4(%rip), %rsi      # 0x1ae88
movq %r15, %rdx
xorl %eax, %eax
callq 0x3530
testl %eax, %eax
jle 0xe8e0
cmpl $0x0, 0x14(%rsp)
je 0xe8f4
cmpl $0x0, 0x14(%rsp)
je 0xe81b
xorl %r13d, %r13d
movq %r14, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0xd274
testl %eax, %eax
je 0xe81e
movq 0x8(%rsp), %rsi
movl 0x4(%rsp), %edx
decl %edx
movq %rbx, %rdi
callq 0xb042
movq %rbx, %rdi
movl $0xa, %esi
callq 0xb0c7
addl 0x4(%rsp), %r13d
cmpl 0x14(%rsp), %r13d
jb 0xe7db
jmp 0xe81e
xorl %r13d, %r13d
movl 0x14(%rsp), %eax
subl %r13d, %eax
jae 0xe839
addl 0x14(%rbx), %eax
movl %eax, 0x14(%rbx)
movq 0x8(%rbx), %rcx
cltq
movb $0x0, (%rcx,%rax)
jmp 0xe870
jne 0xe870
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0xd274
movq 0x8(%rsp), %rdx
testq %rdx, %rdx
je 0xe85e
movzbl (%rdx), %eax
testl %eax, %eax
je 0xe870
cmpl $0xd, %eax
je 0xe870
movq 0x8(%r14), %rsi
leaq 0xbab3(%rip), %rdi      # 0x1a31c
xorl %eax, %eax
callq 0xb632
cmpl $0x0, 0x14(%rsp)
jne 0xe78d
jmp 0xe92c
testl %ebp, %ebp
js 0xe9d7
je 0xea29
xorl %r13d, %r13d
leaq 0x8(%rsp), %r15
leaq 0x4(%rsp), %r12
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xd274
testl %eax, %eax
je 0xea29
movq 0x8(%rsp), %rsi
movl 0x4(%rsp), %edx
decl %edx
movq %rbx, %rdi
callq 0xb042
movq %rbx, %rdi
movl $0xa, %esi
callq 0xb0c7
addl 0x4(%rsp), %r13d
cmpl %ebp, %r13d
jl 0xe89b
jmp 0xea29
movq 0x8(%r14), %rsi
leaq 0xb9fa(%rip), %rdi      # 0x1a2e5
xorl %eax, %eax
callq 0xb632
jmp 0xe92c
leaq 0x8(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0xd274
movq (%r15), %rdx
testq %rdx, %rdx
je 0xe91a
movzbl (%rdx), %eax
testl %eax, %eax
je 0xe92c
cmpl $0xd, %eax
je 0xe92c
movq 0x8(%r14), %rsi
leaq 0xb9f7(%rip), %rdi      # 0x1a31c
xorl %eax, %eax
callq 0xb632
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0xd274
testl %eax, %eax
je 0xea29
movq 0x8(%rsp), %rsi
leaq 0xb8f6(%rip), %rdi      # 0x1a245
callq 0x8214
testl %eax, %eax
je 0xe962
movb $0x1, 0x3c(%r14)
jmp 0xea29
movq 0x8(%r14), %rsi
leaq 0x8(%rsp), %r15
movq (%r15), %rdx
leaq 0xb9e3(%rip), %rdi      # 0x1a358
xorl %eax, %eax
callq 0xb632
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0xd274
testl %eax, %eax
je 0xea29
leaq 0xb9fe(%rip), %r15      # 0x1a396
leaq 0x8(%rsp), %r12
movq 0x8(%rsp), %rsi
movzbl (%rsi), %eax
cmpl $0xd, %eax
je 0xe9b0
testl %eax, %eax
jne 0xe9ba
jmp 0xea29
testb %al, %al
je 0xea29
cmpb $0x0, 0x1(%rsi)
je 0xea29
movq %r15, %rdi
xorl %eax, %eax
callq 0xb632
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0xd274
testl %eax, %eax
jne 0xe99d
jmp 0xea29
leaq 0x8(%rsp), %rsi
leaq 0x4(%rsp), %rdx
movq %r14, %rdi
callq 0xd274
testl %eax, %eax
je 0xea29
leaq 0x8(%rsp), %r15
leaq 0x4(%rsp), %r12
movq 0x8(%rsp), %rsi
movl 0x4(%rsp), %edx
decl %edx
movq %rbx, %rdi
callq 0xb042
movq %rbx, %rdi
movl $0xa, %esi
callq 0xb0c7
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xd274
testl %eax, %eax
jne 0xe9f7
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq