// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, 0x10(%rsp)
movq %rdi, 0x8(%rsp)
movb 0x39bb74(%rip), %al     # 0x3c1a68
testb %al, %al
je 0x260e9
movl 0x399b3a(%rip), %eax    # 0x3bfa3c
movq 0x3965e7(%rip), %rcx    # 0x3bc4f0
movq 0x3965d8(%rip), %rdx    # 0x3bc4e8
subq %rdx, %rcx
shrq $0x3, %rcx
imull $0x8c13521d, %ecx, %r12d # imm = 0x8C13521D
cmpl %r12d, %eax
cmovll %eax, %r12d
movl 0x8(%rdx), %r14d
movslq %r12d, %rax
imulq $0x1a8, %rax, %rax      # imm = 0x1A8
movl %r14d, %r15d
subl -0x1a0(%rdx,%rax), %r15d
leaq 0x17503(%rip), %rax     # 0x3d448
movl (%rax), %eax
cmpl %eax, %r15d
cmovgel %eax, %r15d
movl 0x399adf(%rip), %r13d   # 0x3bfa34
callq 0x173f0
movq %rax, %rcx
movabsq $-0x5c28f5c28f5c28f5, %rdx # imm = 0xA3D70A3D70A3D70B
imulq %rdx
addq %rcx, %rdx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x5, %rdx
addq %rax, %rdx
imulq $0x32, %rdx, %rdx
movq 0x39bac2(%rip), %rsi    # 0x3c1a48
movq 0x39bac3(%rip), %rdi    # 0x3c1a50
movq 0x39bac4(%rip), %rax    # 0x3c1a58
movq 0x39bac5(%rip), %r8     # 0x3c1a60
subq %rdx, %rcx
je 0x25ffc
movq %rcx, %rdx
negq %rdx
cmovsq %rcx, %rdx
incl %edx
movq %rdi, %rcx
rolq $0x7, %rcx
movq %rsi, %r9
movq %rax, %rsi
rolq $0xd, %rsi
xorq %rdi, %rsi
movq %r8, %rdi
rolq $0x25, %rdi
subq %rcx, %r9
addq %rax, %rdi
movq %r8, %rax
addq %r9, %rax
movq %r9, %r8
addq %rsi, %r8
decl %edx
cmpl $0x1, %edx
ja 0x25fac
movq %rsi, 0x39ba61(%rip)    # 0x3c1a48
movq %rdi, 0x39ba62(%rip)    # 0x3c1a50
movq %rax, 0x39ba63(%rip)    # 0x3c1a58
movq %r8, 0x39ba64(%rip)     # 0x3c1a60
testl %r12d, %r12d
jle 0x260da
addl %r13d, %r13d
movl $0x78, %r9d
subl %r13d, %r9d
movq 0x3964d0(%rip), %r10    # 0x3bc4e8
movl %r12d, %ecx
imulq $0x1a8, %rcx, %r11      # imm = 0x1A8
movl $0xffff8acf, %r13d      # imm = 0xFFFF8ACF
xorl %r12d, %r12d
movl 0x8(%r10,%r12), %ebx
testq %r12, %r12
je 0x26049
leal 0x200(%rbx), %ecx
cmpl %ecx, -0x1a0(%r10,%r12)
jg 0x260da
movl %r14d, %ebp
subl %ebx, %ebp
imull %r9d, %ebp
movq %rsi, %rcx
movq %rax, %rsi
rolq $0xd, %rsi
xorq %rdi, %rsi
rolq $0x7, %rdi
subq %rdi, %rcx
movq %r8, %rdi
rolq $0x25, %rdi
movq %rsi, 0x39b9d4(%rip)    # 0x3c1a48
addq %rax, %rdi
movq %rdi, 0x39b9d2(%rip)    # 0x3c1a50
addq %rcx, %r8
movq %r8, 0x39b9d0(%rip)     # 0x3c1a58
addq %rsi, %rcx
movq %rcx, 0x39b9ce(%rip)    # 0x3c1a60
movl %ecx, %eax
xorl %edx, %edx
divl %r9d
imull %r15d, %edx
addl %ebp, %edx
shrl $0x7, %edx
addl %ebx, %edx
cmpl %r13d, %edx
jle 0x260c4
movl 0x10(%r10,%r12), %eax
movq 0x8(%rsp), %rbx
movl %eax, (%rbx)
movl 0x14(%r10,%r12), %eax
movq 0x10(%rsp), %rbx
movl %eax, (%rbx)
movl %edx, %r13d
addq $0x1a8, %r12            # imm = 0x1A8
movq %r8, %rax
movq %rcx, %r8
cmpq %r12, %r11
jne 0x2602b
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x58d5
jmp 0x25efc