// File: /workspace/github_c_makefile_star2_O1/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), %rcx
movq %rcx, %rax
movl $0x4000, %r8d           # imm = 0x4000
shrl %cl, %r8d
sarq $0x20, %rax
movl $0x4000, %edx           # imm = 0x4000
testw %r8w, 0x20(%rdi,%rax,2)
je 0x378e
movq 0xc(%rsi), %rax
movl %eax, %ecx
shrl %cl, %edx
movq %rax, %rcx
sarq $0x20, %rcx
testw %dx, 0x2(%rdi,%rcx,2)
jne 0x378e
testw %dx, 0x20(%rdi,%rcx,2)
jne 0x378e
testw %dx, 0x3e(%rdi,%rcx,2)
je 0x3795
xorl %eax, %eax
popq %rbx
popq %r14
popq %rbp
retq
movl 0x14(%rsi), %edx
testq %rdx, %rdx
je 0x38f6
movl 0xc(%rsi), %r10d
movl 0x4(%rsi), %ecx
movl 0x8(%rsi), %r8d
subl %ecx, %r10d
movl 0x10(%rsi), %ebx
subl %r8d, %ebx
movl %r10d, %r9d
negl %r9d
cmovsl %r10d, %r9d
movl %ebx, %r11d
negl %r11d
cmovsl %ebx, %r11d
cmpl %r11d, %r9d
cmovgl %r9d, %r11d
movl %r10d, %r9d
shrl $0x1f, %r9d
testl %r10d, %r10d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmovgl %ebp, %r9d
movl %ebx, %r10d
shrl $0x1f, %r10d
testl %ebx, %ebx
cmovgl %ebp, %r10d
testl %r11d, %r11d
je 0x378e
cmpl $0x1, %r11d
je 0x3828
movl %r11d, %r11d
addl %r9d, %ecx
addl %r10d, %r8d
decq %r11
movl $0x4000, %ebx           # imm = 0x4000
shrl %cl, %ebx
movslq %r8d, %r14
testw %bx, 0x20(%rdi,%r14,2)
je 0x378e
addl %r9d, %ecx
addl %r10d, %r8d
decq %r11
jne 0x3807
cmpl $0x8, %edx
jg 0x378e
testl %edx, %edx
setg %r8b
jle 0x394d
movq %rax, %r9
shrq $0x20, %r9
xorl %r10d, %r10d
testq %r10, %r10
je 0x3873
movl 0x18(%rsi,%r10,8), %ecx
xorl %r11d, %r11d
cmpl 0x18(%rsi,%r11,8), %ecx
jne 0x386b
movl 0x1c(%rsi,%r10,8), %ebx
cmpl 0x1c(%rsi,%r11,8), %ebx
je 0x3943
incq %r11
cmpq %r11, %r10
jne 0x3854
movq 0x18(%rsi,%r10,8), %rcx
testq %rcx, %rcx
js 0x3943
cmpl $0xe, %ecx
ja 0x3943
movq %rcx, %r11
shrq $0x20, %r11
cmpl $0xe, %r11d
jg 0x3943
movl %ecx, %ebx
subl %eax, %ebx
sete %bpl
cmpq %r9, %r11
sete %r14b
testb %r14b, %bpl
jne 0x3943
movl %r11d, %ebp
subl %r9d, %ebp
movl %ebx, %r14d
negl %r14d
cmovsl %ebx, %r14d
movl %ebp, %ebx
negl %ebx
cmovsl %ebp, %ebx
cmpl $0x1, %r14d
ja 0x3943
cmpl $0x1, %ebx
ja 0x3943
movl $0x4000, %ebx           # imm = 0x4000
shrl %cl, %ebx
testw %bx, 0x2(%rdi,%r11,2)
je 0x3943
incq %r10
cmpq %rdx, %r10
setb %r8b
jne 0x3847
jmp 0x394d
movq 0x4(%rsi), %rsi
movq %rsi, %rdx
shrq $0x20, %rdx
movq %rax, %rcx
shrq $0x20, %rcx
subl %esi, %eax
sete %sil
cmpq %rdx, %rcx
sete %dil
testb %dil, %sil
jne 0x378e
subl %edx, %ecx
movl %eax, %esi
negl %esi
cmovsl %eax, %esi
movl %ecx, %edx
negl %edx
cmovsl %ecx, %edx
xorl %eax, %eax
cmpl $0x1, %esi
ja 0x3790
cmpl $0x1, %edx
jbe 0x394d
jmp 0x3790
testb $0x1, %r8b
jne 0x378e
movb $0x1, %al
jmp 0x3790