// File: /workspace/github_cpp_makefile_star2_O3/FreeSlave[P]vhlt/common/bspfile.cpp
// Function: DoAllocBlock(lightmapblock_s*, int, int)
void DoAllocBlock (lightmapblock_t *blocks, int w, int h)
{
	lightmapblock_t *block;
	// code from Quake
	int i, j;
	int best, best2;
	int x, y;
	if (w < 1 || h < 1)
	{
		Error ("DoAllocBlock: internal error.");
	}
	for (block = blocks; block; block = block->next)
	{
		best = BLOCK_HEIGHT;
		for (i = 0; i < BLOCK_WIDTH - w; i++)
		{
			best2 = 0;
			for (j = 0; j < w; j++)
			{
				if (block->allocated[i + j] >= best)
					break;
				if (block->allocated[i + j] > best2)
					best2 = block->allocated[i + j];
			}
			if (j == w)
			{
				x = i;
				y = best = best2;
			}
		}
		if (best + h <= BLOCK_HEIGHT)
		{
			block->used = true;
			for (i = 0; i < w; i++)
			{
				block->allocated[x + i] = best + h;
			}
			return;
		}
		if (!block->next)
		{ // need to allocate a new block
			if (!block->used)
			{
				Warning ("CountBlocks: invalid extents %dx%d", w, h);
				return;
			}
			block->next = (lightmapblock_t *)malloc (sizeof (lightmapblock_t));
			hlassume (block->next != NULL, assume_NoMemory);
			memset (block->next, 0, sizeof (lightmapblock_t));
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %r14d
movl %esi, %ebp
movq %rdi, %rbx
testl %esi, %esi
setg %al
testl %edx, %edx
setg %cl
testb %cl, %al
jne 0x5f57
leaq 0x16275(%rip), %rdi     # 0x1c1c5
xorl %eax, %eax
callq 0xa164
testq %rbx, %rbx
je 0x612b
movl %ebp, %r15d
movl $0x80, %r12d
subl %ebp, %r12d
cmpl $0x2, %r12d
movl $0x1, %eax
cmovll %eax, %r12d
cmpl $0x7f, %ebp
jg 0x5fc3
leaq 0xc(%rbx), %rcx
movl $0x80, %eax
xorl %edx, %edx
xorl %esi, %esi
xorl %edi, %edi
testl %ebp, %ebp
jle 0x5fac
movl (%rcx,%rsi,4), %r8d
cmpl %eax, %r8d
jge 0x5fac
cmpl %edi, %r8d
cmovgl %r8d, %edi
incq %rsi
cmpq %rsi, %r15
jne 0x5f91
movl %r15d, %esi
cmpl %ebp, %esi
cmovel %edi, %eax
cmovel %edx, %r13d
incq %rdx
addq $0x4, %rcx
cmpq %r12, %rdx
jne 0x5f89
jmp 0x5fc8
movl $0x80, %eax
addl %r14d, %eax
cmpl $0x80, %eax
jle 0x6027
movq (%rbx), %rax
testq %rax, %rax
jne 0x6016
cmpb $0x0, 0x8(%rbx)
je 0x613a
movl $0x210, %edi            # imm = 0x210
callq 0x4420
movq %rax, (%rbx)
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xa91d
movq (%rbx), %rdi
movl $0x210, %edx            # imm = 0x210
xorl %esi, %esi
callq 0x41c0
movq (%rbx), %rax
movq %rax, %rbx
testq %rax, %rax
jne 0x5f79
jmp 0x612b
movb $0x1, 0x8(%rbx)
testl %ebp, %ebp
jle 0x612b
movslq %r13d, %rdx
leal 0x3(%r15), %ecx
decq %r15
movq %r15, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
leaq (%rbx,%rdx,4), %rdx
addq $0x18, %rdx
shrl $0x2, %ecx
shlq $0x4, %rcx
movdqa 0x16002(%rip), %xmm1    # 0x1c060
movdqa 0x1600a(%rip), %xmm2    # 0x1c070
xorl %esi, %esi
movdqa 0x16010(%rip), %xmm3    # 0x1c080
pxor %xmm3, %xmm0
pcmpeqd %xmm4, %xmm4
movdqa 0x16010(%rip), %xmm5    # 0x1c090
movdqa %xmm2, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
movd %xmm6, %edi
notl %edi
testb $0x1, %dil
je 0x60b9
movl %eax, -0xc(%rdx,%rsi)
pxor %xmm4, %xmm6
pextrw $0x4, %xmm6, %edi
testb $0x1, %dil
je 0x60cc
movl %eax, -0x8(%rdx,%rsi)
movdqa %xmm1, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
pxor %xmm4, %xmm6
pextrw $0x0, %xmm6, %edi
testb $0x1, %dil
je 0x6108
movl %eax, -0x4(%rdx,%rsi)
pextrw $0x4, %xmm6, %edi
testb $0x1, %dil
je 0x6116
movl %eax, (%rdx,%rsi)
paddq %xmm5, %xmm2
paddq %xmm5, %xmm1
addq $0x10, %rsi
cmpq %rsi, %rcx
jne 0x6080
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x160a2(%rip), %rdi     # 0x1c1e3
movl %ebp, %esi
movl %r14d, %edx
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa4d6