// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2028, %rsp           # imm = 0x2028
movl %edi, %r13d
leaq 0x1020(%rsp), %rdi
movl $0xfff, %edx            # imm = 0xFFF
xorl %esi, %esi
callq 0x3120
movl 0x208f0d1(%rip), %r14d  # 0x209d7a8
imull %r13d, %r14d
addq 0x208f0be(%rip), %r14   # 0x209d7a0
movslq %r13d, %rax
imulq $0x810, %rax, %rsi      # imm = 0x810
addq 0x208f08d(%rip), %rsi   # 0x209d780
movl %eax, %ecx
sarl $0x3, %ecx
movq %rcx, 0x8(%rsp)
movq %rax, 0x18(%rsp)
movl %eax, %ecx
andb $0x7, %cl
pushq $0x1
popq %rax
shll %cl, %eax
movl %eax, (%rsp)
leaq 0x597b(%rip), %r12      # 0x14091
xorl %ebp, %ebp
xorl %r15d, %r15d
movq %rsi, 0x10(%rsp)
movl %r13d, 0x4(%rsp)
movl (%rsi), %eax
cmpq %rax, %rbp
jae 0xe80d
movq 0x10(%rsi,%rbp,8), %rbx
cmpl $0x2, 0x20(%rbx)
je 0xe751
leaq 0x58e6(%rip), %rdi      # 0x14028
movl %r13d, %esi
xorl %eax, %eax
callq 0x795f
movq 0x10(%rsp), %rsi
movq 0x28(%rbx), %rax
xorl %ecx, %ecx
cmpl 0x208f04b(%rip), %ecx   # 0x209d7a8
jae 0xe76b
movb (%rax,%rcx), %dl
orb %dl, (%r14,%rcx)
incq %rcx
jmp 0xe757
testl %r15d, %r15d
pushq $0x1
popq %r15
jne 0xe805
movq 0x8(%rsp), %rax
movzbl (%r14,%rax), %eax
testl %eax, (%rsp)
je 0xe802
leaq 0x58b4(%rip), %rdi      # 0x14042
xorl %eax, %eax
callq 0x7cd5
movl 0x10(%rbx), %edx
leaq 0x58be(%rip), %rdi      # 0x1405d
movl %r13d, %esi
xorl %eax, %eax
callq 0x75c0
pushq $0x10
popq %r15
xorl %r13d, %r13d
movq 0x18(%rbx), %rax
movslq 0x4(%rax), %rcx
cmpq %rcx, %r13
jge 0xe7e4
cvtss2sd -0x8(%rax,%r15), %xmm0
cvtss2sd -0x4(%rax,%r15), %xmm1
cvtss2sd (%rax,%r15), %xmm2
movq %r12, %rdi
movb $0x3, %al
callq 0x75c0
incq %r13
addq $0xc, %r15
jmp 0xe7b0
leaq 0x341d(%rip), %rdi      # 0x11c08
xorl %eax, %eax
callq 0x75c0
pushq $0x1
popq %r15
movl 0x4(%rsp), %r13d
movq 0x10(%rsp), %rsi
jmp 0xe805
xorl %r15d, %r15d
incq %rbp
jmp 0xe725
movq 0x8(%rsp), %rax
movl (%rsp), %ecx
orb %cl, (%r14,%rax)
movq 0x20defa8(%rip), %rax   # 0x20ed7c8
movq 0x18(%rsp), %r15
cmpb $0x1, (%rax,%r15,2)
jne 0xe851
xorl %edx, %edx
pushq $0x1
popq %rsi
movl 0x208ef3d(%rip), %eax   # 0x209d774
cmpl %eax, %edx
jae 0xe857
movl %edx, %ecx
andb $0x7, %cl
movl %esi, %eax
shll %cl, %eax
movl %edx, %ecx
shrl $0x3, %ecx
orb %al, (%r14,%rcx)
incl %edx
jmp 0xe831
movl 0x208ef1d(%rip), %eax   # 0x209d774
xorl %edx, %edx
movl %eax, %ecx
cmpq %rcx, %rdx
jae 0xe88d
movq 0x20def61(%rip), %rcx   # 0x20ed7c8
cmpb $0x1, 0x1(%rcx,%rdx,2)
jne 0xe888
movl %edx, %ecx
andb $0x7, %cl
movb $0x1, %al
shlb %cl, %al
movq %rdx, %rcx
shrq $0x3, %rcx
orb %al, (%r14,%rcx)
movl 0x208eeec(%rip), %eax   # 0x209d774
incq %rdx
jmp 0xe859
xorl %ebp, %ebp
xorl %ecx, %ecx
cmpl %ecx, %eax
je 0xe8ae
movl %ecx, %edx
shrl $0x3, %edx
movzbl (%r14,%rdx), %edx
movl %ecx, %esi
andl $0x7, %esi
btl %esi, %edx
adcl $0x0, %ebp
incl %ecx
jmp 0xe891
leaq 0x57f5(%rip), %rdi      # 0x140aa
movl %r13d, %esi
xorl %r13d, %r13d
movl %ebp, %edx
xorl %eax, %eax
callq 0x7db6
addl %ebp, 0x20def1e(%rip)   # 0x20ed7e8
movl 0x208eec8(%rip), %ebx   # 0x209d798
addl $0x7, %ebx
sarl $0x3, %ebx
movslq %ebx, %rdx
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x3120
movl 0x208ee89(%rip), %eax   # 0x209d774
movq 0x208ee9e(%rip), %rdx   # 0x209d790
movq 0x208ee8f(%rip), %rsi   # 0x209d788
pushq $0x1
popq %r12
cmpq %rax, %r13
je 0xe94e
movl %r13d, %ecx
andb $0x7, %cl
movl %r12d, %edi
shll %cl, %edi
movl %r13d, %r8d
shrl $0x3, %r8d
movl (%rdx,%r13,4), %r9d
xorl %r10d, %r10d
cmpl %r9d, %r10d
jae 0xe949
movzbl (%r14,%r8), %ecx
testl %ecx, %edi
je 0xe944
movl (%rsi,%r13,4), %r11d
addl %r10d, %r11d
movl %r11d, %ecx
andb $0x7, %cl
movl %r12d, %ebp
shll %cl, %ebp
shrl $0x3, %r11d
orb %bpl, 0x20(%rsp,%r11)
incl %r10d
jmp 0xe91b
incq %r13
jmp 0xe8fd
leaq 0x20(%rsp), %rdi
leaq 0x1020(%rsp), %rdx
movl %ebx, %esi
movl $0xfff, %ecx            # imm = 0xFFF
callq 0x3604
movq 0x20dee62(%rip), %r14   # 0x20ed7d0
movl %eax, %ebx
leaq (%r14,%rbx), %rax
movq %rax, 0x20dee55(%rip)   # 0x20ed7d0
cmpq 0x20dee5e(%rip), %rax   # 0x20ed7e0
jbe 0xe992
leaq 0x5737(%rip), %rdi      # 0x140c2
xorl %eax, %eax
callq 0x795f
movq 0x208edf7(%rip), %rax   # 0x209d790
movl 0x20dee39(%rip), %edx   # 0x20ed7d8
movl %r14d, %ecx
subl %edx, %ecx
movq 0x208eddd(%rip), %rdx   # 0x209d788
leaq 0xa1396e(%rip), %rsi    # 0xa22320
leal -0x1(%r12), %edi
cmpl (%rax,%r15,4), %edi
jae 0xe9d1
movl (%rdx,%r15,4), %edi
addl %r12d, %edi
imulq $0x1c, %rdi, %rdi
movl %ecx, 0x4(%rsi,%rdi)
incl %r12d
jmp 0xe9b2
leaq 0x1020(%rsp), %rsi
movq %r14, %rdi
movq %rbx, %rdx
callq 0x31a0
addq $0x2028, %rsp           # imm = 0x2028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq