// File: /workspace/github_cpp_makefile_star2_O1/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 0x208eb77(%rip), %r14d  # 0x209f7a8
imull %r12d, %r14d
movq 0x208eb44(%rip), %rsi   # 0x209f780
movslq %r12d, %rcx
imulq $0x810, %rcx, %rax      # imm = 0x810
movl %ecx, %edx
sarl $0x3, %edx
movq %rcx, 0x18(%rsp)
andb $0x7, %cl
movl $0x1, %edi
shll %cl, %edi
movl %edi, (%rsp)
movl %edx, %ecx
movq %rcx, 0x8(%rsp)
addq 0x208eb35(%rip), %r14   # 0x209f7a0
cmpl $0x0, (%rsi,%rax)
je 0x10d93
addq %rax, %rsi
leaq 0x5519(%rip), %r13      # 0x16198
xorl %ebx, %ebx
xorl %r15d, %r15d
movq %rsi, 0x10(%rsp)
movl %r12d, 0x4(%rsp)
movq 0x10(%rsi,%rbx,8), %rbp
cmpl $0x2, 0x20(%rbp)
je 0x10caf
leaq 0x548f(%rip), %rdi      # 0x1612f
movl %r12d, %esi
xorl %eax, %eax
callq 0x8d9c
movq 0x10(%rsp), %rsi
cmpl $0x0, 0x208eaf2(%rip)   # 0x209f7a8
je 0x10cd0
movq 0x28(%rbp), %rax
xorl %ecx, %ecx
movb (%rax,%rcx), %dl
orb %dl, (%r14,%rcx)
incq %rcx
cmpl 0x208eada(%rip), %ecx   # 0x209f7a8
jb 0x10cbe
movl $0x1, %ecx
testl %r15d, %r15d
jne 0x10d82
movq 0x8(%rsp), %rax
movzbl (%r14,%rax), %eax
testl %eax, (%rsp)
je 0x10d80
leaq 0x5451(%rip), %rdi      # 0x16149
xorl %eax, %eax
callq 0x910e
movl 0x10(%rbp), %edx
leaq 0x545b(%rip), %rdi      # 0x16164
movl %r12d, %esi
xorl %eax, %eax
callq 0x8aae
movq 0x18(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jle 0x10d61
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 %r13, %rdi
movb $0x3, %al
callq 0x8aae
incq %r12
movq 0x18(%rbp), %rax
movslq 0x4(%rax), %rcx
addq $0xc, %r15
cmpq %rcx, %r12
jl 0x10d26
leaq 0x2f40(%rip), %rdi      # 0x13ca8
xorl %eax, %eax
callq 0x8aae
movl 0x4(%rsp), %r12d
movq 0x10(%rsp), %rsi
movl $0x1, %ecx
jmp 0x10d82
xorl %ecx, %ecx
incq %rbx
movl (%rsi), %eax
movl %ecx, %r15d
cmpq %rax, %rbx
jb 0x10c8e
movl (%rsp), %eax
movq 0x8(%rsp), %rcx
orb %al, (%r14,%rcx)
movq 0x20dea22(%rip), %rax   # 0x20ef7c8
movq 0x18(%rsp), %r15
cmpb $0x1, (%rax,%r15,2)
jne 0x10ddc
cmpl $0x0, 0x208e9bb(%rip)   # 0x209f774
je 0x10ddc
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
cmpl 0x208e99a(%rip), %eax   # 0x209f774
jb 0x10dbd
movl 0x208e992(%rip), %ecx   # 0x209f774
testl %ecx, %ecx
je 0x10e16
xorl %eax, %eax
movq 0x20de9d9(%rip), %rcx   # 0x20ef7c8
cmpb $0x1, 0x1(%rcx,%rax,2)
jne 0x10e08
movl %eax, %ecx
andb $0x7, %cl
movb $0x1, %dl
shlb %cl, %dl
movl %eax, %ecx
shrl $0x3, %ecx
orb %dl, (%r14,%rcx)
incq %rax
movl 0x208e963(%rip), %ecx   # 0x209f774
cmpq %rcx, %rax
jb 0x10de8
testl %ecx, %ecx
je 0x10fd4
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 0x4786(%rip), %xmm2     # 0x155c0
xorl %ecx, %ecx
movdqa 0x124c(%rip), %xmm3     # 0x12090
pxor %xmm3, %xmm0
pcmpeqd %xmm4, %xmm4
movdqa 0x477c(%rip), %xmm5     # 0x155d0
movaps 0x4784(%rip), %xmm8     # 0x155e0
movdqa 0x478b(%rip), %xmm9     # 0x155f0
pxor %xmm11, %xmm11
movdqa %xmm11, %xmm7
movdqa %xmm2, %xmm6
pxor %xmm3, %xmm6
pcmpgtd %xmm0, %xmm6
movd %xmm6, %edx
notl %edx
testb $0x1, %dl
je 0x10e95
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 0x10ed9
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 0x10f08
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 0x10f32
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 0x10e6a
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 0x10fd6
xorl %ebp, %ebp
leaq 0x51d4(%rip), %rdi      # 0x161b1
movl %r12d, %esi
movl %ebp, %edx
xorl %eax, %eax
callq 0x91ef
addl %ebp, 0x20de7f9(%rip)   # 0x20ef7e8
movl 0x208e7a3(%rip), %ebx   # 0x209f798
addl $0x7, %ebx
sarl $0x3, %ebx
movslq %ebx, %rdx
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x3120
movl 0x208e764(%rip), %eax   # 0x209f774
testq %rax, %rax
je 0x1107b
movq 0x208e774(%rip), %rdx   # 0x209f790
movq 0x208e765(%rip), %rsi   # 0x209f788
xorl %edi, %edi
cmpl $0x0, (%rdx,%rdi,4)
je 0x11073
movl %edi, %r8d
shrl $0x3, %r8d
movl %edi, %ecx
andb $0x7, %cl
movl $0x1, %r9d
shll %cl, %r9d
xorl %r10d, %r10d
movzbl (%r14,%r8), %ecx
testl %ecx, %r9d
je 0x1106a
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 (%rdx,%rdi,4), %r10d
jb 0x11043
incq %rdi
cmpq %rax, %rdi
jne 0x11025
leaq 0x20(%rsp), %rdi
leaq 0x1020(%rsp), %rdx
movl %ebx, %esi
movl $0xfff, %ecx            # imm = 0xFFF
callq 0x371c
movq 0x20de735(%rip), %r14   # 0x20ef7d0
movl %eax, %ebx
leaq (%r14,%rbx), %rax
movq %rax, 0x20de728(%rip)   # 0x20ef7d0
cmpq 0x20de731(%rip), %rax   # 0x20ef7e0
jbe 0x110bf
leaq 0x5111(%rip), %rdi      # 0x161c9
xorl %eax, %eax
callq 0x8d9c
movq 0x208e6ca(%rip), %rax   # 0x209f790
cmpl $0x0, (%rax,%r15,4)
je 0x11107
movl 0x20de705(%rip), %edx   # 0x20ef7d8
movl %r14d, %ecx
subl %edx, %ecx
xorl %edx, %edx
movq 0x208e6a7(%rip), %rsi   # 0x209f788
leaq 0xa13238(%rip), %rdi    # 0xa24320
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 0x110e8
leaq 0x1020(%rsp), %rsi
movq %r14, %rdi
movq %rbx, %rdx
callq 0x31b0
addq $0x2028, %rsp           # imm = 0x2028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq