// File: /workspace/github_c_makefile_star2_O0/kobolt[P]curses-collection/racer/racer.c
// Function: main
int main(int argc, char *argv[])
{
  int c;
  bool disable_color = false;
  bool disable_ascii = false;
  int ch;
  int player_speed = 5;
  int player_hpos = 25;
  int cars_passed = 0;
  int curve = 0;
  bool quit = false;
  car_t cars[CARS_MAX];

  srandom(time(NULL));

  /* Initialize: */
  for (int i = 0; i < CARS_MAX; i++) {
    cars[i].distance = 0;
    car_init(&cars[i], false);
  }

  while ((c = getopt(argc, argv, "hca")) != -1) {
    switch (c) {
    case 'h':
      display_help(argv[0]);
      return EXIT_SUCCESS;

    case 'c':
      disable_color = true;
      break;

    case 'a':
      disable_ascii = true;
      break;

    case '?':
    default:
      display_help(argv[0]);
      return EXIT_FAILURE;
    }
  }

  if (disable_color && disable_ascii) {
    fprintf(stdout, "Disabling both color and ASCII makes no sense!\n");
    return EXIT_FAILURE;
  }

  initscr();
  noecho();
  keypad(stdscr, TRUE);
  timeout(0);

  if (has_colors()) {
    if (! disable_color) {
      start_color();
    }
    init_pair(COL_BLACK,   COLOR_WHITE, COLOR_BLACK);
    init_pair(COL_RED,     COLOR_BLACK, COLOR_RED);
    init_pair(COL_GREEN,   COLOR_BLACK, COLOR_GREEN);
    init_pair(COL_YELLOW,  COLOR_BLACK, COLOR_YELLOW);
    init_pair(COL_BLUE,    COLOR_BLACK, COLOR_BLUE);
    init_pair(COL_MAGENTA, COLOR_BLACK, COLOR_MAGENTA);
    init_pair(COL_CYAN,    COLOR_BLACK, COLOR_CYAN);
    init_pair(COL_WHITE,   COLOR_BLACK, COLOR_WHITE);
  }

  while (! quit) {
    /* Player input: */
    ch = getch();
    switch (ch) {
    case KEY_LEFT:
    case 'a':
      if (player_hpos < HPOS_MAX) {
        player_hpos++;
      }
      break;

    case KEY_RIGHT:
    case 'd':
      if (player_hpos > HPOS_MIN) {
        player_hpos--;
      }
      break;

    case KEY_UP:
    case 'w':
      if (player_speed < SPEED_MAX) {
        player_speed++;
      }
      break;

    case KEY_DOWN:
    case 's':
      if (player_speed > SPEED_MIN) {
        player_speed--;
      }
      break;

    case 'q':
      quit = true;
      break;

    default:
      break;
    }

    /* Position update of other cars: */
    for (int i = 0; i < CARS_MAX; i++) {
      cars[i].distance += (cars[i].speed - player_speed);

      /* Re-spawn car if it goes out of range: */
      if (cars[i].distance > 6000) {
        car_init(&cars[i], true);
      } else if (cars[i].distance < -100) {
        car_init(&cars[i], true);
        cars_passed++;
      }

      car_row_update(&cars[i]);
    }

    /* Collision check: */
    for (int i = 0; i < CARS_MAX; i++) {
      if (car_collision(&cars[i], player_hpos)) {
        endwin();
        fprintf(stdout, "Crashed!\n");
        quit = true;
      }
    }

    /* Draw screen: */
    erase();
    draw_horizon(curve, player_speed, disable_ascii);
    draw_road(curve, player_speed, disable_ascii);
    for (int i = 0; i < CARS_MAX; i++) {
      draw_car(curve, cars[i].row, cars[i].hpos, false, disable_ascii);
    }
    draw_car(curve, 20, player_hpos, true, disable_ascii); /* The player */
    mvprintw(6, 0, "Score: %d", cars_passed);
    mvprintw(7, 0, "%dkm/h", player_speed * 10);
    refresh();

    usleep(20000);

    /* Trajectory and road change: */
    curve = curve_ahead(player_speed);
  }

  endwin();
  fprintf(stdout, "Final score: %d\n", cars_passed);
  return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb $0x0, -0x15(%rbp)
movb $0x0, -0x16(%rbp)
movl $0x5, -0x20(%rbp)
movl $0x19, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movb $0x0, -0x2d(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x1110
movl %eax, %edi
callq 0x1050
movl $0x0, -0x84(%rbp)
cmpl $0x5, -0x84(%rbp)
jge 0x1357
movslq -0x84(%rbp), %rcx
leaq -0x80(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
movss %xmm0, (%rax)
movslq -0x84(%rbp), %rax
leaq -0x80(%rbp), %rdi
shlq $0x4, %rax
addq %rax, %rdi
xorl %esi, %esi
callq 0x1890
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x130b
jmp 0x1359
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x2446(%rip), %rdx      # 0x37ad
callq 0x1140
movl %eax, -0x14(%rbp)
cmpl $-0x1, %eax
je 0x13ee
movl -0x14(%rbp), %eax
movl %eax, -0x94(%rbp)
subl $0x3f, %eax
je 0x13cf
jmp 0x1384
movl -0x94(%rbp), %eax
subl $0x61, %eax
je 0x13c9
jmp 0x1391
movl -0x94(%rbp), %eax
subl $0x63, %eax
je 0x13c3
jmp 0x139e
movl -0x94(%rbp), %eax
subl $0x68, %eax
jne 0x13d1
jmp 0x13ab
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x1950
movl $0x0, -0x4(%rbp)
jmp 0x187b
movb $0x1, -0x15(%rbp)
jmp 0x13e9
movb $0x1, -0x16(%rbp)
jmp 0x13e9
jmp 0x13d1
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x1950
movl $0x1, -0x4(%rbp)
jmp 0x187b
jmp 0x1359
testb $0x1, -0x15(%rbp)
je 0x141e
testb $0x1, -0x16(%rbp)
je 0x141e
movq 0x3bd7(%rip), %rax      # 0x4fd8
movq (%rax), %rdi
leaq 0x23a6(%rip), %rsi      # 0x37b1
movb $0x0, %al
callq 0x10d0
movl $0x1, -0x4(%rbp)
jmp 0x187b
callq 0x1080
callq 0x1070
movq 0x3bb1(%rip), %rax      # 0x4fe0
movq (%rax), %rdi
movl $0x1, %esi
callq 0x10b0
movq 0x3b9d(%rip), %rax      # 0x4fe0
movq (%rax), %rdi
xorl %esi, %esi
callq 0x1100
callq 0x1030
testb $0x1, %al
jne 0x145b
jmp 0x14ee
testb $0x1, -0x15(%rbp)
jne 0x1466
callq 0x10a0
movl $0x1, %edi
movl $0x7, %esi
xorl %edx, %edx
callq 0x1130
movl $0x2, %edi
xorl %esi, %esi
movl $0x1, %edx
callq 0x1130
movl $0x3, %edi
xorl %esi, %esi
movl $0x2, %edx
callq 0x1130
movl $0x4, %edi
xorl %esi, %esi
movl $0x3, %edx
callq 0x1130
movl $0x5, %edi
xorl %esi, %esi
movl $0x4, %edx
callq 0x1130
movl $0x6, %edi
xorl %esi, %esi
movl $0x5, %edx
callq 0x1130
movl $0x7, %edi
xorl %esi, %esi
movl $0x6, %edx
callq 0x1130
movl $0x8, %edi
xorl %esi, %esi
movl $0x7, %edx
callq 0x1130
jmp 0x14f0
movb -0x2d(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x14fe
jmp 0x1854
movq 0x3adb(%rip), %rax      # 0x4fe0
movq (%rax), %rdi
callq 0x1060
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x98(%rbp)
subl $0x61, %eax
je 0x1594
jmp 0x1520
movl -0x98(%rbp), %eax
subl $0x64, %eax
je 0x15a5
jmp 0x152d
movl -0x98(%rbp), %eax
subl $0x71, %eax
je 0x15d8
jmp 0x153e
movl -0x98(%rbp), %eax
subl $0x73, %eax
je 0x15c7
jmp 0x154b
movl -0x98(%rbp), %eax
subl $0x77, %eax
je 0x15b6
jmp 0x1558
movl -0x98(%rbp), %eax
subl $0x102, %eax            # imm = 0x102
je 0x15c7
jmp 0x1567
movl -0x98(%rbp), %eax
subl $0x103, %eax            # imm = 0x103
je 0x15b6
jmp 0x1576
movl -0x98(%rbp), %eax
subl $0x104, %eax            # imm = 0x104
je 0x1594
jmp 0x1585
movl -0x98(%rbp), %eax
subl $0x105, %eax            # imm = 0x105
je 0x15a5
jmp 0x15de
cmpl $0x23, -0x24(%rbp)
jge 0x15a3
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x15e0
cmpl $-0x23, -0x24(%rbp)
jle 0x15b4
movl -0x24(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x15e0
cmpl $0x1e, -0x20(%rbp)
jge 0x15c5
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x15e0
cmpl $0x0, -0x20(%rbp)
jle 0x15d6
movl -0x20(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x15e0
movb $0x1, -0x2d(%rbp)
jmp 0x15e0
jmp 0x15e0
movl $0x0, -0x88(%rbp)
cmpl $0x5, -0x88(%rbp)
jge 0x16df
movslq -0x88(%rbp), %rcx
leaq -0x80(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
subl -0x20(%rbp), %eax
cvtsi2ss %eax, %xmm0
movslq -0x88(%rbp), %rcx
leaq -0x80(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
addss (%rax), %xmm0
movss %xmm0, (%rax)
movslq -0x88(%rbp), %rcx
leaq -0x80(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movss (%rax), %xmm0
movss 0x19b9(%rip), %xmm1     # 0x3004
ucomiss %xmm1, %xmm0
jbe 0x166e
movslq -0x88(%rbp), %rax
leaq -0x80(%rbp), %rdi
shlq $0x4, %rax
addq %rax, %rdi
movl $0x1, %esi
callq 0x1890
jmp 0x16b4
movslq -0x88(%rbp), %rcx
leaq -0x80(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movss 0x1980(%rip), %xmm0     # 0x3008
ucomiss (%rax), %xmm0
jbe 0x16b2
movslq -0x88(%rbp), %rax
leaq -0x80(%rbp), %rdi
shlq $0x4, %rax
addq %rax, %rdi
movl $0x1, %esi
callq 0x1890
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x16b4
movslq -0x88(%rbp), %rax
leaq -0x80(%rbp), %rdi
shlq $0x4, %rax
addq %rax, %rdi
callq 0x19a0
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x15ea
movl $0x0, -0x8c(%rbp)
cmpl $0x5, -0x8c(%rbp)
jge 0x1746
movslq -0x8c(%rbp), %rax
leaq -0x80(%rbp), %rdi
shlq $0x4, %rax
addq %rax, %rdi
movl -0x24(%rbp), %esi
callq 0x1cb0
testb $0x1, %al
jne 0x1712
jmp 0x1733
callq 0x1170
movq 0x38ba(%rip), %rax      # 0x4fd8
movq (%rax), %rdi
leaq 0x20b9(%rip), %rsi      # 0x37e1
movb $0x0, %al
callq 0x10d0
movb $0x1, -0x2d(%rbp)
jmp 0x1735
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x16e9
movq 0x3893(%rip), %rax      # 0x4fe0
movq (%rax), %rdi
callq 0x1040
movl -0x2c(%rbp), %edi
movl -0x20(%rbp), %esi
movb -0x16(%rbp), %al
andb $0x1, %al
movzbl %al, %edx
callq 0x1d20
movl -0x2c(%rbp), %edi
movl -0x20(%rbp), %esi
movb -0x16(%rbp), %al
andb $0x1, %al
movzbl %al, %edx
callq 0x1fb0
movl $0x0, -0x90(%rbp)
cmpl $0x5, -0x90(%rbp)
jge 0x17dc
movl -0x2c(%rbp), %edi
movslq -0x90(%rbp), %rcx
leaq -0x80(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %esi
movslq -0x90(%rbp), %rcx
leaq -0x80(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movl 0xc(%rax), %edx
movb -0x16(%rbp), %al
xorl %ecx, %ecx
andb $0x1, %al
movzbl %al, %r8d
callq 0x2320
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x1785
movl -0x2c(%rbp), %edi
movl -0x24(%rbp), %edx
movb -0x16(%rbp), %al
movl $0x14, %esi
movl $0x1, %ecx
andb $0x1, %al
movzbl %al, %r8d
callq 0x2320
movl -0x28(%rbp), %ecx
movl $0x6, %edi
xorl %esi, %esi
leaq 0x1fe0(%rip), %rdx      # 0x37eb
movb $0x0, %al
callq 0x10f0
imull $0xa, -0x20(%rbp), %ecx
movl $0x7, %edi
xorl %esi, %esi
leaq 0x1fd1(%rip), %rdx      # 0x37f5
movb $0x0, %al
callq 0x10f0
movq 0x37ae(%rip), %rax      # 0x4fe0
movq (%rax), %rdi
callq 0x1090
movl $0x4e20, %edi           # imm = 0x4E20
callq 0x11a0
movl -0x20(%rbp), %edi
callq 0x2d70
movl %eax, -0x2c(%rbp)
jmp 0x14f0
callq 0x1170
movq 0x3778(%rip), %rax      # 0x4fd8
movq (%rax), %rdi
movl -0x28(%rbp), %edx
leaq 0x1f8f(%rip), %rsi      # 0x37fc
movb $0x0, %al
callq 0x10d0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nopw (%rax,%rax)