// File: cpirc[P]Monochrome/src/search.cpp
// Function: quiesce(SearchController&, Position&, int, int, SearchStack*)
int quiesce(SearchController& sc, Position& pos, int alpha, int beta,
            SearchStack* ss) {
    assert(ss);

    if (ss->ply >= MAX_PLY) {
        return evaluate(pos);
    }

    // Check time left
    clock_t current_time = clock() * 1000 / CLOCKS_PER_SEC;
    if (current_time >= sc.search_end_time) {
        return 0;
    }

    int movecount, value;

    value = evaluate(pos);
    if (value >= beta) return beta;

    if (value > alpha) alpha = value;

    movecount = generate_captures(pos, ss->ml);

    ++ss->stats->node_count;

    score_moves(pos, ss, movecount, 0);

    Move move;
    while ((move = next_move(ss, movecount))) {
        Position npos = pos;

        make_move(npos, move);
        if (is_checked(npos, THEM)) {
            continue;
        }

        value = -quiesce(sc, npos, -beta, -alpha, ss + 1);

        if (value >= beta) {
            return beta;
        }
        if (value > alpha) {
            alpha = value;
        }
    }

    return alpha;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0xb2e6
jmp 0xb305
leaq 0x8d74(%rip), %rdi      # 0x14061
leaq 0x8d70(%rip), %rsi      # 0x14064
movl $0x6a, %edx
leaq 0x8dd5(%rip), %rcx      # 0x140d5
callq 0x3150
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x40, %eax
jl 0xb322
movq -0x18(%rbp), %rdi
callq 0x3bb0
movl %eax, -0x4(%rbp)
jmp 0xb500
callq 0x3160
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
movl $0xf4240, %ecx          # imm = 0xF4240
cqto
idivq %rcx
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x80(%rcx), %rax
jl 0xb359
movl $0x0, -0x4(%rbp)
jmp 0xb500
movq -0x18(%rbp), %rdi
callq 0x3bb0
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x20(%rbp), %eax
jl 0xb378
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xb500
movl -0x38(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jle 0xb386
movl -0x38(%rbp), %eax
movl %eax, -0x1c(%rbp)
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
addq $0x4, %rsi
callq 0x8b70
movl %eax, -0x34(%rbp)
movq -0x28(%rbp), %rax
movq 0x810(%rax), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl -0x34(%rbp), %edx
xorl %ecx, %ecx
callq 0xb000
movq -0x28(%rbp), %rdi
leaq -0x34(%rbp), %rsi
callq 0xb1c0
movl %eax, -0x3c(%rbp)
cmpl $0x0, %eax
je 0xb4fa
movq -0x18(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
movq %rdi, -0xc0(%rbp)
callq 0x6a30
movq -0xc0(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0x5900
jmp 0xb402
leaq -0xa8(%rbp), %rdi
movl $0x1, %esi
callq 0x6b80
movb %al, -0xc1(%rbp)
jmp 0xb41b
movb -0xc1(%rbp), %al
testb $0x1, %al
jne 0xb427
jmp 0xb459
movl $0x2, -0xb8(%rbp)
jmp 0xb4c5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x6b60
jmp 0xb50c
movq -0x10(%rbp), %rdi
movl -0x20(%rbp), %edx
negl %edx
movl -0x1c(%rbp), %ecx
negl %ecx
movq -0x28(%rbp), %r8
addq $0x818, %r8             # imm = 0x818
leaq -0xa8(%rbp), %rsi
callq 0xb2c0
movl %eax, -0xc8(%rbp)
jmp 0xb486
movl -0xc8(%rbp), %ecx
xorl %eax, %eax
subl %ecx, %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x20(%rbp), %eax
jl 0xb4ad
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0xb8(%rbp)
jmp 0xb4c5
movl -0x38(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jle 0xb4bb
movl -0x38(%rbp), %eax
movl %eax, -0x1c(%rbp)
movl $0x0, -0xb8(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x6b60
movl -0xb8(%rbp), %eax
movl %eax, -0xcc(%rbp)
testl %eax, %eax
je 0xb4f5
jmp 0xb4e3
movl -0xcc(%rbp), %eax
subl $0x1, %eax
je 0xb500
jmp 0xb4f0
jmp 0xb3c1
jmp 0xb3c1
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
movq -0xb0(%rbp), %rdi
callq 0x3350
nopl (%rax,%rax)