// File: /workspace/github_cpp_makefile_star2_O3/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, %r12d
leaq 0x1020(%rsp), %rdi
movl $0xfff, %edx            # imm = 0xFFF
xorl %esi, %esi
callq 0x3120
movl 0x208f0b7(%rip), %r14d  # 0x209f798
imull %r12d, %r14d
movq 0x208f084(%rip), %rdi   # 0x209f770
movslq %r12d, %r15
imulq $0x810, %r15, %rax      # imm = 0x810
movl %r15d, %edx
sarl $0x3, %edx
movl %r15d, %ecx
andb $0x7, %cl
movl $0x1, %esi
shll %cl, %esi
movl %esi, (%rsp)
movl %edx, %ecx
movq %rcx, 0x8(%rsp)
addq 0x208f076(%rip), %r14   # 0x209f790
cmpl $0x0, (%rdi,%rax)
je 0x1084e
movq %r15, 0x18(%rsp)
addq %rax, %rdi
leaq 0x5a52(%rip), %rbp      # 0x16185
xorl %ebx, %ebx
xorl %r15d, %r15d
movq %rdi, 0x10(%rsp)
movl %r12d, 0x4(%rsp)
movq 0x10(%rdi,%rbx,8), %r13
cmpl $0x2, 0x20(%r13)
je 0x10764
leaq 0x59c7(%rip), %rdi      # 0x1611c
movl %r12d, %esi
xorl %eax, %eax
callq 0x8cb4
movq 0x10(%rsp), %rdi
cmpl $0x0, 0x208f02d(%rip)   # 0x209f798
je 0x10785
movq 0x28(%r13), %rax
xorl %ecx, %ecx
movb (%rax,%rcx), %dl
orb %dl, (%r14,%rcx)
incq %rcx
cmpl 0x208f015(%rip), %ecx   # 0x209f798
jb 0x10773
movl $0x1, %ecx
testl %r15d, %r15d
jne 0x10838
movq 0x8(%rsp), %rax
movzbl (%r14,%rax), %eax
testl %eax, (%rsp)
je 0x10836
leaq 0x5989(%rip), %rdi      # 0x16136
xorl %eax, %eax
callq 0x9026
movl 0x10(%r13), %edx
leaq 0x5992(%rip), %rdi      # 0x16151
movl %r12d, %esi
xorl %eax, %eax
callq 0x89c6
movq 0x18(%r13), %rax
cmpl $0x0, 0x4(%rax)
jle 0x10817
movl $0x10, %r15d
xorl %r12d, %r12d
xorps %xmm0, %xmm0
cvtss2sd -0x8(%rax,%r15), %xmm0
xorps %xmm1, %xmm1
cvtss2sd -0x4(%rax,%r15), %xmm1
xorps %xmm2, %xmm2
cvtss2sd (%rax,%r15), %xmm2
movq %rbp, %rdi
movb $0x3, %al
callq 0x89c6
incq %r12
movq 0x18(%r13), %rax
movslq 0x4(%rax), %rcx
addq $0xc, %r15
cmpq %rcx, %r12
jl 0x107dc
leaq 0x347a(%rip), %rdi      # 0x13c98
xorl %eax, %eax
callq 0x89c6
movl 0x4(%rsp), %r12d
movq 0x10(%rsp), %rdi
movl $0x1, %ecx
jmp 0x10838
xorl %ecx, %ecx
incq %rbx
movl (%rdi), %eax
movl %ecx, %r15d
cmpq %rax, %rbx
jb 0x10742
movq 0x18(%rsp), %r15
movl (%rsp), %eax
movq 0x8(%rsp), %rcx
orb %al, (%r14,%rcx)
movq 0x20def57(%rip), %rax   # 0x20ef7b8
movl 0x208eefd(%rip), %ecx   # 0x209f764
testl %ecx, %ecx
setne %dl
andb (%rax,%r15,2), %dl
cmpb $0x1, %dl
jne 0x10898
xorl %eax, %eax
movl %eax, %ecx
andb $0x7, %cl
movl $0x1, %edx
shll %cl, %edx
movl %eax, %ecx
shrl $0x3, %ecx
orb %dl, (%r14,%rcx)
incl %eax
movl 0x208eed0(%rip), %ecx   # 0x209f764
cmpl %ecx, %eax
jb 0x10877
testl %ecx, %ecx
je 0x10a99
movq 0x20def11(%rip), %rdx   # 0x20ef7b8
xorl %eax, %eax
cmpb $0x1, 0x1(%rdx,%rax,2)
jne 0x108d1
movl %eax, %ecx
andb $0x7, %cl
movb $0x1, %dl
shlb %cl, %dl
movq %rax, %rcx
shrq $0x3, %rcx
orb %dl, (%r14,%rcx)
movq 0x20deeed(%rip), %rdx   # 0x20ef7b8
movl 0x208ee93(%rip), %ecx   # 0x209f764
incq %rax
movl %ecx, %esi
cmpq %rsi, %rax
jb 0x108a9
testl %ecx, %ecx
je 0x10a99
leal 0x3(%rcx), %eax
andl $-0x4, %eax
decl %ecx
movd %ecx, %xmm0
pshufd $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
xorps %xmm1, %xmm1
movdqa 0x4cb1(%rip), %xmm2     # 0x155b0
xorl %ecx, %ecx
movdqa 0x1777(%rip), %xmm3     # 0x12080
pxor %xmm3, %xmm0
pcmpeqd %xmm4, %xmm4
movdqa 0x4ca7(%rip), %xmm5     # 0x155c0
movaps 0x4caf(%rip), %xmm8     # 0x155d0
movdqa 0x4cb6(%rip), %xmm9     # 0x155e0
pxor %xmm11, %xmm11
movdqa %xmm11, %xmm7
movdqa %xmm2, %xmm6
pxor %xmm3, %xmm6
pcmpgtd %xmm0, %xmm6
movd %xmm6, %edx
notl %edx
testb $0x1, %dl
je 0x1095a
movl %ecx, %edx
shrl $0x3, %edx
movzbl (%r14,%rdx), %edx
movd %edx, %xmm10
movdqa %xmm6, %xmm11
pxor %xmm4, %xmm11
pextrw $0x2, %xmm11, %edx
testb $0x1, %dl
je 0x1099e
movl %ecx, %edx
shrl $0x3, %edx
movzbl (%r14,%rdx), %edx
shll $0x8, %edx
movd %xmm10, %esi
movzbl %sil, %edi
orl %edx, %edi
movl %esi, %edx
andl $0xff0000, %edx         # imm = 0xFF0000
orl %edi, %edx
andl $0xff000000, %esi       # imm = 0xFF000000
orl %edx, %esi
movd %esi, %xmm10
pextrw $0x4, %xmm11, %edx
testb $0x1, %dl
je 0x109cd
movl %ecx, %edx
shrl $0x3, %edx
movzbl (%r14,%rdx), %edx
shll $0x10, %edx
movd %xmm10, %esi
movzwl %si, %edi
orl %edx, %edi
andl $0xff000000, %esi       # imm = 0xFF000000
orl %edi, %esi
movd %esi, %xmm10
pextrw $0x6, %xmm11, %edx
testb $0x1, %dl
je 0x109f7
movl %ecx, %edx
shrl $0x3, %edx
movzbl (%r14,%rdx), %edx
shll $0x18, %edx
movd %xmm10, %esi
andl $0xffffff, %esi         # imm = 0xFFFFFF
orl %edx, %esi
movd %esi, %xmm10
punpcklbw %xmm1, %xmm10   # xmm10 = xmm10[0],xmm1[0],xmm10[1],xmm1[1],xmm10[2],xmm1[2],xmm10[3],xmm1[3],xmm10[4],xmm1[4],xmm10[5],xmm1[5],xmm10[6],xmm1[6],xmm10[7],xmm1[7]
punpcklwd %xmm1, %xmm10   # xmm10 = xmm10[0],xmm1[0],xmm10[1],xmm1[1],xmm10[2],xmm1[2],xmm10[3],xmm1[3]
movdqa %xmm2, %xmm12
pand %xmm5, %xmm12
pshuflw $0xfe, %xmm12, %xmm11   # xmm11 = xmm12[2,3,3,3,4,5,6,7]
movdqa %xmm10, %xmm13
psrld %xmm11, %xmm13
pshuflw $0x54, %xmm12, %xmm14   # xmm14 = xmm12[0,1,1,1,4,5,6,7]
movdqa %xmm10, %xmm11
psrld %xmm14, %xmm11
pshufd $0xee, %xmm12, %xmm12   # xmm12 = xmm12[2,3,2,3]
pshuflw $0xfe, %xmm12, %xmm14   # xmm14 = xmm12[2,3,3,3,4,5,6,7]
movdqa %xmm10, %xmm15
psrld %xmm14, %xmm15
pshuflw $0x54, %xmm12, %xmm12   # xmm12 = xmm12[0,1,1,1,4,5,6,7]
psrld %xmm12, %xmm10
punpcklqdq %xmm13, %xmm11  # xmm11 = xmm11[0],xmm13[0]
punpckhqdq %xmm15, %xmm10  # xmm10 = xmm10[1],xmm15[1]
shufps $0xcc, %xmm10, %xmm11   # xmm11 = xmm11[0,3],xmm10[0,3]
andps %xmm8, %xmm11
paddd %xmm7, %xmm11
addl $0x4, %ecx
paddd %xmm9, %xmm2
cmpl %ecx, %eax
jne 0x1092f
pand %xmm6, %xmm7
pandn %xmm11, %xmm6
por %xmm7, %xmm6
pshufd $0xee, %xmm6, %xmm0     # xmm0 = xmm6[2,3,2,3]
paddd %xmm6, %xmm0
pshufd $0x55, %xmm0, %xmm1     # xmm1 = xmm0[1,1,1,1]
paddd %xmm0, %xmm1
movd %xmm1, %ebp
jmp 0x10a9b
xorl %ebp, %ebp
leaq 0x56fc(%rip), %rdi      # 0x1619e
movl %r12d, %esi
movl %ebp, %edx
xorl %eax, %eax
callq 0x9107
addl %ebp, 0x20ded24(%rip)   # 0x20ef7d8
movl 0x208ecce(%rip), %ebx   # 0x209f788
addl $0x7, %ebx
sarl $0x3, %ebx
movslq %ebx, %rdx
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x3120
movl 0x208ec8f(%rip), %eax   # 0x209f764
testq %rax, %rax
je 0x10b45
movq 0x208ec9f(%rip), %rdx   # 0x209f780
movq 0x208ec90(%rip), %rsi   # 0x209f778
xorl %edi, %edi
movl (%rdx,%rdi,4), %r8d
testl %r8d, %r8d
je 0x10b3d
movl %edi, %r9d
movl %edi, %ecx
andb $0x7, %cl
movl $0x1, %r10d
shll %cl, %r10d
shrl $0x3, %r9d
movzbl (%r14,%r9), %r9d
andl %r10d, %r9d
xorl %r10d, %r10d
testl %r9d, %r9d
je 0x10b35
movl (%rsi,%rdi,4), %r11d
addl %r10d, %r11d
movl %r11d, %ecx
andb $0x7, %cl
movl $0x1, %ebp
shll %cl, %ebp
shrl $0x3, %r11d
orb %bpl, 0x20(%rsp,%r11)
incl %r10d
cmpl %r8d, %r10d
jb 0x10b13
incq %rdi
cmpq %rax, %rdi
jne 0x10aea
leaq 0x20(%rsp), %rdi
leaq 0x1020(%rsp), %rdx
movl %ebx, %esi
movl $0xfff, %ecx            # imm = 0xFFF
callq 0x370c
movq 0x20dec5b(%rip), %r14   # 0x20ef7c0
movl %eax, %ebx
leaq (%r14,%rbx), %rax
movq %rax, 0x20dec4e(%rip)   # 0x20ef7c0
cmpq 0x20dec57(%rip), %rax   # 0x20ef7d0
jbe 0x10b89
leaq 0x5634(%rip), %rdi      # 0x161b6
xorl %eax, %eax
callq 0x8cb4
movq 0x208ebf0(%rip), %rax   # 0x209f780
cmpl $0x0, (%rax,%r15,4)
je 0x10bd1
movl 0x20dec2b(%rip), %edx   # 0x20ef7c8
movl %r14d, %ecx
subl %edx, %ecx
xorl %edx, %edx
movq 0x208ebcd(%rip), %rsi   # 0x209f778
leaq 0xa1375e(%rip), %rdi    # 0xa24310
incl %edx
movl (%rsi,%r15,4), %r8d
addl %edx, %r8d
leaq (%r8,%r8,8), %r9
leaq (%r9,%r9,2), %r9
addq %r8, %r9
movl %ecx, 0x4(%rdi,%r9)
cmpl (%rax,%r15,4), %edx
jb 0x10bb2
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