// File: jswaff[P]prophet3/src/book.cpp
// Function: probe_book(position*)
move probe_book(position *p) {
    move_freq_pair mfp[300];
    int32 num_book_moves = get_book_moves(mfp,p);

    // get the total frequency of all book moves
    int32 total_freq=0;
    char move_buffer[8];
    for (int i=0;i<num_book_moves;i++) {
        if (mfp[i].freq > 0) {
            move_to_str(mfp[i].mv,move_buffer);
            printd("# book move: %s, freq: %d\n",move_buffer,mfp[i].freq);
            total_freq += mfp[i].freq;
        }
    }
    printd("# total freq: %d\n",total_freq);
    if (total_freq==0) {
        return 0;
    }

    // do a weighted random selection
    srand(time(NULL));
    double g = rand() / ((double) RAND_MAX); // [0-1]
    double random_weight = g * total_freq;
    printd("# random weight: %f\n",random_weight);

    int sum_freq = 0;
    move selected_mv = 0;
    for (int i=0;i<num_book_moves;i++) {
        if (mfp[i].freq > 0) {
            sum_freq += mfp[i].freq;
            if (sum_freq >= random_weight) {
                selected_mv = mfp[i].mv;
                move_to_str(selected_mv,move_buffer);
                printd("# selected move %s with sum_freq=%d\n",move_buffer,sum_freq);
                break;
            }
        }
    }

    return selected_mv;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1310, %rsp           # imm = 0x1310
movq %rdi, -0x10(%rbp)
leaq -0x12d0(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x5ed0
movl %eax, -0x12d4(%rbp)
movl $0x0, -0x12d8(%rbp)
movl $0x0, -0x12e4(%rbp)
movl -0x12e4(%rbp), %eax
cmpl -0x12d4(%rbp), %eax
jge 0x5d41
movslq -0x12e4(%rbp), %rcx
leaq -0x12d0(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
cmpl $0x0, 0x8(%rax)
jle 0x5d2b
movslq -0x12e4(%rbp), %rcx
leaq -0x12d0(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdi
leaq -0x12e0(%rbp), %rsi
callq 0x14350
leaq -0x12e0(%rbp), %rsi
movslq -0x12e4(%rbp), %rcx
leaq -0x12d0(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %edx
leaq 0x2f705(%rip), %rdi     # 0x35405
movb $0x0, %al
callq 0x8db0
movslq -0x12e4(%rbp), %rcx
leaq -0x12d0(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
addl -0x12d8(%rbp), %eax
movl %eax, -0x12d8(%rbp)
jmp 0x5d2d
movl -0x12e4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x12e4(%rbp)
jmp 0x5c89
movl -0x12d8(%rbp), %esi
leaq 0x2f6d2(%rip), %rdi     # 0x35420
movb $0x0, %al
callq 0x8db0
cmpl $0x0, -0x12d8(%rbp)
jne 0x5d6b
movq $0x0, -0x8(%rbp)
jmp 0x5ec2
xorl %eax, %eax
movl %eax, %edi
callq 0x2130
movl %eax, %edi
callq 0x2150
callq 0x2060
cvtsi2sd %eax, %xmm0
movsd 0x2f5d4(%rip), %xmm1    # 0x35360
divsd %xmm1, %xmm0
movsd %xmm0, -0x12f0(%rbp)
movsd -0x12f0(%rbp), %xmm0
cvtsi2sdl -0x12d8(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x12f8(%rbp)
movsd -0x12f8(%rbp), %xmm0
leaq 0x2f66f(%rip), %rdi     # 0x35432
movb $0x1, %al
callq 0x8db0
movl $0x0, -0x12fc(%rbp)
movq $0x0, -0x1308(%rbp)
movl $0x0, -0x130c(%rbp)
movl -0x130c(%rbp), %eax
cmpl -0x12d4(%rbp), %eax
jge 0x5eb7
movslq -0x130c(%rbp), %rcx
leaq -0x12d0(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
cmpl $0x0, 0x8(%rax)
jle 0x5ea1
movslq -0x130c(%rbp), %rcx
leaq -0x12d0(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
addl -0x12fc(%rbp), %eax
movl %eax, -0x12fc(%rbp)
cvtsi2sdl -0x12fc(%rbp), %xmm0
ucomisd -0x12f8(%rbp), %xmm0
jb 0x5e9f
movslq -0x130c(%rbp), %rcx
leaq -0x12d0(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0x1308(%rbp)
movq -0x1308(%rbp), %rdi
leaq -0x12e0(%rbp), %rsi
callq 0x14350
leaq -0x12e0(%rbp), %rsi
movl -0x12fc(%rbp), %edx
leaq 0x2f5b1(%rip), %rdi     # 0x35447
movb $0x0, %al
callq 0x8db0
jmp 0x5eb7
jmp 0x5ea1
jmp 0x5ea3
movl -0x130c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x130c(%rbp)
jmp 0x5de9
movq -0x1308(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x1310, %rsp           # imm = 0x1310
popq %rbp
retq
nop