// File: /workspace/github_c_makefile_star2_O1/kobolt[P]curses-collection/snake/snake.c
// Function: main
int main(void)
{
  int c, maxy, maxx;
  direction_t current_direction, original_direction;

  /* Initialize curses. */
  initscr();
  atexit(exit_handler);
  noecho();
  keypad(stdscr, TRUE);
  timeout(0); /* Non-blocking mode */

  getmaxyx(stdscr, maxy, maxx);
  srandom(time(NULL));

  /* Set initial variables. */
  current_direction = DIRECTION_RIGHT;
  queue_insert(DIRECTION_RIGHT, 0);
  queue_insert(DIRECTION_RIGHT, 0);
  queue_insert(DIRECTION_RIGHT, 0);
  score = 0;
  pos_y = maxy / 2;
  pos_x = maxx / 2;
  place_apple();

  update_screen();

  while (1) {

    /* Read character buffer until empty. */
    original_direction = current_direction;
    while ((c = getch()) != ERR) {
      switch (c) {
      case KEY_RESIZE:
        return 0; /* Not allowed! */
      case KEY_UP:
        if (original_direction != DIRECTION_DOWN)
          current_direction = DIRECTION_UP;
        break;
      case KEY_DOWN:
        if (original_direction != DIRECTION_UP)
          current_direction = DIRECTION_DOWN;
        break;
      case KEY_LEFT:
        if (original_direction != DIRECTION_RIGHT)
          current_direction = DIRECTION_LEFT;
        break;
      case KEY_RIGHT:
        if (original_direction != DIRECTION_LEFT)
          current_direction = DIRECTION_RIGHT;
        break;
      case 'q':
      case 'Q':
      case '\e':
        exit(0);
      }
    }

    /* Move player. */
    switch (current_direction) {
    case DIRECTION_UP:
      pos_y--;
      break;
    case DIRECTION_DOWN:
      pos_y++;
      break;
    case DIRECTION_LEFT:
      pos_x--;
      break;
    case DIRECTION_RIGHT:
      pos_x++;
      break;
    }

    /* Check for collisions. */
    c = mvinch(pos_y, pos_x);
    if (c == ' ') { /* Normal progression. */
      queue_insert(current_direction, 1);
    } else if (c == '@') { /* Expand. */
      queue_insert(current_direction, 0);
      score++;
      place_apple();
    } else { /* Died. */
      flash();
      exit(0);
    }

    /* Redraw screen. */
    update_screen();
    usleep(75000);
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
callq 0x10a0
leaq 0x1fd(%rip), %rdi       # 0x14a0
callq 0x1720
callq 0x1080
movq 0x2d2c(%rip), %r14      # 0x3fe0
movq (%r14), %rdi
movl $0x1, %esi
callq 0x10c0
movq (%r14), %rdi
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x10e0
movq (%r14), %rax
movl $0x0, %ebp
testq %rax, %rax
je 0x12ee
movswl 0x4(%rax), %ecx
movl %ecx, %edx
incl %edx
shrl $0x1f, %edx
addl %edx, %ecx
incl %ecx
shrl %ecx
movswl %cx, %ebp
testq %rax, %rax
je 0x1307
movswl 0x6(%rax), %eax
movl %eax, %ecx
incl %ecx
shrl $0x1f, %ecx
addl %ecx, %eax
incl %eax
shrl %eax
movswl %ax, %ebx
xorl %edi, %edi
callq 0x10f0
movl %eax, %edi
callq 0x1050
movl $0x3, %edi
xorl %esi, %esi
callq 0x14ed
movl $0x3, %edi
xorl %esi, %esi
callq 0x14ed
movl $0x3, %edi
xorl %esi, %esi
callq 0x14ed
movl $0x0, 0x2d9d(%rip)      # 0x40e0
movl %ebp, 0x2d9b(%rip)      # 0x40e4
movl %ebx, 0x2d99(%rip)      # 0x40e8
callq 0x1581
callq 0x1623
movl $0x1, %r15d
leaq 0xc9e(%rip), %rbp       # 0x2004
movl $0x2, %r12d
movl $0x3, %r13d
movl $0x3, %ebx
movq (%r14), %rdi
callq 0x1070
cmpl $0x70, %eax
jle 0x13a4
leal -0x102(%rax), %ecx
cmpl $0x3, %ecx
jbe 0x13bc
cmpl $0x19a, %eax            # imm = 0x19A
je 0x1483
cmpl $0x71, %eax
jne 0x1377
jmp 0x1499
cmpl $-0x1, %eax
je 0x13cf
cmpl $0x1b, %eax
je 0x1499
cmpl $0x51, %eax
jne 0x1377
jmp 0x1499
movslq (%rbp,%rcx,4), %rax
addq %rbp, %rax
jmpq *%rax
testl %r13d, %r13d
cmovnel %r15d, %ebx
jmp 0x1377
movslq %ebx, %rax
leaq 0x29d7(%rip), %rcx      # 0x3db0
movq (%rcx,%rax,8), %rcx
leaq 0xc4c(%rip), %rdx       # 0x2030
movl (%rdx,%rax,4), %eax
addl %eax, (%rcx)
movq (%r14), %rdi
movl 0x2cf2(%rip), %esi      # 0x40e4
movl 0x2cf0(%rip), %edx      # 0x40e8
callq 0x1130
cmpl $-0x1, %eax
je 0x143b
movq (%r14), %rdi
callq 0x1110
jmp 0x1440
cmpl $0x3, %r13d
cmovnel %r12d, %ebx
jmp 0x1377
cmpl $0x2, %r13d
movl $0x3, %eax
cmovnel %eax, %ebx
jmp 0x1377
cmpl $0x1, %r13d
movl $0x0, %eax
cmovnel %eax, %ebx
jmp 0x1377
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $0x40, %eax
je 0x1458
cmpl $0x20, %eax
jne 0x1494
movl %ebx, %edi
movl $0x1, %esi
callq 0x14ed
jmp 0x146c
movl %ebx, %edi
xorl %esi, %esi
callq 0x14ed
incl 0x2c79(%rip)            # 0x40e0
callq 0x1581
callq 0x1623
movl $0x124f8, %edi          # imm = 0x124F8
callq 0x1180
movl %ebx, %r13d
jmp 0x1377
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x10d0
xorl %edi, %edi
callq 0x1160