// File: 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
movq %rsp, %rbp
subq $0x2430, %rsp           # imm = 0x2430
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x2430(%rbp)
movl $0x0, -0x2414(%rbp)
movb $0x0, -0x2419(%rbp)
movb -0x2419(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0xc1ac
jmp 0xc47b
movq -0x2430(%rbp), %rdi
addq $0x20, %rdi
leaq -0x2010(%rbp), %rsi
movl $0x2000, %edx           # imm = 0x2000
xorl %ecx, %ecx
callq 0x9d70
movl %eax, -0x2418(%rbp)
cmpl $0x0, -0x2418(%rbp)
jle 0xc1e5
cmpl $0x2000, -0x2418(%rbp)  # imm = 0x2000
jne 0xc205
movl -0x2418(%rbp), %esi
leaq 0x3a9a(%rip), %rdi      # 0xfc8c
movb $0x0, %al
callq 0x4050
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xc482
movl $0x0, -0x2420(%rbp)
movl -0x2420(%rbp), %eax
cmpl -0x2418(%rbp), %eax
jge 0xc476
movslq -0x2420(%rbp), %rax
movzbl -0x2010(%rbp,%rax), %eax
cmpl $0xd, %eax
jne 0xc23a
jmp 0xc462
movslq -0x2420(%rbp), %rax
movzbl -0x2010(%rbp,%rax), %eax
cmpl $0xa, %eax
jne 0xc40c
movslq -0x2414(%rbp), %rax
movb $0x0, -0x2410(%rbp,%rax)
movl $0x0, -0x2414(%rbp)
movsbl -0x2410(%rbp), %eax
cmpl $0x0, %eax
jne 0xc27e
movb $0x1, -0x2419(%rbp)
leaq -0x2410(%rbp), %rdi
leaq 0x3a2a(%rip), %rsi      # 0xfcb6
movl $0x5, %edx
callq 0x4140
cmpl $0x0, %eax
jne 0xc40a
movl $0x4, -0x2424(%rbp)
movl $0x0, -0x2428(%rbp)
movslq -0x2424(%rbp), %rax
movsbl -0x2410(%rbp,%rax), %eax
cmpl $0x0, %eax
je 0xc342
movslq -0x2424(%rbp), %rax
movsbl -0x2410(%rbp,%rax), %eax
cmpl $0x20, %eax
jne 0xc2dd
jmp 0xc342
movslq -0x2424(%rbp), %rax
movsbl -0x2410(%rbp,%rax), %eax
cmpl $0x3f, %eax
jne 0xc2f3
jmp 0xc342
movq -0x2430(%rbp), %rax
movl -0x2424(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x2424(%rbp)
movslq %ecx, %rcx
movb -0x2410(%rbp,%rcx), %dl
movl -0x2428(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x2428(%rbp)
movl %ecx, %ecx
movb %dl, 0xd0(%rax,%rcx)
movl -0x2428(%rbp), %eax
cmpq $0x7f, %rax
jb 0xc33d
jmp 0xc342
jmp 0xc2b3
movq -0x2430(%rbp), %rax
movl -0x2428(%rbp), %ecx
movb $0x0, 0xd0(%rax,%rcx)
movl $0x0, -0x2428(%rbp)
movl -0x2424(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x2424(%rbp)
cltq
movsbl -0x2410(%rbp,%rax), %eax
cmpl $0x3f, %eax
jne 0xc3f8
jmp 0xc383
movslq -0x2424(%rbp), %rax
movsbl -0x2410(%rbp,%rax), %eax
cmpl $0x0, %eax
je 0xc3f6
movslq -0x2424(%rbp), %rax
movsbl -0x2410(%rbp,%rax), %eax
cmpl $0x20, %eax
jne 0xc3ad
jmp 0xc3f6
movq -0x2430(%rbp), %rax
movl -0x2424(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x2424(%rbp)
movslq %ecx, %rcx
movb -0x2410(%rbp,%rcx), %dl
movl -0x2428(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x2428(%rbp)
movl %ecx, %ecx
movb %dl, 0x50(%rax,%rcx)
movl -0x2428(%rbp), %eax
cmpq $0x7f, %rax
jb 0xc3f4
jmp 0xc3f6
jmp 0xc383
jmp 0xc3f8
movq -0x2430(%rbp), %rax
movl -0x2428(%rbp), %ecx
movb $0x0, 0x50(%rax,%rcx)
jmp 0xc462
movslq -0x2420(%rbp), %rax
movb -0x2010(%rbp,%rax), %cl
movl -0x2414(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2414(%rbp)
cltq
movb %cl, -0x2410(%rbp,%rax)
movslq -0x2414(%rbp), %rax
cmpq $0x3ff, %rax            # imm = 0x3FF
jne 0xc460
movl -0x2418(%rbp), %esi
leaq 0x386c(%rip), %rdi      # 0xfcbc
movb $0x0, %al
callq 0x4050
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xc482
jmp 0xc462
movl -0x2420(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2420(%rbp)
jmp 0xc20f
jmp 0xc19b
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x2430, %rsp           # imm = 0x2430
popq %rbp
retq
nop