// File: ATOMIC-LICHESS[P]ATOMKRAFT/position.cpp
// Function: Position::to_fen[abi:cxx11]() const
const string Position::to_fen() const {

  string fen;
  Square sq;
  char emptyCnt = '0';

  for (Rank rank = RANK_8; rank >= RANK_1; rank--, fen += '/')
  {
      for (File file = FILE_A; file <= FILE_H; file++)
      {
          sq = make_square(file, rank);

          if (square_is_occupied(sq))
          {
              if (emptyCnt != '0')
              {
                  fen += emptyCnt;
                  emptyCnt = '0';
              }
              fen += pieceLetters.from_piece(piece_on(sq));
          } else
              emptyCnt++;
      }

      if (emptyCnt != '0')
      {
          fen += emptyCnt;
          emptyCnt = '0';
      }
  }

  fen += (sideToMove == WHITE ? " w " : " b ");

  if (st->castleRights != CASTLES_NONE)
  {
      if (can_castle_kingside(WHITE))
          fen += chess960 ? char(toupper(file_to_char(initialKRFile))) : 'K';

      if (can_castle_queenside(WHITE))
          fen += chess960 ? char(toupper(file_to_char(initialQRFile))) : 'Q';

      if (can_castle_kingside(BLACK))
          fen += chess960 ? file_to_char(initialKRFile) : 'k';

      if (can_castle_queenside(BLACK))
          fen += chess960 ? file_to_char(initialQRFile) : 'q';
  } else
      fen += '-';

  fen += (ep_square() == SQ_NONE ? " -" : " " + square_to_string(ep_square()));
  return fen;
}

// Assembly:
subq $0xd8, %rsp
movq %rdi, 0x38(%rsp)
movq %rdi, %rax
movq %rax, 0x40(%rsp)
movq %rdi, 0xd0(%rsp)
movq %rsi, 0xc8(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x48(%rsp)
movb $0x0, 0xc7(%rsp)
callq 0x6730
movb $0x30, 0xbf(%rsp)
movl $0x7, 0xb8(%rsp)
cmpl $0x0, 0xb8(%rsp)
jl 0x38130
movl $0x0, 0xb4(%rsp)
cmpl $0x7, 0xb4(%rsp)
jg 0x380de
movl 0xb4(%rsp), %edi
movl 0xb8(%rsp), %esi
callq 0xd740
movl %eax, 0x34(%rsp)
jmp 0x38001
movq 0x48(%rsp), %rdi
movl 0x34(%rsp), %eax
movl %eax, 0xc0(%rsp)
movl 0xc0(%rsp), %esi
callq 0x33ac0
movb %al, 0x33(%rsp)
jmp 0x38023
movb 0x33(%rsp), %al
testb $0x1, %al
jne 0x38030
jmp 0x380b6
movsbl 0xbf(%rsp), %eax
cmpl $0x30, %eax
je 0x38074
movq 0x38(%rsp), %rdi
movsbl 0xbf(%rsp), %esi
callq 0x62d0
jmp 0x38051
movb $0x30, 0xbf(%rsp)
jmp 0x38074
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa8(%rsp)
movl %eax, 0xa4(%rsp)
jmp 0x384cb
movq 0x48(%rsp), %rdi
movl 0xc0(%rsp), %esi
callq 0xf530
movl %eax, 0x2c(%rsp)
jmp 0x3808b
movl 0x2c(%rsp), %esi
leaq 0x3b3402(%rip), %rdi    # 0x3eb498
callq 0x384f0
movb %al, 0x2b(%rsp)
jmp 0x380a1
movq 0x38(%rsp), %rdi
movb 0x2b(%rsp), %al
movsbl %al, %esi
callq 0x62d0
jmp 0x380b4
jmp 0x380c6
movb 0xbf(%rsp), %al
addb $0x1, %al
movb %al, 0xbf(%rsp)
jmp 0x380c8
leaq 0xb4(%rsp), %rdi
xorl %esi, %esi
callq 0xe560
jmp 0x380d9
jmp 0x37fda
movsbl 0xbf(%rsp), %eax
cmpl $0x30, %eax
je 0x38107
movq 0x38(%rsp), %rdi
movsbl 0xbf(%rsp), %esi
callq 0x62d0
jmp 0x380ff
movb $0x30, 0xbf(%rsp)
jmp 0x38109
leaq 0xb8(%rsp), %rdi
xorl %esi, %esi
callq 0xe590
jmp 0x3811a
movq 0x38(%rsp), %rdi
movl $0x2f, %esi
callq 0x62d0
jmp 0x3812b
jmp 0x37fc1
movq 0x48(%rsp), %rax
cmpl $0x0, 0x1090(%rax)
jne 0x3814c
leaq 0x230a1(%rip), %rax     # 0x5b1e6
movq %rax, 0x20(%rsp)
jmp 0x3815a
leaq 0x23097(%rip), %rax     # 0x5b1ea
movq %rax, 0x20(%rsp)
jmp 0x3815a
movq 0x38(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x6480
jmp 0x3816b
movq 0x48(%rsp), %rax
movq 0x1938(%rax), %rax
cmpl $0x0, 0x10(%rax)
je 0x38335
movq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0xf550
movb %al, 0x1f(%rsp)
jmp 0x38193
movb 0x1f(%rsp), %al
testb $0x1, %al
jne 0x3819d
jmp 0x381f0
movq 0x48(%rsp), %rax
testb $0x1, 0x1924(%rax)
je 0x381d3
movq 0x48(%rsp), %rax
movl 0x191c(%rax), %edi
callq 0x2e550
movb %al, 0x1e(%rsp)
jmp 0x381c1
movb 0x1e(%rsp), %al
movsbl %al, %edi
callq 0x69f0
movb %al, 0x1d(%rsp)
jmp 0x381db
movb $0x4b, %al
movb %al, 0x1d(%rsp)
jmp 0x381db
movq 0x38(%rsp), %rdi
movb 0x1d(%rsp), %al
movsbl %al, %esi
callq 0x62d0
jmp 0x381ee
jmp 0x381f0
movq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0xf580
movb %al, 0x1c(%rsp)
jmp 0x38202
movb 0x1c(%rsp), %al
testb $0x1, %al
jne 0x3820c
jmp 0x3825f
movq 0x48(%rsp), %rax
testb $0x1, 0x1924(%rax)
je 0x38242
movq 0x48(%rsp), %rax
movl 0x1920(%rax), %edi
callq 0x2e550
movb %al, 0x1b(%rsp)
jmp 0x38230
movb 0x1b(%rsp), %al
movsbl %al, %edi
callq 0x69f0
movb %al, 0x1a(%rsp)
jmp 0x3824a
movb $0x51, %al
movb %al, 0x1a(%rsp)
jmp 0x3824a
movq 0x38(%rsp), %rdi
movb 0x1a(%rsp), %al
movsbl %al, %esi
callq 0x62d0
jmp 0x3825d
jmp 0x3825f
movq 0x48(%rsp), %rdi
movl $0x1, %esi
callq 0xf550
movb %al, 0x19(%rsp)
jmp 0x38274
movb 0x19(%rsp), %al
testb $0x1, %al
jne 0x3827e
jmp 0x382c9
movq 0x48(%rsp), %rax
testb $0x1, 0x1924(%rax)
je 0x382ac
movq 0x48(%rsp), %rax
movl 0x191c(%rax), %edi
callq 0x2e550
movb %al, 0x18(%rsp)
jmp 0x382a2
movb 0x18(%rsp), %al
movb %al, 0x17(%rsp)
jmp 0x382b4
movb $0x6b, %al
movb %al, 0x17(%rsp)
jmp 0x382b4
movq 0x38(%rsp), %rdi
movb 0x17(%rsp), %al
movsbl %al, %esi
callq 0x62d0
jmp 0x382c7
jmp 0x382c9
movq 0x48(%rsp), %rdi
movl $0x1, %esi
callq 0xf580
movb %al, 0x16(%rsp)
jmp 0x382de
movb 0x16(%rsp), %al
testb $0x1, %al
jne 0x382e8
jmp 0x38333
movq 0x48(%rsp), %rax
testb $0x1, 0x1924(%rax)
je 0x38316
movq 0x48(%rsp), %rax
movl 0x1920(%rax), %edi
callq 0x2e550
movb %al, 0x15(%rsp)
jmp 0x3830c
movb 0x15(%rsp), %al
movb %al, 0x14(%rsp)
jmp 0x3831e
movb $0x71, %al
movb %al, 0x14(%rsp)
jmp 0x3831e
movq 0x38(%rsp), %rdi
movb 0x14(%rsp), %al
movsbl %al, %esi
callq 0x62d0
jmp 0x38331
jmp 0x38333
jmp 0x38348
movq 0x38(%rsp), %rdi
movl $0x2d, %esi
callq 0x62d0
jmp 0x38346
jmp 0x38348
movq 0x48(%rsp), %rdi
movb $0x0, 0x7e(%rsp)
movb $0x0, 0x57(%rsp)
callq 0xf5b0
movl %eax, 0x10(%rsp)
jmp 0x38362
movl 0x10(%rsp), %eax
cmpl $0x40, %eax
jne 0x3839c
leaq 0x7f(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x6980
movq 0x8(%rsp), %rdx
movb $0x1, 0x7e(%rsp)
leaq 0x217dd(%rip), %rsi     # 0x59b68
leaq 0x80(%rsp), %rdi
callq 0x12070
jmp 0x3839a
jmp 0x383de
movq 0x48(%rsp), %rdi
callq 0xf5b0
movl %eax, 0x4(%rsp)
jmp 0x383ac
movl 0x4(%rsp), %esi
leaq 0x58(%rsp), %rdi
callq 0x2e410
jmp 0x383bc
movb $0x1, 0x57(%rsp)
leaq 0x21198(%rip), %rsi     # 0x59560
leaq 0x80(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x3e080
jmp 0x383dc
jmp 0x383de
movq 0x38(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x6690
jmp 0x383f2
leaq 0x80(%rsp), %rdi
callq 0x6a78
testb $0x1, 0x57(%rsp)
jne 0x38408
jmp 0x38412
leaq 0x58(%rsp), %rdi
callq 0x6a78
testb $0x1, 0x7e(%rsp)
jne 0x3841b
jmp 0x38425
leaq 0x7f(%rsp), %rdi
callq 0x6550
movb $0x1, 0xc7(%rsp)
testb $0x1, 0xc7(%rsp)
jne 0x384be
jmp 0x384b4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa8(%rsp)
movl %eax, 0xa4(%rsp)
jmp 0x3849f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa8(%rsp)
movl %eax, 0xa4(%rsp)
jmp 0x3848a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa8(%rsp)
movl %eax, 0xa4(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x6a78
testb $0x1, 0x57(%rsp)
jne 0x38493
jmp 0x3849d
leaq 0x58(%rsp), %rdi
callq 0x6a78
jmp 0x3849f
testb $0x1, 0x7e(%rsp)
jne 0x384a8
jmp 0x384b2
leaq 0x7f(%rsp), %rdi
callq 0x6550
jmp 0x384cb
movq 0x38(%rsp), %rdi
callq 0x6a78
movq 0x40(%rsp), %rax
addq $0xd8, %rsp
retq
movq 0x38(%rsp), %rdi
callq 0x6a78
movq 0xa8(%rsp), %rdi
callq 0x6970
nopw %cs:(%rax,%rax)