// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %rax
movl %esi, %ebp
movq %rdi, %r14
movl %esi, %eax
leaq (%rdi,%rax,4), %r12
addq $-0x4, %r12
xorl %r13d, %r13d
testl %esi, %esi
je 0x36b18
xorl %eax, %eax
movl %eax, %ecx
leaq (%r14,%rcx,4), %rdi
xorl %ecx, %ecx
cmpq %r12, %rdi
jae 0x36b0d
movl (%rdi), %esi
incl %esi
cmpl 0x4(%rdi), %esi
jne 0x36b0d
addq $0x4, %rdi
incl %ecx
cmpl $0xfff, %ecx            # imm = 0xFFF
jne 0x36aec
movl $0xfff, %ecx            # imm = 0xFFF
incl %r13d
addl %ecx, %eax
incl %eax
cmpl %ebp, %eax
jb 0x36ae4
xorl %ebx, %ebx
testl %r13d, %r13d
je 0x36bcf
movq %rdx, (%rsp)
movl %r13d, %edi
shlq $0x2, %rdi
callq 0x492c
movq %rax, %r15
testl %ebp, %ebp
je 0x36b9d
xorl %eax, %eax
movl $0xfff, %ecx            # imm = 0xFFF
movq %r15, %rdx
xorl %ebx, %ebx
movl (%r14), %esi
shll $0xc, %esi
movl (%rdx), %edi
andl %ecx, %edi
orl %esi, %edi
movl %edi, (%rdx)
movq %r14, %r8
xorl %edi, %edi
cmpq %r12, %r8
jae 0x36b7d
movl (%r8), %r9d
incl %r9d
cmpl 0x4(%r8), %r9d
jne 0x36b7d
addq $0x4, %r8
incl %edi
cmpl $0xfff, %edi            # imm = 0xFFF
jne 0x36b59
movl $0xfff, %edi            # imm = 0xFFF
andl $0xfff, %edi            # imm = 0xFFF
orl %edi, %esi
movl %esi, (%rdx)
incl %ebx
addl %edi, %eax
incl %eax
leaq (%r14,%rdi,4), %r14
addq $0x4, %r14
addq $0x4, %rdx
cmpl %ebp, %eax
jb 0x36b46
movq (%rsp), %rax
movl %ebx, (%rax)
cmpl %r13d, %ebx
je 0x36bb6
leaq 0x9606(%rip), %rdi      # 0x401b5
xorl %eax, %eax
callq 0xa01c
callq 0xb411
movl %ebx, %eax
shlq $0x2, %rax
addq %rax, 0x66c3f80(%rip)   # 0x66fab48
callq 0xb422
jmp 0x36bd2
xorl %r15d, %r15d
movq %r15, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq