// File: /workspace/github_cpp_makefile_star2_O2/Thilotilo[P]DoaERando/Randomizer.cpp
// Function: DoaERando::Randomizer::ReconfigureQingZhouCave()
void Randomizer::ReconfigureQingZhouCave()
{
    // This closes the Qing Zhou Exit
    myRom->WriteByte(0x19BD1, 0x62);
    myRom->WriteByte(0x19BD9, 0x80);

    // This prevents the Qing Zhou cave from disappearing after defeating
    // the Yellow Scarves.
    // Easiest way for now is just to replace the "inactive" write
    // to the flag with the "active" write (00 -> 01)
    myRom->WriteByte(0x3639F, 0x01);

    // We want to force bridge creation again.  Since we care about this trigger,
    // overwrite any previous updates that make the bridge permanently available.
    myRom->WriteByte(0x34781, 0x00);

    // Update the cave NPC data to lose the yellow scarves and instead have a single
    // Talk-and-join general.  With this update and the new use of the cave, we
    // now no longer need special code to execute when we load this map, so we
    // save 2 additional bytes on top of the 14 bytes we save by decreasing the
    // character count by 2
    std::vector<BYTE> caveNPCData {
        // NPC in cave -> actual NPC to be set by the manipulator, so we just
        // set it to 0 for now.
        0x1B, 0x04, 0x83, 0x03, 0x19, 0x19, 0x00,
        0x27, 0x03, 0x88, 0x04, 0x69, 0x69, 0x02, // Brigand guarding bridge
        0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Cleared NPC Data
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Cleared NPC Data
        // Since there's no longer any code to execute, we need to clear
        // those extra 2 bytes too (plus the original FF)
        0x00, 0x00, 0x00
    };

    for (int i = 0; i < caveNPCData.size(); ++i)
    {
        myRom->WriteByte(0x31188 + i, caveNPCData[i]);
    }

    // Remove now-unused code for loading the Qing Zhou cave so we can reclaim
    // it for other nefarious purposes.
    const size_t NUM_EVENT_BYTES_TO_CLEAR = 22;
    for (size_t i = 0; i < NUM_EVENT_BYTES_TO_CLEAR; ++i)
    {
        myRom->WriteByte(0x363BF + i, 0x00);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x40, %rsp
movq %rdi, %rbx
movq (%rdi), %rdi
pushq $0x62
popq %rdx
movl $0x19bd1, %esi          # imm = 0x19BD1
callq 0x404a00
movq (%rbx), %rdi
movl $0x19bd9, %esi          # imm = 0x19BD9
movl $0x80, %edx
callq 0x404a00
movq (%rbx), %rdi
pushq $0x1
popq %rdx
movl $0x3639f, %esi          # imm = 0x3639F
callq 0x404a00
movq (%rbx), %rdi
xorl %r14d, %r14d
movl $0x34781, %esi          # imm = 0x34781
xorl %edx, %edx
callq 0x404a00
movups 0x1849a8(%rip), %xmm0   # 0x58b3e3
leaq -0x50(%rbp), %rsi
movups %xmm0, 0xf(%rsi)
movups 0x18498a(%rip), %xmm0   # 0x58b3d4
movaps %xmm0, (%rsi)
leaq -0x30(%rbp), %rdi
pushq $0x1f
popq %rdx
leaq -0x11(%rbp), %rcx
callq 0x409c64
movq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
subq %rax, %rcx
cmpq %r14, %rcx
jbe 0x406a86
movq (%rbx), %rdi
movzbl (%rax,%r14), %edx
leal 0x31188(%r14), %esi
callq 0x404a00
incq %r14
jmp 0x406a5d
pushq $-0x16
popq %r14
testq %r14, %r14
je 0x406aa5
movq (%rbx), %rdi
leal 0x363d5(%r14), %esi
xorl %edx, %edx
callq 0x404a00
incq %r14
jmp 0x406a8a
leaq -0x30(%rbp), %rdi
callq 0x404baa
addq $0x40, %rsp
popq %rbx
popq %r14
popq %rbp
retq
jmp 0x406ab9
movq %rax, %rbx
leaq -0x30(%rbp), %rdi
callq 0x404baa
movq %rbx, %rdi
callq 0x4ba600
nop