// File: /workspace/github_cpp_makefile_star2_O1/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, %ebp
movl %esi, %ebx
movq %rdi, %r14
testl %esi, %esi
setg %al
testl %edx, %edx
setg %cl
testb %cl, %al
jne 0x5f4c
leaq 0x16288(%rip), %rdi     # 0x1c1cd
xorl %eax, %eax
callq 0xa2a0
testq %r14, %r14
je 0x611b
movl %ebx, %r15d
movl $0x80, %r12d
subl %ebx, %r12d
cmpl $0x2, %r12d
movl $0x1, %eax
cmovll %eax, %r12d
cmpl $0x7f, %ebx
jg 0x5fb8
leaq 0xc(%r14), %rcx
movl $0x80, %eax
xorl %edx, %edx
xorl %esi, %esi
xorl %edi, %edi
testl %ebx, %ebx
jle 0x5fa1
movl (%rcx,%rsi,4), %r8d
cmpl %eax, %r8d
jge 0x5fa1
cmpl %edi, %r8d
cmovgl %r8d, %edi
incq %rsi
cmpq %rsi, %r15
jne 0x5f86
movl %r15d, %esi
cmpl %ebx, %esi
cmovel %edi, %eax
cmovel %edx, %r13d
incq %rdx
addq $0x4, %rcx
cmpq %r12, %rdx
jne 0x5f7e
jmp 0x5fbd
movl $0x80, %eax
addl %ebp, %eax
cmpl $0x80, %eax
jle 0x6017
cmpq $0x0, (%r14)
jne 0x6006
cmpb $0x0, 0x8(%r14)
je 0x612a
movl $0x210, %edi            # imm = 0x210
callq 0x4430
movq %rax, (%r14)
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xaa4d
movq (%r14), %rdi
movl $0x210, %edx            # imm = 0x210
xorl %esi, %esi
callq 0x41c0
movq (%r14), %r14
testq %r14, %r14
jne 0x5f6e
jmp 0x611b
movb $0x1, 0x8(%r14)
testl %ebx, %ebx
jle 0x611b
movslq %r13d, %rcx
addl $0x3, %ebx
decq %r15
movq %r15, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
leaq (%r14,%rcx,4), %rcx
addq $0x18, %rcx
shrl $0x2, %ebx
shlq $0x4, %rbx
movdqa 0x16022(%rip), %xmm1    # 0x1c070
movdqa 0x1602a(%rip), %xmm2    # 0x1c080
xorl %edx, %edx
movdqa 0x16030(%rip), %xmm3    # 0x1c090
pxor %xmm3, %xmm0
pcmpeqd %xmm4, %xmm4
movdqa 0x16030(%rip), %xmm5    # 0x1c0a0
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, %esi
notl %esi
testb $0x1, %sil
je 0x60a9
movl %eax, -0xc(%rcx,%rdx)
pxor %xmm4, %xmm6
pextrw $0x4, %xmm6, %esi
testb $0x1, %sil
je 0x60bc
movl %eax, -0x8(%rcx,%rdx)
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, %esi
testb $0x1, %sil
je 0x60f8
movl %eax, -0x4(%rcx,%rdx)
pextrw $0x4, %xmm6, %esi
testb $0x1, %sil
je 0x6106
movl %eax, (%rcx,%rdx)
paddq %xmm5, %xmm2
paddq %xmm5, %xmm1
addq $0x10, %rdx
cmpq %rdx, %rbx
jne 0x6070
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x160ba(%rip), %rdi     # 0x1c1eb
movl %ebx, %esi
movl %ebp, %edx
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa612