// File: /workspace/github_cpp_makefile_star2_O3/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 0x17e59
movl %edx, %r13d
testl %edx, %edx
jne 0x17e7b
leaq 0x10(%rbx), %rax
movq %rax, (%rbx)
leaq 0x1a8ae(%rip), %rsi     # 0x326f3
leaq 0x1a8ad(%rip), %rdx     # 0x326f9
movq %rbx, %rdi
callq 0xad16
jmp 0x17f8b
leaq 0x10(%rbx), %rax
movq %rax, (%rbx)
leaq 0x1a8d4(%rip), %rsi     # 0x3273b
leaq 0x1a8d3(%rip), %rdx     # 0x32741
movq %rbx, %rdi
callq 0xad16
jmp 0x17f8b
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 0x17eaa
movl $0x5, %r8d
leaq 0x197e8(%rip), %rcx     # 0x31690
jmp 0x17ed2
movl %edx, 0x28(%rsp)
cmpl $0xc000, %edx           # imm = 0xC000
setne %al
cmpl %r15d, %r14d
setbe %cl
orb %al, %cl
jne 0x17fbc
movl $0x3, %r8d
leaq 0x197c0(%rip), %rcx     # 0x31692
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x5640
leaq 0x40(%rsp), %rdx
movq %r12, %rdi
movl %r13d, %esi
callq 0x1f3d6
movq 0x1938(%r12), %rax
cmpq $0x0, 0x40(%rax)
je 0x17f1d
movq %r12, %rdi
callq 0x20c08
leaq 0x1a83d(%rip), %rcx     # 0x3274b
leaq 0x198e5(%rip), %rsi     # 0x317fa
testb %al, %al
cmovneq %rcx, %rsi
jmp 0x17f30
movq %r12, %rdi
callq 0x20c08
testb %al, %al
je 0x17f3a
leaq 0x1a81b(%rip), %rsi     # 0x3274b
leaq 0x8(%rsp), %rdi
callq 0x5820
movq %r12, %rdi
movl %r13d, %esi
callq 0x2051a
leaq 0x10(%rbx), %rax
movq %rax, (%rbx)
movq 0x8(%rsp), %rcx
leaq 0x18(%rsp), %rdx
cmpq %rdx, %rcx
je 0x17f69
movq %rcx, (%rbx)
movq 0x18(%rsp), %rax
movq %rax, 0x10(%rbx)
jmp 0x17f6f
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 0x17fa7
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
movq %r12, 0x30(%rsp)
andl $0x7, %esi
movq %rsi, 0x38(%rsp)
cmpl $0x1, %esi
je 0x180a5
leaq 0x1a774(%rip), %rax     # 0x3274d
movq 0x38(%rsp), %rcx
movsbl (%rcx,%rax), %r8d
leaq 0x8(%rsp), %rdi
movl $0x1, %ecx
xorl %esi, %esi
xorl %edx, %edx
callq 0x5800
movq 0x30(%rsp), %rdi
leaq 0xe0(%rsp), %r12
movq %r12, %rsi
callq 0x1a5c0
cmpq %rax, %r12
je 0x180a5
xorl %ecx, %ecx
xorl %edx, %edx
movl (%r12), %esi
movl %esi, %edi
andl $0x3f, %edi
cmpl %r14d, %edi
jne 0x1805d
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 0x1805d
xorl %ebp, %esi
xorl %r15d, %edi
cmpl $0x8, %edi
adcl $0x0, %ecx
testb $0x7, %sil
je 0x1805b
cmpl $0x8, %edi
sbbl $-0x1, %ecx
jmp 0x1805d
incl %edx
addq $0x8, %r12
cmpq %rax, %r12
jne 0x18018
cmpl $0x1, %edx
jne 0x1807d
cmpl $0x1, %ecx
jle 0x1807d
movl %ebp, %r15d
andb $0x7, %r15b
addb $0x61, %r15b
jmp 0x18097
cmpl $0x2, %edx
jl 0x181ae
cmpl $0x1, %ecx
jne 0x181ae
shrl $0x3, %r15d
addb $0x31, %r15b
movzbl %r15b, %esi
leaq 0x8(%rsp), %rdi
callq 0x5330
testl $0x18000, %r13d         # imm = 0x18000
je 0x180bf
cmpl $0xc000, 0x28(%rsp)     # imm = 0xC000
movq 0x30(%rsp), %r12
jne 0x180ce
jmp 0x180fa
movl %r14d, %eax
movq 0x30(%rsp), %r12
cmpl $0x10, (%r12,%rax,4)
je 0x180fa
cmpl $0x1, 0x38(%rsp)
jne 0x180eb
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 0xad16
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
leaq 0x8(%rsp), %rdi
callq 0x5130
movl 0x28(%rsp), %ebp
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x1816f
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x5400
cmpl $0x4000, %ebp           # imm = 0x4000
jne 0x17ee0
leaq 0x8(%rsp), %rdi
movl $0x3d, %esi
callq 0x5330
movl %r13d, %eax
shrl $0xc, %eax
andl $0x3, %eax
leaq 0x1a5b3(%rip), %rcx     # 0x3274d
movsbl 0x2(%rax,%rcx), %esi
leaq 0x8(%rsp), %rdi
callq 0x5330
jmp 0x17ee0
cmpl $0x2, %edx
jl 0x180a5
cmpl $0x2, %ecx
jl 0x180a5
leaq 0x40(%rsp), %rdi
movl %r15d, %esi
callq 0x18b39
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 0x180a5
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x5400
jmp 0x180a5
movq %rax, %rbx
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
jne 0x1822b
jmp 0x18245
jmp 0x18242
jmp 0x18242
jmp 0x18242
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x18245
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x5400
jmp 0x18245
jmp 0x18242
jmp 0x18242
jmp 0x18242
jmp 0x18242
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x18261
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5400
movq %rbx, %rdi
callq 0x5790