// File: /workspace/github_cpp_makefile_star2_O1/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
movl $0x19bd1, %esi          # imm = 0x19BD1
movl $0x62, %edx
callq 0x404a28
movq (%rbx), %rdi
movl $0x19bd9, %esi          # imm = 0x19BD9
movl $0x80, %edx
callq 0x404a28
movq (%rbx), %rdi
movl $0x3639f, %esi          # imm = 0x3639F
movl $0x1, %edx
callq 0x404a28
movq (%rbx), %rdi
movl $0x34781, %esi          # imm = 0x34781
xorl %edx, %edx
callq 0x404a28
movups 0x185665(%rip), %xmm0   # 0x58c3e3
leaq -0x50(%rbp), %rsi
movups %xmm0, 0xf(%rsi)
movups 0x185647(%rip), %xmm0   # 0x58c3d4
movaps %xmm0, (%rsi)
leaq -0x30(%rbp), %r14
leaq -0x11(%rbp), %rcx
movl $0x1f, %edx
movq %r14, %rdi
callq 0x40a9c2
movq (%r14), %rax
cmpq %rax, 0x8(%r14)
je 0x406dd8
xorl %r14d, %r14d
movq (%rbx), %rdi
movzbl (%rax,%r14), %edx
leal 0x31188(%r14), %esi
callq 0x404a28
incq %r14
movq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
subq %rax, %rcx
cmpq %r14, %rcx
ja 0x406db1
movq $-0x16, %r14
movq (%rbx), %rdi
leal 0x363d5(%r14), %esi
xorl %edx, %edx
callq 0x404a28
incq %r14
jne 0x406ddf
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x406e03
callq 0x410b90
addq $0x40, %rsp
popq %rbx
popq %r14
popq %rbp
retq
jmp 0x406e0e
movq %rax, %rbx
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x406e1f
callq 0x410b90
movq %rbx, %rdi
callq 0x4bb660
nop