// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r15
movl 0x1090(%rdi), %ebx
movq %rbx, %rax
shlq $0x5, %rax
cmpl $0x0, 0x168(%rdi,%rax)
je 0x1611c
movq %rdi, %r14
movq %rbx, %rax
xorq $0x1, %rax
movq 0x140(%rdi,%rax,8), %r12
movq %r15, %rsi
movq %r12, %rdx
testq %rbx, %rbx
je 0x15eb5
callq 0x15621
movq 0x100(%r14), %rdx
notq %rdx
movq %r14, %rdi
movq %rax, %rsi
callq 0x1589a
jmp 0x15ecf
callq 0x154b5
movq 0x100(%r14), %rdx
notq %rdx
movq %r14, %rdi
movq %rax, %rsi
callq 0x15786
movq %rax, %r15
movq 0x100(%r14), %rax
notq %rax
orq %rax, %r12
leaq 0x190(%r14), %rax
movq %rax, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
shlq $0x9, %rbx
movl 0x210(%r14,%rbx), %eax
cmpl $0x40, %eax
movq %rbx, 0x10(%rsp)
je 0x15f5b
addq %r14, %rbx
addq $0x210, %rbx            # imm = 0x210
leaq 0x8(%rsp), %r13
movslq %eax, %rbp
leaq 0xfb831(%rip), %rax     # 0x111750
movq 0x400(%rax,%rbp,8), %rax
andq %r12, %rax
movq %rax, 0x8(%rsp)
shll $0x6, %ebp
testq %rax, %rax
je 0x15f4f
movq %r13, %rdi
callq 0x7b62
orl %ebp, %eax
movl %eax, (%r15)
addq $0x8, %r15
movq 0x8(%rsp), %rax
jmp 0x15f32
movl 0x4(%rbx), %eax
addq $0x4, %rbx
cmpl $0x40, %eax
jne 0x15f15
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
movl 0xc0(%rax,%rcx), %ebp
cmpl $0x40, %ebp
je 0x15fbf
leaq (%rax,%rcx), %rbx
addq $0xc0, %rbx
leaq 0x8(%rsp), %r13
movq %r14, %rdi
movl %ebp, %esi
callq 0xcf20
andq %r12, %rax
movq %rax, 0x8(%rsp)
shll $0x6, %ebp
testq %rax, %rax
je 0x15fb3
movq %r13, %rdi
callq 0x7b62
orl %ebp, %eax
movl %eax, (%r15)
addq $0x8, %r15
movq 0x8(%rsp), %rax
jmp 0x15f96
movl 0x4(%rbx), %ebp
addq $0x4, %rbx
cmpl $0x40, %ebp
jne 0x15f81
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
movl 0x100(%rax,%rcx), %ebp
cmpl $0x40, %ebp
je 0x16023
leaq (%rax,%rcx), %rbx
addq $0x100, %rbx            # imm = 0x100
leaq 0x8(%rsp), %r13
movq %r14, %rdi
movl %ebp, %esi
callq 0x12330
andq %r12, %rax
movq %rax, 0x8(%rsp)
shll $0x6, %ebp
testq %rax, %rax
je 0x16017
movq %r13, %rdi
callq 0x7b62
orl %ebp, %eax
movl %eax, (%r15)
addq $0x8, %r15
movq 0x8(%rsp), %rax
jmp 0x15ffa
movl 0x4(%rbx), %ebp
addq $0x4, %rbx
cmpl $0x40, %ebp
jne 0x15fe5
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
movl 0x140(%rax,%rcx), %ebp
cmpl $0x40, %ebp
je 0x16087
leaq (%rax,%rcx), %rbx
addq $0x140, %rbx            # imm = 0x140
leaq 0x8(%rsp), %r13
movq %r14, %rdi
movl %ebp, %esi
callq 0x1230a
andq %r12, %rax
movq %rax, 0x8(%rsp)
shll $0x6, %ebp
testq %rax, %rax
je 0x1607b
movq %r13, %rdi
callq 0x7b62
orl %ebp, %eax
movl %eax, (%r15)
addq $0x8, %r15
movq 0x8(%rsp), %rax
jmp 0x1605e
movl 0x4(%rbx), %ebp
addq $0x4, %rbx
cmpl $0x40, %ebp
jne 0x16049
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
movslq 0x180(%rax,%rcx), %rbx
leaq 0xfb6b0(%rip), %rax     # 0x111750
andq 0xc00(%rax,%rbx,8), %r12
leaq 0x8(%rsp), %r13
movq %r12, (%r13)
shll $0x6, %ebx
testq %r12, %r12
je 0x160d1
movq %r13, %rdi
callq 0x7b62
orl %ebx, %eax
movl %eax, (%r15)
addq $0x8, %r15
movq 0x8(%rsp), %r12
jmp 0x160b4
movq 0x1938(%r14), %rax
movl 0x10(%rax), %eax
movq 0x20(%rsp), %rbx
leal 0x1(%rbx), %ecx
testl %ecx, %eax
je 0x16101
movq %r14, %rdi
movq %r15, %rsi
movl %ebx, %edx
callq 0x14060
movq %rax, %r15
movq 0x1938(%r14), %rax
movl 0x10(%rax), %eax
leal 0x4(,%rbx,4), %ecx
testl %ecx, %eax
je 0x1611c
movq %r14, %rdi
movq %r15, %rsi
movl %ebx, %edx
callq 0x14236
movq %rax, %r15
movq %r15, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop