// File: FreeSlave[P]vhlt/hlbsp/qbsp.cpp
// Function: SurflistFromValidFaces()
static surfchain_t* SurflistFromValidFaces()
{
    surface_t*      n;
    int             i;
    face_t*         f;
    face_t*         next;
    surfchain_t*    sc;

    sc = (surfchain_t*)malloc(sizeof(*sc));
    ClearBounds(sc->mins, sc->maxs);
    sc->surfaces = NULL;

    // grab planes from both sides
    for (i = 0; i < g_numplanes; i += 2)
    {
        if (!validfaces[i] && !validfaces[i + 1])
        {
            continue;
        }
        n = AllocSurface();
        n->next = sc->surfaces;
        sc->surfaces = n;
        ClearBounds(n->mins, n->maxs);
#ifdef ZHLT_DETAILBRUSH
		n->detaillevel = -1;
#endif
        n->planenum = i;

        n->faces = NULL;
        for (f = validfaces[i]; f; f = next)
        {
            next = f->next;
            f->next = n->faces;
            n->faces = f;
            AddFaceToBounds(f, n->mins, n->maxs);
#ifdef ZHLT_DETAILBRUSH
			if (n->detaillevel == -1 || f->detaillevel < n->detaillevel)
			{
				n->detaillevel = f->detaillevel;
			}
#endif
        }
        for (f = validfaces[i + 1]; f; f = next)
        {
            next = f->next;
            f->next = n->faces;
            n->faces = f;
            AddFaceToBounds(f, n->mins, n->maxs);
#ifdef ZHLT_DETAILBRUSH
			if (n->detaillevel == -1 || f->detaillevel < n->detaillevel)
			{
				n->detaillevel = f->detaillevel;
			}
#endif
        }

        AddPointToBounds(n->mins, sc->mins, sc->maxs);
        AddPointToBounds(n->maxs, sc->mins, sc->maxs);

        validfaces[i] = NULL;
        validfaces[i + 1] = NULL;
    }

    // merge all possible polygons

    MergeAll(sc->surfaces);

    return sc;
}

// Assembly:
subq $0x28, %rsp
movl $0x38, %edi
callq 0x43e0
movq %rax, (%rsp)
movq (%rsp), %rdi
movq (%rsp), %rsi
addq $0x18, %rsi
callq 0x29720
movq (%rsp), %rax
movq $0x0, 0x30(%rax)
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
leaq 0xb05f12(%rip), %rcx    # 0xb2f3b4
cmpl (%rcx), %eax
jge 0x29701
movslq 0x1c(%rsp), %rcx
leaq 0x30c65ba(%rip), %rax   # 0x30efa70
cmpq $0x0, (%rax,%rcx,8)
jne 0x294da
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rcx
leaq 0x30c65a2(%rip), %rax   # 0x30efa70
cmpq $0x0, (%rax,%rcx,8)
jne 0x294da
jmp 0x296f1
callq 0x24f10
movq %rax, 0x20(%rsp)
movq (%rsp), %rax
movq 0x30(%rax), %rcx
movq 0x20(%rsp), %rax
movq %rcx, (%rax)
movq 0x20(%rsp), %rcx
movq (%rsp), %rax
movq %rcx, 0x30(%rax)
movq 0x20(%rsp), %rdi
addq $0x10, %rdi
movq 0x20(%rsp), %rsi
addq $0x28, %rsi
callq 0x29720
movq 0x20(%rsp), %rax
movl $0xffffffff, 0x50(%rax) # imm = 0xFFFFFFFF
movl 0x1c(%rsp), %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x8(%rax)
movq 0x20(%rsp), %rax
movq $0x0, 0x48(%rax)
movslq 0x1c(%rsp), %rcx
leaq 0x30c6527(%rip), %rax   # 0x30efa70
movq (%rax,%rcx,8), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rsp)
je 0x295e4
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x20(%rsp), %rax
movq 0x48(%rax), %rcx
movq 0x10(%rsp), %rax
movq %rcx, (%rax)
movq 0x10(%rsp), %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x48(%rax)
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rsi
addq $0x10, %rsi
movq 0x20(%rsp), %rdx
addq $0x28, %rdx
callq 0x297a0
movq 0x20(%rsp), %rax
cmpl $-0x1, 0x50(%rax)
je 0x295c3
movq 0x10(%rsp), %rax
movl 0x14(%rax), %eax
movq 0x20(%rsp), %rcx
cmpl 0x50(%rcx), %eax
jge 0x295d3
movq 0x10(%rsp), %rax
movl 0x14(%rax), %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x50(%rax)
jmp 0x295d5
movq 0x8(%rsp), %rax
movq %rax, 0x10(%rsp)
jmp 0x29552
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rcx
leaq 0x30c647b(%rip), %rax   # 0x30efa70
movq (%rax,%rcx,8), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rsp)
je 0x29690
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x20(%rsp), %rax
movq 0x48(%rax), %rcx
movq 0x10(%rsp), %rax
movq %rcx, (%rax)
movq 0x10(%rsp), %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x48(%rax)
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rsi
addq $0x10, %rsi
movq 0x20(%rsp), %rdx
addq $0x28, %rdx
callq 0x297a0
movq 0x20(%rsp), %rax
cmpl $-0x1, 0x50(%rax)
je 0x2966f
movq 0x10(%rsp), %rax
movl 0x14(%rax), %eax
movq 0x20(%rsp), %rcx
cmpl 0x50(%rcx), %eax
jge 0x2967f
movq 0x10(%rsp), %rax
movl 0x14(%rax), %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x50(%rax)
jmp 0x29681
movq 0x8(%rsp), %rax
movq %rax, 0x10(%rsp)
jmp 0x295fe
movq 0x20(%rsp), %rdi
addq $0x10, %rdi
movq (%rsp), %rsi
movq (%rsp), %rdx
addq $0x18, %rdx
callq 0x25bb0
movq 0x20(%rsp), %rdi
addq $0x28, %rdi
movq (%rsp), %rsi
movq (%rsp), %rdx
addq $0x18, %rdx
callq 0x25bb0
movslq 0x1c(%rsp), %rcx
leaq 0x30c63a0(%rip), %rax   # 0x30efa70
movq $0x0, (%rax,%rcx,8)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rcx
leaq 0x30c6387(%rip), %rax   # 0x30efa70
movq $0x0, (%rax,%rcx,8)
movl 0x1c(%rsp), %eax
addl $0x2, %eax
movl %eax, 0x1c(%rsp)
jmp 0x29497
movq (%rsp), %rax
movq 0x30(%rax), %rdi
callq 0x20950
movq (%rsp), %rax
addq $0x28, %rsp
retq
nopw (%rax,%rax)