// File: /workspace/github_cpp_makefile_star2_O2/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, %r15
leaq 0x20(%rdi), %rax
movq %rax, 0x8(%rsp)
movabsq $-0x7ffffffeffffffff, %r12 # imm = 0x8000000100000001
xorl %ebp, %ebp
xorl %eax, %eax
testb $0x1, %al
jne 0x7fca
movq 0x8(%rsp), %rdi
leaq 0x410(%rsp), %rsi
movl $0x2000, %edx           # imm = 0x2000
xorl %ecx, %ecx
callq 0x6e4a
testl %eax, %eax
jle 0x7fce
cmpl $0x2000, %eax           # imm = 0x2000
je 0x7fce
movl %eax, 0x4(%rsp)
movl %eax, %r13d
xorl %ebx, %ebx
xorl %eax, %eax
cmpq %r13, %rbx
je 0x7e7f
movzbl 0x410(%rsp,%rbx), %ecx
cmpl $0xd, %ecx
je 0x7f35
movslq %ebp, %rdx
cmpl $0xa, %ecx
jne 0x7f3a
movb $0x0, 0x10(%rsp,%rdx)
cmpb $0x0, 0x10(%rsp)
sete %r14b
orb %al, %r14b
leaq 0x10(%rsp), %rdi
leaq 0x2d3d(%rip), %rsi      # 0xac32
pushq $0x5
popq %rdx
callq 0x4210
xorl %ebp, %ebp
testl %eax, %eax
jne 0x7f96
xorl %eax, %eax
movzbl 0x14(%rsp,%rax), %ecx
cmpq $0x3f, %rcx
ja 0x7f1a
btq %rcx, %r12
jb 0x7f4e
movb %cl, 0xd0(%r15,%rax)
incq %rax
cmpq $0x7f, %rax
jne 0x7f09
movl $0x83, %ecx
pushq $0x7f
popq %rax
jmp 0x7f51
movl %eax, %r14d
jmp 0x7f96
leal 0x1(%rdx), %ebp
movb %cl, 0x10(%rsp,%rdx)
movl %eax, %r14d
cmpl $0x3ff, %ebp            # imm = 0x3FF
jne 0x7f96
jmp 0x7fa1
leal 0x4(%rax), %ecx
movb $0x0, 0xd0(%r15,%rax)
movslq %ecx, %rax
cmpb $0x3f, 0x10(%rsp,%rax)
jne 0x7f8e
movl %ecx, %ecx
leaq 0x11(%rsp), %rax
addq %rax, %rcx
xorl %eax, %eax
movzbl (%rcx,%rax), %edx
testb $-0x21, %dl
je 0x7f8a
movb %dl, 0x50(%r15,%rax)
incq %rax
cmpq $0x7f, %rax
jne 0x7f70
pushq $0x7f
popq %rax
movl %eax, %eax
jmp 0x7f90
xorl %eax, %eax
movb $0x0, 0x50(%r15,%rax)
incq %rbx
movl %r14d, %eax
jmp 0x7ebe
leaq 0x2c90(%rip), %rdi      # 0xac38
movl 0x4(%rsp), %eax
movl %eax, %esi
xorl %eax, %eax
callq 0x4050
pushq $-0x1
popq %rax
addq $0x2418, %rsp           # imm = 0x2418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0x7fb8
leaq 0x2c33(%rip), %rdi      # 0xac08
jmp 0x7fac
nop