// File: ATOMIC-LICHESS[P]ATOMKRAFT/movegen.cpp
// Function: MoveStack* generate<(MoveType)5>(Position const&, MoveStack*)
MoveStack* generate(const Position& pos, MoveStack* mlist) {
  NEW assert(pos.piece_count(WHITE, KING) && pos.piece_count(BLACK, KING));
		
  assert(pos.is_ok());
  assert(!pos.in_check());
  
  STARTNEW
  if (pos.piece_count(pos.side_to_move(), KING) == 0) {
	  return mlist;
  }
  ENDNEW

  Color us = pos.side_to_move();
  Bitboard target;

  if (Type == MV_CAPTURE || Type == MV_NON_EVASION)
      target = pos.pieces_of_color(opposite_color(us));
  else if (Type == MV_NON_CAPTURE)
      target = pos.empty_squares();
  else
      assert(false);

  if (Type == MV_NON_EVASION)
  {
      mlist = generate_piece_moves<PAWN, MV_CAPTURE>(pos, mlist, us, target);
      mlist = generate_piece_moves<PAWN, MV_NON_CAPTURE>(pos, mlist, us, pos.empty_squares());
      target |= pos.empty_squares();
  }
  else
      mlist = generate_piece_moves<PAWN, Type>(pos, mlist, us, target);

  mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
  mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
  mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
  mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
  OLD mlist = generate_piece_moves<KING>(pos, mlist, us, target);
  STARTNEW
  // captures by the king are not allowed
  if (Type != MV_CAPTURE) {
	  mlist = generate_piece_moves<KING>(pos, mlist, us, target);
  }
  ENDNEW

  if (Type != MV_CAPTURE)
  {
      if (pos.can_castle_kingside(us))
          mlist = generate_castle_moves<KING_SIDE>(pos, mlist, us);

      if (pos.can_castle_queenside(us))
          mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist, us);
  }

  return mlist;
}

// Assembly:
subq $0x1c8, %rsp            # imm = 0x1C8
movq %rdi, 0x68(%rsp)
movq %rsi, 0x60(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x68(%rsp), %rdi
callq 0xf5d0
movq 0x48(%rsp), %rdi
movl %eax, %esi
movl $0x6, %edx
callq 0x164c0
cmpl $0x0, %eax
jne 0x3328a
movq 0x60(%rsp), %rax
movq %rax, 0x70(%rsp)
jmp 0x33997
movq 0x68(%rsp), %rdi
callq 0xf5d0
movl %eax, 0x5c(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x40(%rsp)
movl 0x5c(%rsp), %edi
callq 0x122b0
movq 0x40(%rsp), %rdi
movl %eax, %esi
callq 0x298c0
movq %rax, 0x50(%rsp)
movq 0x68(%rsp), %rsi
movq 0x60(%rsp), %rdx
movl 0x5c(%rsp), %ecx
movq 0x50(%rsp), %rax
movq %rsi, 0x90(%rsp)
movq %rdx, 0x88(%rsp)
movl %ecx, 0x84(%rsp)
movq %rax, 0x78(%rsp)
cmpl $0x0, 0x84(%rsp)
jne 0x3331b
movq 0x90(%rsp), %rdi
movq 0x88(%rsp), %rsi
movq 0x78(%rsp), %rdx
movl $0x40, %ecx
callq 0x30c80
movq %rax, 0x38(%rsp)
jmp 0x3333f
movq 0x90(%rsp), %rdi
movq 0x88(%rsp), %rsi
movq 0x78(%rsp), %rdx
movl $0x40, %ecx
callq 0x30e60
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x28(%rsp)
movl 0x5c(%rsp), %eax
movl %eax, 0x34(%rsp)
movq 0x68(%rsp), %rdi
callq 0x33220
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movl 0x34(%rsp), %ecx
movq %rsi, 0x190(%rsp)
movq %rdx, 0x188(%rsp)
movl %ecx, 0x184(%rsp)
movq %rax, 0x178(%rsp)
cmpl $0x0, 0x184(%rsp)
jne 0x333cf
movq 0x190(%rsp), %rdi
movq 0x188(%rsp), %rsi
movq 0x178(%rsp), %rdx
movl $0x40, %ecx
callq 0x31460
movq %rax, 0x18(%rsp)
jmp 0x333f6
movq 0x190(%rsp), %rdi
movq 0x188(%rsp), %rsi
movq 0x178(%rsp), %rdx
movl $0x40, %ecx
callq 0x31660
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x68(%rsp), %rdi
callq 0x33220
orq 0x50(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x68(%rsp), %rsi
movq 0x60(%rsp), %rdx
movl 0x5c(%rsp), %ecx
movq 0x50(%rsp), %rax
movq %rsi, 0xc8(%rsp)
movq %rdx, 0xc0(%rsp)
movl %ecx, 0xbc(%rsp)
movq %rax, 0xb0(%rsp)
movq 0xc8(%rsp), %rdi
movl 0xbc(%rsp), %esi
movl $0x2, %edx
callq 0x299d0
movq %rax, 0x98(%rsp)
movq 0x98(%rsp), %rax
cmpl $0x40, (%rax)
je 0x3351d
jmp 0x3347a
movq 0x98(%rsp), %rax
movl (%rax), %eax
movl %eax, 0xa4(%rsp)
movq 0xc8(%rsp), %rdi
movl 0xa4(%rsp), %esi
callq 0x29aa0
andq 0xb0(%rsp), %rax
movq %rax, 0xa8(%rsp)
cmpq $0x0, 0xa8(%rsp)
je 0x334fa
movl 0xa4(%rsp), %eax
movl %eax, 0x14(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0xd940
movl 0x14(%rsp), %edi
movl %eax, %esi
callq 0xf810
movl %eax, %ecx
movq 0xc0(%rsp), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0xc0(%rsp)
movl %ecx, (%rax)
jmp 0x334af
movq 0x98(%rsp), %rax
movq %rax, %rcx
addq $0x4, %rcx
movq %rcx, 0x98(%rsp)
cmpl $0x40, 0x4(%rax)
jne 0x3347a
jmp 0x3351d
movq 0xc0(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x68(%rsp), %rsi
movq 0x60(%rsp), %rdx
movl 0x5c(%rsp), %ecx
movq 0x50(%rsp), %rax
movq %rsi, 0x100(%rsp)
movq %rdx, 0xf8(%rsp)
movl %ecx, 0xf4(%rsp)
movq %rax, 0xe8(%rsp)
movq 0x100(%rsp), %rdi
movl 0xf4(%rsp), %esi
movl $0x3, %edx
callq 0x299d0
movq %rax, 0xd0(%rsp)
movq 0xd0(%rsp), %rax
cmpl $0x40, (%rax)
je 0x33633
jmp 0x33590
movq 0xd0(%rsp), %rax
movl (%rax), %eax
movl %eax, 0xdc(%rsp)
movq 0x100(%rsp), %rdi
movl 0xdc(%rsp), %esi
callq 0x1b550
andq 0xe8(%rsp), %rax
movq %rax, 0xe0(%rsp)
cmpq $0x0, 0xe0(%rsp)
je 0x33610
movl 0xdc(%rsp), %eax
movl %eax, 0x10(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0xd940
movl 0x10(%rsp), %edi
movl %eax, %esi
callq 0xf810
movl %eax, %ecx
movq 0xf8(%rsp), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0xf8(%rsp)
movl %ecx, (%rax)
jmp 0x335c5
movq 0xd0(%rsp), %rax
movq %rax, %rcx
addq $0x4, %rcx
movq %rcx, 0xd0(%rsp)
cmpl $0x40, 0x4(%rax)
jne 0x33590
jmp 0x33633
movq 0xf8(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x68(%rsp), %rsi
movq 0x60(%rsp), %rdx
movl 0x5c(%rsp), %ecx
movq 0x50(%rsp), %rax
movq %rsi, 0x138(%rsp)
movq %rdx, 0x130(%rsp)
movl %ecx, 0x12c(%rsp)
movq %rax, 0x120(%rsp)
movq 0x138(%rsp), %rdi
movl 0x12c(%rsp), %esi
movl $0x4, %edx
callq 0x299d0
movq %rax, 0x108(%rsp)
movq 0x108(%rsp), %rax
cmpl $0x40, (%rax)
je 0x33749
jmp 0x336a6
movq 0x108(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x114(%rsp)
movq 0x138(%rsp), %rdi
movl 0x114(%rsp), %esi
callq 0x29d40
andq 0x120(%rsp), %rax
movq %rax, 0x118(%rsp)
cmpq $0x0, 0x118(%rsp)
je 0x33726
movl 0x114(%rsp), %eax
movl %eax, 0xc(%rsp)
leaq 0x118(%rsp), %rdi
callq 0xd940
movl 0xc(%rsp), %edi
movl %eax, %esi
callq 0xf810
movl %eax, %ecx
movq 0x130(%rsp), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0x130(%rsp)
movl %ecx, (%rax)
jmp 0x336db
movq 0x108(%rsp), %rax
movq %rax, %rcx
addq $0x4, %rcx
movq %rcx, 0x108(%rsp)
cmpl $0x40, 0x4(%rax)
jne 0x336a6
jmp 0x33749
movq 0x130(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x68(%rsp), %rsi
movq 0x60(%rsp), %rdx
movl 0x5c(%rsp), %ecx
movq 0x50(%rsp), %rax
movq %rsi, 0x170(%rsp)
movq %rdx, 0x168(%rsp)
movl %ecx, 0x164(%rsp)
movq %rax, 0x158(%rsp)
movq 0x170(%rsp), %rdi
movl 0x164(%rsp), %esi
movl $0x5, %edx
callq 0x299d0
movq %rax, 0x140(%rsp)
movq 0x140(%rsp), %rax
cmpl $0x40, (%rax)
je 0x3385f
jmp 0x337bc
movq 0x140(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x14c(%rsp)
movq 0x170(%rsp), %rdi
movl 0x14c(%rsp), %esi
callq 0x29cf0
andq 0x158(%rsp), %rax
movq %rax, 0x150(%rsp)
cmpq $0x0, 0x150(%rsp)
je 0x3383c
movl 0x14c(%rsp), %eax
movl %eax, 0x8(%rsp)
leaq 0x150(%rsp), %rdi
callq 0xd940
movl 0x8(%rsp), %edi
movl %eax, %esi
callq 0xf810
movl %eax, %ecx
movq 0x168(%rsp), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0x168(%rsp)
movl %ecx, (%rax)
jmp 0x337f1
movq 0x140(%rsp), %rax
movq %rax, %rcx
addq $0x4, %rcx
movq %rcx, 0x140(%rsp)
cmpl $0x40, 0x4(%rax)
jne 0x337bc
jmp 0x3385f
movq 0x168(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x68(%rsp), %rsi
movq 0x60(%rsp), %rdx
movl 0x5c(%rsp), %ecx
movq 0x50(%rsp), %rax
movq %rsi, 0x1c0(%rsp)
movq %rdx, 0x1b8(%rsp)
movl %ecx, 0x1b4(%rsp)
movq %rax, 0x1a8(%rsp)
movq 0x1c0(%rsp), %rdi
movl 0x1b4(%rsp), %esi
callq 0x1b250
movl %eax, 0x19c(%rsp)
movq 0x1c0(%rsp), %rdi
movl 0x19c(%rsp), %esi
callq 0x1b440
andq 0x1a8(%rsp), %rax
movq %rax, 0x1a0(%rsp)
cmpq $0x0, 0x1a0(%rsp)
je 0x33928
movl 0x19c(%rsp), %eax
movl %eax, 0x4(%rsp)
leaq 0x1a0(%rsp), %rdi
callq 0xd940
movl 0x4(%rsp), %edi
movl %eax, %esi
callq 0xf810
movl %eax, %ecx
movq 0x1b8(%rsp), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0x1b8(%rsp)
movl %ecx, (%rax)
jmp 0x338dd
movq 0x1b8(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x68(%rsp), %rdi
movl 0x5c(%rsp), %esi
callq 0xf550
testb $0x1, %al
jne 0x33949
jmp 0x33961
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %rsi
movl 0x5c(%rsp), %edx
callq 0x2e6c0
movq %rax, 0x60(%rsp)
movq 0x68(%rsp), %rdi
movl 0x5c(%rsp), %esi
callq 0xf580
testb $0x1, %al
jne 0x33975
jmp 0x3398d
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %rsi
movl 0x5c(%rsp), %edx
callq 0x2eb20
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rax
addq $0x1c8, %rsp            # imm = 0x1C8
retq
nopw %cs:(%rax,%rax)
nop