// File: /workspace/github_c_makefile_star2_O3/k0n10pl4tr[P]minimal_crafter/rendering.c
// Function: generateChunkModel
void
generateChunkModel(unsigned int x, unsigned int y, unsigned int z)
{
	wChunk = getWorldChunk(x, y, z);
	if(wChunk->numBlocks == 0) {
		return;
	}
	unsigned int chunkId = chunkCachedStackSize % NUM_CHUNK_CACHED;
	for(unsigned int i = 0; i < NUM_CHUNK_CACHED; i++) {
		if(chunkCached[i].canDraw && 
				chunkCached[i].xOffset == x &&
				chunkCached[i].yOffset == y &&
				chunkCached[i].zOffset == z) {
			return;
		}
	}
	
	chunkCached[chunkId].faces = 0;
	chunkCached[chunkId].xOffset = x;
	chunkCached[chunkId].yOffset = y;
	chunkCached[chunkId].zOffset = z;
	
	glBindBuffer(GL_ARRAY_BUFFER, chunkCached[chunkId].vbo);
	float* bufferData = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
	float* vertexData = &bufferData[0];
	float* texcoordData = &bufferData[WORLD_CHUNK_NBLOCKS * 18 * 6];
	
	for(int i = 0; i < WORLD_CHUNK_NBLOCKS; i++) { 
		int xb = (i  % WORLD_CHUNK_SIZE);
		int yb = (i  / WORLD_CHUNK_SIZE) % WORLD_CHUNK_SIZE;
		int zb = (i / (WORLD_CHUNK_SIZE * WORLD_CHUNK_SIZE));

		int xp = xb + (int)x * WORLD_CHUNK_SIZE;
		int yp = yb + (int)y * WORLD_CHUNK_SIZE;
		int zp = zb + (int)z * WORLD_CHUNK_SIZE;
		
		#define GET_BLOCK(X, Y, Z) wChunk->blocks[X][Y][Z]
		currentBlock = GET_BLOCK(xb, yb, zb);
		if(currentBlock == 0)
			continue;
		if(xb == 0 || GET_BLOCK(xb - 1, yb, zb) == 0) {
			generateFace(BLOCK_LEFT, currentBlock, vertexData, texcoordData, xp, yp, zp);
			
			vertexData += 18;
			texcoordData += 12;
			chunkCached[chunkId].faces++;
		}

		if(yb == 0 || GET_BLOCK(xb, yb - 1, zb) == 0) {
			generateFace(BLOCK_BOTTOM, currentBlock, vertexData, texcoordData, xp, yp, zp);

			vertexData += 18;
			texcoordData += 12;
			chunkCached[chunkId].faces++;
		}
		if(xb == WORLD_CHUNK_SIZE - 1 || GET_BLOCK(xb + 1, yb, zb) == 0) {
			generateFace(BLOCK_RIGHT, currentBlock, vertexData, texcoordData, xp, yp, zp);

			vertexData += 18;
			texcoordData += 12;
			chunkCached[chunkId].faces++;
		}

		if(yb == WORLD_CHUNK_SIZE - 1 || GET_BLOCK(xb, yb + 1, zb) == 0) {
			generateFace(BLOCK_TOP, currentBlock, vertexData, texcoordData, xp, yp, zp);

			vertexData += 18;
			texcoordData += 12;
			chunkCached[chunkId].faces++;
		}

		if(zb == WORLD_CHUNK_SIZE - 1 || !GET_BLOCK(xb, yb, zb + 1)) {
			generateFace(BLOCK_FRONT, currentBlock, vertexData, texcoordData, xp, yp, zp);

			vertexData += 18;
			texcoordData += 12;
			chunkCached[chunkId].faces++;
		}

		if(zb == 0 || !GET_BLOCK(xb, yb, zb - 1)) {
			generateFace(BLOCK_BACK, currentBlock, vertexData, texcoordData, xp, yp, zp);

			vertexData += 18;
			texcoordData += 12;
			chunkCached[chunkId].faces++;
		}
	}
	glUnmapBuffer(GL_ARRAY_BUFFER);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	
	chunkCached[chunkId].canDraw = 1;
	chunkCachedStackSize ++;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edi, %r15d
movl %esi, %r14d
movl %edx, %ebx
callq 0x6ade
movq %rax, 0x8cda(%rip)      # 0xdf70
cmpl $0x0, 0x400c(%rax)
je 0x5617
movzbl 0x8cce(%rip), %ecx      # 0xdf78
movl $0x18, %edx
leaq 0x70ba(%rip), %rax      # 0xc370
cmpb $0x0, (%rdx,%rax)
je 0x52d4
cmpl %r15d, -0xc(%rdx,%rax)
jne 0x52d4
cmpl %r14d, -0x8(%rdx,%rax)
jne 0x52d4
cmpl %ebx, -0x4(%rdx,%rax)
je 0x5617
addq $0x1c, %rdx
cmpq $0x1c18, %rdx           # imm = 0x1C18
jne 0x52b6
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
leaq (%rax,%rdx), %rcx
movq %rcx, -0x40(%rbp)
movl $0x0, 0x8(%rax,%rdx)
movl %r15d, 0xc(%rax,%rdx)
movl %r14d, 0x10(%rax,%rdx)
movl %ebx, 0x14(%rax,%rdx)
leaq 0x63b7(%rip), %rcx      # 0xb6c8
movl 0x4(%rax,%rdx), %esi
movl $0x8892, %edi           # imm = 0x8892
callq *(%rcx)
leaq 0x695d(%rip), %rax      # 0xbc80
movl $0x8892, %edi           # imm = 0x8892
movl $0x88b9, %esi           # imm = 0x88B9
callq *(%rax)
movq %rax, %rdx
movq %rax, %r12
addq $0x1b0000, %r12         # imm = 0x1B0000
shll $0x4, %r15d
movl %r15d, -0x4c(%rbp)
shll $0x4, %r14d
movl %r14d, -0x50(%rbp)
shll $0x4, %ebx
movl %ebx, -0x54(%rbp)
xorl %r15d, %r15d
movq 0x8c14(%rip), %rax      # 0xdf70
movl %r15d, %r14d
andl $0xf, %r14d
movl %r15d, %ebx
shrl $0x4, %ebx
andl $0xf, %ebx
movl %r15d, %edi
shrl $0x8, %edi
movl %r14d, %ecx
shll $0xa, %ecx
addq %rax, %rcx
addq $0xc, %rcx
movl %ebx, %esi
shll $0x6, %esi
addq %rcx, %rsi
movl (%rsi,%rdi,4), %esi
movl %esi, 0x8bec(%rip)      # 0xdf7c
testl %esi, %esi
je 0x55db
movl %r14d, %ecx
orl -0x4c(%rbp), %ecx
movl %ecx, -0x34(%rbp)
movl %ebx, %ecx
orl -0x50(%rbp), %ecx
movl %ecx, -0x30(%rbp)
movl %edi, %ecx
addl -0x54(%rbp), %ecx
movl %ecx, -0x2c(%rbp)
testq %r14, %r14
movq %rdi, -0x48(%rbp)
je 0x53d7
addq $0xc, %rax
leal -0x1(%r14), %ecx
shlq $0xa, %rcx
addq %rax, %rcx
movl %ebx, %eax
shll $0x6, %eax
addq %rcx, %rax
cmpl $0x0, (%rax,%rdi,4)
jne 0x540b
movl -0x2c(%rbp), %eax
movl %eax, (%rsp)
movl $0x2, %edi
movq %r12, %rcx
movq %rdx, %r13
movl -0x34(%rbp), %r8d
movl -0x30(%rbp), %r9d
callq 0x5626
movq -0x48(%rbp), %rdi
movq %r13, %rdx
addq $0x48, %rdx
addq $0x30, %r12
movq -0x40(%rbp), %rax
incl 0x8(%rax)
testq %rbx, %rbx
je 0x542e
movl %r14d, %eax
shll $0xa, %eax
addq 0x8b53(%rip), %rax      # 0xdf70
leal -0x1(%rbx), %ecx
shlq $0x6, %rcx
addq %rax, %rcx
cmpl $0x0, 0xc(%rcx,%rdi,4)
jne 0x5468
movl 0x8b48(%rip), %esi      # 0xdf7c
movl -0x2c(%rbp), %eax
movl %eax, (%rsp)
movl $0x1, %edi
movq %r12, %rcx
movq %rdx, %r13
movl -0x34(%rbp), %r8d
movl -0x30(%rbp), %r9d
callq 0x5626
movq -0x48(%rbp), %rdi
movq %r13, %rdx
addq $0x48, %rdx
addq $0x30, %r12
movq -0x40(%rbp), %rax
incl 0x8(%rax)
cmpl $0xf, %r14d
je 0x548d
movl %r14d, %eax
shll $0xa, %eax
movl %ebx, %ecx
addq 0x8af3(%rip), %rax      # 0xdf70
shll $0x6, %ecx
addq %rcx, %rax
cmpl $0x0, 0x40c(%rax,%rdi,4)
jne 0x54c7
movl 0x8ae9(%rip), %esi      # 0xdf7c
movl -0x2c(%rbp), %eax
movl %eax, (%rsp)
movl $0x3, %edi
movq %r12, %rcx
movq %rdx, %r13
movl -0x34(%rbp), %r8d
movl -0x30(%rbp), %r9d
callq 0x5626
movq -0x48(%rbp), %rdi
movq %r13, %rdx
addq $0x48, %rdx
addq $0x30, %r12
movq -0x40(%rbp), %rax
incl 0x8(%rax)
cmpl $0xf, %ebx
je 0x54e8
movl %r14d, %eax
shll $0xa, %eax
addq 0x8a97(%rip), %rax      # 0xdf70
movl %ebx, %ecx
shll $0x6, %ecx
addq %rax, %rcx
cmpl $0x0, 0x4c(%rcx,%rdi,4)
jne 0x551f
movl 0x8a8e(%rip), %esi      # 0xdf7c
movl -0x2c(%rbp), %eax
movl %eax, (%rsp)
xorl %edi, %edi
movq %r12, %rcx
movq %rdx, %r13
movl -0x34(%rbp), %r8d
movl -0x30(%rbp), %r9d
callq 0x5626
movq -0x48(%rbp), %rdi
movq %r13, %rdx
addq $0x48, %rdx
addq $0x30, %r12
movq -0x40(%rbp), %rax
incl 0x8(%rax)
cmpl $0xf, %edi
je 0x5540
movl %r14d, %eax
shll $0xa, %eax
addq 0x8a3f(%rip), %rax      # 0xdf70
movl %ebx, %ecx
shll $0x6, %ecx
addq %rax, %rcx
cmpl $0x0, 0x10(%rcx,%rdi,4)
jne 0x557a
movl 0x8a36(%rip), %esi      # 0xdf7c
movl -0x2c(%rbp), %eax
movl %eax, (%rsp)
movl $0x4, %edi
movq %r12, %rcx
movq %rdx, %r13
movl -0x34(%rbp), %r8d
movl -0x30(%rbp), %r9d
callq 0x5626
movq -0x48(%rbp), %rdi
movq %r13, %rdx
addq $0x48, %rdx
addq $0x30, %r12
movq -0x40(%rbp), %rax
incl 0x8(%rax)
cmpl $0x100, %r15d           # imm = 0x100
jb 0x559e
movq 0x89e6(%rip), %rax      # 0xdf70
shll $0xa, %r14d
shll $0x6, %ebx
addq %r14, %rbx
addq %rax, %rbx
cmpl $0x0, 0x8(%rbx,%rdi,4)
jne 0x55db
movl 0x89d8(%rip), %esi      # 0xdf7c
movl -0x2c(%rbp), %eax
movl %eax, (%rsp)
movl $0x5, %edi
movq %r12, %rcx
movq %rdx, %rbx
movl -0x34(%rbp), %r8d
movl -0x30(%rbp), %r9d
callq 0x5626
movq %rbx, %rdx
addq $0x48, %rdx
addq $0x30, %r12
movq -0x40(%rbp), %rax
incl 0x8(%rax)
movq 0x8995(%rip), %rax      # 0xdf70
incl %r15d
cmpl $0x1000, %r15d          # imm = 0x1000
jne 0x535c
leaq 0x69e6(%rip), %rax      # 0xbfd8
movl $0x8892, %edi           # imm = 0x8892
callq *(%rax)
movl $0x8892, %edi           # imm = 0x8892
xorl %esi, %esi
leaq 0x60c1(%rip), %rax      # 0xb6c8
callq *(%rax)
movq -0x40(%rbp), %rax
movb $0x1, 0x18(%rax)
incl 0x8961(%rip)            # 0xdf78
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq