// File: FreeSlave[P]vhlt/hlvis/vis.cpp
// Function: LeafFlow(int)
static void     LeafFlow(const int leafnum)
{
    leaf_t*         leaf;
    byte*           outbuffer;
    byte            compressed[MAX_MAP_LEAFS / 8];
    unsigned        i;
    unsigned        j;
    int             k;
    int             tmp;
    int             numvis;
    byte*           dest;
    portal_t*       p;

    //
    // flow through all portals, collecting visible bits
    //
    memset(compressed, 0, sizeof(compressed));
    outbuffer = g_uncompressed + leafnum * g_bitbytes;
    leaf = &g_leafs[leafnum];
    tmp = 0;

    const unsigned offset = leafnum >> 3;
    const unsigned bit = (1 << (leafnum & 7));

    for (i = 0; i < leaf->numportals; i++)
    {
        p = leaf->portals[i];
        if (p->status != stat_done)
        {
            Error("portal not done (leaf %d)", leafnum);
        }

        {
            byte* dst = outbuffer;
            byte* src = p->visbits;
            for (j=0; j<g_bitbytes; j++, dst++, src++)
            {
                *dst |= *src;
            }
        }

        if ((tmp == 0) && (outbuffer[offset] & bit))
        {
            tmp = 1;
            Warning("Leaf portals saw into leaf");
            Log("    Problem at portal between leaves %i and %i:\n   ", leafnum, p->leaf);
            for (k = 0; k < p->winding->numpoints; k++)
            {
                Log("    (%4.3f %4.3f %4.3f)\n", p->winding->points[k][0], p->winding->points[k][1], p->winding->points[k][2]);
            }
            Log("\n");
        }
    }

    outbuffer[offset] |= bit;

#ifdef HLVIS_OVERVIEW
	if (g_leafinfos[leafnum].isoverviewpoint)
	{
		for (i = 0; i < g_portalleafs; i++)
		{
			outbuffer[i >> 3] |= (1 << (i & 7));
		}
	}
#ifdef HLVIS_SKYBOXMODEL
	for (i = 0; i < g_portalleafs; i++)
	{
		if (g_leafinfos[i].isskyboxpoint)
		{
			outbuffer[i >> 3] |= (1 << (i & 7));
		}
	}
#endif
#endif
    numvis = 0;
    for (i = 0; i < g_portalleafs; i++)
    {
        if (outbuffer[i >> 3] & (1 << (i & 7)))
        {
            numvis++;
        }
    }

    //
    // compress the bit string
    //
    Verbose("leaf %4i : %4i visible\n", leafnum, numvis);
    totalvis += numvis;

#ifdef ZHLT_DETAILBRUSH
	byte buffer2[MAX_MAP_LEAFS / 8];
	int diskbytes = (g_leafcount_all + 7) >> 3;
	memset (buffer2, 0, diskbytes);
	for (i = 0; i < g_portalleafs; i++)
	{
		for (j = 0; j < g_leafcounts[i]; j++)
		{
			int srcofs = i >> 3;
			int srcbit = 1 << (i & 7);
			int dstofs = (g_leafstarts[i] + j) >> 3;
			int dstbit = 1 << ((g_leafstarts[i] + j) & 7);
			if (outbuffer[srcofs] & srcbit)
			{
				buffer2[dstofs] |= dstbit;
			}
		}
	}
	i = CompressVis (buffer2, diskbytes, compressed, sizeof (compressed));
#else
    i = CompressVis(outbuffer, g_bitbytes, compressed, sizeof(compressed));
#endif

    dest = vismap_p;
    vismap_p += i;

    if (vismap_p > vismap_end)
    {
        Error("Vismap expansion overflow");
    }

#ifdef ZHLT_DETAILBRUSH
	for (j = 0; j < g_leafcounts[leafnum]; j++)
	{
		g_dleafs[g_leafstarts[leafnum] + j + 1].visofs = dest - vismap;
	}
#else
    g_dleafs[leafnum + 1].visofs = dest - vismap;            // leaf 0 is a common solid
#endif

    memcpy(dest, compressed, i);
}

// Assembly:
subq $0x2078, %rsp           # imm = 0x2078
movl %edi, 0x2074(%rsp)
leaq 0x1060(%rsp), %rdi
xorl %esi, %esi
movl $0xfff, %edx            # imm = 0xFFF
callq 0x3120
movq 0x208e217(%rip), %rax   # 0x20a57a0
movl 0x2074(%rsp), %ecx
imull 0x208e211(%rip), %ecx   # 0x20a57a8
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0x2060(%rsp)
movq 0x208e1d5(%rip), %rax   # 0x20a5780
movslq 0x2074(%rsp), %rcx
imulq $0x810, %rcx, %rcx      # imm = 0x810
addq %rcx, %rax
movq %rax, 0x2068(%rsp)
movl $0x0, 0x1050(%rsp)
movl 0x2074(%rsp), %eax
sarl $0x3, %eax
movl %eax, 0x1034(%rsp)
movl 0x2074(%rsp), %ecx
andl $0x7, %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, 0x1030(%rsp)
movl $0x0, 0x105c(%rsp)
movl 0x105c(%rsp), %eax
movq 0x2068(%rsp), %rcx
cmpl (%rcx), %eax
jae 0x1783f
movq 0x2068(%rsp), %rax
movl 0x105c(%rsp), %ecx
movq 0x10(%rax,%rcx,8), %rax
movq %rax, 0x1038(%rsp)
movq 0x1038(%rsp), %rax
cmpl $0x2, 0x20(%rax)
je 0x1765a
movl 0x2074(%rsp), %esi
leaq 0x5c30(%rip), %rdi      # 0x1d283
movb $0x0, %al
callq 0xa380
movq 0x2060(%rsp), %rax
movq %rax, 0x1028(%rsp)
movq 0x1038(%rsp), %rax
movq 0x28(%rax), %rax
movq %rax, 0x1020(%rsp)
movl $0x0, 0x1058(%rsp)
movl 0x1058(%rsp), %eax
cmpl 0x208e112(%rip), %eax   # 0x20a57a8
jae 0x176ed
movq 0x1020(%rsp), %rax
movzbl (%rax), %edx
movq 0x1028(%rsp), %rax
movzbl (%rax), %ecx
orl %edx, %ecx
movb %cl, (%rax)
movl 0x1058(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1058(%rsp)
movq 0x1028(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x1028(%rsp)
movq 0x1020(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x1020(%rsp)
jmp 0x17689
cmpl $0x0, 0x1050(%rsp)
jne 0x17827
movq 0x2060(%rsp), %rax
movl 0x1034(%rsp), %ecx
movzbl (%rax,%rcx), %eax
andl 0x1030(%rsp), %eax
cmpl $0x0, %eax
je 0x17827
movl $0x1, 0x1050(%rsp)
leaq 0x5b6d(%rip), %rdi      # 0x1d29d
movb $0x0, %al
callq 0xa7f0
movl 0x2074(%rsp), %esi
movq 0x1038(%rsp), %rax
movl 0x10(%rax), %edx
leaq 0x5b68(%rip), %rdi      # 0x1d2b8
movb $0x0, %al
callq 0x9ed0
movl $0x0, 0x1054(%rsp)
movl 0x1054(%rsp), %eax
movq 0x1038(%rsp), %rcx
movq 0x18(%rcx), %rcx
cmpl 0x4(%rcx), %eax
jge 0x17819
movq 0x1038(%rsp), %rax
movq 0x18(%rax), %rax
addq $0x8, %rax
movslq 0x1054(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movq 0x1038(%rsp), %rax
movq 0x18(%rax), %rax
addq $0x8, %rax
movslq 0x1054(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm1
cvtss2sd %xmm1, %xmm1
movq 0x1038(%rsp), %rax
movq 0x18(%rax), %rax
addq $0x8, %rax
movslq 0x1054(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm2
cvtss2sd %xmm2, %xmm2
leaq 0x5af0(%rip), %rdi      # 0x1d2ec
movb $0x3, %al
callq 0x9ed0
movl 0x1054(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1054(%rsp)
jmp 0x17762
leaq 0x3508(%rip), %rdi      # 0x1ad28
movb $0x0, %al
callq 0x9ed0
jmp 0x17829
movl 0x105c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x105c(%rsp)
jmp 0x17604
movl 0x1030(%rsp), %esi
movq 0x2060(%rsp), %rax
movl 0x1034(%rsp), %ecx
movzbl (%rax,%rcx), %edx
orl %esi, %edx
movb %dl, (%rax,%rcx)
movq 0x20ddf63(%rip), %rax   # 0x20f57c8
movslq 0x2074(%rsp), %rcx
testb $0x1, (%rax,%rcx,2)
je 0x178d0
movl $0x0, 0x105c(%rsp)
movl 0x105c(%rsp), %eax
cmpl 0x208dee9(%rip), %eax   # 0x20a5774
jae 0x178ce
movl 0x105c(%rsp), %ecx
andl $0x7, %ecx
movl $0x1, %esi
shll %cl, %esi
movq 0x2060(%rsp), %rax
movl 0x105c(%rsp), %ecx
shrl $0x3, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %edx
orl %esi, %edx
movb %dl, (%rax,%rcx)
movl 0x105c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x105c(%rsp)
jmp 0x1787e
jmp 0x178d0
movl $0x0, 0x105c(%rsp)
movl 0x105c(%rsp), %eax
cmpl 0x208de8c(%rip), %eax   # 0x20a5774
jae 0x17942
movq 0x20dded7(%rip), %rax   # 0x20f57c8
movl 0x105c(%rsp), %ecx
testb $0x1, 0x1(%rax,%rcx,2)
je 0x1792d
movl 0x105c(%rsp), %ecx
andl $0x7, %ecx
movl $0x1, %esi
shll %cl, %esi
movq 0x2060(%rsp), %rax
movl 0x105c(%rsp), %ecx
shrl $0x3, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %edx
orl %esi, %edx
movb %dl, (%rax,%rcx)
jmp 0x1792f
movl 0x105c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x105c(%rsp)
jmp 0x178db
movl $0x0, 0x104c(%rsp)
movl $0x0, 0x105c(%rsp)
movl 0x105c(%rsp), %eax
cmpl 0x208de0f(%rip), %eax   # 0x20a5774
jae 0x179bf
movq 0x2060(%rsp), %rax
movl 0x105c(%rsp), %ecx
shrl $0x3, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
movl 0x105c(%rsp), %ecx
andl $0x7, %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x179aa
movl 0x104c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x104c(%rsp)
jmp 0x179ac
movl 0x105c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x105c(%rsp)
jmp 0x17958
movl 0x2074(%rsp), %esi
movl 0x104c(%rsp), %edx
leaq 0x5931(%rip), %rdi      # 0x1d305
movb $0x0, %al
callq 0xa900
movl 0x104c(%rsp), %eax
addl 0x20dde08(%rip), %eax   # 0x20f57f0
movl %eax, 0x20dde02(%rip)   # 0x20f57f0
movl 0x208dda4(%rip), %eax   # 0x20a5798
addl $0x7, %eax
sarl $0x3, %eax
movl %eax, 0x1c(%rsp)
leaq 0x20(%rsp), %rdi
movslq 0x1c(%rsp), %rdx
xorl %esi, %esi
callq 0x3120
movl $0x0, 0x105c(%rsp)
movl 0x105c(%rsp), %eax
cmpl 0x208dd4d(%rip), %eax   # 0x20a5774
jae 0x17b1c
movl $0x0, 0x1058(%rsp)
movl 0x1058(%rsp), %eax
movq 0x208dd4a(%rip), %rcx   # 0x20a5790
movl 0x105c(%rsp), %edx
cmpl (%rcx,%rdx,4), %eax
jae 0x17b04
movl 0x105c(%rsp), %eax
shrl $0x3, %eax
movl %eax, 0x18(%rsp)
movl 0x105c(%rsp), %ecx
andl $0x7, %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, 0x14(%rsp)
movq 0x208dd08(%rip), %rax   # 0x20a5788
movl 0x105c(%rsp), %ecx
movl (%rax,%rcx,4), %eax
addl 0x1058(%rsp), %eax
shrl $0x3, %eax
movl %eax, 0x10(%rsp)
movq 0x208dce9(%rip), %rax   # 0x20a5788
movl 0x105c(%rsp), %ecx
movl (%rax,%rcx,4), %ecx
addl 0x1058(%rsp), %ecx
andl $0x7, %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, 0xc(%rsp)
movq 0x2060(%rsp), %rax
movslq 0x18(%rsp), %rcx
movzbl (%rax,%rcx), %eax
andl 0x14(%rsp), %eax
cmpl $0x0, %eax
je 0x17aec
movl 0xc(%rsp), %edx
movslq 0x10(%rsp), %rax
movzbl 0x20(%rsp,%rax), %ecx
orl %edx, %ecx
movb %cl, 0x20(%rsp,%rax)
jmp 0x17aee
movl 0x1058(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1058(%rsp)
jmp 0x17a38
jmp 0x17b06
movl 0x105c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x105c(%rsp)
jmp 0x17a1a
leaq 0x20(%rsp), %rdi
movl 0x1c(%rsp), %esi
leaq 0x1060(%rsp), %rdx
movl $0xfff, %ecx            # imm = 0xFFF
callq 0x3630
movl %eax, 0x105c(%rsp)
movq 0x20ddc8b(%rip), %rax   # 0x20f57d0
movq %rax, 0x1040(%rsp)
movl 0x105c(%rsp), %ecx
movq 0x20ddc75(%rip), %rax   # 0x20f57d0
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0x20ddc69(%rip)   # 0x20f57d0
movq 0x20ddc62(%rip), %rax   # 0x20f57d0
cmpq 0x20ddc73(%rip), %rax   # 0x20f57e8
jbe 0x17b85
leaq 0x579f(%rip), %rdi      # 0x1d31d
movb $0x0, %al
callq 0xa380
movl $0x0, 0x1058(%rsp)
movl 0x1058(%rsp), %eax
movq 0x208dbf2(%rip), %rcx   # 0x20a5790
movslq 0x2074(%rsp), %rdx
cmpl (%rcx,%rdx,4), %eax
jae 0x17c03
movq 0x1040(%rsp), %rax
movq 0x20ddc26(%rip), %rcx   # 0x20f57e0
subq %rcx, %rax
movl %eax, %ecx
movq 0x208dbc2(%rip), %rax   # 0x20a5788
movslq 0x2074(%rsp), %rdx
movl (%rax,%rdx,4), %eax
addl 0x1058(%rsp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edx
leaq 0xa1272a(%rip), %rax    # 0xa2a310
imulq $0x1c, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movl 0x1058(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1058(%rsp)
jmp 0x17b90
movq 0x1040(%rsp), %rdi
leaq 0x1060(%rsp), %rsi
movl 0x105c(%rsp), %eax
movl %eax, %edx
callq 0x31b0
addq $0x2078, %rsp           # imm = 0x2078
retq
nopl (%rax)