// File: /workspace/github_c_makefile_star2_O1/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
sarq $0x20, %rcx
testw %ax, 0x2(%rdi,%rcx,2)
je 0x35e9
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movl 0x4(%rsi), %eax
movl 0xc(%rsi), %r10d
movl 0x10(%rsi), %r9d
movslq 0x8(%rsi), %rdx
movl %r9d, %ecx
subl %edx, %ecx
movl %r10d, %ebx
subl %eax, %ebx
jne 0x35ec
cmpl %edx, %r9d
jne 0x360e
jmp 0x3736
xorl %eax, %eax
retq
cmpl %edx, %r9d
je 0x360e
movl %ebx, %r8d
negl %r8d
cmovsl %ebx, %r8d
movl %ecx, %r11d
negl %r11d
cmovsl %ecx, %r11d
cmpl %r11d, %r8d
jne 0x3736
movl %ebx, %r8d
negl %r8d
cmovsl %ebx, %r8d
movl %ecx, %r11d
negl %r11d
cmovsl %ecx, %r11d
cmpl %r11d, %r8d
cmovgl %r8d, %r11d
movl %ebx, %r8d
sarl $0x1f, %r8d
testl %ebx, %ebx
movl $0x1, %ebp
cmovgl %ebp, %r8d
movl %ecx, %ebx
sarl $0x1f, %ebx
testl %ecx, %ecx
cmovgl %ebp, %ebx
cmpl $0x2, %r11d
jb 0x369c
movslq %ebx, %r14
movl %r11d, %r15d
leal (%r8,%rax), %ecx
leaq (%r14,%rdx), %r12
leaq (%rdi,%r12,2), %r12
addq $0x3e, %r12
addq %r14, %r14
decq %r15
movl $0x4000, %ebp           # imm = 0x4000
shrl %cl, %ebp
testw %bp, -0x3c(%r12)
jne 0x3736
testw %bp, -0x1e(%r12)
jne 0x3736
testw %bp, (%r12)
jne 0x3736
addl %r8d, %ecx
addq %r14, %r12
decq %r15
jne 0x3667
movl 0x14(%rsi), %ecx
testl %ecx, %ecx
je 0x3712
cmpl $0x1, %ecx
jg 0x3736
cmpl %r10d, 0x18(%rsi)
jne 0x3736
cmpl %r9d, 0x1c(%rsi)
jne 0x3736
movq 0x18(%rsi), %rcx
movl $0x4000, %esi           # imm = 0x4000
shrl %cl, %esi
sarq $0x20, %rcx
testw %si, 0x20(%rdi,%rcx,2)
je 0x3736
cmpl $0x2, %r11d
jb 0x3741
movslq %ebx, %rsi
movl %r11d, %r9d
subl %r8d, %eax
addq %rdx, %rdx
addq %rsi, %rsi
subq %rsi, %rdx
addq %rdi, %rdx
addq $0x2, %rdx
negq %rsi
decq %r9
movl $0x4000, %edi           # imm = 0x4000
movl %eax, %ecx
shrl %cl, %edi
testw %di, (%rdx)
je 0x3736
subl %r8d, %eax
addq %rsi, %rdx
decq %r9
jne 0x36f7
jmp 0x3741
movq 0xc(%rsi), %rcx
movl $0x4000, %eax           # imm = 0x4000
shrl %cl, %eax
sarq $0x20, %rcx
testw %ax, 0x2(%rdi,%rcx,2)
jne 0x3736
testw %ax, 0x20(%rdi,%rcx,2)
jne 0x3736
testw %ax, 0x3e(%rdi,%rcx,2)
je 0x3741
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, %al
jmp 0x3738