// File: /workspace/github_cpp_makefile_star2_O1/mikeakohn[P]cloudtari/build/../src/TelevisionHttp.cxx
// Function: TelevisionHttp::read_http()
int TelevisionHttp::read_http()
{
  uint8_t buffer[8192];
  char line[1024];
  int ptr = 0, length;
  bool end_of_headers = false;

  while (!end_of_headers)
  {
    length = net_recv(buffer, sizeof(buffer), false);

    if (length <= 0 || length == 8192)
    {
      printf("Network receive error (return value %d).\n", length);
      return -1;
    }

    for (int n = 0; n < length; n++)
    {
      if (buffer[n] == '\r') { continue; }
      if (buffer[n] == '\n')
      {
        line[ptr] = 0;
        ptr = 0;

        if (line[0] == 0) { end_of_headers = true; }

        if (strncmp(line, "GET /", 5) == 0)
        {
//printf("line=%s\n", line);
          int ptr = 4;
          uint32_t n = 0;

          while (line[ptr] != 0)
          {
            if (line[ptr] == ' ') { break; }
            if (line[ptr] == '?') { break; }

            filename[n++] = line[ptr++];
            if (n >= sizeof(filename) - 1) { break; }
          }

          filename[n] = 0;
          n = 0;

          if (line[ptr++] == '?')
          {
            while (line[ptr] != 0)
            {
              if (line[ptr] == ' ') { break; }

              query_string[n++] = line[ptr++];
              if (n >= sizeof(query_string) - 1) { break; }
            }
          }

          query_string[n] = 0;
        }

        continue;
      }

      line[ptr++] = buffer[n];

      if (ptr == sizeof(line) - 1)
      {
        printf("Error: Http line too long (%d).\n", length);
        return -1;
      }
    }
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2418, %rsp           # imm = 0x2418
movq %rdi, %r14
leaq 0x20(%rdi), %rax
movq %rax, 0x8(%rsp)
movl $0x20544547, %ebx       # imm = 0x20544547
movabsq $-0x7ffffffeffffffff, %r12 # imm = 0x8000000100000001
xorl %r15d, %r15d
xorl %ebp, %ebp
testb $0x1, %bpl
jne 0x8690
movq 0x8(%rsp), %rdi
leaq 0x410(%rsp), %rsi
movl $0x2000, %edx           # imm = 0x2000
xorl %ecx, %ecx
callq 0x72e2
testl %eax, %eax
jle 0x8694
cmpl $0x2000, %eax           # imm = 0x2000
je 0x8694
movl %eax, %ecx
movb $0x1, %r13b
xorl %edx, %edx
movl $0x1, %r9d
movzbl 0x410(%rsp,%rdx), %esi
cmpl $0xd, %esi
je 0x8662
movslq %r15d, %rdi
cmpl $0xa, %esi
jne 0x8600
movb $0x0, 0x10(%rsp,%rdi)
cmpb $0x0, 0x10(%rsp)
movzbl %bpl, %ebp
cmovel %r9d, %ebp
movl 0x10(%rsp), %esi
xorl %ebx, %esi
movzbl 0x14(%rsp), %edi
xorl $0x2f, %edi
xorl %r15d, %r15d
orl %esi, %edi
jne 0x8662
xorl %esi, %esi
movzbl 0x14(%rsp,%rsi), %edi
cmpq $0x3f, %rdi
ja 0x85e3
btq %rdi, %r12
jb 0x8614
movb %dil, 0xd0(%r14,%rsi)
incq %rsi
cmpq $0x7f, %rsi
jne 0x85d2
movl $0x7f, %esi
movl $0x83, %edi
jmp 0x8617
leal 0x1(%rdi), %r15d
movb %sil, 0x10(%rsp,%rdi)
cmpl $0x3ff, %r15d           # imm = 0x3FF
jne 0x8662
jmp 0x8674
leal 0x4(%rsi), %edi
movl %esi, %esi
movb $0x0, 0xd0(%r14,%rsi)
movslq %edi, %rsi
cmpb $0x3f, 0x10(%rsp,%rsi)
jne 0x865a
movl %edi, %edi
leaq 0x11(%rsp), %rsi
addq %rsi, %rdi
xorl %esi, %esi
movzbl (%rdi,%rsi), %r8d
testb $-0x21, %r8b
je 0x8656
movb %r8b, 0x50(%r14,%rsi)
incq %rsi
cmpq $0x7f, %rsi
jne 0x8638
movl $0x7f, %esi
movl %esi, %esi
jmp 0x865c
xorl %esi, %esi
movb $0x0, 0x50(%r14,%rsi)
incq %rdx
cmpq %rcx, %rdx
setb %r13b
jne 0x858c
jmp 0x8684
leaq 0x25a9(%rip), %rdi      # 0xac24
movl %eax, %esi
xorl %eax, %eax
callq 0x4050
testb $0x1, %r13b
je 0x8549
jmp 0x86a4
xorl %eax, %eax
jmp 0x86a9
leaq 0x2559(%rip), %rdi      # 0xabf4
movl %eax, %esi
xorl %eax, %eax
callq 0x4050
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x2418, %rsp           # imm = 0x2418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop