// File: /workspace/github_c_makefile_star2_O2/icarroll[P]clunk/thudlib.c
// Function: absearch
int absearch(struct thudboard * board, int depth, int width,
             int trmin, int dwmax, struct move * bestmove,
             time_t stoptime, jmp_buf stopsearch)
{
    if (stoptime && time(NULL) > stoptime) longjmp(stopsearch, true);

    if (depth < 1) return evaluate(board);

    struct move * move;
    bool cutoff = false;

    int nextdepth = depth - 1;

    bool isdwarfturn = board->isdwarfturn;

    struct movelist list = allmoves(board);
    struct moveheap queue = heapof(board, & list);

    for (int i = width; i > 0 && queue.used > 0; --i)
    {
        move = pop(& queue);

        domove(board, move);

        int result;

        struct tableentry * entry = ttget(board->hash);
        if (entry && entry->depth >= nextdepth)
        {
            if (entry->min == entry->max) result = entry->min;
            else
            {
                if (entry->max <= trmin || entry->min >= dwmax)
                {
                    result = entry->scoreguess;
                }
                else
                {
                    int windlow = min(trmin, entry->min);
                    int windhigh = max(dwmax, entry->max);
                    result = absearch(board, nextdepth, width,
                                      windlow, windhigh, NULL,
                                      stoptime, stopsearch);

                    if (result < entry->min || result > entry->max)
                    {
                        goto search;
                    }

                    if (entry->depth == nextdepth)
                    {
                        entry->scoreguess = result;
                        if (result < windhigh) entry->max = result;
                        if (result > windlow) entry->min = result;
                    }
                }
            }
        }
        else
        {
search:
            result = absearch(board, nextdepth, width,
                              trmin, dwmax, NULL,
                              stoptime, stopsearch);

            int min, max;
            min = max = result;
            if (result < trmin) min = INT_MIN;
            else if (result > dwmax) max = INT_MAX;

            if (! entry) ttput((struct tableentry)
                    {board->hash, nextdepth, nextdepth, result, min, max});
        }

        // the only problem with not doing negamax
        if (isdwarfturn)
        {
            if (result < dwmax)
            {
                dwmax = result;
                if (bestmove) * bestmove = * move;
            }
        }
        else
        {
            if (result > trmin)
            {
                trmin = result;
                if (bestmove) * bestmove = * move;
            }
        }

        undomove(board, move);

        if (trmin >= dwmax)
        {
            cutoff = true;
            break;
        }
    }

    closeheap(& queue);
    closelist(& list);

    return (isdwarfturn ^ cutoff) ? dwmax : trmin;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r9, 0x30(%rsp)
movl %r8d, %ebx
movl %ecx, %r14d
movl %edx, 0x14(%rsp)
movl %esi, %r12d
movq %rdi, %r15
movq 0xc8(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0xc0(%rsp), %r13
testq %r13, %r13
je 0x1ac9
xorl %edi, %edi
callq 0x11d0
cmpq %r13, %rax
jg 0x1d69
testl %r12d, %r12d
jle 0x1d50
movq %r13, 0x40(%rsp)
decl %r12d
movb (%r15), %al
movb %al, 0xb(%rsp)
movq %r15, %rdi
callq 0x1f02
leaq 0x78(%rsp), %rsi
movq %rax, (%rsi)
movq %rdx, 0x8(%rsi)
movq %r15, %rdi
callq 0x1f10
leaq 0x68(%rsp), %rcx
movq %rax, (%rcx)
movq %rdx, 0x8(%rcx)
movl 0x14(%rsp), %r13d
xorl %ebp, %ebp
testl %r13d, %r13d
jle 0x1d1f
cmpl $0x0, 0x68(%rsp)
jle 0x1d1f
movl %r14d, 0xc(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x38ce
movq %r15, %rdi
movq %rax, 0x18(%rsp)
movq %rax, %rsi
callq 0x1fa4
movq %r15, %r14
movq 0x68(%r15), %rdi
callq 0x4097
movq %rax, %r15
testq %rax, %rax
movl %ebx, 0x24(%rsp)
je 0x1b65
cmpl %r12d, 0x8(%r15)
jge 0x1c06
movl %r13d, 0x10(%rsp)
movq %r14, %rdi
movl %r12d, %esi
movl 0x14(%rsp), %edx
movl %r12d, %r13d
movl 0xc(%rsp), %r12d
movl %r12d, %ecx
movl %ebx, %r8d
xorl %r9d, %r9d
pushq 0x28(%rsp)
pushq 0x48(%rsp)
callq 0x1a79
popq %rcx
popq %rdx
movl %eax, %ebp
testq %r15, %r15
jne 0x1bf6
cmpl %ebx, %ebp
movl %ebp, %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmovgl %ecx, %eax
cmpl %r12d, %ebp
cmovll %ebp, %eax
movl %ebp, %ecx
movl $0x80000000, %edx       # imm = 0x80000000
cmovll %edx, %ecx
movq 0x68(%r14), %rdx
movq %rdx, 0x48(%rsp)
movl %r13d, 0x50(%rsp)
movl %r13d, 0x54(%rsp)
movl %ebp, 0x58(%rsp)
movl %ecx, 0x5c(%rsp)
movl %eax, 0x60(%rsp)
subq $0x20, %rsp
movups 0x68(%rsp), %xmm0
movups 0x78(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x4052
addq $0x20, %rsp
movq %r14, %r15
movl %r13d, %r12d
movl 0x10(%rsp), %r13d
jmp 0x1ca0
movl 0x14(%r15), %ebp
movl 0x18(%r15), %r8d
cmpl %r8d, %ebp
je 0x1c9d
cmpl 0xc(%rsp), %r8d
jle 0x1c99
cmpl %ebx, %ebp
jge 0x1c99
movl 0xc(%rsp), %eax
cmpl %ebp, %eax
cmovll %eax, %ebp
cmpl %r8d, %ebx
cmovgl %ebx, %r8d
movq %r14, %rdi
movl %r12d, %esi
movl 0x14(%rsp), %edx
movl %ebp, 0x10(%rsp)
movl %ebp, %ecx
movl %r8d, 0x3c(%rsp)
xorl %r9d, %r9d
pushq 0x28(%rsp)
pushq 0x48(%rsp)
callq 0x1a79
addq $0x10, %rsp
cmpl 0x14(%r15), %eax
jl 0x1b65
movl %eax, %ebp
cmpl 0x18(%r15), %eax
jg 0x1b65
cmpl %r12d, 0x8(%r15)
jne 0x1c9d
movl %ebp, 0x10(%r15)
cmpl 0x3c(%rsp), %ebp
movq 0x18(%rsp), %rdx
jge 0x1c8a
movl %ebp, 0x18(%r15)
cmpl 0x10(%rsp), %ebp
jle 0x1c94
movl %ebp, 0x14(%r15)
movq %r14, %r15
jmp 0x1ca5
movl 0x10(%r15), %ebp
movq %r14, %r15
movq 0x18(%rsp), %rdx
cmpb $0x0, 0xb(%rsp)
je 0x1cd1
cmpq $0x0, 0x30(%rsp)
sete %al
movl 0x24(%rsp), %ebx
cmpl %ebx, %ebp
cmovll %ebp, %ebx
setge %cl
orb %al, %cl
movl %ebp, %eax
movl 0xc(%rsp), %r14d
movl %r14d, %ebp
je 0x1cf3
jmp 0x1d05
cmpq $0x0, 0x30(%rsp)
sete %al
movl 0xc(%rsp), %r14d
cmpl %r14d, %ebp
setle %cl
cmovgl %ebp, %r14d
orb %al, %cl
movl 0x24(%rsp), %ebx
movl %ebx, %eax
jne 0x1d05
pushq $0x16
popq %rcx
movq 0x30(%rsp), %rdi
movq %rdx, %rsi
rep  movsl (%rsi), %es:(%rdi)
movl %eax, %ebx
movl %ebp, %r14d
movq %r15, %rdi
movq %rdx, %rsi
callq 0x1fdc
decl %r13d
cmpl %ebx, %r14d
jl 0x1b0e
movb $0x1, %bpl
leaq 0x68(%rsp), %rdi
callq 0x3839
leaq 0x78(%rsp), %rdi
callq 0x3a55
cmpb %bpl, 0xb(%rsp)
cmovel %r14d, %ebx
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2053
pushq $0x1
popq %rsi
movq 0x28(%rsp), %rdi
callq 0x1140