// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq 0x4(%rsi), %r12
movl $0x4000, %eax           # imm = 0x4000
movl %r12d, %ecx
shrl %cl, %eax
movq %r12, %rcx
sarq $0x20, %rcx
testw %ax, 0x20(%rdi,%rcx,2)
je 0x34ab
movq %rsi, %rbx
movq %rdi, %r14
movl 0xc(%rsi), %r13d
movl 0x10(%rsi), %ebp
movq 0xc(%rsi), %r15
movq %r15, %rsi
callq 0x2d08
testb %al, %al
je 0x34bc
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x14(%rbx), %r11d
testl %r11d, %r11d
je 0x3535
movq %r12, %rax
shrq $0x20, %rax
subl %r12d, %r13d
subl %eax, %ebp
movl %r13d, %edx
negl %edx
cmovsl %r13d, %edx
movl %ebp, %ecx
negl %ecx
cmovsl %ebp, %ecx
cmpl %ecx, %edx
cmoval %edx, %ecx
movl %r13d, %edx
shrl $0x1f, %edx
testl %r13d, %r13d
pushq $-0x1
popq %rdi
cmovgl %edi, %edx
movl %ebp, %esi
shrl $0x1f, %esi
testl %ebp, %ebp
cmovgl %edi, %esi
testl %ecx, %ecx
je 0x34ab
movl %ecx, %edi
negq %rdi
addl %esi, %eax
leal (%rdx,%r12), %ecx
incq %rdi
je 0x354f
leal (%rcx,%rdx), %r8d
movslq %eax, %r9
movl $0x4000, %r10d          # imm = 0x4000
shrl %cl, %r10d
addl %esi, %eax
movl %r8d, %ecx
testw %r10w, 0x20(%r14,%r9,2)
jne 0x350e
jmp 0x34ab
movq %r12, %rdi
movq %r15, %rsi
callq 0x35e6
testb %al, %al
je 0x34ab
movb $0x1, %al
jmp 0x34ad
xorl %eax, %eax
cmpl $0x8, %r11d
jg 0x34ad
testl %r11d, %r11d
cmovlel %eax, %r11d
xorl %ebp, %ebp
movq %r11, (%rsp)
cmpq %r11, %rbp
je 0x3548
xorl %eax, %eax
cmpq %rax, %rbp
je 0x3591
movl 0x18(%rbx,%rbp,8), %ecx
cmpl 0x18(%rbx,%rax,8), %ecx
jne 0x358c
movl 0x1c(%rbx,%rbp,8), %ecx
cmpl 0x1c(%rbx,%rax,8), %ecx
je 0x34ab
incq %rax
jmp 0x356f
movq 0x18(%rbx,%rbp,8), %r12
movq %r12, %r13
shrq $0x20, %r13
xorl %eax, %eax
cmpl $0xe, %r13d
ja 0x34ad
cmpl $0xe, %r12d
ja 0x34ad
movq %r15, %rdi
movq %r12, %rsi
callq 0x35e6
testb %al, %al
je 0x34ab
movl $0x4000, %edx           # imm = 0x4000
movl %r12d, %ecx
shrl %cl, %edx
incq %rbp
xorl %eax, %eax
testw %dx, 0x2(%r14,%r13,2)
movq (%rsp), %r11
jne 0x3568
jmp 0x34ad