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

    int dx = move->to.x - move->from.x;
    int dy = move->to.y - move->from.y;
    // can only move horizontal, vertical, or diagonal
    if (dx == 0)
    {
        // move length must be non-zero
        if (dy == 0) return false;
    }
    else if (dy != 0 && abs(dx) != abs(dy)) return false;

    int dist = max(abs(dx), abs(dy));
    int sx = signum(dx);
    int sy = signum(dy);

    // path must be clear
    for (int i=1; i < dist; ++i)
    {
        struct coord cur = {move->from.x + sx * i,
                            move->from.y + sy * i};
        if (occupied(board, cur)) return false;
    }

    if (move->numcapts)
    {
        // can only make one capture
        if (move->numcapts > 1) return false;
        // can only capture at move target
        else if (move->capts[0].x != move->to.x
                 || move->capts[0].y != move->to.y) return false;
        // target troll must be present
        else if (! trollat(board, move->capts[0])) return false;

        // can only capture using throw line
        for (int i=1; i < dist; ++i)
        {
            struct coord cur = {move->from.x - sx * i,
                                move->from.y - sy * i};
            if (! dwarfat(board, cur)) return false;
        }
    }
    // if not capturing, target must be clear
    else if (occupied(board, move->to)) return false;

    return true;
}

// Assembly:
movq 0x4(%rsi), %rcx
movl $0x4000, %eax           # imm = 0x4000
shrl %cl, %eax
movq %rcx, %r9
sarq $0x20, %r9
testw %ax, 0x2(%rdi,%r9,2)
je 0x32f0
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbp
movq %rcx, %rdx
shrq $0x20, %rdx
movl 0xc(%rsi), %ebx
movl 0x10(%rsi), %r11d
movl %r11d, %eax
subl %edx, %eax
movl %ebx, %r8d
subl %ecx, %r8d
jne 0x32f3
cmpl %edx, %r11d
jne 0x3314
jmp 0x3452
xorl %eax, %eax
retq
cmpl %edx, %r11d
je 0x3314
movl %r8d, %edx
negl %edx
cmovsl %r8d, %edx
movl %eax, %r10d
negl %r10d
cmovsl %eax, %r10d
cmpl %r10d, %edx
jne 0x3452
movl %ebx, 0x14(%rsp)
movl %r11d, 0x10(%rsp)
movq %rsi, 0x30(%rsp)
movl %r8d, %edx
negl %edx
cmovsl %r8d, %edx
movl %eax, %r13d
negl %r13d
cmovsl %eax, %r13d
cmpl %r13d, %edx
cmoval %edx, %r13d
movl %r8d, %r10d
sarl $0x1f, %r10d
testl %r8d, %r8d
pushq $0x1
popq %rsi
cmovgl %esi, %r10d
movl %eax, %edx
sarl $0x1f, %edx
testl %eax, %eax
cmovgl %esi, %edx
movslq %edx, %r15
movq %r9, 0x20(%rsp)
leal (%r15,%r9), %edx
shlq $0x20, %rdx
movq %r15, 0x18(%rsp)
shlq $0x20, %r15
movq %rcx, 0x28(%rsp)
movq %r10, 0x8(%rsp)
leal (%r10,%rcx), %ebx
xorl %r12d, %r12d
incq %r12
cmpq %r13, %r12
jae 0x33aa
leaq (%rdx,%r15), %r14
movl %ebx, %esi
addl 0x8(%rsp), %ebx
addq %rdx, %rsi
movq %rbp, %rdi
callq 0x2d08
movq %r14, %rdx
testb %al, %al
je 0x3381
jmp 0x3452
movq 0x30(%rsp), %rcx
movl 0x14(%rcx), %eax
testl %eax, %eax
je 0x3442
cmpl $0x1, %eax
jg 0x3452
movl 0x14(%rsp), %eax
cmpl %eax, 0x18(%rcx)
jne 0x3452
movl 0x10(%rsp), %eax
cmpl %eax, 0x1c(%rcx)
jne 0x3452
movq 0x18(%rcx), %rcx
movl $0x4000, %eax           # imm = 0x4000
shrl %cl, %eax
sarq $0x20, %rcx
testw %ax, 0x20(%rbp,%rcx,2)
je 0x3452
movq 0x28(%rsp), %rcx
subl 0x8(%rsp), %ecx
movq 0x20(%rsp), %rax
addq %rax, %rax
movq 0x18(%rsp), %r10
addq %r10, %r10
subq %r10, %rax
leaq (%rax,%rbp), %rdx
addq $0x2, %rdx
negq %r10
xorl %esi, %esi
xorl %eax, %eax
incq %rsi
cmpq %r13, %rsi
jae 0x3463
movl %ecx, %edi
subl 0x8(%rsp), %edi
leaq (%rdx,%r10), %r8
movl $0x4000, %r9d           # imm = 0x4000
shrl %cl, %r9d
movl %edi, %ecx
testw %r9w, (%rdx)
movq %r8, %rdx
jne 0x341a
jmp 0x3454
movq 0xc(%rcx), %rsi
movq %rbp, %rdi
callq 0x2d08
testb %al, %al
je 0x3463
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, %al
jmp 0x3454