// File: /workspace/github_c_makefile_star2_O3/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 %r14
pushq %rbx
movq 0x4(%rsi), %rdx
movq %rdx, %r8
movl $0x4000, %r9d           # imm = 0x4000
movl %edx, %ecx
shrl %cl, %r9d
sarq $0x20, %r8
movl $0x4000, %eax           # imm = 0x4000
testw %r9w, 0x20(%rdi,%r8,2)
je 0x36d1
movq 0xc(%rsi), %r8
movl %r8d, %ecx
shrl %cl, %eax
movq %r8, %rcx
sarq $0x20, %rcx
testw %ax, 0x2(%rdi,%rcx,2)
jne 0x36d1
testw %ax, 0x20(%rdi,%rcx,2)
jne 0x36d1
testw %ax, 0x3e(%rdi,%rcx,2)
je 0x36d8
xorl %eax, %eax
popq %rbx
popq %r14
popq %rbp
retq
movq %rdx, %rax
shrq $0x20, %rax
movl 0x14(%rsi), %r9d
testq %r9, %r9
je 0x3837
movl 0xc(%rsi), %r11d
movl 0x10(%rsi), %ebx
subl %edx, %r11d
subl %eax, %ebx
movl %r11d, %r10d
negl %r10d
cmovsl %r11d, %r10d
movl %ebx, %ecx
negl %ecx
cmovsl %ebx, %ecx
cmpl %ecx, %r10d
cmoval %r10d, %ecx
movl %r11d, %r10d
shrl $0x1f, %r10d
testl %r11d, %r11d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmovgl %ebp, %r10d
movl %ebx, %r11d
shrl $0x1f, %r11d
testl %ebx, %ebx
cmovgl %ebp, %r11d
testl %ecx, %ecx
je 0x36d1
cmpl $0x1, %ecx
je 0x376b
movl %ecx, %ebx
addl %r11d, %eax
leal (%r10,%rdx), %ecx
movl $0x1, %edx
subq %rbx, %rdx
movl $0x4000, %ebx           # imm = 0x4000
shrl %cl, %ebx
movslq %eax, %r14
testw %bx, 0x20(%rdi,%r14,2)
je 0x36d1
addl %r11d, %eax
addl %r10d, %ecx
incq %rdx
jne 0x374a
cmpl $0x8, %r9d
jg 0x36d1
testl %r9d, %r9d
jle 0x387a
movq %r8, %rdx
shrq $0x20, %rdx
xorl %r10d, %r10d
testq %r10, %r10
je 0x37b2
movl 0x18(%rsi,%r10,8), %eax
xorl %ecx, %ecx
cmpl 0x18(%rsi,%rcx,8), %eax
jne 0x37aa
movl 0x1c(%rsi,%r10,8), %r11d
cmpl 0x1c(%rsi,%rcx,8), %r11d
je 0x36d1
incq %rcx
cmpq %rcx, %r10
jne 0x3794
movq 0x18(%rsi,%r10,8), %rcx
movq %rcx, %r11
shrq $0x20, %r11
xorl %eax, %eax
cmpl $0xe, %r11d
ja 0x36d3
cmpl $0xe, %ecx
ja 0x36d3
movl %ecx, %eax
subl %r8d, %eax
sete %bl
cmpq %rdx, %r11
sete %bpl
testb %bpl, %bl
jne 0x36d1
movl %r11d, %ebp
subl %edx, %ebp
movl %eax, %r14d
negl %r14d
cmovsl %eax, %r14d
movl %ebp, %ebx
negl %ebx
cmovsl %ebp, %ebx
xorl %eax, %eax
cmpl $0x1, %r14d
ja 0x36d3
cmpl $0x1, %ebx
ja 0x36d3
movl $0x4000, %eax           # imm = 0x4000
shrl %cl, %eax
testw %ax, 0x2(%rdi,%r11,2)
je 0x36d1
incq %r10
cmpq %r9, %r10
jne 0x3788
jmp 0x387a
movq %r8, %rcx
shrq $0x20, %rcx
subl %edx, %r8d
sete %dl
cmpq %rax, %rcx
sete %sil
testb %sil, %dl
jne 0x36d1
subl %eax, %ecx
movl %r8d, %esi
negl %esi
cmovsl %r8d, %esi
movl %ecx, %edx
negl %edx
cmovsl %ecx, %edx
xorl %eax, %eax
cmpl $0x1, %esi
ja 0x36d3
cmpl $0x1, %edx
ja 0x36d3
movb $0x1, %al
jmp 0x36d3