// File: jswaff[P]prophet3/src/apply.cpp
// Function: undo_last_move(position*, undo*)
void undo_last_move(position *p,undo *undo_stack) {
    assert(p);
    assert(p->move_counter > 0);
    assert(undo_stack);

    undo *u = undo_stack + p->move_counter - 1;

    p->player = opposite_player(p->player);
    p->ep_sq = u->ep_sq;
    p->castling_rights = u->castling_rights;
    p->fifty_counter = u->fifty_counter;
    p->move_counter--;

    move mv = u->mv;
    assert(abs(u->captured)==get_captured_piece(mv));

    // remove the moving piece from to_sq
    square_t to_sq = get_to_sq(mv);
    square_t from_sq = get_from_sq(mv);
    piece_t piece = get_piece(mv);

    if (p->piece[to_sq] != NO_PIECE) {
        remove_piece(p,to_sq);
    }

    // place the moving piece on from_sq
    add_piece(p,piece * (p->player==WHITE ? 1 : -1),from_sq);
    if (piece==KING) {
        if (p->player==WHITE) {
            p->white_king = from_sq;
        } else {
            p->black_king = from_sq;
        }
    }

    // restore the captured piece
    if (is_capture(mv)) {
        if (is_epcapture(mv)) {
            if (p->player==BLACK) {
                add_piece(p,PAWN,(square_t)(p->ep_sq-8));
            } else {
                add_piece(p,-PAWN,(square_t)(p->ep_sq+8));
            }
        } else {
            add_piece(p,u->captured,to_sq);
        }
    } else if (is_castle(mv)) {
        if (from_sq==E1) {
            if (to_sq==G1) {
                remove_piece(p,F1);
                add_piece(p,ROOK,H1);
            } else {
                remove_piece(p,D1);
                add_piece(p,ROOK,A1);
            }
        } else {
            if (to_sq==G8) {
                remove_piece(p,F8);
                add_piece(p,-ROOK,H8);
            } else {
                remove_piece(p,D8);
                add_piece(p,-ROOK,A8);
            }
        }
    }

    p->hash_key = u->hash_key;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpq $0x0, -0x8(%rbp)
je 0x39b9
jmp 0x39d8
leaq 0x3e1de(%rip), %rdi     # 0x41b9e
leaq 0x3167f(%rip), %rsi     # 0x35046
movl $0x95, %edx
leaq 0x317c4(%rip), %rcx     # 0x35197
callq 0x20c0
movq -0x8(%rbp), %rax
cmpl $0x0, 0x144(%rax)
jbe 0x39e7
jmp 0x3a06
leaq 0x317d1(%rip), %rdi     # 0x351bf
leaq 0x31651(%rip), %rsi     # 0x35046
movl $0x96, %edx
leaq 0x31796(%rip), %rcx     # 0x35197
callq 0x20c0
cmpq $0x0, -0x10(%rbp)
je 0x3a0f
jmp 0x3a2e
leaq 0x3174e(%rip), %rdi     # 0x35164
leaq 0x31629(%rip), %rsi     # 0x35046
movl $0x97, %edx
leaq 0x3176e(%rip), %rcx     # 0x35197
callq 0x20c0
movq -0x10(%rbp), %rax
movq -0x8(%rbp), %rcx
movl 0x144(%rcx), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
addq $-0x20, %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movl 0x138(%rax), %edi
callq 0x14130
movl %eax, %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x138(%rax)
movq -0x18(%rbp), %rax
movl 0x14(%rax), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x13c(%rax)
movq -0x18(%rbp), %rax
movl 0x1c(%rax), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x140(%rax)
movq -0x18(%rbp), %rax
movl 0x18(%rax), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x148(%rax)
movq -0x8(%rbp), %rax
movl 0x144(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x144(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
movl %eax, -0x30(%rbp)
movq -0x20(%rbp), %rdi
callq 0xab00
movl %eax, %ecx
movl -0x30(%rbp), %eax
cmpl %ecx, %eax
jne 0x3adc
jmp 0x3afb
leaq 0x316f0(%rip), %rdi     # 0x351d3
leaq 0x3155c(%rip), %rsi     # 0x35046
movl $0xa2, %edx
leaq 0x316a1(%rip), %rcx     # 0x35197
callq 0x20c0
movq -0x20(%rbp), %rdi
callq 0xaaa0
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rdi
callq 0xaa80
movl %eax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
callq 0xaac0
movl %eax, -0x2c(%rbp)
movq -0x8(%rbp), %rax
movl -0x24(%rbp), %ecx
cmpl $0x0, (%rax,%rcx,4)
je 0x3b38
movq -0x8(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0xee40
movq -0x8(%rbp), %rdi
movl -0x2c(%rbp), %esi
movq -0x8(%rbp), %rax
movl 0x138(%rax), %edx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0x1, %ecx
cmpl $0x1, %edx
cmovel %ecx, %eax
imull %eax, %esi
movl -0x28(%rbp), %edx
callq 0xeb20
cmpl $0x6, -0x2c(%rbp)
jne 0x3b95
movq -0x8(%rbp), %rax
cmpl $0x1, 0x138(%rax)
jne 0x3b86
movl -0x28(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x14c(%rax)
jmp 0x3b93
movl -0x28(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x150(%rax)
jmp 0x3b95
movq -0x20(%rbp), %rdi
callq 0xab20
testb $0x1, %al
jne 0x3ba4
jmp 0x3c12
movq -0x20(%rbp), %rdi
callq 0xab50
testb $0x1, %al
jne 0x3bb3
jmp 0x3bfa
movq -0x8(%rbp), %rax
cmpl $0x0, 0x138(%rax)
jne 0x3bdd
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0x13c(%rax), %edx
subl $0x8, %edx
movl $0x1, %esi
callq 0xeb20
jmp 0x3bf8
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0x13c(%rax), %edx
addl $0x8, %edx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xeb20
jmp 0x3c0d
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x10(%rax), %esi
movl -0x24(%rbp), %edx
callq 0xeb20
jmp 0x3cc3
movq -0x20(%rbp), %rdi
callq 0xab70
testb $0x1, %al
jne 0x3c24
jmp 0x3cc1
cmpl $0x3c, -0x28(%rbp)
jne 0x3c76
cmpl $0x3e, -0x24(%rbp)
jne 0x3c53
movq -0x8(%rbp), %rdi
movl $0x3d, %esi
callq 0xee40
movq -0x8(%rbp), %rdi
movl $0x4, %esi
movl $0x3f, %edx
callq 0xeb20
jmp 0x3c74
movq -0x8(%rbp), %rdi
movl $0x3b, %esi
callq 0xee40
movq -0x8(%rbp), %rdi
movl $0x4, %esi
movl $0x38, %edx
callq 0xeb20
jmp 0x3cbf
cmpl $0x6, -0x24(%rbp)
jne 0x3c9f
movq -0x8(%rbp), %rdi
movl $0x5, %esi
callq 0xee40
movq -0x8(%rbp), %rdi
movl $0xfffffffc, %esi       # imm = 0xFFFFFFFC
movl $0x7, %edx
callq 0xeb20
jmp 0x3cbd
movq -0x8(%rbp), %rdi
movl $0x3, %esi
callq 0xee40
movq -0x8(%rbp), %rdi
movl $0xfffffffc, %esi       # imm = 0xFFFFFFFC
xorl %edx, %edx
callq 0xeb20
jmp 0x3cbf
jmp 0x3cc1
jmp 0x3cc3
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x1b8(%rax)
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)