// File: ATOMIC-LICHESS[P]ATOMKRAFT/move.cpp
// Function: move_to_san[abi:cxx11](Position&, Move)
const string move_to_san(Position& pos, Move m) {
  
  STARTNEW
#ifndef NDEBUG
  int failstep;
  int pos_is_ok = pos.is_ok(&failstep);
  if (!pos_is_ok) cout << "failstep: " << failstep << endl;
#endif
  ENDNEW
  assert(pos_is_ok);
  assert(move_is_ok(m));

  MoveStack mlist[MAX_MOVES];
  Square from = move_from(m);
  Square to = move_to(m);
  PieceType pt = pos.type_of_piece_on(from);
  string san;

  if (m == MOVE_NONE)
      return "(none)";

  if (m == MOVE_NULL)
      return "(null)";

  if (move_is_long_castle(m))
      san = "O-O-O";
  else if (move_is_short_castle(m))
      san = "O-O";
  else
  {
      if (pt != PAWN)
      {
          san = piece_type_to_char(pt);

          // Collect all legal moves of piece type 'pt' with destination 'to'
          MoveStack* last = generate<MV_LEGAL>(pos, mlist);
          int f = 0, r = 0;

          for (MoveStack* cur = mlist; cur != last; cur++)
              if (   move_to(cur->move) == to
                  && pos.type_of_piece_on(move_from(cur->move)) == pt)
              {
                  if (square_file(move_from(cur->move)) == square_file(from))
                      f++;

                  if (square_rank(move_from(cur->move)) == square_rank(from))
                      r++;
                  
                  STARTNEW
                  // something like knights on f7 and b5 moving to d6 is not regarded in
                  // the original stockfish code
                  if (   square_file(move_from(cur->move)) != square_file(from)
                	  && square_rank(move_from(cur->move)) != square_rank(from)) {
                	  r++;
                  }
                  ENDNEW
              }

          assert(f > 0 && r > 0);

          // Disambiguation if we have more then one piece with destination 'to'
          if (f == 1 && r > 1)
              san += file_to_char(square_file(from));
          else if (f > 1 && r == 1)
              san += rank_to_char(square_rank(from));
          else if (f > 1 && r > 1)
              san += square_to_string(from);
      }

      if (pos.move_is_capture(m))
      {
          if (pt == PAWN)
              san += file_to_char(square_file(from));

          san += 'x';
      }

      san += square_to_string(to);

      if (move_is_promotion(m))
      {
          san += '=';
          san += piece_type_to_char(move_promotion_piece(m));
      }
  }

  // The move gives check? We don't use pos.move_gives_check() here
  // because we need to test for a mate after the move is done.
  StateInfo st;
  pos.do_move(m, st);
  OLD if (pos.in_check())
      san += pos.is_mate() ? "#" : "+";
  STARTNEW
  if (pos.in_check()) {
        san += pos.is_mate() ? "#" : "+";
  } else if (pos.is_mate()) {
	  san += "#";
  }
  ENDNEW
  pos.undo_move(m);

  return san;
}


/// pretty_pv() creates a human-readable string from a position and a PV.
/// It is used to write search information to the log file (which is created
/// when the UCI parameter "Use Search Log" is "true").

const string pretty_pv(Position& pos, int depth, Value score, int time, Move pv[]) {

  const int64_t K = 1000;
  const int64_t M = 1000000;
  const int startColumn = 28;
  const size_t maxLength = 80 - startColumn;
  const string lf = string("\n") + string(startColumn, ' ');

  StateInfo state[PLY_MAX_PLUS_2], *st = state;
  Move* m = pv;
  string san;
  std::stringstream s;
  size_t length = 0;

  // First print depth, score, time and searched nodes...
  s << std::setw(2) << depth
    << std::setw(8) << score_string(score)
    << std::setw(8) << time_string(time);

  if (pos.nodes_searched() < M)
      s << std::setw(8) << pos.nodes_searched() / 1 << "  ";
  else if (pos.nodes_searched() < K * M)
      s << std::setw(7) << pos.nodes_searched() / K << "K  ";
  else
      s << std::setw(7) << pos.nodes_searched() / M << "M  ";

  // ...then print the full PV line in short algebraic notation
  while (*m != MOVE_NONE)
  {
      san = move_to_san(pos, *m);
      length += san.length() + 1;

      if (length > maxLength)
      {
          length = san.length() + 1;
          s << lf;
      }
      s << san << ' ';

      pos.do_move(*m++, *st++);
  }

  // Restore original position before to leave
  while (m != pv) pos.undo_move(*--m);

  return s.str();
}


namespace {

  const string time_string(int millisecs) {

    const int MSecMinute = 1000 * 60;
    const int MSecHour   = 1000 * 60 * 60;

    int hours = millisecs / MSecHour;
    int minutes =  (millisecs % MSecHour) / MSecMinute;
    int seconds = ((millisecs % MSecHour) % MSecMinute) / 1000;

    std::stringstream s;

    if (hours)
        s << hours << ':';

    s << std::setfill('0') << std::setw(2) << minutes << ':' << std::setw(2) << seconds;
    return s.str();
  }


  const string score_string(Value v) {

    std::stringstream s;

    if (v >= VALUE_MATE - 200)
        s << "#" << (VALUE_MATE - v + 1) / 2;
    else if (v <= -VALUE_MATE + 200)
        s << "-#" << (VALUE_MATE + v) / 2;
    else
        s << std::setprecision(2) << std::fixed << std::showpos << float(v) / PawnValueMidgame;

    return s.str();
  }
}

// Assembly:
subq $0xa08, %rsp            # imm = 0xA08
movq %rdi, 0x98(%rsp)
movq %rdi, %rax
movq %rax, 0xa0(%rsp)
movq %rdi, 0xa00(%rsp)
movq %rsi, 0x9f8(%rsp)
movl %edx, 0x9f4(%rsp)
movl 0x9f4(%rsp), %edi
callq 0xf680
movl %eax, 0x1ec(%rsp)
movl 0x9f4(%rsp), %edi
callq 0xf690
movl %eax, 0x1e8(%rsp)
movq 0x9f8(%rsp), %rdi
movl 0x1ec(%rsp), %esi
callq 0x11f10
movl %eax, 0x1e4(%rsp)
leaq 0x1c0(%rsp), %rdi
callq 0x6730
cmpl $0x0, 0x9f4(%rsp)
jne 0x2cfdf
leaq 0x1bf(%rsp), %rdi
movq %rdi, 0x90(%rsp)
callq 0x6980
movq 0x98(%rsp), %rdi
movq 0x90(%rsp), %rdx
leaq 0x2d880(%rip), %rsi     # 0x5a815
callq 0x12070
jmp 0x2cf9c
leaq 0x1bf(%rsp), %rdi
callq 0x6550
movl $0x1, 0x1a8(%rsp)
jmp 0x2d6b2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
leaq 0x1bf(%rsp), %rdi
callq 0x6550
jmp 0x2d6cf
cmpl $0x41, 0x9f4(%rsp)
jne 0x2d05f
leaq 0x1a7(%rsp), %rdi
movq %rdi, 0x88(%rsp)
callq 0x6980
movq 0x98(%rsp), %rdi
movq 0x88(%rsp), %rdx
leaq 0x2d848(%rip), %rsi     # 0x5a85d
callq 0x12070
jmp 0x2d01c
leaq 0x1a7(%rsp), %rdi
callq 0x6550
movl $0x1, 0x1a8(%rsp)
jmp 0x2d6b2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
leaq 0x1a7(%rsp), %rdi
callq 0x6550
jmp 0x2d6cf
movl 0x9f4(%rsp), %edi
callq 0x11ec0
movb %al, 0x87(%rsp)
jmp 0x2d074
movb 0x87(%rsp), %al
testb $0x1, %al
jne 0x2d081
jmp 0x2d0b5
leaq 0x2c4a8(%rip), %rsi     # 0x59530
leaq 0x1c0(%rsp), %rdi
callq 0x6710
jmp 0x2d097
jmp 0x2d5bb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
jmp 0x2d6cf
movl 0x9f4(%rsp), %edi
callq 0x11e70
movb %al, 0x86(%rsp)
jmp 0x2d0ca
movb 0x86(%rsp), %al
testb $0x1, %al
jne 0x2d0d7
jmp 0x2d0f2
leaq 0x2c454(%rip), %rsi     # 0x59532
leaq 0x1c0(%rsp), %rdi
callq 0x6710
jmp 0x2d0ed
jmp 0x2d5b9
cmpl $0x1, 0x1e4(%rsp)
je 0x2d471
movl 0x1e4(%rsp), %edi
callq 0x2e340
movb %al, 0x85(%rsp)
jmp 0x2d115
movb 0x85(%rsp), %al
movsbl %al, %esi
leaq 0x1c0(%rsp), %rdi
callq 0x68b0
jmp 0x2d12e
movq 0x9f8(%rsp), %rdi
leaq 0x1f0(%rsp), %rsi
callq 0x30420
movq %rax, 0x78(%rsp)
jmp 0x2d14a
movq 0x78(%rsp), %rax
movq %rax, 0x198(%rsp)
movl $0x0, 0x194(%rsp)
movl $0x0, 0x190(%rsp)
leaq 0x1f0(%rsp), %rax
movq %rax, 0x188(%rsp)
movq 0x188(%rsp), %rax
cmpq 0x198(%rsp), %rax
je 0x2d358
movq 0x188(%rsp), %rax
movl (%rax), %edi
callq 0xf690
movl %eax, 0x74(%rsp)
jmp 0x2d1a8
movl 0x74(%rsp), %eax
cmpl 0x1e8(%rsp), %eax
jne 0x2d33d
movq 0x9f8(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x188(%rsp), %rax
movl (%rax), %edi
callq 0xf680
movl %eax, 0x70(%rsp)
jmp 0x2d1db
movl 0x70(%rsp), %esi
movq 0x68(%rsp), %rdi
callq 0x11f10
movl %eax, 0x64(%rsp)
jmp 0x2d1ef
movl 0x64(%rsp), %eax
cmpl 0x1e4(%rsp), %eax
jne 0x2d33d
movq 0x188(%rsp), %rax
movl (%rax), %edi
callq 0xf680
movl %eax, 0x60(%rsp)
jmp 0x2d215
movl 0x60(%rsp), %edi
callq 0xce10
movl %eax, 0x5c(%rsp)
jmp 0x2d224
movl 0x1ec(%rsp), %edi
callq 0xce10
movl %eax, 0x58(%rsp)
jmp 0x2d236
movl 0x5c(%rsp), %eax
movl 0x58(%rsp), %ecx
cmpl %ecx, %eax
jne 0x2d253
movl 0x194(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x194(%rsp)
movq 0x188(%rsp), %rax
movl (%rax), %edi
callq 0xf680
movl %eax, 0x54(%rsp)
jmp 0x2d268
movl 0x54(%rsp), %edi
callq 0xce40
movl %eax, 0x50(%rsp)
jmp 0x2d277
movl 0x1ec(%rsp), %edi
callq 0xce40
movl %eax, 0x4c(%rsp)
jmp 0x2d289
movl 0x50(%rsp), %eax
movl 0x4c(%rsp), %ecx
cmpl %ecx, %eax
jne 0x2d2a6
movl 0x190(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x190(%rsp)
movq 0x188(%rsp), %rax
movl (%rax), %edi
callq 0xf680
movl %eax, 0x48(%rsp)
jmp 0x2d2bb
movl 0x48(%rsp), %edi
callq 0xce10
movl %eax, 0x44(%rsp)
jmp 0x2d2ca
movl 0x1ec(%rsp), %edi
callq 0xce10
movl %eax, 0x40(%rsp)
jmp 0x2d2dc
movl 0x44(%rsp), %eax
movl 0x40(%rsp), %ecx
cmpl %ecx, %eax
je 0x2d33b
movq 0x188(%rsp), %rax
movl (%rax), %edi
callq 0xf680
movl %eax, 0x3c(%rsp)
jmp 0x2d2fd
movl 0x3c(%rsp), %edi
callq 0xce40
movl %eax, 0x38(%rsp)
jmp 0x2d30c
movl 0x1ec(%rsp), %edi
callq 0xce40
movl %eax, 0x34(%rsp)
jmp 0x2d31e
movl 0x38(%rsp), %eax
movl 0x34(%rsp), %ecx
cmpl %ecx, %eax
je 0x2d33b
movl 0x190(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x190(%rsp)
jmp 0x2d33d
jmp 0x2d33f
movq 0x188(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x188(%rsp)
jmp 0x2d17d
cmpl $0x1, 0x194(%rsp)
jne 0x2d3a8
cmpl $0x1, 0x190(%rsp)
jle 0x2d3a8
movl 0x1ec(%rsp), %edi
callq 0xce10
movl %eax, 0x30(%rsp)
jmp 0x2d37e
movl 0x30(%rsp), %edi
callq 0x2e550
movb %al, 0x2f(%rsp)
jmp 0x2d38d
movb 0x2f(%rsp), %al
movsbl %al, %esi
leaq 0x1c0(%rsp), %rdi
callq 0x62d0
jmp 0x2d3a3
jmp 0x2d46f
cmpl $0x1, 0x194(%rsp)
jle 0x2d3f5
cmpl $0x1, 0x190(%rsp)
jne 0x2d3f5
movl 0x1ec(%rsp), %edi
callq 0xce40
movl %eax, 0x28(%rsp)
jmp 0x2d3ce
movl 0x28(%rsp), %edi
callq 0x2e570
movb %al, 0x27(%rsp)
jmp 0x2d3dd
movb 0x27(%rsp), %al
movsbl %al, %esi
leaq 0x1c0(%rsp), %rdi
callq 0x62d0
jmp 0x2d3f3
jmp 0x2d46d
cmpl $0x1, 0x194(%rsp)
jle 0x2d46b
cmpl $0x1, 0x190(%rsp)
jle 0x2d46b
movl 0x1ec(%rsp), %esi
leaq 0x168(%rsp), %rdi
callq 0x2e410
jmp 0x2d41f
leaq 0x1c0(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0x6690
jmp 0x2d436
leaq 0x168(%rsp), %rdi
callq 0x6a78
jmp 0x2d46b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
leaq 0x168(%rsp), %rdi
callq 0x6a78
jmp 0x2d6cf
jmp 0x2d46d
jmp 0x2d46f
jmp 0x2d471
movq 0x9f8(%rsp), %rdi
movl 0x9f4(%rsp), %esi
callq 0x2e590
movb %al, 0x26(%rsp)
jmp 0x2d48b
movb 0x26(%rsp), %al
testb $0x1, %al
jne 0x2d495
jmp 0x2d4ee
cmpl $0x1, 0x1e4(%rsp)
jne 0x2d4d8
movl 0x1ec(%rsp), %edi
callq 0xce10
movl %eax, 0x20(%rsp)
jmp 0x2d4b1
movl 0x20(%rsp), %edi
callq 0x2e550
movb %al, 0x1f(%rsp)
jmp 0x2d4c0
movb 0x1f(%rsp), %al
movsbl %al, %esi
leaq 0x1c0(%rsp), %rdi
callq 0x62d0
jmp 0x2d4d6
jmp 0x2d4d8
leaq 0x1c0(%rsp), %rdi
movl $0x78, %esi
callq 0x62d0
jmp 0x2d4ec
jmp 0x2d4ee
movl 0x1e8(%rsp), %esi
leaq 0x148(%rsp), %rdi
callq 0x2e410
jmp 0x2d504
leaq 0x1c0(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0x6690
jmp 0x2d51b
leaq 0x148(%rsp), %rdi
callq 0x6a78
movl 0x9f4(%rsp), %edi
callq 0xfb40
movb %al, 0x1e(%rsp)
jmp 0x2d53a
movb 0x1e(%rsp), %al
testb $0x1, %al
jne 0x2d544
jmp 0x2d5b7
leaq 0x1c0(%rsp), %rdi
movl $0x3d, %esi
callq 0x62d0
jmp 0x2d558
movl 0x9f4(%rsp), %edi
callq 0xf830
movl %eax, 0x18(%rsp)
jmp 0x2d56a
movl 0x18(%rsp), %edi
callq 0x2e340
movb %al, 0x17(%rsp)
jmp 0x2d579
movb 0x17(%rsp), %al
movsbl %al, %esi
leaq 0x1c0(%rsp), %rdi
callq 0x62d0
jmp 0x2d58f
jmp 0x2d5b7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x6a78
jmp 0x2d6cf
jmp 0x2d5b9
jmp 0x2d5bb
movq 0x9f8(%rsp), %rdi
movl 0x9f4(%rsp), %esi
leaq 0xa8(%rsp), %rdx
callq 0x3a390
jmp 0x2d5d9
movq 0x9f8(%rsp), %rdi
callq 0x2e600
movb %al, 0x16(%rsp)
jmp 0x2d5ec
movb 0x16(%rsp), %al
testb $0x1, %al
jne 0x2d5f6
jmp 0x2d645
movq 0x9f8(%rsp), %rdi
callq 0x3d4c0
movb %al, 0x15(%rsp)
jmp 0x2d609
movb 0x15(%rsp), %al
testb $0x1, %al
jne 0x2d613
jmp 0x2d621
leaq 0x2d253(%rip), %rax     # 0x5a86d
movq %rax, 0x8(%rsp)
jmp 0x2d62f
leaq 0x2c072(%rip), %rax     # 0x5969a
movq %rax, 0x8(%rsp)
jmp 0x2d62f
movq 0x8(%rsp), %rsi
leaq 0x1c0(%rsp), %rdi
callq 0x6480
jmp 0x2d643
jmp 0x2d67c
movq 0x9f8(%rsp), %rdi
callq 0x3d4c0
movb %al, 0x7(%rsp)
jmp 0x2d658
movb 0x7(%rsp), %al
testb $0x1, %al
jne 0x2d662
jmp 0x2d67a
leaq 0x2d204(%rip), %rsi     # 0x5a86d
leaq 0x1c0(%rsp), %rdi
callq 0x6480
jmp 0x2d678
jmp 0x2d67a
jmp 0x2d67c
movq 0x9f8(%rsp), %rdi
movl 0x9f4(%rsp), %esi
callq 0x3c620
jmp 0x2d692
movq 0x98(%rsp), %rdi
leaq 0x1c0(%rsp), %rsi
callq 0x62b0
movl $0x1, 0x1a8(%rsp)
leaq 0x1c0(%rsp), %rdi
callq 0x6a78
movq 0xa0(%rsp), %rax
addq $0xa08, %rsp            # imm = 0xA08
retq
leaq 0x1c0(%rsp), %rdi
callq 0x6a78
movq 0x1b0(%rsp), %rdi
callq 0x6970
nopl (%rax)