// File: /workspace/github_c_makefile_star2_O3/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, %rbx
movl %edi, %ebp
xorl %edi, %edi
callq 0x7260
movl %eax, %edi
callq 0x7290
leaq 0x65921(%rip), %r15     # 0x6d042
leaq 0x65930(%rip), %r14     # 0x6d058
movq 0x828a1(%rip), %r12     # 0x89fd0
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x7080
cmpl $-0x1, %eax
je 0x774c
cmpl $0x64, %eax
jne 0x772f
movq (%r12), %r15
jmp 0x772f
leaq 0x8(%rsp), %r14
movq %r14, %rdi
movl $0x28, %esi
movl $0x14, %edx
callq 0x7a04
movl (%r14), %edx
decl %edx
leaq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0x8ab8
leaq 0x88(%rsp), %rbx
movsd 0x65883(%rip), %xmm1    # 0x6d008
movabsq $-0x4016666666666666, %rax # imm = 0xBFE999999999999A
leaq 0x30(%rsp), %r13
movq 0x82845(%rip), %rbp     # 0x89fe0
movq %r14, 0x68(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0x8(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movq %rax, %r12
movq %rax, 0x78(%rsp)
movq %r14, 0x80(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8006
leaq 0x68(%rsp), %rdi
callq 0x7a58
movl %eax, %r15d
testl %eax, %eax
js 0x7941
movl 0x8(%rsp), %edi
incl %edi
xorl %esi, %esi
leaq 0x6586c(%rip), %rdx     # 0x6d05e
xorl %eax, %eax
callq 0x7390
movq %r14, %rdi
movq %r13, %rsi
callq 0x760b
movq %r13, %rdi
movl %r15d, %esi
callq 0x8bc0
testb %al, %al
je 0x78ef
movq (%rbp), %rdi
movl $0x200000, %esi         # imm = 0x200000
xorl %edx, %edx
callq 0x7160
movl 0x8(%rsp), %edi
addl $0x2, %edi
xorl %esi, %esi
leaq 0x65832(%rip), %rdx     # 0x6d069
xorl %eax, %eax
callq 0x7390
movl 0x8(%rsp), %edi
addl $0x3, %edi
xorl %esi, %esi
leaq 0x65832(%rip), %rdx     # 0x6d080
xorl %eax, %eax
callq 0x7390
movq (%rbp), %rdi
movl $0x200000, %esi         # imm = 0x200000
xorl %edx, %edx
callq 0x7380
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
movq $0x0, 0x1f(%rsp)
movl 0x8(%rsp), %esi
addl $0x3, %esi
movl $0x17, %r8d
movq %r14, %rdi
movl $0xc, %edx
leaq 0x10(%rsp), %rcx
callq 0x7c7a
testb %al, %al
je 0x7941
cmpb $0x0, 0x10(%rsp)
jne 0x78bd
movabsq $0x6f6d796e6f6e6128, %rax # imm = 0x6F6D796E6F6E6128
movq %rax, 0x10(%rsp)
movl $0x297375, 0x18(%rsp)   # imm = 0x297375
movq %r13, %rdi
leaq 0x10(%rsp), %rsi
movl %r15d, %edx
callq 0x8c02
movl 0x8(%rsp), %edi
addl $0x3, %edi
xorl %esi, %esi
callq 0x71a0
movq (%rbp), %rdi
callq 0x72e0
movq %r14, %rdi
movq %r13, %rsi
callq 0x760b
movl 0x8(%rsp), %edi
addl $0x2, %edi
xorl %esi, %esi
leaq 0x6579a(%rip), %rdx     # 0x6d099
xorl %eax, %eax
callq 0x7390
movq %r14, %rdi
callq 0x7e66
cmpl $0x70, %eax
jg 0x791f
cmpl $-0x1, %eax
je 0x7941
cmpl $0x3, %eax
jne 0x7906
jmp 0x7941
cmpl $0x71, %eax
je 0x7941
cmpl $0x72, %eax
jne 0x7906
movq %rbx, %rdi
callq 0x8466
movsd 0x656cf(%rip), %xmm1    # 0x6d008
movq %r12, %rax
jmp 0x779b
movq %rbx, %rdi
callq 0x8466
leaq 0x30(%rsp), %rdi
callq 0x8ba6
leaq 0x8(%rsp), %rdi
callq 0x7ea2
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
jmp 0x7994
movq %rax, %r14
jmp 0x798a
jmp 0x797f
jmp 0x797f
movq %rax, %r14
movq %rbx, %rdi
callq 0x8466
leaq 0x30(%rsp), %rdi
callq 0x8ba6
leaq 0x8(%rsp), %rdi
callq 0x7ea2
movq %r14, %rdi
callq 0x74b0