// File: /workspace/github_cpp_makefile_star2_O3/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 %rbx
subq $0x38, %rsp
movl (%rsi), %eax
cmpl 0x4(%rdi), %eax
jne 0x38f9
movq %rsi, %r14
movq %rdi, %rbx
movl 0x14(%rdi), %ecx
testl %ecx, %ecx
jle 0x35a3
decl %ecx
movl %ecx, 0x14(%rbx)
movl 0x4(%r14), %ecx
movb $0x1, %bpl
cmpq $0x7, %rcx
ja 0x38fb
leaq 0x7ab1(%rip), %rdx      # 0xb06c
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
cltd
idivl (%rbx)
incl %edx
movl %edx, 0x4(%rbx)
movq %rbx, %rdi
callq 0x283e
testl %eax, %eax
jne 0x38fb
movb $0x0, 0x8(%rbx)
movl $0x0, 0x14(%rbx)
jmp 0x38f4
movl 0x8(%r14), %esi
leaq 0x10(%rsp), %rdx
movl %eax, (%rdx)
movq 0xc(%r14), %rax
movq %rax, 0x4(%rdx)
jmp 0x37cb
movq 0x18(%r14), %rax
leaq 0x28(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x10(%rsp), %r15
leaq 0xf(%rsp), %rcx
movl $0x1, %edx
movq %r15, %rdi
callq 0x4396
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x3cc2
movl %eax, %ebp
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x364d
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
movl %ebp, 0x14(%r14)
movl 0x8(%r14), %esi
movl (%r14), %eax
jmp 0x3662
movl 0x14(%r14), %ebp
movl 0x8(%r14), %esi
leaq 0x28(%rsp), %rdx
movl %eax, (%rdx)
movq 0xc(%r14), %rax
movq %rax, 0x4(%rdx)
movq %rbx, %rdi
callq 0x3950
leaq 0x10(%rsp), %rcx
movq %rax, (%rcx)
movl %edx, 0x8(%rcx)
movq %rbx, %rdi
movl %ebp, %esi
movq %rcx, %rdx
callq 0x3bbc
movl 0x8(%r14), %esi
movq %rbx, %rdi
callq 0x2956
movq 0x28(%rax), %rcx
cmpq 0x20(%rax), %rcx
jne 0x38f0
movl 0x8(%r14), %esi
movq %rbx, %rdi
callq 0x3da2
jmp 0x38f0
movl 0x8(%r14), %esi
movl 0x10(%r14), %edx
movq %rbx, %rdi
callq 0x3ed0
jmp 0x38fb
movl 0x8(%r14), %esi
movq %rbx, %rdi
callq 0x2956
movq 0x20(%rax), %rcx
movq 0x28(%rax), %rax
movq %rax, %rdi
subq %rcx, %rdi
movq %rdi, %rsi
sarq $0x2, %rsi
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rsi
sarq $0x2, %rsi
testq %rsi, %rsi
jle 0x3826
movl (%r14), %edi
movl 0xc(%r14), %r8d
movl 0x10(%r14), %r9d
leaq (%rsi,%rsi,2), %r15
shlq $0x4, %r15
addq %rcx, %r15
incq %rsi
addq $0x18, %rcx
cmpl %r8d, -0x14(%rcx)
jne 0x373f
cmpl %r9d, -0x10(%rcx)
jne 0x373f
cmpl %edi, -0x18(%rcx)
jne 0x38ab
cmpl %r8d, -0x8(%rcx)
jne 0x3754
cmpl %r9d, -0x4(%rcx)
jne 0x3754
cmpl %edi, -0xc(%rcx)
jne 0x38b1
cmpl %r8d, 0x4(%rcx)
jne 0x3768
cmpl %r9d, 0x8(%rcx)
jne 0x3768
cmpl %edi, (%rcx)
jne 0x38bb
cmpl %r8d, 0x10(%rcx)
jne 0x377d
cmpl %r9d, 0x14(%rcx)
jne 0x377d
cmpl %edi, 0xc(%rcx)
jne 0x38b7
decq %rsi
addq $0x30, %rcx
cmpq $0x1, %rsi
jg 0x372a
movq %rax, %rdi
subq %r15, %rdi
jmp 0x3829
movl 0x8(%r14), %esi
leaq 0x10(%rsp), %r15
movl %eax, (%r15)
movq 0xc(%r14), %rax
movq %rax, 0x4(%r15)
movq %rbx, %rdi
movq %r15, %rdx
callq 0x3950
movl 0x8(%r14), %esi
movl (%r14), %eax
movl %eax, (%r15)
movq 0x18(%r14), %rax
movq %rax, 0x4(%r15)
leaq 0x10(%rsp), %rdx
movq %rbx, %rdi
callq 0x3bbc
movb $0x1, 0x8(%rbx)
jmp 0x38fb
movl 0x8(%r14), %esi
leaq 0x10(%rsp), %rdx
movl %eax, (%rdx)
movq 0xc(%r14), %rax
movq %rax, 0x4(%rdx)
movq %rbx, %rdi
callq 0x3950
movl 0x8(%r14), %esi
movq %rbx, %rdi
callq 0x2956
movq 0x28(%rax), %rcx
cmpq 0x20(%rax), %rcx
jne 0x3819
movl 0x8(%r14), %esi
movq %rbx, %rdi
callq 0x3da2
movq 0xc(%r14), %rax
movq %rax, 0x14(%rbx)
jmp 0x38f0
movq %rcx, %r15
sarq $0x2, %rdi
imulq %rdx, %rdi
cmpq $0x3, %rdi
je 0x385b
cmpq $0x2, %rdi
je 0x3851
cmpq $0x1, %rdi
jne 0x38f9
movl 0xc(%r14), %edx
movl 0x10(%r14), %ecx
jmp 0x3893
movl 0xc(%r14), %edx
movl 0x10(%r14), %ecx
jmp 0x387b
movl 0xc(%r14), %edx
movl 0x10(%r14), %ecx
cmpl %edx, 0x4(%r15)
jne 0x3877
cmpl %ecx, 0x8(%r15)
jne 0x3877
movl (%r15), %esi
cmpl (%r14), %esi
jne 0x38be
addq $0xc, %r15
cmpl %edx, 0x4(%r15)
jne 0x388f
cmpl %ecx, 0x8(%r15)
jne 0x388f
movl (%r15), %esi
cmpl (%r14), %esi
jne 0x38be
addq $0xc, %r15
xorl %ebp, %ebp
cmpl %edx, 0x4(%r15)
jne 0x38fb
cmpl %ecx, 0x8(%r15)
jne 0x38fb
movl (%r15), %ecx
cmpl (%r14), %ecx
jne 0x38be
jmp 0x38f9
addq $-0x18, %rcx
jmp 0x38bb
addq $-0xc, %rcx
jmp 0x38bb
addq $0xc, %rcx
movq %rcx, %r15
cmpq %rax, %r15
je 0x38f9
movl 0x8(%r14), %esi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x3950
movl 0x8(%r14), %esi
movl (%r14), %eax
leaq 0x10(%rsp), %rdx
movl %eax, (%rdx)
movq 0x4(%r15), %rax
movq %rax, 0x4(%rdx)
movq %rbx, %rdi
callq 0x3bbc
movb $0x1, 0x8(%rbx)
movb $0x1, %bpl
jmp 0x38fb
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3922
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
movq %rbx, %rdi
callq 0x22d0