// File: /workspace/github_c_makefile_star2_O2/skeeto[P]flappy/flappy.cc
// Function: main
int main(int argc, char **argv) {
  srand(std::time(NULL));

  /* Parse command line arguments. */
  int opt;
  const char *filename = "/tmp/flappy-scores.db", *host = "localhost";
  while ((opt = getopt(argc, argv, "d:h:p")) != -1) {
    switch (opt) {
      case 'd':
        filename = optarg;
        break;
      case 'h':
        host = optarg;
        break;
      case 'p':  // ignore
        break;
    }
  }

  Display display;
  HighScores scores{filename, display.height - 1};

  while (true) {
    Game game{&display};

    int score = game.run();
    if (score < 0) {
      return 0;  // game quit early
    }

    /* Game over */
    mvprintw(display.height + 1, 0, "Game over!");
    print_scores(display, scores);

    /* Enter new high score */
    if (scores.is_best(score)) {
      attron(A_BOLD);
      mvprintw(display.height + 2, 0, "You have a high score!");
      mvprintw(display.height + 3, 0, "Enter name: ");
      attroff(A_BOLD);
      char name[23] = {0};
      if (!display.read_name(display.height + 3, 12, name, sizeof(name))) {
        return 0;
      }
      if (std::strlen(name) == 0) {
        std::strcpy(name, "(anonymous)");
      }
      scores.insert_score(name, score);
      move(display.height + 3, 0);
      clrtoeol();
      print_scores(display, scores);
    }

    /* Handle quit/restart */
    mvprintw(display.height + 2, 0, "Press 'q' to quit, 'r' to retry.");
    int c;
    while ((c = display.block_getch()) != 'r') {
      if (is_exit(c) || c == ERR) {
        return 0;
      }
    }
  }
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rsi, %r14
movl %edi, %ebp
xorl %edi, %edi
callq 0x7270
movl %eax, %edi
callq 0x72a0
leaq 0x57926(%rip), %rbx     # 0x5f042
leaq 0x57935(%rip), %r15     # 0x5f058
movq 0x788a6(%rip), %r12     # 0x7ffd0
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x7080
cmpl $-0x1, %eax
je 0x7747
cmpl $0x64, %eax
jne 0x772a
movq (%r12), %rbx
jmp 0x772a
leaq 0x8(%rsp), %r14
pushq $0x28
popq %rsi
pushq $0x14
popq %rdx
movq %r14, %rdi
callq 0x797c
movl (%r14), %edx
decl %edx
leaq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x8a50
leaq 0x68(%rsp), %r14
leaq 0x8(%rsp), %r15
leaq 0x30(%rsp), %r13
movq 0x7885e(%rip), %rbp     # 0x7ffe0
leaq 0x57910(%rip), %rbx     # 0x5f099
movq %r14, %rdi
movq %r15, %rsi
callq 0x79ce
movq %r14, %rdi
callq 0x79fe
movl %eax, %r12d
testl %eax, %eax
js 0x78eb
movl 0x8(%rsp), %edi
incl %edi
xorl %esi, %esi
leaq 0x578a8(%rip), %rdx     # 0x5f05e
xorl %eax, %eax
callq 0x73b0
movq %r15, %rdi
movq %r13, %rsi
callq 0x760b
movq %r13, %rdi
movl %r12d, %esi
callq 0x8b4a
testb %al, %al
je 0x78aa
movq (%rbp), %rdi
movl $0x200000, %esi         # imm = 0x200000
xorl %edx, %edx
callq 0x7170
movl 0x8(%rsp), %edi
addl $0x2, %edi
xorl %esi, %esi
leaq 0x5786e(%rip), %rdx     # 0x5f069
xorl %eax, %eax
callq 0x73b0
movl 0x8(%rsp), %edi
addl $0x3, %edi
xorl %esi, %esi
leaq 0x5786e(%rip), %rdx     # 0x5f080
xorl %eax, %eax
callq 0x73b0
movq (%rbp), %rdi
movl $0x200000, %esi         # imm = 0x200000
xorl %edx, %edx
callq 0x7390
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x1f(%rsp)
movl 0x8(%rsp), %esi
addl $0x3, %esi
movq %r15, %rdi
pushq $0xc
popq %rdx
leaq 0x10(%rsp), %rcx
pushq $0x17
popq %r8
callq 0x7bd0
testb %al, %al
je 0x78eb
cmpb $0x0, 0x10(%rsp)
jne 0x7878
movabsq $0x6f6d796e6f6e6128, %rax # imm = 0x6F6D796E6F6E6128
movq %rax, 0x10(%rsp)
movl $0x297375, 0x18(%rsp)   # imm = 0x297375
movq %r13, %rdi
leaq 0x10(%rsp), %rsi
movl %r12d, %edx
callq 0x8b8a
movl 0x8(%rsp), %edi
addl $0x3, %edi
xorl %esi, %esi
callq 0x71b0
movq (%rbp), %rdi
callq 0x72f0
movq %r15, %rdi
movq %r13, %rsi
callq 0x760b
movl 0x8(%rsp), %edi
addl $0x2, %edi
xorl %esi, %esi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x73b0
movq %r15, %rdi
callq 0x7dae
cmpl $-0x1, %eax
je 0x78eb
cmpl $0x3, %eax
je 0x78eb
cmpl $0x71, %eax
je 0x78eb
cmpl $0x72, %eax
jne 0x78bd
leaq 0x88(%rsp), %rdi
callq 0x8406
jmp 0x7789
leaq 0x88(%rsp), %rdi
callq 0x8406
leaq 0x30(%rsp), %rdi
callq 0x8b30
leaq 0x8(%rsp), %rdi
callq 0x7de8
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x7948
movq %rax, %rbx
jmp 0x793e
jmp 0x792e
jmp 0x792e
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x8406
leaq 0x30(%rsp), %rdi
callq 0x8b30
leaq 0x8(%rsp), %rdi
callq 0x7de8
movq %rbx, %rdi
callq 0x74e0