// File: ATOMIC-LICHESS[P]ATOMKRAFT/search.cpp
// Function: (anonymous namespace)::do_skill_level(Move*, Move*)
void do_skill_level(Move* best, Move* ponder) {

    assert(MultiPV > 1);

    static RKISS rk;

    // Rml list is already sorted by pv_score in descending order
    int s;
    int max_s = -VALUE_INFINITE;
    int size = Min(MultiPV, (int)Rml.size());
    int max = Rml[0].pv_score;
    int var = Min(max - Rml[size - 1].pv_score, PawnValueMidgame);
    int wk = 120 - 2 * SkillLevel;

    // PRNG sequence should be non deterministic
    for (int i = abs(get_system_time() % 50); i > 0; i--)
        rk.rand<unsigned>();

    // Choose best move. For each move's score we add two terms both dependent
    // on wk, one deterministic and bigger for weaker moves, and one random,
    // then we choose the move with the resulting highest score.
    for (int i = 0; i < size; i++)
    {
        s = Rml[i].pv_score;

        // Don't allow crazy blunders even at very low skills
        if (i > 0 && Rml[i-1].pv_score > s + EasyMoveMargin)
            break;

        // This is our magical formula
        s += ((max - s) * wk + var * (rk.rand<unsigned>() % wk)) / 128;

        if (s > max_s)
        {
            max_s = s;
            *best = Rml[i].pv[0];
            *ponder = Rml[i].pv[1];
        }
    }
  }

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
cmpb $0x0, 0x3a9a93(%rip)    # 0x3f0c18
jne 0x471b2
leaq 0x3a9a8a(%rip), %rdi    # 0x3f0c18
callq 0x69c0
cmpl $0x0, %eax
je 0x471b2
leaq 0x3a9a59(%rip), %rdi    # 0x3f0bf8
callq 0xf5f0
jmp 0x471a6
leaq 0x3a9a6b(%rip), %rdi    # 0x3f0c18
callq 0x62f0
movl $0x7531, %edi           # imm = 0x7531
callq 0x1b2b0
movl %eax, 0x34(%rsp)
movl 0x3a7a26(%rip), %eax    # 0x3eebec
movl %eax, 0x18(%rsp)
leaq 0x3a44c7(%rip), %rdi    # 0x3eb698
callq 0x45860
movq %rax, %rcx
movl 0x18(%rsp), %eax
cmpl %ecx, %eax
jge 0x471ed
movl 0x3a7a05(%rip), %eax    # 0x3eebec
movl %eax, 0x14(%rsp)
jmp 0x471fd
leaq 0x3a44a4(%rip), %rdi    # 0x3eb698
callq 0x45860
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0x30(%rsp)
leaq 0x3a448c(%rip), %rdi    # 0x3eb698
xorl %eax, %eax
movl %eax, %esi
callq 0x46fc0
movl 0x8(%rax), %eax
movl %eax, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movl %eax, 0x10(%rsp)
movl 0x30(%rsp), %eax
subl $0x1, %eax
movslq %eax, %rsi
leaq 0x3a4463(%rip), %rdi    # 0x3eb698
callq 0x46fc0
movq %rax, %rcx
movl 0x10(%rsp), %eax
subl 0x8(%rcx), %eax
leaq 0x2530d(%rip), %rcx     # 0x6c558
cmpl (%rcx), %eax
jge 0x4727d
movl 0x2c(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x30(%rsp), %eax
subl $0x1, %eax
movslq %eax, %rsi
leaq 0x3a4430(%rip), %rdi    # 0x3eb698
callq 0x46fc0
movq %rax, %rcx
movl 0x8(%rsp), %eax
subl 0x8(%rcx), %eax
movl %eax, 0xc(%rsp)
jmp 0x4728a
leaq 0x252d4(%rip), %rax     # 0x6c558
movl (%rax), %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x28(%rsp)
movl 0x3a794c(%rip), %ecx    # 0x3eebe4
shll %ecx
movl $0x78, %eax
subl %ecx, %eax
movl %eax, 0x24(%rsp)
callq 0x2c170
movl $0x32, %ecx
cqto
idivq %rcx
movq %rdx, %rdi
callq 0x4ee20
movl %eax, 0x20(%rsp)
cmpl $0x0, 0x20(%rsp)
jle 0x472ff
leaq 0x3a992a(%rip), %rdi    # 0x3f0bf8
callq 0xf600
movl 0x20(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x472c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x40(%rsp)
movl %eax, 0x3c(%rsp)
leaq 0x3a9923(%rip), %rdi    # 0x3f0c18
callq 0x62e0
jmp 0x47400
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
cmpl 0x30(%rsp), %eax
jge 0x473fb
movslq 0x1c(%rsp), %rsi
leaq 0x3a4377(%rip), %rdi    # 0x3eb698
callq 0x46fc0
movl 0x8(%rax), %eax
movl %eax, 0x38(%rsp)
cmpl $0x0, 0x1c(%rsp)
jle 0x47360
movl 0x1c(%rsp), %eax
subl $0x1, %eax
movslq %eax, %rsi
leaq 0x3a4353(%rip), %rdi    # 0x3eb698
callq 0x46fc0
movl 0x8(%rax), %eax
movl 0x38(%rsp), %ecx
addl $0x200, %ecx            # imm = 0x200
cmpl %ecx, %eax
jle 0x47360
jmp 0x473fb
movl 0x2c(%rsp), %eax
subl 0x38(%rsp), %eax
imull 0x24(%rsp), %eax
movl %eax, 0x4(%rsp)
movl 0x28(%rsp), %eax
movl %eax, (%rsp)
leaq 0x3a9879(%rip), %rdi    # 0x3f0bf8
callq 0xf600
movl (%rsp), %ecx
xorl %edx, %edx
divl 0x24(%rsp)
movl 0x4(%rsp), %eax
imull %edx, %ecx
addl %ecx, %eax
shrl $0x7, %eax
addl 0x38(%rsp), %eax
movl %eax, 0x38(%rsp)
movl 0x38(%rsp), %eax
cmpl 0x34(%rsp), %eax
jle 0x473e9
movl 0x38(%rsp), %eax
movl %eax, 0x34(%rsp)
movslq 0x1c(%rsp), %rsi
leaq 0x3a42d9(%rip), %rdi    # 0x3eb698
callq 0x46fc0
movl 0x10(%rax), %ecx
movq 0x50(%rsp), %rax
movl %ecx, (%rax)
movslq 0x1c(%rsp), %rsi
leaq 0x3a42be(%rip), %rdi    # 0x3eb698
callq 0x46fc0
movl 0x14(%rax), %ecx
movq 0x48(%rsp), %rax
movl %ecx, (%rax)
jmp 0x473eb
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x47307
addq $0x58, %rsp
retq
movq 0x40(%rsp), %rdi
callq 0x6970
nopw (%rax,%rax)