// File: /workspace/github_cpp_makefile_star2_O2/Thilotilo[P]DoaERando/BattleRandomizer.cpp
// Function: DoaERando::BattleRandomizer::RandomizeZone6(DoaERando::Generals&, DoaERando::RandomNumberGenerator&)
void BattleRandomizer::RandomizeZone6(Generals& generals, RNG& rng)
{
    // Ideally, we want to add a special consideration for the Luo battle here.  We would like
    // it to yield the Ma Chao/Ma Dai equivalents when accepting their offer at the town,  but
    // coupling that logic is beyond our scope at the moment, so we're going to fully randomize everything.
    // Note that there is no reason that Lu Bu (4F) can't trigger the recruitable flag, so no need to
    // relegate it to duplicates

    std::vector<BYTE> generalIds = generals.GetGeneralIdsFromZone(6);

    // All the other battles can be filled like normal.
    vector<int> battlesToFill = {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x4F};
    // 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 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 %rdi, %r14
leaq -0x60(%rbp), %rdi
pushq $0x6
popq %rdx
callq 0x40be82
movups 0x17e907(%rip), %xmm0   # 0x58b9e0
leaq -0xc0(%rbp), %rsi
movaps %xmm0, 0x10(%rsi)
movups 0x17e8e5(%rip), %xmm0   # 0x58b9d0
movaps %xmm0, (%rsi)
movl $0x4f, 0x20(%rsi)
leaq -0x48(%rbp), %rdi
pushq $0x9
popq %rdx
leaq -0x29(%rbp), %rcx
callq 0x40e4cc
movq -0x60(%rbp), %r12
movq -0x58(%rbp), %r13
cmpq %r13, %r12
je 0x40d175
movq -0x40(%rbp), %rdx
subq -0x48(%rbp), %rdx
shrq $0x2, %rdx
decl %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x40ed8e
movl %eax, %r15d
cltq
shlq $0x2, %rax
addq -0x48(%rbp), %rax
movl (%rax), %edx
movzbl (%r12), %esi
movq %r14, %rdi
callq 0x40c588
testb %al, %al
jne 0x40d170
incl %r15d
movslq %r15d, %rax
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %rsi
subq %rcx, %rsi
sarq $0x2, %rsi
xorl %edx, %edx
divq %rsi
movq %rdx, %r15
movslq %r15d, %rax
leaq (%rcx,%rax,4), %rax
jmp 0x40d137
incq %r12
jmp 0x40d10d
leaq -0x90(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x409dc8
leaq -0x78(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x40e502
leaq -0x90(%rbp), %rsi
leaq -0x78(%rbp), %rdx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x40c5e0
leaq -0x78(%rbp), %rdi
callq 0x409e52
leaq -0x90(%rbp), %rdi
callq 0x404baa
leaq -0x48(%rbp), %rdi
callq 0x409e52
leaq -0x60(%rbp), %rdi
callq 0x404baa
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x78(%rbp), %rdi
callq 0x409e52
jmp 0x40d1f2
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x404baa
jmp 0x40d20a
jmp 0x40d207
movq %rax, %rbx
jmp 0x40d213
movq %rax, %rbx
leaq -0x48(%rbp), %rdi
callq 0x409e52
leaq -0x60(%rbp), %rdi
callq 0x404baa
movq %rbx, %rdi
callq 0x4ba600