// File: 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:
subq $0x178, %rsp            # imm = 0x178
movq 0x190(%rsp), %rax
movq 0x188(%rsp), %rax
movq 0x180(%rsp), %rax
movq %rdi, 0x168(%rsp)
movq %rsi, 0x160(%rsp)
movq %rdx, 0x158(%rsp)
movq %rcx, 0x150(%rsp)
movq %r8, 0x148(%rsp)
movq %r9, 0x140(%rsp)
movl $0x0, 0x13c(%rsp)
movq 0x160(%rsp), %rax
movq %rax, 0x130(%rsp)
movq 0x130(%rsp), %rdi
callq 0x4390
movq %rax, 0x128(%rsp)
movq 0x130(%rsp), %rdi
callq 0x4690
movq %rax, 0x120(%rsp)
leaq 0x128(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0x13810
testb $0x1, %al
jne 0xa762
jmp 0xad10
leaq 0x128(%rsp), %rdi
callq 0x13850
movq %rax, 0x118(%rsp)
movq 0x140(%rsp), %rdi
callq 0x137d0
movq %rax, 0x104(%rsp)
movq 0x180(%rsp), %rdx
leaq 0x10c(%rsp), %rdi
leaq 0x104(%rsp), %rsi
callq 0x13860
movq 0x190(%rsp), %rdi
leaq 0x10c(%rsp), %rsi
callq 0x126f0
cmpq $0x0, %rax
je 0xa7d4
movl $0xffffffff, 0x174(%rsp) # imm = 0xFFFFFFFF
jmp 0xad1e
movq 0x190(%rsp), %rdi
leaq 0x10c(%rsp), %rsi
callq 0x12750
movq %rax, 0xf0(%rsp)
movb %dl, 0xf8(%rsp)
movq 0x140(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xe8(%rsp)
movq 0x180(%rsp), %rax
movl (%rax), %eax
movl %eax, 0xe4(%rsp)
movq 0x158(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x150(%rsp), %rdi
movq 0x148(%rsp), %rax
movslq (%rax), %rsi
callq 0x12230
movq 0x30(%rsp), %rdi
movslq (%rax), %rsi
callq 0x13890
movq %rax, 0xd8(%rsp)
movq 0x118(%rsp), %rax
movsbl (%rax), %edi
callq 0x6a20
movl %eax, 0xd4(%rsp)
movl 0xd4(%rsp), %eax
movq %rax, 0x38(%rsp)
subq $0x5, %rax
ja 0xaa10
movq 0x38(%rsp), %rax
leaq 0x2b881(%rip), %rcx     # 0x36110
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0xfffffffe, 0xc8(%rsp) # imm = 0xFFFFFFFE
movl $0x0, 0xc4(%rsp)
leaq 0xcc(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
leaq 0xc4(%rsp), %rdx
callq 0x11530
leaq 0xe8(%rsp), %rdi
leaq 0xcc(%rsp), %rsi
callq 0x127b0
jmp 0xaa10
movl $0x2, 0xb8(%rsp)
movl $0x0, 0xb4(%rsp)
leaq 0xbc(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
leaq 0xb4(%rsp), %rdx
callq 0x11530
leaq 0xe8(%rsp), %rdi
leaq 0xbc(%rsp), %rsi
callq 0x127b0
jmp 0xaa10
movl $0xffffffff, 0xa8(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0xa4(%rsp)
leaq 0xac(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
leaq 0xa4(%rsp), %rdx
callq 0x11530
leaq 0xe8(%rsp), %rdi
leaq 0xac(%rsp), %rsi
callq 0x127b0
jmp 0xaa10
movl $0x1, 0x98(%rsp)
movl $0x1, 0x94(%rsp)
leaq 0x9c(%rsp), %rdi
leaq 0x98(%rsp), %rsi
leaq 0x94(%rsp), %rdx
callq 0x11530
leaq 0xe8(%rsp), %rdi
leaq 0x9c(%rsp), %rsi
callq 0x127b0
jmp 0xaa10
movl 0xe4(%rsp), %eax
addl $0x1, %eax
movq 0xd8(%rsp), %rcx
cltd
idivl 0x20(%rcx)
movl %edx, 0xe4(%rsp)
jmp 0xaa10
movl 0xe4(%rsp), %eax
movq 0xd8(%rsp), %rcx
addl 0x20(%rcx), %eax
subl $0x1, %eax
movq 0xd8(%rsp), %rcx
cltd
idivl 0x20(%rcx)
movl %edx, 0xe4(%rsp)
movq 0x188(%rsp), %rdi
movq 0xd8(%rsp), %rdx
movl 0xe4(%rsp), %ecx
leaq 0xe8(%rsp), %rsi
callq 0x6e60
testb $0x1, %al
jne 0xaa3a
jmp 0xaa63
movq 0x140(%rsp), %rax
movq 0xe8(%rsp), %rcx
movq %rcx, (%rax)
movl 0xe4(%rsp), %ecx
movq 0x180(%rsp), %rax
movl %ecx, (%rax)
jmp 0xacfc
movq 0x168(%rsp), %rax
movl 0x24(%rax), %eax
movl %eax, 0x90(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x88(%rsp), %rdi
callq 0x110b0
movq %rax, 0x80(%rsp)
movq 0x88(%rsp), %rdi
callq 0x110e0
movq %rax, 0x78(%rsp)
leaq 0x80(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x11110
testb $0x1, %al
jne 0xaac7
jmp 0xab67
leaq 0x80(%rsp), %rdi
callq 0x11150
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rdi
movq 0x140(%rsp), %rsi
callq 0x12100
movq %rax, 0x60(%rsp)
movq 0xd8(%rsp), %rdi
addq $0x18, %rdi
movq 0x140(%rsp), %rsi
callq 0x12100
movq %rax, 0x58(%rsp)
movq 0x180(%rsp), %rax
movl (%rax), %edx
leaq 0x60(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x11830
movq %rax, 0x68(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x11930
movl %eax, 0x54(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x54(%rsp), %rsi
callq 0x11a30
movl (%rax), %eax
movl %eax, 0x90(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x11160
jmp 0xaaac
cmpl $0x5, 0x90(%rsp)
jg 0xab81
movl $0xffffffff, 0x174(%rsp) # imm = 0xFFFFFFFF
jmp 0xad1e
movq 0x188(%rsp), %rdi
movq 0x140(%rsp), %rsi
movq 0xd8(%rsp), %rdx
movq 0x180(%rsp), %rax
movl (%rax), %ecx
xorl %r8d, %r8d
callq 0x6f10
movq %rax, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
addl 0x13c(%rsp), %eax
movl %eax, 0x13c(%rsp)
movq 0x148(%rsp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq 0x148(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x2c(%rsp)
movq 0x150(%rsp), %rdi
callq 0x12180
movq %rax, %rcx
movl 0x2c(%rsp), %eax
cmpl %ecx, %eax
jl 0xac07
movl $0xffffffff, 0x174(%rsp) # imm = 0xFFFFFFFF
jmp 0xad1e
movq 0x190(%rsp), %rdi
callq 0x138b0
movq 0x168(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x158(%rsp), %rax
movq %rax, (%rsp)
movq 0x150(%rsp), %rdi
movq 0x148(%rsp), %rax
movslq (%rax), %rsi
callq 0x12230
movq (%rsp), %rdi
movslq (%rax), %rsi
callq 0x13890
movq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x7a10
movq %rax, 0x44(%rsp)
movq 0x140(%rsp), %rax
movq 0x44(%rsp), %rcx
movq %rcx, (%rax)
movq 0x180(%rsp), %rax
movl $0x0, (%rax)
movq 0x188(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x140(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x158(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x150(%rsp), %rdi
movq 0x148(%rsp), %rax
movslq (%rax), %rsi
callq 0x12230
movq 0x10(%rsp), %rdi
movslq (%rax), %rsi
callq 0x13890
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %rax, %rdx
movq 0x180(%rsp), %rax
movl (%rax), %ecx
callq 0x6e60
testb $0x1, %al
jne 0xacfa
movl $0xffffffff, 0x174(%rsp) # imm = 0xFFFFFFFF
jmp 0xad1e
jmp 0xacfc
jmp 0xacfe
leaq 0x128(%rsp), %rdi
callq 0x138c0
jmp 0xa744
movl 0x13c(%rsp), %eax
movl %eax, 0x174(%rsp)
movl 0x174(%rsp), %eax
addq $0x178, %rsp            # imm = 0x178
retq
nopl (%rax)