// File: /workspace/github_c_makefile_star2_O2/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 0x69ec
movq %rax, 0x8c48(%rip)      # 0xdf70
cmpl $0x0, 0x400c(%rax)
je 0x5675
movzbl 0x8c3c(%rip), %ecx      # 0xdf78
pushq $0x18
popq %rdx
leaq 0x702a(%rip), %rax      # 0xc370
cmpq $0x1c18, %rdx           # imm = 0x1C18
je 0x5373
cmpb $0x0, (%rdx,%rax)
je 0x536d
cmpl %r15d, -0xc(%rdx,%rax)
jne 0x536d
cmpl %r14d, -0x8(%rdx,%rax)
jne 0x536d
cmpl %ebx, -0x4(%rdx,%rax)
je 0x5675
addq $0x1c, %rdx
jmp 0x5346
imulq $0x1c, %rcx, %rcx
leaq (%rax,%rcx), %rdx
movq %rdx, -0x40(%rbp)
andl $0x0, 0x8(%rcx,%rax)
movl %r15d, 0xc(%rcx,%rax)
movl %r14d, 0x10(%rcx,%rax)
movl %ebx, 0x14(%rcx,%rax)
leaq 0x632f(%rip), %rdx      # 0xb6c8
movl 0x4(%rcx,%rax), %esi
movl $0x8892, %edi           # imm = 0x8892
callq *(%rdx)
leaq 0x68d5(%rip), %rax      # 0xbc80
movl $0x8892, %edi           # imm = 0x8892
movl $0x88b9, %esi           # imm = 0x88B9
callq *(%rax)
movq %rax, %r12
movq %rax, %r13
addq $0x1b0000, %r13         # imm = 0x1B0000
shll $0x4, %r15d
movl %r15d, -0x48(%rbp)
shll $0x4, %r14d
movl %r14d, -0x4c(%rbp)
shll $0x4, %ebx
movl %ebx, -0x50(%rbp)
xorl %edi, %edi
cmpl $0x1000, %edi           # imm = 0x1000
je 0x5649
movl %edi, %ebx
andl $0xf, %ebx
movl %edi, %r14d
shrl $0x4, %r14d
andl $0xf, %r14d
movl %edi, %r15d
shrl $0x8, %r15d
movq 0x8b6a(%rip), %rax      # 0xdf70
movl %ebx, %ecx
shll $0xa, %ecx
addq %rax, %rcx
addq $0xc, %rcx
movl %r14d, %edx
shll $0x6, %edx
addq %rcx, %rdx
movl (%rdx,%r15,4), %esi
movl %esi, 0x8b57(%rip)      # 0xdf7c
testl %esi, %esi
je 0x5642
movl %edi, -0x44(%rbp)
movl %ebx, %ecx
orl -0x48(%rbp), %ecx
movl %ecx, -0x34(%rbp)
movl %r14d, %ecx
orl -0x4c(%rbp), %ecx
movl %ecx, -0x30(%rbp)
movl %r15d, %ecx
orl -0x50(%rbp), %ecx
movl %ecx, -0x2c(%rbp)
testq %rbx, %rbx
je 0x546d
addq $0xc, %rax
leal -0x1(%rbx), %ecx
shlq $0xa, %rcx
movl %r14d, %edx
shll $0x6, %edx
addq %rcx, %rdx
addq %rax, %rdx
cmpl $0x0, (%rdx,%r15,4)
jne 0x5498
movl -0x2c(%rbp), %eax
movl %eax, (%rsp)
pushq $0x2
popq %rdi
movq %r12, %rdx
movq %r13, %rcx
movl -0x34(%rbp), %r8d
movl -0x30(%rbp), %r9d
callq 0x5684
addq $0x48, %r12
addq $0x30, %r13
movq -0x40(%rbp), %rax
incl 0x8(%rax)
testq %r14, %r14
je 0x54bc
movl %ebx, %eax
shll $0xa, %eax
addq 0x8ac7(%rip), %rax      # 0xdf70
leal -0x1(%r14), %ecx
shlq $0x6, %rcx
addq %rax, %rcx
cmpl $0x0, 0xc(%rcx,%r15,4)
jne 0x54ed
movl 0x8aba(%rip), %esi      # 0xdf7c
movl -0x2c(%rbp), %eax
movl %eax, (%rsp)
pushq $0x1
popq %rdi
movq %r12, %rdx
movq %r13, %rcx
movl -0x34(%rbp), %r8d
movl -0x30(%rbp), %r9d
callq 0x5684
addq $0x48, %r12
addq $0x30, %r13
movq -0x40(%rbp), %rax
incl 0x8(%rax)
cmpl $0xf, %ebx
je 0x5512
movl %ebx, %eax
shll $0xa, %eax
movl %r14d, %ecx
addq 0x8a6f(%rip), %rax      # 0xdf70
shll $0x6, %ecx
addq %rcx, %rax
cmpl $0x0, 0x40c(%rax,%r15,4)
jne 0x5543
movl 0x8a64(%rip), %esi      # 0xdf7c
movl -0x2c(%rbp), %eax
movl %eax, (%rsp)
pushq $0x3
popq %rdi
movq %r12, %rdx
movq %r13, %rcx
movl -0x34(%rbp), %r8d
movl -0x30(%rbp), %r9d
callq 0x5684
addq $0x48, %r12
addq $0x30, %r13
movq -0x40(%rbp), %rax
incl 0x8(%rax)
cmpl $0xf, %r14d
je 0x5566
movl %ebx, %eax
shll $0xa, %eax
addq 0x8a1b(%rip), %rax      # 0xdf70
movl %r14d, %ecx
shll $0x6, %ecx
addq %rax, %rcx
cmpl $0x0, 0x4c(%rcx,%r15,4)
jne 0x5596
movl 0x8a10(%rip), %esi      # 0xdf7c
movl -0x2c(%rbp), %eax
movl %eax, (%rsp)
xorl %edi, %edi
movq %r12, %rdx
movq %r13, %rcx
movl -0x34(%rbp), %r8d
movl -0x30(%rbp), %r9d
callq 0x5684
addq $0x48, %r12
addq $0x30, %r13
movq -0x40(%rbp), %rax
incl 0x8(%rax)
cmpl $0xf, %r15d
je 0x55b9
movl %ebx, %eax
shll $0xa, %eax
addq 0x89c8(%rip), %rax      # 0xdf70
movl %r14d, %ecx
shll $0x6, %ecx
addq %rax, %rcx
cmpl $0x0, 0x10(%rcx,%r15,4)
jne 0x55ea
movl 0x89bd(%rip), %esi      # 0xdf7c
movl -0x2c(%rbp), %eax
movl %eax, (%rsp)
pushq $0x4
popq %rdi
movq %r12, %rdx
movq %r13, %rcx
movl -0x34(%rbp), %r8d
movl -0x30(%rbp), %r9d
callq 0x5684
addq $0x48, %r12
addq $0x30, %r13
movq -0x40(%rbp), %rax
incl 0x8(%rax)
movl -0x44(%rbp), %edi
cmpl $0x100, %edi            # imm = 0x100
jb 0x560e
shll $0xa, %ebx
addq 0x8971(%rip), %rbx      # 0xdf70
shll $0x6, %r14d
addq %rbx, %r14
cmpl $0x0, 0x8(%r14,%r15,4)
jne 0x5642
movl 0x8968(%rip), %esi      # 0xdf7c
movl -0x2c(%rbp), %eax
movl %eax, (%rsp)
pushq $0x5
popq %rdi
movq %r12, %rdx
movq %r13, %rcx
movl -0x34(%rbp), %r8d
movl -0x30(%rbp), %r9d
callq 0x5684
movl -0x44(%rbp), %edi
addq $0x48, %r12
addq $0x30, %r13
movq -0x40(%rbp), %rax
incl 0x8(%rax)
incl %edi
jmp 0x53dc
leaq 0x6988(%rip), %rax      # 0xbfd8
movl $0x8892, %edi           # imm = 0x8892
callq *(%rax)
movl $0x8892, %edi           # imm = 0x8892
xorl %esi, %esi
leaq 0x6063(%rip), %rax      # 0xb6c8
callq *(%rax)
movq -0x40(%rbp), %rax
movb $0x1, 0x18(%rax)
incl 0x8903(%rip)            # 0xdf78
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq