// File: /workspace/github_cpp_makefile_star2_O2/Thilotilo[P]DoaERando/Randomizer.cpp
// Function: DoaERando::Randomizer::LetWangGuiTravelToYangZhou()
void Randomizer::LetWangGuiTravelToYangZhou()
{
    // Because Yang Zhou doesn't have event code, we can't just do 1 NPC swap, we actually
    // have to do 2 in order to have room for event code
    myRom->WriteByte(0x31922, 0x14); // Text Bank 1 YTile 04
    myRom->WriteByte(0x31923, 0x07); // YZone 07
    myRom->WriteByte(0x31924, 0x19); // Move around, XTile 09
    myRom->WriteByte(0x31925, 0x3E); // Trigger code + XZone 1E
    myRom->WriteByte(0x31926, 0x18); // Set text 1
    myRom->WriteByte(0x31927, 0x18); // Set text 2
    BYTE WangGuiId = myRom->ReadByte(0x31976);
    myRom->WriteByte(0x31928, WangGuiId);
    // Trigger code at 0x97E0
    myRom->WriteByte(0x31929, 0xE0);
    myRom->WriteByte(0x3192A, 0x97);
    // Terminator
    myRom->WriteByte(0x3192B, 0xFF);
    // Remaining zeros from the other NPCs
    for (unsigned int i = 0; i < 0x5; ++i)
    {
        myRom->WriteByte(0x3192C + i, 0x00);
    }

    // Trigger code written at 0x97E0 = 0x357F0
    std::vector<BYTE> eventCode {
        // Check if we've obtained Yang Jin
        0xA9, 0x02,       // LDA #$02
        0x20, 0xA0, 0x9C, // JSR $9CA0
        0xB0, 0x05,       // BCC (RTS loc)
        0xA9, 0x00,       // LDA #$00
        0x8D, 0x06, 0x05, // STA $0506
        0x60,             // RTS
    };

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movq (%rdi), %rdi
pushq $0x14
popq %rdx
movl $0x31922, %esi          # imm = 0x31922
callq 0x404a00
movq (%rbx), %rdi
pushq $0x7
popq %rdx
movl $0x31923, %esi          # imm = 0x31923
callq 0x404a00
movq (%rbx), %rdi
pushq $0x19
popq %rdx
movl $0x31924, %esi          # imm = 0x31924
callq 0x404a00
movq (%rbx), %rdi
pushq $0x3e
popq %rdx
movl $0x31925, %esi          # imm = 0x31925
callq 0x404a00
movq (%rbx), %rdi
pushq $0x18
popq %r14
movl $0x31926, %esi          # imm = 0x31926
movl %r14d, %edx
callq 0x404a00
movq (%rbx), %rdi
movl $0x31927, %esi          # imm = 0x31927
movl %r14d, %edx
callq 0x404a00
movq (%rbx), %rdi
movl $0x31976, %esi          # imm = 0x31976
callq 0x4049d6
movq (%rbx), %rdi
movzbl %al, %edx
movl $0x31928, %esi          # imm = 0x31928
callq 0x404a00
movq (%rbx), %rdi
movl $0x31929, %esi          # imm = 0x31929
movl $0xe0, %edx
callq 0x404a00
movq (%rbx), %rdi
movl $0x3192a, %esi          # imm = 0x3192A
movl $0x97, %edx
callq 0x404a00
movq (%rbx), %rdi
movl $0x3192b, %esi          # imm = 0x3192B
movl $0xff, %edx
callq 0x404a00
xorl %r14d, %r14d
pushq $0x5
popq %r15
subl $0x1, %r15d
jb 0x40732e
movq (%rbx), %rdi
leal 0x3192c(%r14), %esi
xorl %edx, %edx
callq 0x404a00
incl %r14d
jmp 0x407312
movabsq $-0x56fa4f635fdffd57, %rax # imm = 0xA905B09CA02002A9
leaq -0x45(%rbp), %rsi
movq %rax, (%rsi)
movl $0x5068d00, 0x8(%rsi)   # imm = 0x5068D00
movb $0x60, 0xc(%rsi)
leaq -0x38(%rbp), %rdi
pushq $0xd
popq %rdx
leaq -0x19(%rbp), %rcx
callq 0x409c64
movl $0x357f0, %r14d         # imm = 0x357F0
leal -0x357f0(%r14), %eax
movq -0x38(%rbp), %rcx
movq -0x30(%rbp), %rdx
subq %rcx, %rdx
cmpq %rax, %rdx
jbe 0x40738b
movq (%rbx), %rdi
movzbl (%rcx,%rax), %edx
movl %r14d, %esi
callq 0x404a00
incl %r14d
jmp 0x407360
leaq -0x38(%rbp), %rdi
callq 0x404baa
addq $0x38, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x38(%rbp), %rdi
callq 0x404baa
movq %rbx, %rdi
callq 0x4ba600
nop