// File: /workspace/github_c_makefile_star2_O2/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
subq $0x18, %rsp
callq 0x10a0
leaq 0x1fc(%rip), %rdi       # 0x14a2
callq 0x16e0
callq 0x1080
movq 0x2d29(%rip), %r15      # 0x3fe0
movq (%r15), %rdi
pushq $0x1
popq %rbx
movl %ebx, %esi
callq 0x10c0
movq (%r15), %rdi
xorl %esi, %esi
callq 0x10e0
movq (%r15), %rax
testq %rax, %rax
je 0x1316
movd 0x4(%rax), %xmm0
pshuflw $0x60, %xmm0, %xmm1     # xmm1 = xmm0[0,0,2,1,4,5,6,7]
psrad $0x10, %xmm1
pcmpeqd %xmm2, %xmm2
psubd %xmm2, %xmm1
psrld $0x1f, %xmm1
packssdw %xmm1, %xmm1
movdqa %xmm0, %xmm2
por %xmm1, %xmm2
pxor %xmm0, %xmm1
psraw $0x1, %xmm1
psubw %xmm1, %xmm2
punpcklwd %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1],xmm0[2],xmm2[2],xmm0[3],xmm2[3]
psrad $0x10, %xmm0
jmp 0x131a
pxor %xmm0, %xmm0
movdqa %xmm0, (%rsp)
xorl %edi, %edi
callq 0x10f0
movl %eax, %edi
callq 0x1050
pushq $0x3
popq %r12
movl %r12d, %edi
xorl %esi, %esi
callq 0x14df
movl %r12d, %edi
xorl %esi, %esi
callq 0x14df
movl %r12d, %edi
xorl %esi, %esi
callq 0x14df
andl $0x0, 0x2d8a(%rip)      # 0x40e0
movdqa (%rsp), %xmm0
movd %xmm0, 0x2d81(%rip)     # 0x40e4
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, 0x2d78(%rip)     # 0x40e8
callq 0x1559
callq 0x15e7
leaq 0xc83(%rip), %r13       # 0x2004
movl %r12d, %ebp
movl %r12d, %r14d
movq (%r15), %rdi
callq 0x1070
leal -0x102(%rax), %ecx
cmpl $0x3, %ecx
jbe 0x13c6
cmpl $-0x1, %eax
je 0x13d8
cmpl $0x19a, %eax            # imm = 0x19A
je 0x1485
cmpl $0x1b, %eax
je 0x149b
cmpl $0x51, %eax
je 0x149b
cmpl $0x71, %eax
jne 0x1387
jmp 0x149b
movslq (%r13,%rcx,4), %rax
addq %r13, %rax
jmpq *%rax
testl %ebp, %ebp
cmovnel %ebx, %r14d
jmp 0x1387
movslq %r14d, %rax
leaq 0x29ce(%rip), %rcx      # 0x3db0
movq (%rcx,%rax,8), %rcx
leaq 0xc43(%rip), %rdx       # 0x2030
movl (%rdx,%rax,4), %eax
addl %eax, (%rcx)
movq (%r15), %rdi
movl 0x2ce9(%rip), %esi      # 0x40e4
movl 0x2ce7(%rip), %edx      # 0x40e8
callq 0x1130
cmpl $-0x1, %eax
je 0x1496
movq (%r15), %rdi
callq 0x1110
cmpl $0x40, %eax
je 0x1459
cmpl $0x20, %eax
jne 0x1496
movl %r14d, %edi
movl %ebx, %esi
callq 0x14df
jmp 0x146e
cmpl $0x3, %ebp
pushq $0x2
popq %rax
cmovnel %eax, %r14d
jmp 0x1387
cmpl $0x2, %ebp
cmovnel %r12d, %r14d
jmp 0x1387
cmpl $0x1, %ebp
movl $0x0, %eax
cmovnel %eax, %r14d
jmp 0x1387
movl %r14d, %edi
xorl %esi, %esi
callq 0x14df
incl 0x2c77(%rip)            # 0x40e0
callq 0x1559
callq 0x15e7
movl $0x124f8, %edi          # imm = 0x124F8
callq 0x1180
movl %r14d, %ebp
jmp 0x1387
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x10d0
xorl %edi, %edi
callq 0x1160