// File: FreeSlave[P]vhlt/hlbsp/qbsp.cpp
// Function: ReadSurfs(_IO_FILE*)
static surfchain_t* ReadSurfs(FILE* file)
{
    int             r;
#ifdef ZHLT_DETAILBRUSH
	int				detaillevel;
#endif
    int             planenum, g_texinfo, contents, numpoints;
    face_t*         f;
    int             i;
    double          v[3];
    int             line = 0;
#ifdef HLCSG_HLBSP_DOUBLEPLANE
	double			inaccuracy, inaccuracy_count = 0.0, inaccuracy_total = 0.0, inaccuracy_max = 0.0;
#endif

    // read in the polygons
    while (1)
    {
#ifdef HLBSP_REMOVEHULL2
		if (file == polyfiles[2] && g_nohull2)
			break;
#endif
        line++;
#ifdef ZHLT_DETAILBRUSH
        r = fscanf(file, "%i %i %i %i %i\n", &detaillevel, &planenum, &g_texinfo, &contents, &numpoints);
#else
        r = fscanf(file, "%i %i %i %i\n", &planenum, &g_texinfo, &contents, &numpoints);
#endif
        if (r == 0 || r == -1)
        {
            return NULL;
        }
        if (planenum == -1)                                // end of model
        {
#ifdef HLCSG_HLBSP_DOUBLEPLANE
			Developer (DEVELOPER_LEVEL_MEGASPAM, "inaccuracy: average %.8f max %.8f\n", inaccuracy_total / inaccuracy_count, inaccuracy_max);
#endif
            break;
        }
#ifdef ZHLT_DETAILBRUSH
		if (r != 5)
#else
        if (r != 4)
#endif
        {
            Error("ReadSurfs (line %i): scanf failure", line);
        }
        if (numpoints > MAXPOINTS)
        {
            Error("ReadSurfs (line %i): %i > MAXPOINTS\nThis is caused by a face with too many verticies (typically found on end-caps of high-poly cylinders)\n", line, numpoints);
        }
        if (planenum > g_numplanes)
        {
            Error("ReadSurfs (line %i): %i > g_numplanes\n", line, planenum);
        }
        if (g_texinfo > g_numtexinfo)
        {
            Error("ReadSurfs (line %i): %i > g_numtexinfo", line, g_texinfo);
        }
#ifdef ZHLT_DETAILBRUSH
		if (detaillevel < 0)
		{
			Error("ReadSurfs (line %i): detaillevel %i < 0", line, detaillevel);
		}
#endif

        if (!strcasecmp(GetTextureByNumber(g_texinfo), "skip"))
        {
            Verbose("ReadSurfs (line %i): skipping a surface", line);

            for (i = 0; i < numpoints; i++)
            {
                line++;
                //Verbose("skipping line %d", line);
                r = fscanf(file, "%lf %lf %lf\n", &v[0], &v[1], &v[2]);
                if (r != 3)
                {
                    Error("::ReadSurfs (face_skip), fscanf of points failed at line %i", line);
                }
            }
            fscanf(file, "\n");
            continue;
        }

        f = AllocFace();
#ifdef ZHLT_DETAILBRUSH
		f->detaillevel = detaillevel;
#endif
        f->planenum = planenum;
        f->texturenum = g_texinfo;
        f->contents = contents;
        f->numpoints = numpoints;
        f->next = validfaces[planenum];
        validfaces[planenum] = f;

        SetFaceType(f);

        for (i = 0; i < f->numpoints; i++)
        {
            line++;
            r = fscanf(file, "%lf %lf %lf\n", &v[0], &v[1], &v[2]);
            if (r != 3)
            {
                Error("::ReadSurfs (face_normal), fscanf of points failed at line %i", line);
            }
            VectorCopy(v, f->pts[i]);
#ifdef HLCSG_HLBSP_DOUBLEPLANE
			 if (DEVELOPER_LEVEL_MEGASPAM <= g_developer)
			 {
				const dplane_t *plane = &g_dplanes[f->planenum];
				inaccuracy = fabs (DotProduct (f->pts[i], plane->normal) - plane->dist);
				inaccuracy_count++;
				inaccuracy_total += inaccuracy;
				inaccuracy_max = qmax (inaccuracy, inaccuracy_max);
			}
#endif
        }
        fscanf(file, "\n");
    }

    return SurflistFromValidFaces();
}

// Assembly:
subq $0x98, %rsp
movq %rdi, 0x88(%rsp)
movl $0x0, 0x3c(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x88(%rsp), %rax
cmpq 0x30c6e3f(%rip), %rax   # 0x30efa40
jne 0x28c11
testb $0x1, 0x20c6e20(%rip)   # 0x20efa2a
je 0x28c11
jmp 0x290bf
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
movq 0x88(%rsp), %rdi
leaq 0x12a0d(%rip), %rsi     # 0x3b638
leaq 0x80(%rsp), %rdx
leaq 0x7c(%rsp), %rcx
leaq 0x78(%rsp), %r8
leaq 0x74(%rsp), %r9
leaq 0x70(%rsp), %rax
movq %rax, (%rsp)
movb $0x0, %al
callq 0x4570
movl %eax, 0x84(%rsp)
cmpl $0x0, 0x84(%rsp)
je 0x28c6d
cmpl $-0x1, 0x84(%rsp)
jne 0x28c7e
movq $0x0, 0x90(%rsp)
jmp 0x290cc
cmpl $-0x1, 0x7c(%rsp)
jne 0x28caf
movsd 0x20(%rsp), %xmm0
divsd 0x28(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movl $0x6, %edi
leaq 0x129a5(%rip), %rsi     # 0x3b648
movb $0x2, %al
callq 0xbb80
jmp 0x290bf
cmpl $0x5, 0x84(%rsp)
je 0x28ccb
movl 0x3c(%rsp), %esi
leaq 0x129a7(%rip), %rdi     # 0x3b66b
movb $0x0, %al
callq 0xb510
cmpl $0x1c, 0x70(%rsp)
jle 0x28ce8
movl 0x3c(%rsp), %esi
movl 0x70(%rsp), %edx
leaq 0x129ad(%rip), %rdi     # 0x3b68e
movb $0x0, %al
callq 0xb510
movl 0x7c(%rsp), %eax
leaq 0xb066c1(%rip), %rcx    # 0xb2f3b4
cmpl (%rcx), %eax
jle 0x28d0d
movl 0x3c(%rsp), %esi
movl 0x7c(%rsp), %edx
leaq 0x12a13(%rip), %rdi     # 0x3b719
movb $0x0, %al
callq 0xb510
movl 0x78(%rsp), %eax
leaq 0x11866a4(%rip), %rcx   # 0x11af3bc
cmpl (%rcx), %eax
jle 0x28d32
movl 0x3c(%rsp), %esi
movl 0x78(%rsp), %edx
leaq 0x12a15(%rip), %rdi     # 0x3b740
movb $0x0, %al
callq 0xb510
cmpl $0x0, 0x80(%rsp)
jge 0x28d55
movl 0x3c(%rsp), %esi
movl 0x80(%rsp), %edx
leaq 0x12a19(%rip), %rdi     # 0x3b767
movb $0x0, %al
callq 0xb510
movl 0x78(%rsp), %edi
callq 0x6950
movq %rax, %rdi
leaq 0x11a25(%rip), %rsi     # 0x3a78d
callq 0x4320
cmpl $0x0, %eax
jne 0x28e1f
movl 0x3c(%rsp), %esi
leaq 0x12a0e(%rip), %rdi     # 0x3b78f
movb $0x0, %al
callq 0xba90
movl $0x0, 0x64(%rsp)
movl 0x64(%rsp), %eax
cmpl 0x70(%rsp), %eax
jge 0x28e04
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
movq 0x88(%rsp), %rdi
leaq 0x40(%rsp), %rdx
leaq 0x40(%rsp), %rcx
addq $0x8, %rcx
leaq 0x40(%rsp), %r8
addq $0x10, %r8
leaq 0x129ec(%rip), %rsi     # 0x3b7b7
movb $0x0, %al
callq 0x4570
movl %eax, 0x84(%rsp)
cmpl $0x3, 0x84(%rsp)
je 0x28df5
movl 0x3c(%rsp), %esi
leaq 0x129d6(%rip), %rdi     # 0x3b7c4
movb $0x0, %al
callq 0xb510
jmp 0x28df7
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x28d90
movq 0x88(%rsp), %rdi
leaq 0xeee5(%rip), %rsi      # 0x37cf8
movb $0x0, %al
callq 0x4570
jmp 0x28bf2
callq 0x23df0
movq %rax, 0x68(%rsp)
movl 0x80(%rsp), %ecx
movq 0x68(%rsp), %rax
movl %ecx, 0x14(%rax)
movl 0x7c(%rsp), %ecx
movq 0x68(%rsp), %rax
movl %ecx, 0x8(%rax)
movl 0x78(%rsp), %ecx
movq 0x68(%rsp), %rax
movl %ecx, 0xc(%rax)
movl 0x74(%rsp), %ecx
movq 0x68(%rsp), %rax
movl %ecx, 0x10(%rax)
movl 0x70(%rsp), %ecx
movq 0x68(%rsp), %rax
movl %ecx, 0x2c(%rax)
movslq 0x7c(%rsp), %rcx
leaq 0x30c6bfc(%rip), %rax   # 0x30efa70
movq (%rax,%rcx,8), %rcx
movq 0x68(%rsp), %rax
movq %rcx, (%rax)
movq 0x68(%rsp), %rdx
movslq 0x7c(%rsp), %rcx
leaq 0x30c6bdf(%rip), %rax   # 0x30efa70
movq %rdx, (%rax,%rcx,8)
movq 0x68(%rsp), %rdi
callq 0x293b0
movl $0x0, 0x64(%rsp)
movl 0x64(%rsp), %eax
movq 0x68(%rsp), %rcx
cmpl 0x2c(%rcx), %eax
jge 0x290a4
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
movq 0x88(%rsp), %rdi
leaq 0x40(%rsp), %rdx
leaq 0x40(%rsp), %rcx
addq $0x8, %rcx
leaq 0x40(%rsp), %r8
addq $0x10, %r8
leaq 0x128cd(%rip), %rsi     # 0x3b7b7
movb $0x0, %al
callq 0x4570
movl %eax, 0x84(%rsp)
cmpl $0x3, 0x84(%rsp)
je 0x28f14
movl 0x3c(%rsp), %esi
leaq 0x128f3(%rip), %rdi     # 0x3b800
movb $0x0, %al
callq 0xb510
movsd 0x40(%rsp), %xmm0
movq 0x68(%rsp), %rax
addq $0x38, %rax
movslq 0x64(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movsd %xmm0, (%rax)
movsd 0x48(%rsp), %xmm0
movq 0x68(%rsp), %rax
addq $0x38, %rax
movslq 0x64(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movsd %xmm0, 0x8(%rax)
movsd 0x50(%rsp), %xmm0
movq 0x68(%rsp), %rax
addq $0x38, %rax
movslq 0x64(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movsd %xmm0, 0x10(%rax)
movl $0x6, %eax
leaq 0x20b4491(%rip), %rcx   # 0x20dd410
cmpl (%rcx), %eax
jg 0x29092
movq 0x68(%rsp), %rax
movslq 0x8(%rax), %rcx
leaq 0x20c6a99(%rip), %rax   # 0x20efa30
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, 0x10(%rsp)
movq 0x68(%rsp), %rax
addq $0x38, %rax
movslq 0x64(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movsd (%rax), %xmm1
movq 0x10(%rsp), %rax
movsd (%rax), %xmm2
movq 0x68(%rsp), %rax
addq $0x38, %rax
movslq 0x64(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movsd 0x8(%rax), %xmm0
movq 0x10(%rsp), %rax
mulsd 0x8(%rax), %xmm0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movq 0x68(%rsp), %rax
addq $0x38, %rax
movslq 0x64(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movsd 0x10(%rax), %xmm0
movq 0x10(%rsp), %rax
movsd 0x10(%rax), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0x10(%rsp), %rax
subsd 0x30(%rax), %xmm0
movaps 0x10442(%rip), %xmm1    # 0x39470
pand %xmm1, %xmm0
movsd %xmm0, 0x30(%rsp)
movsd 0x10108(%rip), %xmm0    # 0x39148
addsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x30(%rsp), %xmm0
addsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x30(%rsp), %xmm0
ucomisd 0x18(%rsp), %xmm0
jbe 0x2907a
movsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
jmp 0x29086
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
jmp 0x29094
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x28ea7
movq 0x88(%rsp), %rdi
leaq 0xec45(%rip), %rsi      # 0x37cf8
movb $0x0, %al
callq 0x4570
jmp 0x28bf2
callq 0x29460
movq %rax, 0x90(%rsp)
movq 0x90(%rsp), %rax
addq $0x98, %rsp
retq
nopl (%rax)