// File: /workspace/github_cpp_makefile_star2_O3/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
xorl %r12d, %r12d
testl %esi, %esi
je 0x35a89
movl %esi, %ebp
movq %rdi, %r14
movl %esi, %eax
leaq (%rdi,%rax,4), %r13
addq $-0x4, %r13
xorl %eax, %eax
movl %eax, %ecx
leaq (%r14,%rcx,4), %rdi
xorl %ecx, %ecx
cmpq %r13, %rdi
jae 0x359c7
movl (%rdi), %esi
incl %esi
cmpl 0x4(%rdi), %esi
jne 0x359c7
addq $0x4, %rdi
incl %ecx
cmpl $0xfff, %ecx            # imm = 0xFFF
jne 0x359a6
movl $0xfff, %ecx            # imm = 0xFFF
incl %r12d
addl %ecx, %eax
incl %eax
cmpl %ebp, %eax
jb 0x3599e
xorl %ebx, %ebx
testl %r12d, %r12d
je 0x35a89
movq %rdx, (%rsp)
movl %r12d, %edi
shlq $0x2, %rdi
callq 0x491c
movq %rax, %r15
testl %ebp, %ebp
je 0x35a57
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 %r13, %r8
jae 0x35a37
movl (%r8), %r9d
incl %r9d
cmpl 0x4(%r8), %r9d
jne 0x35a37
addq $0x4, %r8
incl %edi
cmpl $0xfff, %edi            # imm = 0xFFF
jne 0x35a13
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 0x35a00
movq (%rsp), %rax
movl %ebx, (%rax)
cmpl %r12d, %ebx
je 0x35a70
leaq 0x96cc(%rip), %rdi      # 0x3f135
xorl %eax, %eax
callq 0x9f34
callq 0xb321
movl %ebx, %eax
shlq $0x2, %rax
addq %rax, 0x66c40c6(%rip)   # 0x66f9b48
callq 0xb332
jmp 0x35a8c
xorl %r15d, %r15d
movq %r15, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq