// File: /workspace/github_cpp_makefile_star2_O1/tanakh[P]icfpc2015/main.cpp
// Function: replay(problem const&, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
void replay(const problem &prob, int seed, const string &moves)
{
  vlog() << "replay:" << endl;
  auto bd = make_board(prob);
  rng r(seed);

  const char *p = moves.c_str();
  int move_score = 0;

  for (int turn = 0; turn < prob.source_length && *p; turn++) {
    const unit &u = prob.units[r.get() % prob.units.size()];
    pt pos = get_init_pos(prob, u);
    int rot = 0;
    if (!check(bd, pos, u, rot))
      break;
    vlog() << "*POP*" << endl;
    print_board_(bd, pos, u, rot);

    set<pair<pair<int,int>, set<pair<int,int>>>> prevs;
    prevs.insert(uposs(u, pos, rot));

    for (;*p;) {
      pt npos = pos;
      int nrot = rot;
      char c = *p++;
      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;
      }

      auto tt = uposs(u, npos, nrot);
      if (prevs.count(tt)) {
        vlog() << "ERROR: duplicate positions " << show_command(mov) << "(" << c << ")" << endl;
        return;
      }
      prevs.insert(tt);

      if (check(bd, npos, u, nrot)) {
        pos = npos;
        rot = nrot;
        vlog() << "cmd: " << show_command(mov) << "(" << c << ")" << endl;
        print_board_(bd, pos, u, rot);
      }
      else {
        vlog() << "cmd: LOCK (" << show_command(mov) << ", " << c << ")" << endl;
        move_score += put_unit(bd, pos, u, rot, 0).first;
        print_board(bd);
        break;
      }
    }
  }

  auto psc = power_score(moves);

  vlog() << "move score: " << move_score << endl;
  vlog() << "power score: " << psc << endl;
  vlog() << "total score: " << move_score + psc << endl;

  if (*p)
    vlog() << "ERROR: sequence remains." << endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rdx, 0x80(%rsp)
movl %esi, %ebp
movq %rdi, %r15
cmpb $0x0, 0x15614(%rip)     # 0x1e690
je 0x9087
movq 0x13f63(%rip), %rbx     # 0x1cfe8
jmp 0x908e
leaq 0x1560a(%rip), %rbx     # 0x1e698
leaq 0xc4f9(%rip), %rsi      # 0x1558e
movl $0x7, %edx
movq %rbx, %rdi
callq 0x3350
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3300
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3200
leaq 0x68(%rsp), %rdi
movq %r15, %rsi
callq 0x666c
movq 0x80(%rsp), %rax
movq (%rax), %rbx
cmpl $0x0, 0x40(%r15)
jle 0x96b8
movabsq $-0x3333333333333333, %r14 # imm = 0xCCCCCCCCCCCCCCCD
leaq 0x68(%rsp), %r13
movl $0x0, 0x1c(%rsp)
xorl %eax, %eax
movq %r15, 0x58(%rsp)
movl %eax, 0x64(%rsp)
cmpb $0x0, (%rbx)
je 0x96b1
movl %ebp, %eax
shrl $0x10, %eax
andl $0x7fff, %eax           # imm = 0x7FFF
movq 0x8(%r15), %rcx
movq 0x10(%r15), %rsi
subq %rcx, %rsi
sarq $0x3, %rsi
imulq %r14, %rsi
xorl %edx, %edx
divq %rsi
leaq (%rdx,%rdx,4), %rax
leaq (%rcx,%rax,8), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x5816
movq %rax, 0x28(%rsp)
movq %r13, %rdi
leaq 0x28(%rsp), %rsi
movq %r12, 0x20(%rsp)
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x5062
movl $0x2, 0x34(%rsp)
testb %al, %al
je 0x968d
cmpb $0x0, 0x15511(%rip)     # 0x1e690
leaq 0x15512(%rip), %r14     # 0x1e698
je 0x918f
movq 0x13e59(%rip), %r14     # 0x1cfe8
movl $0x5, %edx
movq %r14, %rdi
leaq 0xc3f8(%rip), %rsi      # 0x15596
callq 0x3350
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3300
movsbl %al, %esi
movq %r14, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3200
movq %r13, %rdi
leaq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdx
xorl %ecx, %ecx
callq 0x5689
movl $0x0, 0x98(%rsp)
xorl %ecx, %ecx
movq %rcx, 0xa0(%rsp)
leaq 0x98(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq %rax, 0xb0(%rsp)
movq %rcx, 0xb8(%rsp)
leaq 0xc0(%rsp), %r14
movq %r14, %rdi
movq 0x20(%rsp), %rsi
leaq 0x28(%rsp), %rdx
xorl %ecx, %ecx
callq 0x7085
movl %ebp, 0x60(%rsp)
leaq 0x90(%rsp), %rdi
movq %r14, %rsi
callq 0x12984
leaq 0xc8(%rsp), %rdi
callq 0xc8cc
movl $0x0, 0x34(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x88(%rsp)
xorl %r13d, %r13d
cmpb $0x0, (%rbx)
je 0x9668
movq 0x88(%rsp), %rax
movq %rax, 0x10(%rsp)
movb (%rbx), %bpl
movsbl %bpl, %edi
callq 0x4ceb
movl %eax, %r14d
movl %r13d, %r15d
cmpl $0x5, %eax
ja 0x9302
movl %r14d, %eax
leaq 0xbef9(%rip), %rcx      # 0x15198
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
addl $-0x2, 0x10(%rsp)
jmp 0x92e9
leal 0x1(%r13), %eax
cltd
movq 0x20(%rsp), %rcx
idivl 0x20(%rcx)
jmp 0x92ff
movq 0x10(%rsp), %xmm0
paddd 0xbda4(%rip), %xmm0     # 0x15070
jmp 0x92dc
movq 0x10(%rsp), %xmm0
psubd 0xbda4(%rip), %xmm0     # 0x15080
movq %xmm0, 0x10(%rsp)
jmp 0x92e9
addl $0x2, 0x10(%rsp)
movl %r13d, %r15d
jmp 0x9302
movq 0x20(%rsp), %rax
movl 0x20(%rax), %ecx
leal (%rcx,%r13), %eax
decl %eax
cltd
idivl %ecx
movl %edx, %r15d
leaq 0xc0(%rsp), %r12
movq %r12, %rdi
movq 0x20(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movl %r15d, %ecx
callq 0x7085
leaq 0x90(%rsp), %rdi
movq %r12, %rsi
callq 0x12b80
leaq 0x98(%rsp), %rcx
cmpq %rcx, %rax
je 0x9414
cmpb $0x0, 0x15349(%rip)     # 0x1e690
leaq 0x1534a(%rip), %r15     # 0x1e698
je 0x9357
movq 0x13c91(%rip), %r15     # 0x1cfe8
movl $0x1b, %edx
movq %r15, %rdi
leaq 0xc236(%rip), %rsi      # 0x1559c
callq 0x3350
leaq 0x38(%rsp), %rdi
movl %r14d, %esi
callq 0x4c63
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq %r15, %rdi
callq 0x3350
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0xc22c(%rip), %rsi      # 0x155c8
callq 0x3350
movb %bpl, 0xf(%rsp)
movl $0x1, %edx
movq %r14, %rdi
leaq 0xf(%rsp), %rsi
callq 0x3350
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0xc4da(%rip), %rsi      # 0x158a4
callq 0x3350
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3300
movsbl %al, %esi
movq %r14, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3200
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x940a
callq 0x32f0
movl $0x1, %ebp
jmp 0x9641
leaq 0x90(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x12bec
leaq 0x68(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq 0x20(%rsp), %rdx
movl %r15d, %ecx
callq 0x5062
testb %al, %al
je 0x9545
movq 0x10(%rsp), %rax
movq %rax, 0x88(%rsp)
movq %rax, 0x28(%rsp)
cmpb $0x0, 0x1522f(%rip)     # 0x1e690
leaq 0x15230(%rip), %r13     # 0x1e698
je 0x9471
movq 0x13b77(%rip), %r13     # 0x1cfe8
movl $0x5, %edx
movq %r13, %rdi
leaq 0xc138(%rip), %rsi      # 0x155b8
callq 0x3350
leaq 0x38(%rsp), %rdi
movl %r14d, %esi
callq 0x4c63
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq %r13, %rdi
callq 0x3350
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0xc112(%rip), %rsi      # 0x155c8
callq 0x3350
movb %bpl, 0xf(%rsp)
movl $0x1, %edx
movq %r14, %rdi
leaq 0xf(%rsp), %rsi
callq 0x3350
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0xc3c0(%rip), %rsi      # 0x158a4
callq 0x3350
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3300
movsbl %al, %esi
movq %r14, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3200
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
leaq 0x68(%rsp), %r14
je 0x9529
callq 0x32f0
xorl %ebp, %ebp
movq %r14, %rdi
leaq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdx
movl %r15d, %ecx
callq 0x5689
jmp 0x9644
cmpb $0x0, 0x15144(%rip)     # 0x1e690
leaq 0x15145(%rip), %r15     # 0x1e698
je 0x955c
movq 0x13a8c(%rip), %r15     # 0x1cfe8
movl $0xb, %edx
movq %r15, %rdi
leaq 0xc053(%rip), %rsi      # 0x155be
callq 0x3350
leaq 0x38(%rsp), %rdi
movl %r14d, %esi
callq 0x4c63
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq %r15, %rdi
callq 0x3350
movq %rax, %r14
movl $0x2, %edx
movq %rax, %rdi
leaq 0xbdae(%rip), %rsi      # 0x1534f
callq 0x3350
movb %bpl, 0xf(%rsp)
movl $0x1, %edx
movq %r14, %rdi
leaq 0xf(%rsp), %rsi
callq 0x3350
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0xc2d5(%rip), %rsi      # 0x158a4
callq 0x3350
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3300
movsbl %al, %esi
movq %r14, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3200
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x960f
callq 0x32f0
leaq 0x68(%rsp), %r15
movq %r15, %rdi
leaq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdx
movl %r13d, %ecx
xorl %r8d, %r8d
callq 0x514f
movq %rax, %r14
movq %r15, %rdi
callq 0x54bd
addl %r14d, 0x1c(%rsp)
movl $0x5, %ebp
movl %r13d, %r15d
incq %rbx
leaq 0xc8(%rsp), %rdi
callq 0xc8cc
movl %r15d, %r13d
testl %ebp, %ebp
je 0x9268
cmpl $0x5, %ebp
je 0x9668
movl %ebp, 0x34(%rsp)
movq 0x58(%rsp), %r15
movabsq $-0x3333333333333333, %r14 # imm = 0xCCCCCCCCCCCCCCCD
leaq 0x68(%rsp), %r13
leaq 0x90(%rsp), %rdi
callq 0xf892
movl 0x60(%rsp), %ebp
movl 0x34(%rsp), %ecx
testl %ecx, %ecx
movl 0x64(%rsp), %eax
jne 0x96c4
imull $0x41c64e6d, %ebp, %ebp # imm = 0x41C64E6D
addl $0x3039, %ebp           # imm = 0x3039
incl %eax
cmpl 0x40(%r15), %eax
jl 0x910a
movl $0x2, %eax
jmp 0x96c6
movl $0x0, 0x1c(%rsp)
movb $0x1, %al
jmp 0x96cc
movl %ecx, %eax
cmpl $0x2, %eax
sete %al
testb %al, %al
je 0x9865
movq 0x80(%rsp), %rdi
callq 0x686e
movl %eax, %ebp
cmpb $0x0, 0x14fa6(%rip)     # 0x1e690
je 0x96f5
movq 0x138f5(%rip), %r14     # 0x1cfe8
jmp 0x96fc
leaq 0x14f9c(%rip), %r14     # 0x1e698
leaq 0xbec7(%rip), %rsi      # 0x155ca
movl $0xc, %edx
movq %r14, %rdi
callq 0x3350
movq %r14, %rdi
movl 0x1c(%rsp), %esi
callq 0x34a0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3300
movsbl %al, %esi
movq %r14, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3200
cmpb $0x0, 0x14f43(%rip)     # 0x1e690
je 0x9758
movq 0x13892(%rip), %r14     # 0x1cfe8
jmp 0x975f
leaq 0x14f39(%rip), %r14     # 0x1e698
leaq 0xbe71(%rip), %rsi      # 0x155d7
movl $0xd, %edx
movq %r14, %rdi
callq 0x3350
movq %r14, %rdi
movl %ebp, %esi
callq 0x34a0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3300
movsbl %al, %esi
movq %r14, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3200
cmpb $0x0, 0x14ee2(%rip)     # 0x1e690
je 0x97b9
movq 0x13831(%rip), %r14     # 0x1cfe8
jmp 0x97c0
leaq 0x14ed8(%rip), %r14     # 0x1e698
leaq 0xbe1e(%rip), %rsi      # 0x155e5
movl $0xd, %edx
movq %r14, %rdi
callq 0x3350
addl 0x1c(%rsp), %ebp
movq %r14, %rdi
movl %ebp, %esi
callq 0x34a0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3300
movsbl %al, %esi
movq %r14, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3200
cmpb $0x0, (%rbx)
je 0x9865
cmpb $0x0, 0x14e78(%rip)     # 0x1e690
je 0x9823
movq 0x137c7(%rip), %rbx     # 0x1cfe8
jmp 0x982a
leaq 0x14e6e(%rip), %rbx     # 0x1e698
leaq 0xbdc2(%rip), %rsi      # 0x155f3
movl $0x18, %edx
movq %rbx, %rdi
callq 0x3350
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3300
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3200
leaq 0x68(%rsp), %rdi
callq 0xbbf8
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x9896
jmp 0x989f
jmp 0x989f
jmp 0x989f
jmp 0x989f
jmp 0x988f
jmp 0x989f
movq %rax, %rbx
jmp 0x98d5
jmp 0x9896
movq %rax, %rbx
jmp 0x98c8
jmp 0x98a4
jmp 0x98a4
movq %rax, %rbx
jmp 0x98bb
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x98bb
callq 0x32f0
leaq 0xc8(%rsp), %rdi
callq 0xc8cc
leaq 0x90(%rsp), %rdi
callq 0xf892
leaq 0x68(%rsp), %rdi
callq 0xbbf8
movq %rbx, %rdi
callq 0x34b0