// File: 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:
subq $0x58, %rsp
movq %rdi, 0x48(%rsp)
movl %esi, 0x44(%rsp)
movq %rdx, 0x38(%rsp)
movl 0x44(%rsp), %eax
movl %eax, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x48(%rsp), %rax
movl 0x44(%rsp), %ecx
shlq $0x2, %rcx
addq %rcx, %rax
addq $-0x4, %rax
movq %rax, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
movl $0x0, 0x34(%rsp)
movl 0x34(%rsp), %eax
cmpl 0x44(%rsp), %eax
jae 0x68408
movq 0x48(%rsp), %rdi
movl 0x34(%rsp), %eax
shlq $0x2, %rax
addq %rax, %rdi
movq 0x48(%rsp), %rsi
movl 0x44(%rsp), %eax
shlq $0x2, %rax
addq %rax, %rsi
addq $-0x4, %rsi
callq 0x69740
addl 0x34(%rsp), %eax
movl %eax, 0x34(%rsp)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x683b5
cmpl $0x0, 0x14(%rsp)
jne 0x6841d
movq $0x0, 0x50(%rsp)
jmp 0x6854c
movl 0x14(%rsp), %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x47a0
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
movq %rax, (%rsp)
movl $0x0, 0x34(%rsp)
movl 0x34(%rsp), %eax
cmpl 0x30(%rsp), %eax
jae 0x684ff
movq 0x20(%rsp), %rax
movl (%rax), %edx
movq (%rsp), %rax
movl (%rax), %ecx
andl $0xfffff, %edx          # imm = 0xFFFFF
shll $0xc, %edx
andl $0xfff, %ecx            # imm = 0xFFF
orl %edx, %ecx
movl %ecx, (%rax)
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x69740
movl %eax, %edx
movq (%rsp), %rax
movl (%rax), %ecx
andl $0xfff, %edx            # imm = 0xFFF
andl $0xfffff000, %ecx       # imm = 0xFFFFF000
orl %edx, %ecx
movl %ecx, (%rax)
movq (%rsp), %rax
movl (%rax), %ecx
andl $0xfff, %ecx            # imm = 0xFFF
movq 0x20(%rsp), %rax
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0x20(%rsp)
movq (%rsp), %rax
movl (%rax), %eax
andl $0xfff, %eax            # imm = 0xFFF
addl 0x34(%rsp), %eax
movl %eax, 0x34(%rsp)
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
movq 0x20(%rsp), %rax
addq $0x4, %rax
movq %rax, 0x20(%rsp)
movq (%rsp), %rax
addq $0x4, %rax
movq %rax, (%rsp)
jmp 0x68442
movl 0x2c(%rsp), %ecx
movq 0x38(%rsp), %rax
movl %ecx, (%rax)
movl 0x2c(%rsp), %eax
cmpl 0x14(%rsp), %eax
je 0x68522
leaq 0x9c02(%rip), %rdi      # 0x7211d
movb $0x0, %al
callq 0xb580
callq 0xd220
movl 0x2c(%rsp), %eax
shlq $0x2, %rax
addq 0x66c7632(%rip), %rax   # 0x672fb68
movq %rax, 0x66c762b(%rip)   # 0x672fb68
callq 0xd240
movq 0x8(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
addq $0x58, %rsp
retq
nopw %cs:(%rax,%rax)