// File: /workspace/github_cpp_makefile_star2_O1/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, %r12
movq %rdi, %rbx
movl %edx, %ebp
shrl $0x6, %ebp
movl %ebp, %r15d
andl $0x3f, %r15d
movl (%rsi,%r15,4), %esi
leaq 0x18(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
cmpl $0x41, %edx
je 0x178fe
movl %edx, %r13d
testl %edx, %edx
jne 0x17920
leaq 0x10(%rbx), %rax
movq %rax, (%rbx)
leaq 0x19d89(%rip), %rsi     # 0x31673
leaq 0x19d88(%rip), %rdx     # 0x31679
movq %rbx, %rdi
callq 0xa7de
jmp 0x17a30
leaq 0x10(%rbx), %rax
movq %rax, (%rbx)
leaq 0x19daf(%rip), %rsi     # 0x316bb
leaq 0x19dae(%rip), %rdx     # 0x316c1
movq %rbx, %rdi
callq 0xa7de
jmp 0x17a30
movl %r13d, %r14d
andl $0x3f, %r14d
movl $0xc000, %eax           # imm = 0xC000
movl %r13d, %edx
andl %eax, %edx
cmpl %eax, %edx
sete %al
cmpl %r15d, %r14d
setb %cl
testb %cl, %al
je 0x1794f
movl $0x5, %r8d
leaq 0x18cc3(%rip), %rcx     # 0x30610
jmp 0x17977
movl %edx, 0x28(%rsp)
cmpl $0xc000, %edx           # imm = 0xC000
setne %al
cmpl %r15d, %r14d
setbe %cl
orb %al, %cl
jne 0x17a61
movl $0x3, %r8d
leaq 0x18c9b(%rip), %rcx     # 0x30612
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x5640
leaq 0x40(%rsp), %rdx
movq %r12, %rdi
movl %r13d, %esi
callq 0x1ef10
movq 0x1938(%r12), %rax
cmpq $0x0, 0x40(%rax)
je 0x179c2
movq %r12, %rdi
callq 0x2074c
leaq 0x19d18(%rip), %rcx     # 0x316cb
leaq 0x18dc0(%rip), %rsi     # 0x3077a
testb %al, %al
cmovneq %rcx, %rsi
jmp 0x179d5
movq %r12, %rdi
callq 0x2074c
testb %al, %al
je 0x179df
leaq 0x19cf6(%rip), %rsi     # 0x316cb
leaq 0x8(%rsp), %rdi
callq 0x5820
movq %r12, %rdi
movl %r13d, %esi
callq 0x20056
leaq 0x10(%rbx), %rax
movq %rax, (%rbx)
movq 0x8(%rsp), %rcx
leaq 0x18(%rsp), %rdx
cmpq %rdx, %rcx
je 0x17a0e
movq %rcx, (%rbx)
movq 0x18(%rsp), %rax
movq %rax, 0x10(%rbx)
jmp 0x17a14
movups (%rdx), %xmm0
movups %xmm0, (%rax)
movq 0x10(%rsp), %rax
movq %rax, 0x8(%rbx)
movq %rdx, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movb $0x0, 0x18(%rsp)
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x17a4c
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5400
movq %rbx, %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 0x17b5e
leaq 0x19c54(%rip), %rax     # 0x316cd
movq 0x38(%rsp), %rcx
movsbl (%rcx,%rax), %r8d
leaq 0x8(%rsp), %rdi
movl $0x1, %ecx
xorl %esi, %esi
xorl %edx, %edx
callq 0x5800
movq %r12, %rdi
leaq 0xe0(%rsp), %r12
movq %rdi, 0x30(%rsp)
movq %r12, %rsi
callq 0x1a0ce
cmpq %rax, %r12
je 0x17b11
xorl %ecx, %ecx
xorl %edx, %edx
movl (%r12), %esi
movl %esi, %edi
andl $0x3f, %edi
cmpl %r14d, %edi
jne 0x17b06
shrl $0x6, %esi
movl %esi, %edi
andl $0x3f, %edi
movq 0x30(%rsp), %r8
movl (%r8,%rdi,4), %r8d
andl $0x7, %r8d
cmpl 0x38(%rsp), %r8d
jne 0x17b06
xorl %ebp, %esi
xorl %r8d, %r8d
testb $0x7, %sil
sete %r8b
addl %r8d, %edx
xorl %r15d, %edi
cmpl $0x8, %edi
adcl $0x0, %ecx
testb $0x7, %sil
je 0x17b06
cmpl $0x8, %edi
sbbl $-0x1, %ecx
addq $0x8, %r12
cmpq %rax, %r12
jne 0x17ab7
jmp 0x17b15
xorl %edx, %edx
xorl %ecx, %ecx
cmpl $0x1, %edx
jne 0x17b31
cmpl $0x1, %ecx
jle 0x17b31
movl %ebp, %r15d
andb $0x7, %r15b
addb $0x61, %r15b
movq 0x30(%rsp), %r12
jmp 0x17b50
cmpl $0x2, %edx
movq 0x30(%rsp), %r12
jl 0x17c5e
cmpl $0x1, %ecx
jne 0x17c5e
shrl $0x3, %r15d
addb $0x31, %r15b
movzbl %r15b, %esi
leaq 0x8(%rsp), %rdi
callq 0x5330
testl $0x18000, %r13d         # imm = 0x18000
je 0x17b71
cmpl $0xc000, 0x28(%rsp)     # imm = 0xC000
jmp 0x17b79
movl %r14d, %eax
cmpl $0x10, (%r12,%rax,4)
setne %al
testb %al, %al
je 0x17bac
cmpl $0x1, 0x38(%rsp)
jne 0x17b9d
andb $0x7, %bpl
addb $0x61, %bpl
movzbl %bpl, %esi
leaq 0x8(%rsp), %rdi
callq 0x5330
leaq 0x8(%rsp), %rdi
movl $0x78, %esi
callq 0x5330
movl %r13d, %eax
andb $0x7, %al
addb $0x61, %al
leaq 0x2d(%rsp), %r15
movb %al, (%r15)
shrl $0x3, %r14d
addb $0x31, %r14b
movb %r14b, 0x1(%r15)
movb $0x0, 0x2(%r15)
leaq 0x50(%rsp), %r14
movq %r14, -0x10(%r14)
movq %r15, %rdi
callq 0x5190
leaq (%rsp,%rax), %rdx
addq $0x2d, %rdx
leaq 0x40(%rsp), %rdi
movq %r15, %rsi
callq 0xa7de
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
leaq 0x8(%rsp), %rdi
callq 0x5130
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x17c1d
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x5400
cmpl $0x4000, 0x28(%rsp)     # imm = 0x4000
jne 0x17985
leaq 0x8(%rsp), %rdi
movl $0x3d, %esi
callq 0x5330
movl %r13d, %eax
shrl $0xc, %eax
andl $0x3, %eax
leaq 0x19a83(%rip), %rcx     # 0x316cd
movsbl 0x2(%rax,%rcx), %esi
leaq 0x8(%rsp), %rdi
callq 0x5330
jmp 0x17985
cmpl $0x2, %edx
jl 0x17b5e
cmpl $0x2, %ecx
jl 0x17b5e
leaq 0x40(%rsp), %rdi
movl %r15d, %esi
callq 0x185f4
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
leaq 0x8(%rsp), %rdi
callq 0x5130
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x17b5e
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x5400
jmp 0x17b5e
movq %rax, %rbx
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
jne 0x17cdb
jmp 0x17cf5
jmp 0x17cf2
jmp 0x17cf2
jmp 0x17cf2
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x17cf5
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x5400
jmp 0x17cf5
jmp 0x17cf2
jmp 0x17cf2
jmp 0x17cf2
jmp 0x17cf2
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x17d11
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5400
movq %rbx, %rdi
callq 0x5790