// File: mikeakohn[P]cloudtari/build/../src/TelevisionHttp.cxx
// Function: TelevisionHttp::handle_events()
int TelevisionHttp::handle_events()
{
  if (net_has_data())
  {
    read_http();

    // The query string from the browser is ?keys#timestamp.
    // The timestamp is unfortunate since it seems the browser is ignoring
    // the cache expire headers.
    int n = 0;

    while (query_string[n] != 0)
    {
      if (query_string[n] == '#') { break; }
      key_queue.append(query_string[n]);
      n++;
    }

    if (strcmp(filename, "/") == 0)
    {
      send_index_html();
    }
      else
    if (strcmp(filename, "/image.gif") == 0)
    {
      send_gif();
    }
      else
    {
      send_404();
    }

    filename[0] = 0;
    query_string[0] = 0;
    no_data_count = 0;
  }
    else
  {
    no_data_count++;

    // If there aren't any web requests for a while, assume the connection
    // is broken.
    if (no_data_count == 30 * 60) { return KEY_QUIT; }
  }

  if (!net_is_connected()) { return KEY_QUIT; }

  if (!key_queue.is_empty())
  {
    char c = key_queue.get_next();
    switch (c)
    {
      case 's': return KEY_DOWN_DOWN;
      case 'w': return KEY_UP_DOWN;
      case 'a': return KEY_LEFT_DOWN;
      case 'd': return KEY_RIGHT_DOWN;
      case 'f': return KEY_FIRE_DOWN;
      case 'e': return KEY_RESET_DOWN;
      case 'c': return KEY_SELECT_DOWN;
      case 'S': return KEY_DOWN_UP;
      case 'W': return KEY_UP_UP;
      case 'A': return KEY_LEFT_UP;
      case 'D': return KEY_RIGHT_UP;
      case 'F': return KEY_FIRE_UP;
      case 'E': return KEY_RESET_UP;
      case 'C': return KEY_SELECT_UP;
    }
  }

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x20(%rbp)
addq $0x20, %rdi
callq 0x9fe0
testb $0x1, %al
jne 0xc6d6
jmp 0xc7a4
movq -0x20(%rbp), %rdi
callq 0xc170
movl $0x0, -0x14(%rbp)
movq -0x20(%rbp), %rax
movslq -0x14(%rbp), %rcx
movsbl 0x50(%rax,%rcx), %eax
cmpl $0x0, %eax
je 0xc733
movq -0x20(%rbp), %rax
movslq -0x14(%rbp), %rcx
movsbl 0x50(%rax,%rcx), %eax
cmpl $0x23, %eax
jne 0xc70c
jmp 0xc733
movq -0x20(%rbp), %rax
movq %rax, %rdi
addq $0x150, %rdi            # imm = 0x150
movslq -0x14(%rbp), %rcx
movsbl 0x50(%rax,%rcx), %esi
callq 0xccf0
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0xc6e6
movq -0x20(%rbp), %rdi
addq $0xd0, %rdi
leaq 0x3575(%rip), %rsi      # 0xfcba
callq 0x4360
cmpl $0x0, %eax
jne 0xc75a
movq -0x20(%rbp), %rdi
callq 0xc490
jmp 0xc78c
movq -0x20(%rbp), %rdi
addq $0xd0, %rdi
leaq 0x3515(%rip), %rsi      # 0xfc81
callq 0x4360
cmpl $0x0, %eax
jne 0xc781
movq -0x20(%rbp), %rdi
callq 0xc8d0
jmp 0xc78a
movq -0x20(%rbp), %rdi
callq 0xca00
jmp 0xc78c
movq -0x20(%rbp), %rax
movb $0x0, 0xd0(%rax)
movb $0x0, 0x50(%rax)
movl $0x0, 0x44(%rax)
jmp 0xc7c8
movq -0x20(%rbp), %rax
movl 0x44(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x44(%rax)
cmpl $0x708, 0x44(%rax)      # imm = 0x708
jne 0xc7c6
movl $0x1, -0x4(%rbp)
jmp 0xc8c1
jmp 0xc7c8
movq -0x20(%rbp), %rdi
addq $0x20, %rdi
callq 0xcd60
testb $0x1, %al
jne 0xc7e5
movl $0x1, -0x4(%rbp)
jmp 0xc8c1
movq -0x20(%rbp), %rdi
addq $0x150, %rdi            # imm = 0x150
callq 0xcd80
testb $0x1, %al
jne 0xc8ba
movq -0x20(%rbp), %rdi
addq $0x150, %rdi            # imm = 0x150
callq 0xcda0
movb %al, -0x15(%rbp)
movsbl -0x15(%rbp), %eax
addl $-0x41, %eax
movl %eax, %ecx
movq %rcx, -0x28(%rbp)
subl $0x36, %eax
ja 0xc8b8
movq -0x28(%rbp), %rax
leaq 0x3363(%rip), %rcx      # 0xfb94
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0xc, -0x4(%rbp)
jmp 0xc8c1
movl $0xa, -0x4(%rbp)
jmp 0xc8c1
movl $0x6, -0x4(%rbp)
jmp 0xc8c1
movl $0x8, -0x4(%rbp)
jmp 0xc8c1
movl $0xe, -0x4(%rbp)
jmp 0xc8c1
movl $0x4, -0x4(%rbp)
jmp 0xc8c1
movl $0x2, -0x4(%rbp)
jmp 0xc8c1
movl $0xd, -0x4(%rbp)
jmp 0xc8c1
movl $0xb, -0x4(%rbp)
jmp 0xc8c1
movl $0x7, -0x4(%rbp)
jmp 0xc8c1
movl $0x9, -0x4(%rbp)
jmp 0xc8c1
movl $0xf, -0x4(%rbp)
jmp 0xc8c1
movl $0x5, -0x4(%rbp)
jmp 0xc8c1
movl $0x3, -0x4(%rbp)
jmp 0xc8c1
jmp 0xc8ba
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopw (%rax,%rax)