// File: /workspace/github_cpp_makefile_star2_O3/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 %r14
pushq %rbx
subq $0x30, %rsp
movq %rdi, %rbx
movq (%rdi), %rdi
movl $0x31922, %esi          # imm = 0x31922
movl $0x14, %edx
callq 0x404a24
movq (%rbx), %rdi
movl $0x31923, %esi          # imm = 0x31923
movl $0x7, %edx
callq 0x404a24
movq (%rbx), %rdi
movl $0x31924, %esi          # imm = 0x31924
movl $0x19, %edx
callq 0x404a24
movq (%rbx), %rdi
movl $0x31925, %esi          # imm = 0x31925
movl $0x3e, %edx
callq 0x404a24
movq (%rbx), %rdi
movl $0x31926, %esi          # imm = 0x31926
movl $0x18, %edx
callq 0x404a24
movq (%rbx), %rdi
movl $0x31927, %esi          # imm = 0x31927
movl $0x18, %edx
callq 0x404a24
movq (%rbx), %rdi
movl $0x31976, %esi          # imm = 0x31976
callq 0x4049fa
movq (%rbx), %rdi
movzbl %al, %edx
movl $0x31928, %esi          # imm = 0x31928
callq 0x404a24
movq (%rbx), %rdi
movl $0x31929, %esi          # imm = 0x31929
movl $0xe0, %edx
callq 0x404a24
movq (%rbx), %rdi
movl $0x3192a, %esi          # imm = 0x3192A
movl $0x97, %edx
callq 0x404a24
movq (%rbx), %rdi
movl $0x3192b, %esi          # imm = 0x3192B
movl $0xff, %edx
callq 0x404a24
movl $0x3192c, %r14d         # imm = 0x3192C
movq (%rbx), %rdi
movl %r14d, %esi
xorl %edx, %edx
callq 0x404a24
incl %r14d
cmpl $0x31931, %r14d         # imm = 0x31931
jne 0x40770d
movabsq $-0x56fa4f635fdffd57, %rax # imm = 0xA905B09CA02002A9
leaq -0x3d(%rbp), %rsi
movq %rax, (%rsi)
movl $0x5068d00, 0x8(%rsi)   # imm = 0x5068D00
movb $0x60, 0xc(%rsi)
leaq -0x30(%rbp), %r14
leaq -0x11(%rbp), %rcx
movl $0xd, %edx
movq %r14, %rdi
callq 0x40a9ca
movq (%r14), %rdi
cmpq %rdi, 0x8(%r14)
je 0x407794
movl $0x357f0, %r14d         # imm = 0x357F0
xorl %ecx, %ecx
movq (%rbx), %rax
movzbl (%rdi,%rcx), %edx
movq %rax, %rdi
movl %r14d, %esi
callq 0x404a24
leal -0x357ef(%r14), %ecx
movq -0x30(%rbp), %rdi
movq -0x28(%rbp), %rax
subq %rdi, %rax
incl %r14d
cmpq %rcx, %rax
ja 0x407768
testq %rdi, %rdi
je 0x40779e
callq 0x410b90
addq $0x30, %rsp
popq %rbx
popq %r14
popq %rbp
retq
movq %rax, %rbx
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x4077b8
callq 0x410b90
movq %rbx, %rdi
callq 0x4bb660