// File: 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
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x88(%rbp)
movq (%rax), %rdi
movl $0x19bd1, %esi          # imm = 0x19BD1
movl $0x62, %edx
callq 0x404c20
movq -0x88(%rbp), %rax
movq (%rax), %rdi
movl $0x19bd9, %esi          # imm = 0x19BD9
movl $0x80, %edx
callq 0x404c20
movq -0x88(%rbp), %rax
movq (%rax), %rdi
movl $0x3639f, %esi          # imm = 0x3639F
movl $0x1, %edx
callq 0x404c20
movq -0x88(%rbp), %rax
movq (%rax), %rdi
movl $0x34781, %esi          # imm = 0x34781
xorl %edx, %edx
callq 0x404c20
movups 0x191a55(%rip), %xmm0   # 0x59b370
movups %xmm0, -0x41(%rbp)
movups 0x191a3b(%rip), %xmm0   # 0x59b361
movaps %xmm0, -0x50(%rbp)
leaq -0x50(%rbp), %rax
movq %rax, -0x30(%rbp)
movq $0x1f, -0x28(%rbp)
leaq -0x51(%rbp), %rdi
movq %rdi, -0x80(%rbp)
callq 0x404e70
movq -0x80(%rbp), %rcx
movq -0x30(%rbp), %rsi
movq -0x28(%rbp), %rdx
leaq -0x20(%rbp), %rdi
callq 0x40fb50
jmp 0x40995e
leaq -0x51(%rbp), %rdi
callq 0x404fb0
movl $0x0, -0x68(%rbp)
movslq -0x68(%rbp), %rax
movq %rax, -0x90(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x405010
movq %rax, %rcx
movq -0x90(%rbp), %rax
cmpq %rcx, %rax
jae 0x409a0e
movq -0x88(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xa0(%rbp)
movslq -0x68(%rbp), %rsi
movl %esi, %eax
addl $0x31188, %eax          # imm = 0x31188
movl %eax, -0x94(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x405030
movq -0xa0(%rbp), %rdi
movl -0x94(%rbp), %esi
movzbl (%rax), %edx
callq 0x404c20
jmp 0x4099d3
jmp 0x4099d5
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x40996e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x51(%rbp), %rdi
callq 0x404fb0
jmp 0x409a63
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x404f50
jmp 0x409a63
movq $0x16, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
cmpq $0x16, -0x78(%rbp)
jae 0x409a51
movq -0x88(%rbp), %rax
movq (%rax), %rdi
movl -0x78(%rbp), %esi
addl $0x363bf, %esi          # imm = 0x363BF
xorl %edx, %edx
callq 0x404c20
jmp 0x409a41
jmp 0x409a43
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x409a1e
leaq -0x20(%rbp), %rdi
callq 0x404f50
addq $0xa0, %rsp
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x4c9ba0
nopl (%rax)