// File: /workspace/github_cpp_makefile_star2_O3/tanakh[P]icfpc2015/main.cpp
// Function: can_use(problem const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unit, std::allocator<unit>> const&, std::vector<int, std::allocator<int>> const&, int&, std::complex<int>&, int&, std::vector<std::vector<int, std::allocator<int>>, std::allocator<std::vector<int, std::allocator<int>>>>&, std::set<std::pair<std::pair<int, int>, int>, std::less<std::pair<std::pair<int, int>, int>>, std::allocator<std::pair<std::pair<int, int>, int>>>&)
int can_use(const problem &prob,
            const string &seq, const vector<unit> &units, const vector<int> &unit_ord,
            int &turn, pt &pos, int &rot,
            vector<vector<int>> &bd,
            set<pair<pair<int,int>, int>> &prevs)
{

  int score = 0;

  for (auto &c: seq) {
    pair<pair<int,int>, int> cc(pt2pair(pos), rot);

    if (prevs.count(cc)) return -1;
    prevs.insert(cc);

    pt npos = pos;
    int nrot = rot;

    const unit &u = units[unit_ord[turn]];
    command mov = to_command(c);
    switch(mov) {
    case W:   npos += pt(-2, 0); break;
    case E:   npos += pt(2, 0); break;
    case SW:  npos += pt(-1, 1); break;
    case SE:  npos += pt(1, 1); break;
    case CW:  nrot = (nrot + 1) % u.rot_max; break;
    case CCW: nrot = (nrot + u.rot_max - 1) % u.rot_max; break;
    }

    if (check(bd, npos, u, nrot)) {
      pos = npos;
      rot = nrot;
    }
    else {
      // cerr << "*** " << seq << endl;
      // print_board(bd);
      // print_board_(bd, pos, u, rot);

      int highest = prob.height;
      for (auto &c: u.members) {
        auto d = rot_pivot(c + pos, u.pivot + pos, rot);
        highest = min(highest, d.imag());
      }

      if (highest <= 5) return -1;

      score += put_unit(bd, pos, u, rot, 0).first;
      turn++;
      if (turn >= (int)unit_ord.size())
        return -1;
      prevs.clear();

      pos = get_init_pos(prob, units[unit_ord[turn]]);
      rot = 0;

      if (!check(bd, pos, units[unit_ord[turn]], rot))
        return -1;
    }
  }

  return score;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdx, 0x48(%rsp)
movq %rdi, 0x38(%rsp)
movq 0x8(%rsi), %r14
testq %r14, %r14
je 0x7474
movq %r9, %r12
movq %rcx, %rbp
movq %r8, 0x40(%rsp)
movq 0xc0(%rsp), %r13
movq 0xb0(%rsp), %rbx
movq (%rsi), %rax
movq %rax, 0x20(%rsp)
addq %rax, %r14
leaq 0x8(%r13), %rax
movq %rax, 0x60(%rsp)
movl $0x0, 0xc(%rsp)
leaq 0x28(%rsp), %r15
movq %rcx, 0x50(%rsp)
movq %r14, 0x58(%rsp)
movq %r9, 0x18(%rsp)
movq (%r12), %rax
movq %rax, 0x28(%rsp)
movl (%rbx), %eax
movl %eax, 0x30(%rsp)
movq %r13, %rdi
movq %r15, %rsi
callq 0x10884
cmpq 0x60(%rsp), %rax
jne 0x746a
movq %r13, %rdi
movq %r15, %rsi
callq 0x1092a
movq (%r12), %r14
movq %r14, 0x10(%rsp)
movl (%rbx), %r12d
movq 0x40(%rsp), %rax
movslq (%rax), %rax
movq (%rbp), %rcx
movslq (%rcx,%rax,4), %rax
leaq (%rax,%rax,4), %r13
shlq $0x3, %r13
movq 0x48(%rsp), %rax
addq (%rax), %r13
movq 0x20(%rsp), %rax
movsbl (%rax), %edi
callq 0x4cdd
cmpl $0x5, %eax
ja 0x72ad
movq %r14, %rcx
shrq $0x20, %rcx
movl %eax, %eax
leaq 0xdeff(%rip), %rdx      # 0x15160
movslq (%rdx,%rax,4), %rax
addq %rdx, %rax
jmpq *%rax
leal -0x2(%r14), %eax
jmp 0x7297
incl %r12d
movl %r12d, %eax
cltd
idivl 0x20(%r13)
jmp 0x72aa
leal -0x1(%r14), %eax
jmp 0x7287
leal 0x1(%r14), %eax
movl %eax, 0x10(%rsp)
incl %ecx
movl %ecx, 0x14(%rsp)
jmp 0x72ad
leal 0x2(%r14), %eax
movl %eax, 0x10(%rsp)
jmp 0x72ad
movl 0x20(%r13), %ecx
leal (%r12,%rcx), %eax
decl %eax
cltd
idivl %ecx
movl %edx, %r12d
movq 0xb8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq %r13, %rdx
movl %r12d, %ecx
callq 0x504f
testb %al, %al
je 0x72ee
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rax, (%rcx)
movq 0xb0(%rsp), %rbx
movl %r12d, (%rbx)
movq 0xc0(%rsp), %r13
jmp 0x7443
movq 0x38(%rsp), %rax
movl 0x24(%rax), %ebp
movq (%r13), %r12
movq 0x8(%r13), %rax
movq %rax, 0x68(%rsp)
cmpq %rax, %r12
movq 0xb0(%rsp), %rdi
movq 0x18(%rsp), %r15
je 0x737c
movq (%r12), %rax
movq %rax, %rcx
shrq $0x20, %rcx
movl 0x4(%r15), %edx
addl %edx, %ecx
movq 0x18(%r13), %rsi
subl %esi, %eax
movq %r13, %r14
movq %r15, %r13
movq %rsi, %r15
shrq $0x20, %r15
addl %edx, %r15d
movl (%rdi), %esi
subl %r15d, %ecx
shlq $0x20, %rcx
orq %rcx, %rax
movq %rax, 0x70(%rsp)
movq %rdi, %rbx
leaq 0x70(%rsp), %rdi
callq 0xc93e
movq %rbx, %rdi
shrq $0x20, %rax
addl %r15d, %eax
movq %r13, %r15
movq %r14, %r13
cmpl %ebp, %eax
cmovgel %ebp, %eax
addq $0x8, %r12
movl %eax, %ebp
cmpq 0x68(%rsp), %r12
jne 0x7315
cmpl $0x6, %ebp
movq 0x40(%rsp), %rbx
movq 0x50(%rsp), %rbp
jl 0x746a
movl (%rdi), %ecx
movq 0xb8(%rsp), %rdi
movq %r15, %rsi
movq %r13, %rdx
xorl %r8d, %r8d
callq 0x5135
movq %rax, %r12
movl (%rbx), %eax
incl %eax
movl %eax, (%rbx)
movq 0x8(%rbp), %rcx
subq (%rbp), %rcx
shrq $0x2, %rcx
cmpl %ecx, %eax
jge 0x746a
movq 0xc0(%rsp), %r13
movq %r13, %rdi
callq 0x11e36
movslq (%rbx), %rax
movq (%rbp), %rcx
movslq (%rcx,%rax,4), %rax
leaq (%rax,%rax,4), %rsi
shlq $0x3, %rsi
movq 0x48(%rsp), %r14
addq (%r14), %rsi
movq 0x38(%rsp), %rdi
callq 0x5805
movq %rax, (%r15)
movq 0xb0(%rsp), %rax
movl $0x0, (%rax)
movslq (%rbx), %rax
movq (%rbp), %rcx
movslq (%rcx,%rax,4), %rax
leaq (%rax,%rax,4), %rdx
shlq $0x3, %rdx
addq (%r14), %rdx
movq 0xb8(%rsp), %rdi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x504f
testb %al, %al
je 0x746a
addl %r12d, 0xc(%rsp)
movq 0xb0(%rsp), %rbx
leaq 0x28(%rsp), %r15
movq 0x20(%rsp), %rax
incq %rax
movq 0x58(%rsp), %r14
movq %rax, 0x20(%rsp)
cmpq %r14, %rax
movq 0x18(%rsp), %r12
jne 0x71e3
jmp 0x747c
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
jmp 0x747c
movl $0x0, 0xc(%rsp)
movl 0xc(%rsp), %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq