// File: /workspace/github_c_makefile_star2_O3/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 0x21b(%rip), %rdi       # 0x14c1
callq 0x1720
callq 0x1080
movq 0x2d29(%rip), %r14      # 0x3fe0
movq (%r14), %rdi
movl $0x1, %esi
callq 0x10c0
movq (%r14), %rdi
xorl %esi, %esi
callq 0x10e0
movq (%r14), %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
movl $0x3, %edi
xorl %esi, %esi
callq 0x150e
movl $0x3, %edi
xorl %esi, %esi
callq 0x150e
movl $0x3, %edi
xorl %esi, %esi
callq 0x150e
movl $0x0, 0x2d85(%rip)      # 0x40e0
movdqa (%rsp), %xmm0
movd %xmm0, 0x2d7c(%rip)     # 0x40e4
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, 0x2d73(%rip)     # 0x40e8
callq 0x1592
callq 0x162c
leaq 0xcaa(%rip), %rbp       # 0x2030
leaq 0xc77(%rip), %r15       # 0x2004
movl $0x2, %r12d
movl $0x3, %r13d
movl $0x3, %ebx
movq (%r14), %rdi
callq 0x1070
cmpl $0x70, %eax
jle 0x13cb
leal -0x102(%rax), %ecx
cmpl $0x3, %ecx
jbe 0x13e3
cmpl $0x19a, %eax            # imm = 0x19A
je 0x14a4
cmpl $0x71, %eax
jne 0x139e
jmp 0x14ba
cmpl $-0x1, %eax
je 0x13f9
cmpl $0x1b, %eax
je 0x14ba
cmpl $0x51, %eax
jne 0x139e
jmp 0x14ba
movslq (%r15,%rcx,4), %rax
addq %r15, %rax
jmpq *%rax
testl %r13d, %r13d
movl $0x1, %eax
cmovnel %eax, %ebx
jmp 0x139e
movslq %ebx, %rax
leaq 0x29ad(%rip), %rcx      # 0x3db0
movq (%rcx,%rax,8), %rcx
movl (%rbp,%rax,4), %eax
addl %eax, (%rcx)
movq (%r14), %rdi
movl 0x2cce(%rip), %esi      # 0x40e4
movl 0x2ccc(%rip), %edx      # 0x40e8
callq 0x1130
cmpl $-0x1, %eax
je 0x14b5
movq (%r14), %rdi
callq 0x1110
cmpl $0x40, %eax
je 0x1479
cmpl $0x20, %eax
jne 0x14b5
movl %ebx, %edi
movl $0x1, %esi
callq 0x150e
jmp 0x148d
cmpl $0x3, %r13d
cmovnel %r12d, %ebx
jmp 0x139e
cmpl $0x2, %r13d
movl $0x3, %eax
cmovnel %eax, %ebx
jmp 0x139e
cmpl $0x1, %r13d
movl $0x0, %eax
cmovnel %eax, %ebx
jmp 0x139e
movl %ebx, %edi
xorl %esi, %esi
callq 0x150e
incl 0x2c58(%rip)            # 0x40e0
callq 0x1592
callq 0x162c
movl $0x124f8, %edi          # imm = 0x124F8
callq 0x1180
movl %ebx, %r13d
jmp 0x139e
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