// File: /workspace/github_cpp_makefile_star2_O3/Thilotilo[P]DoaERando/BattleRandomizer.cpp
// Function: DoaERando::BattleRandomizer::RandomizeZone8(std::vector<unsigned char, std::allocator<unsigned char>>&, DoaERando::RandomNumberGenerator&)
void BattleRandomizer::RandomizeZone8(std::vector<BYTE>& generalIds, RNG& rng)
{
    // Note that this assumes Cao Pi & Sima Yi have already been assigned externally.

    // There are special considerations here.
    // 1) There are Chen Jiao (40) and Xu Huang(41) battles that are not triggerable, but
    //    we'll fill them anyway (with duplicates of course)

    // Other than the battles mentioned above, we can now fill the battles like normal.
    vector<int> battlesToFill = {0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
                                 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x4E};

    // Make sure every general goes to a relevant battle
    for (auto& id : generalIds)
    {
        int battleIndex = rng.GetRandomInt(0, battlesToFill.size() - 1);
        int battleId = battlesToFill[battleIndex];
        while (!PlaceGeneralInBattle(id, battleId))
        {
            battleIndex = ((battleIndex + 1) % battlesToFill.size());
            battleId = battlesToFill[battleIndex];
        }
    }

    // Now add the battles we only want duplicates in
    battlesToFill.push_back(0x40);
    battlesToFill.push_back(0x41);

    // Now fill all battles with generals/rebel forces
    FillBattlesRandomly(generalIds, battlesToFill, rng);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
movups 0x17cd80(%rip), %xmm0   # 0x58ca8c
leaq -0xc0(%rbp), %rsi
movaps %xmm0, 0x30(%rsi)
movups 0x17cd5e(%rip), %xmm0   # 0x58ca7c
movaps %xmm0, 0x20(%rsi)
movups 0x17cd43(%rip), %xmm0   # 0x58ca6c
movaps %xmm0, 0x10(%rsi)
movups 0x17cd28(%rip), %xmm0   # 0x58ca5c
movaps %xmm0, (%rsi)
leaq -0x48(%rbp), %rdi
leaq -0x29(%rbp), %rcx
movl $0x10, %edx
callq 0x40ff6a
movq (%r15), %r13
movq %r15, -0x50(%rbp)
movq 0x8(%r15), %r15
cmpq %r15, %r13
je 0x40fdb5
movq -0x40(%rbp), %rdx
subq -0x48(%rbp), %rdx
shrq $0x2, %rdx
decl %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x41086c
movl %eax, %r12d
cltq
movq -0x48(%rbp), %rcx
movl (%rcx,%rax,4), %edx
movzbl (%r13), %esi
movq %r14, %rdi
callq 0x40d678
testb %al, %al
jne 0x40fdb0
incl %r12d
movslq %r12d, %rax
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %rsi
subq %rcx, %rsi
sarq $0x2, %rsi
xorl %edx, %edx
divq %rsi
movq %rdx, %r12
movslq %r12d, %rax
jmp 0x40fd7a
incq %r13
jmp 0x40fd54
movl $0x40, -0xc0(%rbp)
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rax
cmpq %rax, %rsi
je 0x40fddc
movl $0x40, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x40(%rbp)
jmp 0x40fdf4
leaq -0x48(%rbp), %rdi
leaq -0xc0(%rbp), %rdx
callq 0x40d076
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rax
movl $0x41, -0xc0(%rbp)
cmpq %rax, %rsi
je 0x40fe13
movl $0x41, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x40(%rbp)
jmp 0x40fe23
leaq -0x48(%rbp), %rdi
leaq -0xc0(%rbp), %rdx
callq 0x40d076
leaq -0x80(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x40abb0
leaq -0x68(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x40ffa8
leaq -0x80(%rbp), %rsi
leaq -0x68(%rbp), %rdx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x40d6d4
movq -0x68(%rbp), %rdi
testq %rdi, %rdi
je 0x40fe5e
callq 0x410b90
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x40fe6c
callq 0x410b90
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x40fe7a
callq 0x410b90
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x40feb8
jmp 0x40feb8
movq %rax, %rbx
movq -0x68(%rbp), %rdi
testq %rdi, %rdi
je 0x40fea6
callq 0x410b90
jmp 0x40fea6
movq %rax, %rbx
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x40febb
callq 0x410b90
jmp 0x40febb
jmp 0x40feb8
movq %rax, %rbx
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x40fec9
callq 0x410b90
movq %rbx, %rdi
callq 0x4bb660
nop