// File: /workspace/github_c_makefile_star2_O3/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), %rax
movl $0x4000, %r8d           # imm = 0x4000
movl %eax, %ecx
shrl %cl, %r8d
movq %rax, %rdx
sarq $0x20, %rdx
testw %r8w, 0x2(%rdi,%rdx,2)
je 0x3518
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rax, %r8
shrq $0x20, %r8
movl 0xc(%rsi), %r10d
movl 0x10(%rsi), %r9d
movl %r9d, %ecx
subl %r8d, %ecx
movl %r10d, %ebx
subl %eax, %ebx
jne 0x351b
cmpl %r8d, %r9d
jne 0x353d
jmp 0x3674
xorl %eax, %eax
retq
cmpl %r8d, %r9d
je 0x353d
movl %ebx, %r8d
negl %r8d
cmovsl %ebx, %r8d
movl %ecx, %r11d
negl %r11d
cmovsl %ecx, %r11d
cmpl %r11d, %r8d
jne 0x3674
movl %ebx, %r8d
negl %r8d
cmovsl %ebx, %r8d
movl %ecx, %r11d
negl %r11d
cmovsl %ecx, %r11d
cmpl %r11d, %r8d
cmoval %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 0x35ce
movslq %ebx, %r14
movl %r11d, %r13d
leaq (%rdx,%r14), %rcx
leaq (%rdi,%rcx,2), %r15
addq $0x3e, %r15
addq %r14, %r14
leal (%r8,%rax), %ecx
movl $0x1, %r12d
subq %r13, %r12
movl $0x4000, %ebp           # imm = 0x4000
shrl %cl, %ebp
testw %bp, -0x3c(%r15)
jne 0x3674
testw %bp, -0x1e(%r15)
jne 0x3674
testw %bp, (%r15)
jne 0x3674
addq %r14, %r15
addl %r8d, %ecx
incq %r12
jne 0x359c
movl 0x14(%rsi), %ecx
testl %ecx, %ecx
je 0x3650
cmpl $0x1, %ecx
jg 0x3674
cmpl %r10d, 0x18(%rsi)
jne 0x3674
cmpl %r9d, 0x1c(%rsi)
jne 0x3674
movq 0x18(%rsi), %rcx
movl $0x4000, %esi           # imm = 0x4000
shrl %cl, %esi
sarq $0x20, %rcx
testw %si, 0x20(%rdi,%rcx,2)
je 0x3674
cmpl $0x2, %r11d
jb 0x3681
movslq %ebx, %rsi
movl %r11d, %ecx
addq %rdx, %rdx
addq %rsi, %rsi
subq %rsi, %rdx
addq %rdi, %rdx
addq $0x2, %rdx
negq %rsi
subl %r8d, %eax
movl $0x1, %edi
subq %rcx, %rdi
movl $0x4000, %r9d           # imm = 0x4000
movl %eax, %ecx
shrl %cl, %r9d
testw %r9w, (%rdx)
je 0x3674
addq %rsi, %rdx
subl %r8d, %eax
incq %rdi
jne 0x3632
jmp 0x3681
movq 0xc(%rsi), %rcx
movl $0x4000, %eax           # imm = 0x4000
shrl %cl, %eax
sarq $0x20, %rcx
testw %ax, 0x2(%rdi,%rcx,2)
jne 0x3674
testw %ax, 0x20(%rdi,%rcx,2)
jne 0x3674
testw %ax, 0x3e(%rdi,%rcx,2)
je 0x3681
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, %al
jmp 0x3676