// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
testq %rdi, %rdi
je 0x33cf
movq %rdi, %rbx
movl 0x144(%rdi), %ebp
testq %rbp, %rbp
je 0x33ee
movq %rsi, %r14
testq %rsi, %rsi
je 0x340d
shlq $0x5, %rbp
movl 0x138(%rbx), %edi
callq 0xd4d8
movl %eax, 0x138(%rbx)
movl -0xc(%r14,%rbp), %eax
movl %eax, 0x13c(%rbx)
movl -0x4(%r14,%rbp), %eax
movl %eax, 0x140(%rbx)
movl -0x8(%r14,%rbp), %eax
movl %eax, 0x148(%rbx)
decl 0x144(%rbx)
movq -0x20(%r14,%rbp), %r15
movl -0x10(%r14,%rbp), %eax
movl %eax, %r12d
negl %r12d
cmovsl %eax, %r12d
movq %r15, %rdi
callq 0x7390
cmpl %eax, %r12d
jne 0x342c
movq %r15, %rdi
callq 0x7372
movl %eax, %r12d
movq %r15, %rdi
callq 0x736b
movl %eax, 0x4(%rsp)
movq %r15, %rdi
callq 0x737c
movl %eax, %r13d
movl %r12d, %eax
cmpl $0x0, (%rbx,%rax,4)
je 0x32c1
movq %rbx, %rdi
movl %r12d, %esi
callq 0x9f37
movl %r13d, %esi
negl %esi
cmpl $0x1, 0x138(%rbx)
cmovel %r13d, %esi
movq %rbx, %rdi
movl 0x4(%rsp), %edx
callq 0x9db0
cmpl $0x6, %r13d
jne 0x3302
cmpl $0x1, 0x138(%rbx)
jne 0x32f8
movl 0x4(%rsp), %eax
movl %eax, 0x14c(%rbx)
jmp 0x3302
movl 0x4(%rsp), %eax
movl %eax, 0x150(%rbx)
addq %rbp, %r14
movq %r15, %rdi
callq 0x739a
movq %r15, %rdi
testb %al, %al
je 0x3333
callq 0x73a4
testb %al, %al
je 0x335b
cmpl $0x0, 0x138(%rbx)
movl 0x13c(%rbx), %edx
je 0x337f
addl $0x8, %edx
pushq $-0x1
jmp 0x3384
callq 0x73ae
testb %al, %al
je 0x33a0
cmpl $0x3c, 0x4(%rsp)
jne 0x3367
cmpl $0x3e, %r12d
jne 0x3387
pushq $0x3d
popq %rsi
movq %rbx, %rdi
callq 0x9f37
pushq $0x4
popq %rsi
pushq $0x3f
jmp 0x3397
movl -0x10(%r14), %esi
movq %rbx, %rdi
movl %r12d, %edx
jmp 0x339b
cmpl $0x6, %r12d
jne 0x33ba
pushq $0x5
popq %rsi
movq %rbx, %rdi
callq 0x9f37
pushq $-0x4
popq %rsi
pushq $0x7
jmp 0x3397
addl $-0x8, %edx
pushq $0x1
popq %rsi
jmp 0x3398
pushq $0x3b
popq %rsi
movq %rbx, %rdi
callq 0x9f37
pushq $0x4
popq %rsi
pushq $0x38
popq %rdx
movq %rbx, %rdi
callq 0x9db0
movq -0x18(%r14), %rax
movq %rax, 0x1b8(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x3
popq %rsi
movq %rbx, %rdi
callq 0x9f37
pushq $-0x4
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
jmp 0x339b
leaq 0x2ffe9(%rip), %rdi     # 0x333bf
leaq 0x23c35(%rip), %rsi     # 0x27012
leaq 0x23d7f(%rip), %rcx     # 0x27163
movl $0x95, %edx
callq 0x20c0
leaq 0x23d96(%rip), %rdi     # 0x2718b
leaq 0x23c16(%rip), %rsi     # 0x27012
leaq 0x23d60(%rip), %rcx     # 0x27163
movl $0x96, %edx
callq 0x20c0
leaq 0x23d1c(%rip), %rdi     # 0x27130
leaq 0x23bf7(%rip), %rsi     # 0x27012
leaq 0x23d41(%rip), %rcx     # 0x27163
movl $0x97, %edx
callq 0x20c0
leaq 0x23d6c(%rip), %rdi     # 0x2719f
leaq 0x23bd8(%rip), %rsi     # 0x27012
leaq 0x23d22(%rip), %rcx     # 0x27163
movl $0xa2, %edx
callq 0x20c0