// File: /workspace/github_cpp_makefile_star2_O3/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 %rbx
movq %rdi, %rbx
addq $0x20, %rdi
callq 0x74c0
testb %al, %al
je 0x8b31
movq %rbx, %rdi
callq 0x8708
leaq 0x50(%rbx), %rax
movzbl (%rax), %ecx
testl %ecx, %ecx
je 0x8b57
cmpl $0x23, %ecx
je 0x8b57
cmpl $0x20, 0x150(%rbx)
je 0x8b2c
movslq 0x158(%rbx), %rdx
leal 0x1(%rdx), %esi
movl %esi, 0x158(%rbx)
movb %cl, 0x15c(%rbx,%rdx)
cmpl $0x20, 0x158(%rbx)
jne 0x8b26
movl $0x0, 0x158(%rbx)
incl 0x150(%rbx)
incq %rax
jmp 0x8ae7
movl 0x20(%rbx), %ecx
movl 0x44(%rbx), %edx
incl %edx
movl %edx, 0x44(%rbx)
movl $0x1, %eax
cmpl $0x708, %edx            # imm = 0x708
je 0x8c1e
cmpl $-0x1, %ecx
jne 0x8bb9
jmp 0x8c1e
cmpw $0x2f, 0xd0(%rbx)
jne 0x8b6b
movq %rbx, %rdi
callq 0x8880
jmp 0x8ba1
movabsq $0x672e6567616d692f, %rax # imm = 0x672E6567616D692F
xorq 0xd0(%rbx), %rax
movabsq $0x6669672e656761, %rcx # imm = 0x6669672E656761
xorq 0xd3(%rbx), %rcx
movq %rbx, %rdi
orq %rax, %rcx
je 0x8b9c
callq 0x8ed8
jmp 0x8ba1
callq 0x8cae
xorl %eax, %eax
movb %al, 0xd0(%rbx)
movb %al, 0x50(%rbx)
movl $0x0, 0x44(%rbx)
cmpl $-0x1, 0x20(%rbx)
je 0x8c19
movl 0x150(%rbx), %edx
testl %edx, %edx
je 0x8c15
movslq 0x154(%rbx), %rax
leal 0x1(%rax), %esi
movl %esi, 0x154(%rbx)
movzbl 0x15c(%rbx,%rax), %ecx
xorl %eax, %eax
cmpl $0x20, %esi
cmovel %eax, %esi
movl %esi, 0x154(%rbx)
decl %edx
movl %edx, 0x150(%rbx)
cmpl $0x60, %ecx
jg 0x8c20
leal -0x41(%rcx), %edx
cmpl $0x5, %edx
ja 0x8c3f
leaq 0x3127(%rip), %rcx      # 0xbd2c
movslq (%rcx,%rdx,4), %rdx
addq %rcx, %rdx
jmpq *%rdx
movl $0x7, %eax
jmp 0x8c1e
xorl %eax, %eax
jmp 0x8c1e
movl $0x1, %eax
popq %rbx
retq
leal -0x61(%rcx), %edx
cmpl $0x5, %edx
ja 0x8c50
leaq 0x3115(%rip), %rcx      # 0xbd44
movslq (%rcx,%rdx,4), %rdx
addq %rcx, %rdx
jmpq *%rdx
movl $0x6, %eax
jmp 0x8c1e
cmpl $0x53, %ecx
je 0x8c99
cmpl $0x57, %ecx
jne 0x8c1e
movl $0xb, %eax
jmp 0x8c1e
cmpl $0x73, %ecx
je 0x8ca3
cmpl $0x77, %ecx
jne 0x8c1e
movl $0xa, %eax
jmp 0x8c1e
movl $0x5, %eax
jmp 0x8c1e
movl $0x4, %eax
jmp 0x8c1e
movl $0x3, %eax
jmp 0x8c1e
movl $0x2, %eax
jmp 0x8c1e
movl $0x9, %eax
jmp 0x8c1e
movl $0x8, %eax
jmp 0x8c1e
movl $0xf, %eax
jmp 0x8c1e
movl $0xe, %eax
jmp 0x8c1e
movl $0xd, %eax
jmp 0x8c1e
movl $0xc, %eax
jmp 0x8c1e
nop