// File: /workspace/github_cpp_makefile_star2_O1/czinn[P]homeworlds/game.cc
// Function: Game::perform_action(Action&)
bool Game::perform_action(Action& action) {
  if (action.player != cur_player_) {
    return false;
  }

  if (sacrifice_actions_ > 0) {
    sacrifice_actions_--;
  }

  switch(action.type) {
    case PASS:
      advance_cur_player();
      break;

    case ATTACK: {
        // Need to find player of attacked ship
        const System& system = get_system(action.system);
        const auto& ship = find_if(system.ships.begin(), system.ships.end(),
            [&action](const Ship& ship) {
              return ship.pyramid == action.ship && ship.player != action.player;
            });
        if (ship == system.ships.end()) {
          return false;
        }
        remove_ship(action.system, *ship);
        add_ship(action.system, Ship{action.player, ship->pyramid});
        done_main_action_ = true;
      }
      break;

    case DISCOVER: {
        int system_id = create_system({action.target});
        action.system_target = system_id;
        add_ship(system_id,
            remove_ship(action.system, Ship{action.player, action.ship}));
        if (get_system(action.system).ships.size() == 0) {
          destroy_system(action.system);
        }
        done_main_action_ = true;
      }
      break;

    case TRAVEL:
      add_ship(action.system_target,
          remove_ship(action.system, Ship{action.player, action.ship}));
      if (get_system(action.system).ships.size() == 0) {
        destroy_system(action.system);
      }
      done_main_action_ = true;
      break;

    case BUILD:
      add_ship(action.system, Ship{action.player, action.ship});
      done_main_action_ = true;
      break;

    case TRADE:
      remove_ship(action.system, Ship{action.player, action.ship});
      add_ship(action.system, Ship{action.player, action.target});
      done_main_action_ = true;
      break;

    case SACRIFICE:
      remove_ship(action.system, Ship{action.player, action.ship});
      if (get_system(action.system).ships.size() == 0) {
        destroy_system(action.system);
      }
      sacrifice_colour_ = action.ship.colour;
      sacrifice_actions_ = action.ship.size;
      done_main_action_ = true;
      break;

    case CATASTROPHE:
      apply_catastrophe(action.system, action.ship.colour);
      break;

  }

  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x30, %rsp
movl (%rsi), %ecx
movl 0x4(%rdi), %eax
cmpl %eax, %ecx
jne 0x39da
movq %rsi, %r15
movq %rdi, %r14
movl 0x14(%rdi), %edx
testl %edx, %edx
jle 0x3670
decl %edx
movl %edx, 0x14(%r14)
movl 0x4(%r15), %edx
movb $0x1, %bl
cmpq $0x7, %rdx
ja 0x39dc
leaq 0x79e5(%rip), %rsi      # 0xb06c
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
cltd
idivl (%r14)
incl %edx
movl %edx, 0x4(%r14)
movq %r14, %rdi
callq 0x2822
testl %eax, %eax
jne 0x39dc
movb $0x0, 0x8(%r14)
movl $0x0, 0x14(%r14)
jmp 0x39dc
movl 0x8(%r15), %esi
leaq 0x8(%rsp), %rdx
movl %ecx, (%rdx)
movq 0xc(%r15), %rax
jmp 0x39c7
movq 0x18(%r15), %rax
leaq 0x20(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x8(%rsp), %r12
leaq 0x7(%rsp), %rcx
movl $0x1, %edx
movq %r12, %rdi
callq 0x4478
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x3d66
movl %eax, %ebp
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x3719
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
movl %ebp, 0x14(%r15)
movl 0x8(%r15), %esi
movl (%r15), %eax
leaq 0x20(%rsp), %rdx
movl %eax, (%rdx)
jmp 0x373c
movl 0x14(%r15), %ebp
movl 0x8(%r15), %esi
leaq 0x20(%rsp), %rdx
movl %ecx, (%rdx)
movq 0xc(%r15), %rax
movq %rax, 0x4(%rdx)
movq %r14, %rdi
callq 0x3a34
leaq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movl %edx, 0x8(%rcx)
movq %r14, %rdi
movl %ebp, %esi
movq %rcx, %rdx
callq 0x3c68
movl 0x8(%r15), %esi
movq %r14, %rdi
callq 0x294a
movq 0x28(%rax), %rcx
cmpq 0x20(%rax), %rcx
jne 0x39d3
movl 0x8(%r15), %esi
movq %r14, %rdi
callq 0x3e46
jmp 0x39d3
movl 0x8(%r15), %esi
movl 0x10(%r15), %edx
movq %r14, %rdi
callq 0x3f6e
jmp 0x39dc
movl 0x8(%r15), %esi
movq %r14, %rdi
callq 0x294a
movq 0x20(%rax), %rcx
movq 0x28(%rax), %rax
movq %rax, %rsi
subq %rcx, %rsi
sarq $0x2, %rsi
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rsi
sarq $0x2, %rsi
testq %rsi, %rsi
jle 0x3870
movl (%r15), %edi
movl 0xc(%r15), %r8d
movl 0x10(%r15), %r10d
leaq (%rsi,%rsi,2), %r9
shlq $0x4, %r9
addq %rcx, %r9
incq %rsi
addq $0x18, %rcx
movq %rcx, %r12
cmpl %r8d, -0x14(%r12)
jne 0x3816
cmpl %r10d, -0x10(%r12)
jne 0x3816
cmpl %edi, -0x18(%r12)
jne 0x3990
cmpl %r8d, -0x8(%r12)
jne 0x382f
cmpl %r10d, -0x4(%r12)
jne 0x382f
cmpl %edi, -0xc(%r12)
jne 0x3996
cmpl %r8d, 0x4(%r12)
jne 0x3847
cmpl %r10d, 0x8(%r12)
jne 0x3847
cmpl %edi, (%r12)
jne 0x39a0
cmpl %r8d, 0x10(%r12)
jne 0x3860
cmpl %r10d, 0x14(%r12)
jne 0x3860
cmpl %edi, 0xc(%r12)
jne 0x399c
decq %rsi
addq $0x30, %r12
cmpq $0x1, %rsi
jg 0x37fd
movq %r9, %rcx
movq %rax, %rsi
subq %rcx, %rsi
sarq $0x2, %rsi
imulq %rdx, %rsi
cmpq $0x1, %rsi
je 0x38d1
cmpq $0x2, %rsi
je 0x38b4
movq %rax, %r12
cmpq $0x3, %rsi
jne 0x39a0
movl 0x4(%rcx), %edx
cmpl 0xc(%r15), %edx
jne 0x38b0
movl 0x10(%r15), %edx
cmpl %edx, 0x8(%rcx)
jne 0x38b0
movl (%rcx), %edx
cmpl (%r15), %edx
jne 0x38fa
addq $0xc, %rcx
movl 0x4(%rcx), %edx
cmpl 0xc(%r15), %edx
jne 0x38cd
movl 0x10(%r15), %edx
cmpl %edx, 0x8(%rcx)
jne 0x38cd
movl (%rcx), %edx
cmpl (%r15), %edx
jne 0x38fa
addq $0xc, %rcx
movl 0x4(%rcx), %edx
movq %rax, %r12
cmpl 0xc(%r15), %edx
jne 0x39a0
movl 0x10(%r15), %edx
movq %rax, %r12
cmpl %edx, 0x8(%rcx)
jne 0x39a0
movl (%rcx), %edx
cmpl (%r15), %edx
cmoveq %rax, %rcx
movq %rcx, %r12
jmp 0x39a0
movl 0x8(%r15), %esi
leaq 0x8(%rsp), %r12
movl %ecx, (%r12)
movq 0xc(%r15), %rax
movq %rax, 0x4(%r12)
movq %r14, %rdi
movq %r12, %rdx
callq 0x3a34
movl 0x8(%r15), %esi
movl (%r15), %eax
movl %eax, (%r12)
movq 0x18(%r15), %rax
movq %rax, 0x4(%r12)
leaq 0x8(%rsp), %rdx
jmp 0x39cb
movl 0x8(%r15), %esi
leaq 0x8(%rsp), %rdx
movl %ecx, (%rdx)
movq 0xc(%r15), %rax
movq %rax, 0x4(%rdx)
movq %r14, %rdi
callq 0x3a34
movl 0x8(%r15), %esi
movq %r14, %rdi
callq 0x294a
movq 0x28(%rax), %rcx
cmpq 0x20(%rax), %rcx
jne 0x397e
movl 0x8(%r15), %esi
movq %r14, %rdi
callq 0x3e46
movl 0x10(%r15), %eax
movl %eax, 0x18(%r14)
movl 0xc(%r15), %eax
movl %eax, 0x14(%r14)
jmp 0x39d3
addq $-0x18, %r12
jmp 0x39a0
addq $-0xc, %r12
jmp 0x39a0
addq $0xc, %r12
cmpq %rax, %r12
je 0x39da
movl 0x8(%r15), %esi
movq %r14, %rdi
movq %r12, %rdx
callq 0x3a34
movl 0x8(%r15), %esi
movl (%r15), %eax
leaq 0x8(%rsp), %rdx
movl %eax, (%rdx)
movq 0x4(%r12), %rax
movq %rax, 0x4(%rdx)
movq %r14, %rdi
callq 0x3c68
movb $0x1, 0x8(%r14)
jmp 0x39dc
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x3a05
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
movq %rbx, %rdi
callq 0x22d0
nop