// File: /workspace/github_cpp_makefile_star2_O2/FreeSlave[P]vhlt/hlrad/vismatrixutil.cpp
// Function: CompressTransferIndicies(unsigned int*, unsigned int, unsigned int*)
static transfer_index_t* CompressTransferIndicies(transfer_raw_index_t* tRaw, const unsigned rawSize, unsigned* iSize)
{
    unsigned        x;
    unsigned        size = rawSize;
    unsigned        compressed_count = 0;

    transfer_raw_index_t* raw = tRaw;
    transfer_raw_index_t* end = tRaw + rawSize - 1;        // -1 since we are comparing current with next and get errors when bumping into the 'end'

#ifdef HLRAD_MORE_PATCHES
    unsigned        compressed_count_1 = 0;

	for (x = 0; x < rawSize; x++)
	{
		x += GetLengthOfRun (tRaw + x, tRaw + rawSize - 1);
		compressed_count_1++;
	}

	if (!compressed_count_1)
	{
		return NULL;
	}

	transfer_index_t* CompressedArray = (transfer_index_t*)AllocBlock(sizeof(transfer_index_t) * compressed_count_1);
#else
    transfer_index_t CompressedArray[MAX_PATCHES];         // somewhat big stack object (1 Mb with 256k patches)
#endif
    transfer_index_t* compressed = CompressedArray;

    for (x = 0; x < size; x++, raw++, compressed++)
    {
        compressed->index = (*raw);
        compressed->size = GetLengthOfRun(raw, end);       // Zero based (count 0 still implies 1 item in the list, so 256 max entries result)
        raw += compressed->size;
        x += compressed->size;
        compressed_count++;                                // number of entries in compressed table
    }

    *iSize = compressed_count;

#ifdef HLRAD_MORE_PATCHES
	if (compressed_count != compressed_count_1)
	{
		Error ("CompressTransferIndicies: internal error");
	}

	ThreadLock();
	g_transfer_index_bytes += sizeof(transfer_index_t) * compressed_count;
	ThreadUnlock();

	return CompressedArray;
#else
    if (compressed_count)
    {
        unsigned        compressed_array_size = sizeof(transfer_index_t) * compressed_count;
        transfer_index_t* rval = (transfer_index_t*)AllocBlock(compressed_array_size);

        ThreadLock();
        g_transfer_index_bytes += compressed_array_size;
        ThreadUnlock();

        memcpy(rval, CompressedArray, compressed_array_size);
        return rval;
    }
    else
    {
        return NULL;
    }
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r15
movl %esi, %ebp
movq %rdi, %r14
movl %esi, %eax
leaq (%rdi,%rax,4), %r12
addq $-0x4, %r12
xorl %ebx, %ebx
xorl %r13d, %r13d
cmpl %ebp, %ebx
jae 0x3123c
movl %ebx, %eax
leaq (%r14,%rax,4), %rdi
movq %r12, %rsi
callq 0x31c27
incl %r13d
addl %eax, %ebx
incl %ebx
jmp 0x31221
testl %r13d, %r13d
je 0x312fd
movl %ebp, 0xc(%rsp)
movq $0x0, (%rsp)
movq %r12, 0x20(%rsp)
movq %r15, 0x18(%rsp)
movl %r13d, %edi
shlq $0x2, %rdi
callq 0x477c
movq %rax, %rbx
xorl %r12d, %r12d
movq %rax, 0x10(%rsp)
movl $0xfff, %ebp            # imm = 0xFFF
cmpl 0xc(%rsp), %r12d
jae 0x312c1
movl (%r14), %r15d
shll $0xc, %r15d
movl (%rbx), %eax
andl %ebp, %eax
orl %r15d, %eax
movl %eax, (%rbx)
movq %r14, %rdi
movq 0x20(%rsp), %rsi
callq 0x31c27
andl %ebp, %eax
orl %eax, %r15d
movl %r15d, (%rbx)
movq (%rsp), %rcx
incl %ecx
movq %rcx, (%rsp)
addl %eax, %r12d
incl %r12d
leaq (%r14,%rax,4), %r14
addq $0x4, %r14
addq $0x4, %rbx
jmp 0x31277
movq 0x18(%rsp), %rax
movq (%rsp), %rbp
movl %ebp, (%rax)
cmpl %ebp, %r13d
je 0x312df
leaq 0x8dad(%rip), %rdi      # 0x3a085
xorl %eax, %eax
callq 0x8b2f
callq 0x9e0c
movl %ebp, %eax
shlq $0x2, %rax
addq %rax, 0x66c5867(%rip)   # 0x66f6b58
callq 0x9e1d
movq 0x10(%rsp), %rax
jmp 0x312ff
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq