// File: /workspace/github_c_makefile_star2_O1/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, 0x28(%rsp)
movl %r8d, %r14d
movl %ecx, %r12d
movl %edx, 0x10(%rsp)
movl %esi, %ebx
movq %rdi, %rbp
movq 0xc8(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0xc0(%rsp), %rax
testq %rax, %rax
je 0x1b49
xorl %edi, %edi
movq %rax, %r15
callq 0x11d0
cmpq %r15, %rax
movq %r15, %rax
jg 0x1e43
testl %ebx, %ebx
jle 0x1b67
movq %rax, 0x30(%rsp)
movb (%rbp), %al
movb %al, 0xb(%rsp)
movq %rbp, %rdi
cmpb $0x1, %al
jne 0x1b80
callq 0x21d1
jmp 0x1b85
movq %rbp, %rdi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x219f
callq 0x2500
leaq 0x78(%rsp), %rsi
movq %rax, (%rsi)
movq %rdx, 0x8(%rsi)
movq %rbp, %rdi
callq 0x2057
movq %rax, 0x48(%rsp)
movq %rdx, 0x50(%rsp)
xorl %r15d, %r15d
cmpl $0x0, 0x10(%rsp)
jle 0x1e11
testl %eax, %eax
jle 0x1e11
decl %ebx
movl %ebx, 0x14(%rsp)
xorl %r15d, %r15d
movl 0x10(%rsp), %ebx
movq %rbp, 0x38(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x3c74
movq %rbp, %rdi
movq %rax, 0x40(%rsp)
movq %rax, %rsi
callq 0x20ee
movq 0x68(%rbp), %rdi
callq 0x45b0
movq %rax, %r13
testq %rax, %rax
je 0x1c04
movl 0x14(%rsp), %eax
cmpl %eax, 0x8(%r13)
jge 0x1ca5
movq %r15, 0x18(%rsp)
movq %rbp, %rdi
movl 0x14(%rsp), %r15d
movl %r15d, %esi
movl 0x10(%rsp), %edx
movl %r12d, %ecx
movl %r14d, %r8d
xorl %r9d, %r9d
pushq 0x20(%rsp)
pushq 0x38(%rsp)
callq 0x1af4
addq $0x10, %rsp
movq %rbp, %rdx
movl %eax, %ebp
testq %r13, %r13
jne 0x1c9b
cmpl %r14d, %ebp
movl %ebp, %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmovgl %ecx, %eax
cmpl %r12d, %ebp
cmovll %ebp, %eax
movl %ebp, %ecx
movl $0x80000000, %esi       # imm = 0x80000000
cmovll %esi, %ecx
movq 0x68(%rdx), %rdx
movq %rdx, 0x58(%rsp)
movl %r15d, 0x60(%rsp)
movl %r15d, 0x64(%rsp)
movl %ebp, 0x68(%rsp)
movl %ecx, 0x6c(%rsp)
movl %eax, 0x70(%rsp)
subq $0x20, %rsp
movups 0x78(%rsp), %xmm0
movups 0x88(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x456b
addq $0x20, %rsp
movq 0x18(%rsp), %r15
jmp 0x1d79
movl %r12d, 0x18(%rsp)
movl %r14d, 0xc(%rsp)
movl 0x14(%r13), %r12d
movl 0x18(%r13), %r14d
cmpl %r14d, %r12d
jne 0x1cc4
movl %r12d, %ebp
jmp 0x1d6f
cmpl 0x18(%rsp), %r14d
jle 0x1d6b
cmpl 0xc(%rsp), %r12d
jge 0x1d6b
movl 0x18(%rsp), %eax
cmpl %r12d, %eax
cmovll %eax, %r12d
movl 0xc(%rsp), %eax
cmpl %r14d, %eax
cmovgl %eax, %r14d
movq %rbp, %rdi
movl 0x14(%rsp), %esi
movl 0x10(%rsp), %edx
movl %r12d, %ecx
movl %r14d, %r8d
xorl %r9d, %r9d
pushq 0x20(%rsp)
pushq 0x38(%rsp)
callq 0x1af4
addq $0x10, %rsp
movl %eax, %ebp
movl $0x5, %eax
cmpl 0x14(%r13), %ebp
jl 0x1d4a
cmpl 0x18(%r13), %ebp
jg 0x1d4a
xorl %eax, %eax
movl 0x14(%rsp), %ecx
cmpl %ecx, 0x8(%r13)
jne 0x1d4a
movl %ebp, 0x10(%r13)
cmpl %r14d, %ebp
jge 0x1d41
movl %ebp, 0x18(%r13)
cmpl %r12d, %ebp
jle 0x1d4a
movl %ebp, 0x14(%r13)
testl %eax, %eax
movl 0xc(%rsp), %r14d
movl 0x18(%rsp), %r12d
je 0x1d79
cmpl $0x5, %eax
movq 0x38(%rsp), %rbp
je 0x1c04
jmp 0x1df7
movl 0x10(%r13), %ebp
movl 0xc(%rsp), %r14d
movl 0x18(%rsp), %r12d
cmpb $0x0, 0xb(%rsp)
je 0x1d9a
cmpq $0x0, 0x28(%rsp)
sete %al
cmpl %r14d, %ebp
cmovll %ebp, %r14d
setge %cl
orb %al, %cl
movl %r12d, %eax
jmp 0x1db4
cmpq $0x0, 0x28(%rsp)
sete %al
cmpl %r12d, %ebp
cmovgl %ebp, %r12d
setle %cl
orb %al, %cl
movl %ebp, %eax
movl %r14d, %ebp
movq 0x40(%rsp), %rdx
jne 0x1dd0
movl $0x16, %ecx
movq 0x28(%rsp), %rdi
movq %rdx, %rsi
rep  movsl (%rsi), %es:(%rdi)
movl %ebp, %r14d
movl %eax, %r12d
movq 0x38(%rsp), %rbp
movq %rbp, %rdi
movq %rdx, %rsi
callq 0x2126
xorl %eax, %eax
cmpl %r14d, %r12d
movzbl %r15b, %r15d
movl $0x1, %ecx
cmovgel %ecx, %r15d
setge %al
addl %eax, %eax
testl %eax, %eax
jne 0x1e0d
cmpl $0x2, %ebx
jl 0x1e0d
decl %ebx
cmpl $0x0, 0x48(%rsp)
jg 0x1bcb
andb $0x1, %r15b
leaq 0x48(%rsp), %rdi
callq 0x3bc5
leaq 0x78(%rsp), %rdi
callq 0x3df5
cmpb %r15b, 0xb(%rsp)
cmovel %r12d, %r14d
movl %r14d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x20(%rsp), %rdi
movl $0x1, %esi
callq 0x1140