// File: /workspace/github_cpp_makefile_star2_O2/ATOMIC-LICHESS[P]ATOMKRAFT/movegen.cpp
// Function: MoveStack* (anonymous namespace)::generate_castle_moves<((anonymous namespace)::CastlingSide)1>(Position const&, MoveStack*, Color)
MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist, Color us) {
	NEW assert(pos.piece_count(WHITE, KING) && pos.piece_count(BLACK, KING));
	  	
    Color them = opposite_color(us);
    NEW assert(pos.piece_count(us, KING));
    Square ksq = pos.king_square(us);

    assert(pos.piece_on(ksq) == make_piece(us, KING));

    Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
    Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
    Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
    Square s;
    bool illegal = false;
    

    // assert(pos.piece_on(rsq) == make_piece(us, ROOK)); //can fail in atomic?
    if (pos.piece_on(rsq) != make_piece(us, ROOK)) return mlist;
    
    STARTNEW
#ifndef NDEBUG
    if (pos.piece_on(rsq) != make_piece(us, ROOK)) {
    	cout << "miau" << endl;
    	print_debuginfo(pos);
    }
#endif
    ENDNEW
    	
    STARTOLD
    // It is a bit complicated to correctly handle Chess960
    for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
        if (  (s != ksq && s != rsq && pos.square_is_occupied(s))
            ||(pos.attackers_to(s) & pos.pieces_of_color(them)))
            illegal = true;
    ENDOLD
    
    STARTNEW
    // It is a bit complicated to correctly handle Chess960
    // castling is allowed even if the opponent king gives "check" on castling squares
    Bitboard att_to;

    for (s = Min(ksq, s1); s <= Max(ksq, s1); s++) {
    	att_to =  (pos.attacks_from<PAWN>(s, BLACK) & pos.pieces(PAWN, WHITE))
				| (pos.attacks_from<PAWN>(s, WHITE) & pos.pieces(PAWN, BLACK))
				| (pos.attacks_from<KNIGHT>(s)      & pos.pieces(KNIGHT))
				| (pos.attacks_from<ROOK>(s)        & pos.pieces(ROOK, QUEEN))
				| (pos.attacks_from<BISHOP>(s)      & pos.pieces(BISHOP, QUEEN));
    	
        if (  (s != ksq && s != rsq && pos.square_is_occupied(s))
            ||(att_to & pos.pieces_of_color(them))) // att_to is the only modification
            illegal = true;
    }
    ENDNEW

    for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
        if (s != ksq && s != rsq && pos.square_is_occupied(s))
            illegal = true;

    if (   Side == QUEEN_SIDE
        && square_file(rsq) == FILE_B
        && (   pos.piece_on(relative_square(us, SQ_A1)) == make_piece(them, ROOK)
            || pos.piece_on(relative_square(us, SQ_A1)) == make_piece(them, QUEEN)))
        illegal = true;

    if (!illegal)
        (*mlist++).move = make_castle_move(ksq, rsq);

    return mlist;
  }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %rax
movq %rdi, %r15
movl 0x1920(%rdi), %edi
imull $0x38, %edx, %esi
movl %edi, %ecx
xorl %esi, %ecx
movq %rcx, 0x8(%rsp)
movslq %ecx, %r8
leal 0x4(,%rdx,8), %ecx
cmpl %ecx, (%r15,%r8,4)
jne 0x1444c
movq %r8, 0x18(%rsp)
movl %edi, 0x14(%rsp)
movq %rax, 0x38(%rsp)
movl %edx, %ecx
xorl $0x1, %edx
shlq $0x9, %rcx
movslq 0x310(%r15,%rcx), %rcx
movl %ecx, %eax
leal 0x3(%rsi), %edi
movl %edi, 0x10(%rsp)
movslq %esi, %r12
orl $0x2, %esi
cmpl %esi, %eax
movq %rax, 0x28(%rsp)
cmovgl %eax, %esi
movq %rdx, 0x30(%rsp)
movl %edx, %eax
movq %rax, 0x40(%rsp)
movq %r12, 0x20(%rsp)
orq $0x2, %r12
cmpq %rcx, %r12
cmovgeq %rcx, %r12
movslq %esi, %rax
movq %rax, 0x50(%rsp)
movl %ecx, %eax
movq %rax, 0x48(%rsp)
movl $0x0, 0x4(%rsp)
cmpq 0x50(%rsp), %r12
jg 0x1439a
movq 0x110(%r15), %rbx
movq 0x140(%r15), %rax
leaq 0xfd455(%rip), %rcx     # 0x111750
andq 0x1200(%rcx,%r12,8), %rax
movq 0x148(%r15), %r14
andq 0x200(%rcx,%r12,8), %r14
orq %rax, %r14
andq 0x108(%r15), %r14
andq 0x400(%rcx,%r12,8), %rbx
movq %r15, %rdi
movl %r12d, %esi
callq 0x12330
movq %rax, %r13
movq 0x128(%r15), %rbp
orq 0x120(%r15), %rbp
movq %r15, %rdi
movl %r12d, %esi
callq 0xcf20
movq 0x128(%r15), %rcx
orq 0x118(%r15), %rcx
cmpl %r12d, 0x48(%rsp)
je 0x1436e
cmpl %r12d, 0x8(%rsp)
je 0x1436e
cmpl $0x10, (%r15,%r12,4)
jne 0x1438c
andq %r13, %rbp
orq %rbp, %rbx
andq %rax, %rcx
orq %rbx, %rcx
orq %rcx, %r14
movq 0x40(%rsp), %rax
testq %r14, 0x140(%r15,%rax,8)
je 0x14392
movb $0x1, %al
movl %eax, 0x4(%rsp)
incq %r12
jmp 0x142db
movq 0x8(%rsp), %rax
movl 0x10(%rsp), %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
movq 0x20(%rsp), %r11
movq %r11, %rdi
orq $0x3, %rdi
movq 0x18(%rsp), %rax
cmpq %rax, %rdi
cmovgeq %rax, %rdi
movslq %ecx, %rcx
movl %eax, %edx
movq 0x38(%rsp), %rax
movq 0x30(%rsp), %r8
movq 0x28(%rsp), %r9
movl 0x14(%rsp), %r10d
movl 0x4(%rsp), %ebx
cmpq %rcx, %rdi
jg 0x143fc
cmpl %edi, %r9d
je 0x143f7
cmpl %edi, %edx
je 0x143f7
cmpl $0x10, (%r15,%rdi,4)
setne %sil
orb %sil, %bl
incq %rdi
jmp 0x143dd
andl $0x7, %r10d
cmpl $0x1, %r10d
jne 0x1442f
movl (%r15,%r11,4), %esi
leal 0x4(,%r8,8), %ecx
cmpl %ecx, %esi
sete %cl
leal 0x5(,%r8,8), %edx
cmpl %edx, %esi
sete %dl
orb %cl, %dl
orb %bl, %dl
testb $0x1, %dl
je 0x14434
jmp 0x1444c
testb $0x1, %bl
jne 0x1444c
shll $0x6, %r9d
movq 0x8(%rsp), %rcx
orl %r9d, %ecx
orl $0xc000, %ecx           # imm = 0xC000
movl %ecx, (%rax)
addq $0x8, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq