// File: /workspace/github_cpp_makefile_star2_O2/FreeSlave[P]vhlt/hlvis/zones.cpp
// Function: MakeZones()
Zones* MakeZones(void)
{
    UINT32 x;
    UINT32 func_vis_count = 0;

    ParseEntities();

    // Note: we arent looping through entities because we only care if it has a winding/bounding box

    // First count the number of func_vis's
    for (x=0; x<g_nummodels; x++)
    {
        entity_t*       ent = EntityForModel(x);

        if (!strcasecmp(ValueForKey(ent, "classname"), "func_vis"))
        {
            UINT32 value = atoi(ValueForKey(ent, "node"));
            if (value)
            {
                func_vis_count++;
            }
            else
            {
                Error("func_vis with no \"node\" id\n");
            }
        }
    }

    if (!func_vis_count)
    {
        return NULL;
    }

    Zones* zones = new Zones(func_vis_count);

    for (x=0; x<g_nummodels; x++)
    {
        dmodel_t*       mod = g_dmodels + x;
        entity_t*       ent = EntityForModel(x);

        if (!strcasecmp(ValueForKey(ent, "classname"), "func_vis"))
        {
            UINT32 func_vis_id = atoi(ValueForKey(ent, "node"));

            {
                epair_t* keyvalue;
    
                for (keyvalue = ent->epairs; keyvalue; keyvalue = keyvalue->next)
                {
                    UINT32 other_id = atoi(keyvalue->key);
                    if (other_id)
                    {
                        zones->flag(func_vis_id, other_id);
                    }
                }
            }
    
            {
                UINT32          j;
                BoundingBox     bounds;
                dface_t*        f = g_dfaces + mod->firstface;
            
                for (j = 0; j < mod->numfaces; j++, f++)
                {
                    Winding*        w = WindingFromFace(f);
                    UINT32          k;

                    for (k = 0; k < w->m_NumPoints; k++)
                    {
                        bounds.add(w->m_Points[k]);
                    }
                    delete w;
                }

                zones->set(func_vis_id, bounds);

                Log("Adding zone %u : mins(%4.3f %4.3f %4.3f) maxs(%4.3f %4.3f %4.3f)\n", func_vis_id, 
                    bounds.m_Mins[0],bounds.m_Mins[1],bounds.m_Mins[2],
                    bounds.m_Maxs[0],bounds.m_Maxs[1],bounds.m_Maxs[2]);
            }
        }
    }

    return zones;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
callq 0x589c
leaq 0x19c3(%rip), %rbx      # 0x10570
leaq 0x5528(%rip), %r14      # 0x140dc
leaq 0x552a(%rip), %r15      # 0x140e5
xorl %ebp, %ebp
xorl %r13d, %r13d
leaq 0xb6f9(%rip), %rax      # 0x1a2c0
cmpl (%rax), %r13d
jae 0xec1f
movl %r13d, %edi
callq 0x60b8
movq %rax, %r12
movq %rax, %rdi
movq %rbx, %rsi
callq 0x5759
movq %rax, %rdi
movq %r14, %rsi
callq 0x3260
testl %eax, %eax
jne 0xec1a
movq %r12, %rdi
movq %r15, %rsi
callq 0x5759
movq %rax, %rdi
callq 0x33d0
testl %eax, %eax
je 0xec0c
incl %ebp
jmp 0xec1a
leaq 0x54d7(%rip), %rdi      # 0x140ea
xorl %eax, %eax
callq 0x795f
incl %r13d
jmp 0xebc0
testl %ebp, %ebp
je 0xee91
pushq $0x28
popq %rdi
callq 0x3230
movq %rax, 0x8(%rsp)
movq %rax, %rdi
movl %ebp, %esi
callq 0xeecc
xorl %ecx, %ecx
leaq 0x1e0d639(%rip), %r14   # 0x1e1c280
leaq 0x1cf3612(%rip), %rbp   # 0x1d02260
leaq 0xff360b(%rip), %rbx    # 0x1002260
leaq 0xb664(%rip), %rax      # 0x1a2c0
movl (%rax), %eax
cmpq %rax, %rcx
jae 0xee8a
movq %rcx, 0x10(%rsp)
movq 0x10(%rsp), %rdi
callq 0x60b8
movq %rax, %r12
movq %rax, %rdi
leaq 0x18ed(%rip), %rsi      # 0x10570
callq 0x5759
movq %rax, %rdi
leaq 0x544a(%rip), %rsi      # 0x140dc
callq 0x3260
movq 0x10(%rsp), %rcx
testl %eax, %eax
jne 0xee82
shlq $0x6, %rcx
addq 0xa321(%rip), %rcx      # 0x18fd0
movq %rcx, 0x40(%rsp)
movq %r12, %rdi
leaq 0x5427(%rip), %rsi      # 0x140e5
callq 0x5759
movq %rax, %rdi
callq 0x33d0
movl %eax, 0x4(%rsp)
addq $0x18, %r12
movq (%r12), %r12
testq %r12, %r12
je 0xecfc
movq 0x8(%r12), %rdi
callq 0x33d0
testl %eax, %eax
je 0xecd3
movq 0x8(%rsp), %rdi
movl 0x4(%rsp), %esi
movl %eax, %edx
callq 0xef8c
jmp 0xecd3
movaps 0x44ed(%rip), %xmm0     # 0x131f0
movaps %xmm0, 0x20(%rsp)
movsd 0x44f0(%rip), %xmm0     # 0x13200
movsd %xmm0, 0x30(%rsp)
movq 0x40(%rsp), %rax
movslq 0x38(%rax), %rax
imulq $0x14, %rax, %r15
addq 0xa2be(%rip), %r15      # 0x18fe8
xorl %r13d, %r13d
movq 0x40(%rsp), %rax
cmpl 0x3c(%rax), %r13d
jae 0xee15
pushq $0x20
popq %rdi
callq 0x3230
movq %rax, %r12
movswl 0x8(%r15), %esi
movq %rax, %rdi
callq 0x98a4
xorl %eax, %eax
xorl %ecx, %ecx
movswq 0x8(%r15), %rdx
cmpq %rdx, %rcx
jge 0xedc3
movslq 0x4(%r15), %rdx
addq %rcx, %rdx
movslq (%r14,%rdx,4), %rdx
movl %edx, %esi
negl %esi
testq %rdx, %rdx
leaq 0x2(%rbp,%rsi,4), %rsi
leaq (%rbp,%rdx,4), %rdx
cmovsq %rsi, %rdx
movzwl (%rdx), %edx
imulq $0xc, %rdx, %rdx
movss (%rbx,%rdx), %xmm0
movq 0x10(%r12), %rsi
movss %xmm0, (%rsi,%rax)
movss 0x4(%rbx,%rdx), %xmm0
movq 0x10(%r12), %rsi
movss %xmm0, 0x4(%rsi,%rax)
movss 0x8(%rbx,%rdx), %xmm0
movq 0x10(%r12), %rdx
movss %xmm0, 0x8(%rdx,%rax)
incq %rcx
addq $0xc, %rax
jmp 0xed58
movl %r13d, 0x1c(%rsp)
xorl %r14d, %r14d
xorl %r13d, %r13d
movl 0x8(%r12), %eax
cmpq %rax, %r13
jae 0xedf3
movq 0x10(%r12), %rsi
addq %r14, %rsi
leaq 0x20(%rsp), %rdi
callq 0xaeca
incq %r13
addq $0xc, %r14
jmp 0xedce
movq (%r12), %rax
movq %r12, %rdi
callq *0x8(%rax)
movl 0x1c(%rsp), %r13d
incl %r13d
addq $0x14, %r15
leaq 0x1e0d470(%rip), %r14   # 0x1e1c280
jmp 0xed2d
movq 0x8(%rsp), %rax
movl 0x4(%rsp), %esi
cmpl %esi, 0x8(%rax)
jbe 0xee4b
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rax
movl 0x4(%rsp), %ecx
imulq $0x18, %rcx, %rcx
movq 0x30(%rsp), %rdx
movq %rdx, 0x10(%rax,%rcx)
movaps 0x20(%rsp), %xmm0
movups %xmm0, (%rax,%rcx)
movl 0x4(%rsp), %esi
cvtss2sd 0x20(%rsp), %xmm0
cvtss2sd 0x24(%rsp), %xmm1
cvtss2sd 0x28(%rsp), %xmm2
cvtss2sd 0x2c(%rsp), %xmm3
cvtss2sd 0x30(%rsp), %xmm4
cvtss2sd 0x34(%rsp), %xmm5
leaq 0x5290(%rip), %rdi      # 0x14106
movb $0x6, %al
callq 0x75c0
movq 0x10(%rsp), %rcx
incq %rcx
jmp 0xec55
movq 0x8(%rsp), %rax
jmp 0xee93
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
pushq $0x28
popq %rsi
movq 0x8(%rsp), %rdi
jmp 0xeeba
jmp 0xeec1
movq %rax, %r14
pushq $0x20
popq %rsi
movq %r12, %rdi
callq 0x3240
jmp 0xeec4
movq %rax, %r14
movq %r14, %rdi
callq 0x3420