// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x8e8, %rsp            # imm = 0x8E8
movq %rsi, %r14
movl %edx, %ebp
shrl $0x6, %ebp
movl %ebp, %r12d
andl $0x3f, %r12d
movl (%rsi,%r12,4), %esi
leaq 0x28(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movq %rdi, 0x10(%rsp)
movb $0x0, (%rax)
cmpl $0x41, %edx
je 0x13735
movl %edx, %r15d
testl %edx, %edx
jne 0x13750
leaq 0x15e62(%rip), %rsi     # 0x29583
leaq 0x40(%rsp), %rdx
movq 0x10(%rsp), %rdi
callq 0x9c42
jmp 0x13850
leaq 0x15e8f(%rip), %rsi     # 0x295cb
leaq 0x40(%rsp), %rdx
movq 0x10(%rsp), %rdi
callq 0x9c42
jmp 0x13850
movl %r15d, %ebx
andl $0x3f, %ebx
movl $0xc000, %eax           # imm = 0xC000
movl %r15d, %edx
andl %eax, %edx
cmpl %eax, %edx
sete %al
cmpl %r12d, %ebx
setb %cl
testb %cl, %al
je 0x13778
leaq 0x14dba(%rip), %rsi     # 0x28530
jmp 0x1379a
movl %edx, 0xc(%rsp)
cmpl $0xc000, %edx           # imm = 0xC000
setne %al
cmpl %r12d, %ebx
setbe %cl
orb %al, %cl
jne 0x13871
leaq 0x14d98(%rip), %rsi     # 0x28532
leaq 0x18(%rsp), %rdi
callq 0x5800
leaq 0x40(%rsp), %rdx
movq %r14, %rdi
movl %r15d, %esi
callq 0x19114
movq 0x1938(%r14), %rax
cmpq $0x0, 0x40(%rax)
je 0x137e0
movq %r14, %rdi
callq 0x1a678
leaq 0x15e0a(%rip), %rcx     # 0x295db
leaq 0x14ec2(%rip), %rsi     # 0x2869a
testb %al, %al
cmovneq %rcx, %rsi
jmp 0x137f3
movq %r14, %rdi
callq 0x1a678
testb %al, %al
je 0x137fd
leaq 0x15de8(%rip), %rsi     # 0x295db
leaq 0x18(%rsp), %rdi
callq 0x57f0
movq %r14, %rdi
movl %r15d, %esi
callq 0x1a056
movq 0x10(%rsp), %rdx
leaq 0x10(%rdx), %rax
movq %rax, (%rdx)
movq 0x18(%rsp), %rcx
leaq 0x28(%rsp), %rsi
cmpq %rsi, %rcx
je 0x13831
movq %rcx, (%rdx)
movq 0x28(%rsp), %rax
movq %rax, 0x10(%rdx)
jmp 0x13837
movups (%rsi), %xmm0
movups %xmm0, (%rax)
movq 0x20(%rsp), %rax
movq %rax, 0x8(%rdx)
movq %rsi, 0x18(%rsp)
andq $0x0, 0x20(%rsp)
movb $0x0, 0x28(%rsp)
leaq 0x18(%rsp), %rdi
callq 0x5858
movq 0x10(%rsp), %rax
addq $0x8e8, %rsp            # imm = 0x8E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
andl $0x7, %esi
movq %rsi, 0x38(%rsp)
cmpl $0x1, %esi
je 0x1393c
leaq 0x15d54(%rip), %rax     # 0x295dd
movq 0x38(%rsp), %rcx
movsbl (%rcx,%rax), %esi
leaq 0x18(%rsp), %rdi
callq 0x5710
leaq 0xe0(%rsp), %r13
movq %r14, %rdi
movq %r13, %rsi
callq 0x1543a
xorl %edx, %edx
xorl %ecx, %ecx
cmpq %rax, %r13
je 0x138fd
movl (%r13), %esi
movl %esi, %edi
andl $0x3f, %edi
cmpl %ebx, %edi
jne 0x138f7
shrl $0x6, %esi
movl %esi, %edi
andl $0x3f, %edi
movl (%r14,%rdi,4), %r8d
andl $0x7, %r8d
cmpl 0x38(%rsp), %r8d
jne 0x138f7
xorl %ebp, %esi
xorl %r12d, %edi
cmpl $0x8, %edi
adcl $0x0, %ecx
testb $0x7, %sil
je 0x138f5
cmpl $0x8, %edi
sbbl $-0x1, %ecx
jmp 0x138f7
incl %edx
addq $0x8, %r13
jmp 0x138b3
cmpl $0x1, %edx
jne 0x13914
cmpl $0x1, %ecx
jle 0x13914
movl %ebp, %r12d
andb $0x7, %r12b
addb $0x61, %r12b
jmp 0x1392e
cmpl $0x2, %edx
jl 0x139ea
cmpl $0x1, %ecx
jne 0x139ea
shrl $0x3, %r12d
addb $0x31, %r12b
movzbl %r12b, %esi
leaq 0x18(%rsp), %rdi
callq 0x5320
testl $0x18000, %r15d         # imm = 0x18000
je 0x13951
cmpl $0xc000, 0xc(%rsp)      # imm = 0xC000
jne 0x1395a
jmp 0x13984
movl %ebx, %eax
cmpl $0x10, (%r14,%rax,4)
je 0x13984
cmpl $0x1, 0x38(%rsp)
jne 0x13977
andb $0x7, %bpl
addb $0x61, %bpl
movzbl %bpl, %esi
leaq 0x18(%rsp), %rdi
callq 0x5320
leaq 0x18(%rsp), %rdi
pushq $0x78
popq %rsi
callq 0x5320
leaq 0x40(%rsp), %rdi
movl %ebx, %esi
callq 0x13fec
leaq 0x18(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x5830
movl 0xc(%rsp), %ebx
leaq 0x40(%rsp), %rdi
callq 0x5858
cmpl $0x4000, %ebx           # imm = 0x4000
jne 0x137a4
leaq 0x18(%rsp), %rdi
pushq $0x3d
popq %rsi
callq 0x5320
movl %r15d, %eax
shrl $0xc, %eax
andl $0x3, %eax
leaq 0x15c07(%rip), %rcx     # 0x295dd
movsbl 0x2(%rax,%rcx), %esi
leaq 0x18(%rsp), %rdi
callq 0x5320
jmp 0x137a4
cmpl $0x2, %edx
jl 0x1393c
cmpl $0x2, %ecx
jl 0x1393c
leaq 0x40(%rsp), %rdi
movl %r12d, %esi
callq 0x13fec
leaq 0x18(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x5830
leaq 0x40(%rsp), %rdi
callq 0x5858
jmp 0x1393c
jmp 0x13a2f
jmp 0x13a46
jmp 0x13a46
jmp 0x13a46
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x5858
jmp 0x13a49
jmp 0x13a46
jmp 0x13a46
jmp 0x13a46
jmp 0x13a46
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x5858
movq %rbx, %rdi
callq 0x5780