// File: 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:
subq $0x38, %rsp
movq %rdi, 0x30(%rsp)
movl %esi, 0x2c(%rsp)
movl %edx, 0x28(%rsp)
cmpl $0x1, 0x2c(%rsp)
jl 0x677f
cmpl $0x1, 0x28(%rsp)
jge 0x678d
leaq 0x2398f(%rip), %rdi     # 0x2a115
movb $0x0, %al
callq 0xba80
movq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x20(%rsp)
je 0x694e
movl $0x80, 0x14(%rsp)
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
movl $0x80, %ecx
subl 0x2c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x686f
movl $0x0, 0x10(%rsp)
movl $0x0, 0x18(%rsp)
movl 0x18(%rsp), %eax
cmpl 0x2c(%rsp), %eax
jge 0x683f
movq 0x20(%rsp), %rax
movl 0x1c(%rsp), %ecx
addl 0x18(%rsp), %ecx
movslq %ecx, %rcx
movl 0xc(%rax,%rcx,4), %eax
cmpl 0x14(%rsp), %eax
jl 0x67fe
jmp 0x683f
movq 0x20(%rsp), %rax
movl 0x1c(%rsp), %ecx
addl 0x18(%rsp), %ecx
movslq %ecx, %rcx
movl 0xc(%rax,%rcx,4), %eax
cmpl 0x10(%rsp), %eax
jle 0x6830
movq 0x20(%rsp), %rax
movl 0x1c(%rsp), %ecx
addl 0x18(%rsp), %ecx
movslq %ecx, %rcx
movl 0xc(%rax,%rcx,4), %eax
movl %eax, 0x10(%rsp)
jmp 0x6832
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x67d8
movl 0x18(%rsp), %eax
cmpl 0x2c(%rsp), %eax
jne 0x685d
movl 0x1c(%rsp), %eax
movl %eax, 0xc(%rsp)
movl 0x10(%rsp), %eax
movl %eax, 0x14(%rsp)
movl %eax, 0x8(%rsp)
jmp 0x685f
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x67b3
movl 0x14(%rsp), %eax
addl 0x28(%rsp), %eax
cmpl $0x80, %eax
jg 0x68c7
movq 0x20(%rsp), %rax
movb $0x1, 0x8(%rax)
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
cmpl 0x2c(%rsp), %eax
jge 0x68c2
movl 0x14(%rsp), %edx
addl 0x28(%rsp), %edx
movq 0x20(%rsp), %rax
movl 0xc(%rsp), %ecx
addl 0x1c(%rsp), %ecx
movslq %ecx, %rcx
movl %edx, 0xc(%rax,%rcx,4)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x688f
jmp 0x694e
movq 0x20(%rsp), %rax
cmpq $0x0, (%rax)
jne 0x693a
movq 0x20(%rsp), %rax
testb $0x1, 0x8(%rax)
jne 0x68f5
movl 0x2c(%rsp), %esi
movl 0x28(%rsp), %edx
leaq 0x23847(%rip), %rdi     # 0x2a133
movb $0x0, %al
callq 0xbef0
jmp 0x694e
movl $0x210, %edi            # imm = 0x210
callq 0x4500
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq %rcx, (%rax)
movq 0x20(%rsp), %rax
cmpq $0x0, (%rax)
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xc400
movq 0x20(%rsp), %rax
movq (%rax), %rdi
xorl %esi, %esi
movl $0x210, %edx            # imm = 0x210
callq 0x4210
jmp 0x693c
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x20(%rsp)
jmp 0x6797
addq $0x38, %rsp
retq
nopw %cs:(%rax,%rax)