// File: /workspace/github_c_makefile_star2_O2/kobolt[P]curses-collection/invaders/game.c
// Function: game_handle_invaders
int game_handle_invaders(long cycle)
{
  int alive, i, j, k, x, y, shield_x;

  alive = game_invaders_alive();
  if (alive == 0)
    return 0;
  if (cycle % alive != 0)
    return 0;

  if (game_state.invaders_moving_right) {
    game_state.invaders_pos_x++;
    if (game_state.invaders_pos_x > (GAME_MAP_WIDTH - 68)) { /* (10*5)+(9*2) */
      game_state.invaders_pos_x--;
      game_state.invaders_pos_y++;
      game_state.invaders_moving_right = 0;
    }
  } else {
    game_state.invaders_pos_x--;
    if (game_state.invaders_pos_x < 0) {
      game_state.invaders_pos_x = 0;
      game_state.invaders_pos_y++;
      game_state.invaders_moving_right = 1;
    }
  }

  /* Check if invaders have reached the player or shields. */
  for (i = 0; i < GAME_INVADERS; i++) {
    if (game_state.invader_alive[i]) {
      y = game_state.invaders_pos_y + ((i / 10) * 4) + 2;

      if (y == (GAME_MAP_HEIGHT - 4)) {
        x = game_state.invaders_pos_x + ((i % 10) * 7);
        for (j = 0; j < (GAME_SHIELDS / 2); j++) {
          for (k = 0; k < GAME_SHIELD_PARTS; k++) {
            shield_x = 12 + ((j % 4) * 15) + k;
            if ((shield_x >= x) && (shield_x <= x + 4))
              game_state.shield_intact[j][k] = false;
          }
        }
      } else if (y == (GAME_MAP_HEIGHT - 5)) {
        x = game_state.invaders_pos_x + ((i % 10) * 7);
        for (j = GAME_SHIELDS / 2; j < GAME_SHIELDS; j++) {
          for (k = 0; k < GAME_SHIELD_PARTS; k++) {
            shield_x = 12 + ((j % 4) * 15) + k;
            if ((shield_x >= x) && (shield_x <= x + 4))
              game_state.shield_intact[j][k] = false;
          }
        }
      } else if (y >= (GAME_MAP_HEIGHT - 1)) {
        game_state.score -= 1000;
        return 1;
      }
    }
  }

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
callq 0x1ab6
xorl %ecx, %ecx
testl %eax, %eax
je 0x1b33
movslq %eax, %rsi
movq %rbx, %rax
cqto
idivq %rsi
testq %rdx, %rdx
je 0x1b44
movl %ecx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpw $0x0, 0x3604(%rip)      # 0x5150
movzwl 0x35fb(%rip), %eax      # 0x514e
je 0x1b70
leal 0x1(%rax), %edx
movw %dx, 0x35ef(%rip)       # 0x514e
movzwl 0x35e6(%rip), %ecx      # 0x514c
cmpw $0xc, %dx
jle 0x1ba2
xorl %edx, %edx
jmp 0x1b89
decw %ax
movw %ax, 0x35d4(%rip)       # 0x514e
movzwl 0x35cb(%rip), %ecx      # 0x514c
jns 0x1ba0
xorl %eax, %eax
movw $0x1, %dx
movw %ax, 0x35be(%rip)       # 0x514e
incl %ecx
movw %cx, 0x35b3(%rip)       # 0x514c
movw %dx, 0x35b0(%rip)       # 0x5150
movl %eax, %edx
movswl %cx, %esi
movswl %dx, %edi
xorl %ecx, %ecx
leaq 0x358f(%rip), %r8       # 0x5140
movb $0xa, %r9b
pushq $0xc
popq %r14
xorl %r15d, %r15d
cmpq $0x1e, %r15
je 0x1b33
cmpb $0x1, 0x1a(%r15,%r8)
jne 0x1cbf
movzbl %r15b, %edx
movl %edx, %eax
divb %r9b
movzbl %ah, %ebx
movzbl %al, %eax
leal (%rsi,%rax,4), %eax
cmpl $0x11, %eax
je 0x1c56
cmpl $0x12, %eax
jne 0x1cba
movl %edx, %eax
divb %r9b
movzbl %ah, %eax
imull $0x7, %eax, %eax
movzbl %al, %eax
leal (%rdi,%rax), %edx
leal (%rdi,%rax), %r10d
addl $0x4, %r10d
movslq %edx, %rax
movslq %r10d, %rdx
movq %r14, %r12
leaq 0x355e(%rip), %r13      # 0x5178
xorl %r10d, %r10d
cmpq $0x4, %r10
je 0x1cbf
xorl %r11d, %r11d
cmpq $0xa, %r11
je 0x1c49
leaq (%r12,%r11), %rbx
cmpq %rax, %rbx
jl 0x1c44
cmpq %rdx, %rbx
jg 0x1c44
movb $0x0, (%r13,%r11)
incq %r11
jmp 0x1c2a
incq %r10
addq $0xa, %r13
addq $0xf, %r12
jmp 0x1c1d
movzbl %bl, %eax
imull $0x7, %eax, %eax
movzbl %al, %eax
leal (%rdi,%rax), %edx
leal (%rdi,%rax), %r10d
addl $0x4, %r10d
movslq %edx, %rax
movslq %r10d, %rdx
leaq 0x3529(%rip), %r12      # 0x51a0
pushq $0x4
popq %r13
cmpq $0x8, %r13
je 0x1cbf
movl %r13d, %r10d
andl $0x3, %r10d
imulq $0xf, %r10, %r10
addq $0xc, %r10
xorl %r11d, %r11d
cmpq $0xa, %r11
je 0x1cb1
leaq (%r10,%r11), %rbx
cmpq %rax, %rbx
jl 0x1cac
cmpq %rdx, %rbx
jg 0x1cac
movb $0x0, (%r12,%r11)
incq %r11
jmp 0x1c93
incq %r13
addq $0xa, %r12
jmp 0x1c7b
cmpl $0x15, %eax
jge 0x1cc7
incq %r15
jmp 0x1bbb
addq $-0x3e8, 0x346e(%rip) # imm = 0xFC18
pushq $0x1
popq %rcx
jmp 0x1b33