// File: /workspace/github_c_makefile_star2_O0/icarroll[P]clunk/thudlib.c
// Function: legaltrollmove
bool legaltrollmove(struct thudboard * board, struct move * move)
{
    // moving troll must be present
    if (! trollat(board, move->from)) return false;

    int dx = move->to.x - move->from.x;
    int dy = move->to.y - move->from.y;

    // move target must be clear
    if (occupied(board, move->to)) return false;

    if (move->numcapts)
    {
        int dist = max(abs(dx), abs(dy));
        int sx = signum(dx);
        int sy = signum(dy);

        // move length must be non-zero
        if (dist == 0) return false;

        // distant captures require shoving line
        for (int i=1; i < dist; ++i)
        {
            struct coord cur = {move->from.x - sx * i,
                                move->from.y - sy * i};
            if (! trollat(board, cur)) return false;
        }

        // check for buffer overrun
        if (move->numcapts > NUMDIRS) return false;

        for (int i=0; i < move->numcapts; ++i)
        {
            // capture targets must be unique
            for (int j=0; j < i; ++j)
            {
                if (move->capts[i].x == move->capts[j].x
                    && move->capts[i].y == move->capts[j].y) return false;
            }

            // capture targets must be in bounds
            if (! inbounds(move->capts[i])) return false;

            // capture targets must be adjacent
            if (! adjacent(move->to, move->capts[i])) return false;

            // target dwarfs must be present
            if (! dwarfat(board, move->capts[i])) return false;
        }
    }
    // if not shoving, can only move one space
    else if (! adjacent(move->from, move->to)) return false;

    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x4(%rax), %rsi
callq 0x37d0
testb $0x1, %al
jne 0x434e
movb $0x0, -0x1(%rbp)
jmp 0x4555
movq -0x18(%rbp), %rax
movl 0xc(%rax), %eax
movq -0x18(%rbp), %rcx
subl 0x4(%rcx), %eax
movl %eax, -0x1c(%rbp)
movq -0x18(%rbp), %rax
movl 0x10(%rax), %eax
movq -0x18(%rbp), %rcx
subl 0x8(%rcx), %eax
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0xc(%rax), %rsi
callq 0x3800
testb $0x1, %al
jne 0x4387
jmp 0x4390
movb $0x0, -0x1(%rbp)
jmp 0x4555
movq -0x18(%rbp), %rax
cmpl $0x0, 0x14(%rax)
je 0x4530
movl -0x1c(%rbp), %edi
movl %edi, %eax
negl %eax
cmovnsl %eax, %edi
movl -0x20(%rbp), %esi
movl %esi, %eax
negl %eax
cmovnsl %eax, %esi
callq 0x2610
movl %eax, -0x24(%rbp)
movl -0x1c(%rbp), %edi
callq 0x4560
movl %eax, -0x28(%rbp)
movl -0x20(%rbp), %edi
callq 0x4560
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x43df
movb $0x0, -0x1(%rbp)
jmp 0x4555
movl $0x1, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x443b
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movl -0x28(%rbp), %ecx
imull -0x30(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movl -0x2c(%rbp), %ecx
imull -0x30(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x37d0
testb $0x1, %al
jne 0x442e
movb $0x0, -0x1(%rbp)
jmp 0x4555
jmp 0x4430
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x43e6
movq -0x18(%rbp), %rax
cmpl $0x8, 0x14(%rax)
jle 0x444e
movb $0x0, -0x1(%rbp)
jmp 0x4555
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jge 0x452e
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0x44be
movq -0x18(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movl 0x18(%rax,%rcx,8), %eax
movq -0x18(%rbp), %rcx
movslq -0x40(%rbp), %rdx
cmpl 0x18(%rcx,%rdx,8), %eax
jne 0x44b1
movq -0x18(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movl 0x1c(%rax,%rcx,8), %eax
movq -0x18(%rbp), %rcx
movslq -0x40(%rbp), %rdx
cmpl 0x1c(%rcx,%rdx,8), %eax
jne 0x44b1
movb $0x0, -0x1(%rbp)
jmp 0x4555
jmp 0x44b3
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x446c
movq -0x18(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movq 0x18(%rax,%rcx,8), %rdi
callq 0x3730
testb $0x1, %al
jne 0x44da
movb $0x0, -0x1(%rbp)
jmp 0x4555
movq -0x18(%rbp), %rdx
movq -0x18(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movq 0xc(%rdx), %rdi
movq 0x18(%rax,%rcx,8), %rsi
callq 0x45a0
testb $0x1, %al
jne 0x44fe
movb $0x0, -0x1(%rbp)
jmp 0x4555
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movq 0x18(%rax,%rcx,8), %rsi
callq 0x3770
testb $0x1, %al
jne 0x451e
movb $0x0, -0x1(%rbp)
jmp 0x4555
jmp 0x4520
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x4455
jmp 0x4551
movq -0x18(%rbp), %rcx
movq -0x18(%rbp), %rax
movq 0x4(%rcx), %rdi
movq 0xc(%rax), %rsi
callq 0x45a0
testb $0x1, %al
jne 0x454f
movb $0x0, -0x1(%rbp)
jmp 0x4555
jmp 0x4551
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x40, %rsp
popq %rbp
retq