// File: /workspace/github_c_makefile_star2_O0/Hazematman[P]BSP-Renderer/bsp.c
// Function: loadBspGeometry
static void loadBspGeometry(Bsp *bsp, void *faceData, int32_t faceSize, void *edgeData, int32_t edgeSize,
		void *ledgeData, int32_t ledgeSize){

	bsp->numFaces = faceSize / sizeof(FaceDef);
	bsp->faces = malloc(sizeof(Face)*bsp->numFaces);
	FaceDef *faces = malloc(faceSize);
	memcpy(faces, faceData, faceSize);

	EdgeDef *edges = malloc(edgeSize);
	memcpy(edges, edgeData, edgeSize);

	int32_t *ledges = malloc(ledgeSize);
	memcpy(ledges, ledgeData, ledgeSize);

	for(int i=0; i < bsp->numFaces; i++){
		// Read all verts from edge data into a vert data structure
		// that is more suitable for GL rendering
		Face *face = &bsp->faces[i];
		face->numVerts = faces[i].edgeNum;
		face->verts = malloc(sizeof(Vert)*face->numVerts);
		TexInfoDef *texInfo = getTexInfo(bsp, faces[i].texinfoId);
		face->texture = getTexture(bsp, texInfo->texId);
		face->glId = 0;
		// If face is a trigger or clip skip loading it's verts
		// TODO make somekind of stack or vector to store faces so we don't bother
		// allocating useless memory
		if(strcmp("trigger", getTexName(face->texture)) == 0 || strcmp("clip", getTexName(face->texture)) == 0){
			continue;
		}
		for(int edge=0; edge < faces[i].edgeNum; edge++){
			int32_t edgeIndex = ledges[edge+faces[i].edgeId];
			vec3Float verts[2];
			// if edgeIndex is negative flip v0 and v1
			if(edgeIndex < 0){
				edgeIndex = -edgeIndex;
				verts[0] = getVert(bsp, edges[edgeIndex].vert1);
				verts[1] = getVert(bsp, edges[edgeIndex].vert0);
			} else {
				verts[0] = getVert(bsp, edges[edgeIndex].vert0);
				verts[1] = getVert(bsp, edges[edgeIndex].vert1);
			}

			face->verts[edge].normal = getPlane(bsp, faces[i].planeId)->normal;

			// Negate y and flip y & z so it's oriented properly
			face->verts[edge].position.x = verts[0].x;
			face->verts[edge].position.y = verts[0].z;
			face->verts[edge].position.z = -verts[0].y;

			// compute s and t coordinate for texture mapping
			face->verts[edge].texCoord.x = (vec3FDot(&verts[0], &texInfo->vecS) + texInfo->distS) / getTexWidth(face->texture);
			face->verts[edge].texCoord.y = (vec3FDot(&verts[0], &texInfo->vecT) + texInfo->distT) / getTexHeight(face->texture);
		}

		// Now that we have loaded all vertex data for this face
		// We can generate a vbo for it
		glGenBuffers(1, &face->glId);
		glBindBuffer(GL_ARRAY_BUFFER, face->glId);
		glBufferData(GL_ARRAY_BUFFER, sizeof(Vert)*face->numVerts, &face->verts[0], GL_STATIC_DRAW);
	}

	free(faces);
	free(edges);
	free(ledges);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movq %r9, -0x30(%rbp)
movslq -0x14(%rbp), %rax
movl $0x14, %ecx
xorl %edx, %edx
divq %rcx
movl %eax, %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x8(%rbp), %rax
movslq 0x28(%rax), %rdi
shlq $0x5, %rdi
callq 0x1170
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x30(%rax)
movslq -0x14(%rbp), %rdi
callq 0x1170
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rsi
movslq -0x14(%rbp), %rdx
callq 0x10d0
movslq -0x24(%rbp), %rdi
callq 0x1170
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
movq -0x20(%rbp), %rsi
movslq -0x24(%rbp), %rdx
callq 0x10d0
movslq 0x10(%rbp), %rdi
callq 0x1170
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
movq -0x30(%rbp), %rsi
movslq 0x10(%rbp), %rdx
callq 0x10d0
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jge 0x6c79
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x4c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x38(%rbp), %rax
movslq -0x4c(%rbp), %rcx
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movswl 0x8(%rax), %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x58(%rbp), %rax
movslq 0x10(%rax), %rdi
shlq $0x5, %rdi
callq 0x1170
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movslq -0x4c(%rbp), %rcx
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movswl 0xa(%rax), %esi
callq 0x6e80
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rax
movl 0x20(%rax), %esi
callq 0x6ea0
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x58(%rbp), %rax
movl $0x0, (%rax)
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x5f60
movq %rax, %rsi
leaq 0x3044(%rip), %rdi      # 0x9916
callq 0x1180
cmpl $0x0, %eax
je 0x68fd
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x5f60
movq %rax, %rsi
leaq 0x302b(%rip), %rdi      # 0x991e
callq 0x1180
cmpl $0x0, %eax
jne 0x6902
jmp 0x6c6b
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
movq -0x38(%rbp), %rcx
movslq -0x4c(%rbp), %rdx
imulq $0x14, %rdx, %rdx
addq %rdx, %rcx
movswl 0x8(%rcx), %ecx
cmpl %ecx, %eax
jge 0x6c15
movq -0x48(%rbp), %rax
movl -0x64(%rbp), %ecx
movq -0x38(%rbp), %rdx
movslq -0x4c(%rbp), %rsi
imulq $0x14, %rsi, %rsi
addq %rsi, %rdx
addl 0x4(%rdx), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x68(%rbp)
cmpl $0x0, -0x68(%rbp)
jge 0x6a07
movl -0x68(%rbp), %eax
negl %eax
movl %eax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movslq -0x68(%rbp), %rcx
movzwl 0x2(%rax,%rcx,4), %esi
callq 0x6ec0
movss %xmm1, -0x98(%rbp)
movlpd %xmm0, -0xa0(%rbp)
movl -0x98(%rbp), %eax
movl %eax, -0x88(%rbp)
movq -0xa0(%rbp), %rax
movq %rax, -0x90(%rbp)
movl -0x88(%rbp), %eax
movl %eax, -0x78(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movslq -0x68(%rbp), %rcx
movzwl (%rax,%rcx,4), %esi
callq 0x6ec0
movss %xmm1, -0xb8(%rbp)
movlpd %xmm0, -0xc0(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0xac(%rbp)
movl -0xb8(%rbp), %eax
movl %eax, -0xa4(%rbp)
movq -0xac(%rbp), %rax
movq %rax, -0x74(%rbp)
movl -0xa4(%rbp), %eax
movl %eax, -0x6c(%rbp)
jmp 0x6aae
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movslq -0x68(%rbp), %rcx
movzwl (%rax,%rcx,4), %esi
callq 0x6ec0
movss %xmm1, -0xd8(%rbp)
movlpd %xmm0, -0xe0(%rbp)
movl -0xd8(%rbp), %eax
movl %eax, -0xc8(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0xd0(%rbp)
movl -0xc8(%rbp), %eax
movl %eax, -0x78(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movslq -0x68(%rbp), %rcx
movzwl 0x2(%rax,%rcx,4), %esi
callq 0x6ec0
movss %xmm1, -0xf8(%rbp)
movlpd %xmm0, -0x100(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0xec(%rbp)
movl -0xf8(%rbp), %eax
movl %eax, -0xe4(%rbp)
movq -0xec(%rbp), %rax
movq %rax, -0x74(%rbp)
movl -0xe4(%rbp), %eax
movl %eax, -0x6c(%rbp)
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x64(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x110(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movslq -0x4c(%rbp), %rcx
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movswl (%rax), %esi
callq 0x6f10
movq %rax, %rcx
movq -0x110(%rbp), %rax
movq (%rcx), %rdx
movq %rdx, 0x14(%rax)
movl 0x8(%rcx), %ecx
movl %ecx, 0x1c(%rax)
movss -0x80(%rbp), %xmm0
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x64(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movss %xmm0, (%rax)
movss -0x78(%rbp), %xmm0
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x64(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movss %xmm0, 0x4(%rax)
movss -0x7c(%rbp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x64(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movss %xmm0, 0x8(%rax)
leaq -0x80(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x6100
movq -0x60(%rbp), %rax
addss 0xc(%rax), %xmm0
movss %xmm0, -0x108(%rbp)
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x5f20
movss -0x108(%rbp), %xmm0
cvtsi2ss %eax, %xmm1
divss %xmm1, %xmm0
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x64(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movss %xmm0, 0xc(%rax)
leaq -0x80(%rbp), %rdi
movq -0x60(%rbp), %rsi
addq $0x10, %rsi
callq 0x6100
movq -0x60(%rbp), %rax
addss 0x1c(%rax), %xmm0
movss %xmm0, -0x104(%rbp)
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x5f40
movss -0x104(%rbp), %xmm0
cvtsi2ss %eax, %xmm1
divss %xmm1, %xmm0
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x64(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movss %xmm0, 0x10(%rax)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x6909
leaq 0x5834(%rip), %rax      # 0xc450
movq (%rax), %rax
movq -0x58(%rbp), %rsi
movl $0x1, %edi
callq *%rax
leaq 0x57ef(%rip), %rax      # 0xc420
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movl (%rcx), %esi
movl $0x8892, %edi           # imm = 0x8892
callq *%rax
leaq 0x57e0(%rip), %rax      # 0xc428
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movslq 0x10(%rcx), %rsi
shlq $0x5, %rsi
movq -0x58(%rbp), %rcx
movq 0x18(%rcx), %rdx
movl $0x8892, %edi           # imm = 0x8892
movl $0x88e4, %ecx           # imm = 0x88E4
callq *%rax
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x6819
movq -0x38(%rbp), %rdi
callq 0x1160
movq -0x40(%rbp), %rdi
callq 0x1160
movq -0x48(%rbp), %rdi
callq 0x1160
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopl (%rax)