// File: /workspace/github_cpp_makefile_star2_O2/FreeSlave[P]vhlt/hlrad/transfers.cpp
// Function: readtransfers(char const*, long)
bool            readtransfers(const char* const transferfile, const long numpatches)
{
    FILE*           file;
    long            total_patches;

    file = fopen(transferfile, "rb");
    if (file != NULL)
    {
        unsigned        amtread;
        patch_t*        patch;

        Log("Reading transfers file [%s]\n", transferfile);

        amtread = fread(&total_patches, sizeof(total_patches), 1, file);
        if (amtread != 1)
        {
            goto FailedRead;
        }
        if (total_patches != numpatches)
        {
            goto FailedRead;
        }

        long patchcount = total_patches;
        for (patch = g_patches; patchcount-- > 0; patch++)
        {
            amtread = fread(&patch->iIndex, sizeof(patch->iIndex), 1, file);
            if (amtread != 1)
            {
                goto FailedRead;
            }
            if (patch->iIndex)
            {
                patch->tIndex = (transfer_index_t*)AllocBlock(patch->iIndex * sizeof(transfer_index_t *));
                hlassume(patch->tIndex != NULL, assume_NoMemory);
                amtread = fread(patch->tIndex, sizeof(transfer_index_t), patch->iIndex, file);
                if (amtread != patch->iIndex)
                {
                    goto FailedRead;
                }
            }

            amtread = fread(&patch->iData, sizeof(patch->iData), 1, file);
            if (amtread != 1)
            {
                goto FailedRead;
            }
            if (patch->iData)
            {
#ifdef HLRAD_HULLU
				if(g_rgb_transfers)
				{
	#ifdef HLRAD_TRANSFERDATA_COMPRESS
                    patch->tRGBData = (rgb_transfer_data_t*)AllocBlock(patch->iData * vector_size[g_rgbtransfer_compress_type] + unused_size);
	#else
                    patch->tRGBData = (rgb_transfer_data_t*)AllocBlock(patch->iData * sizeof(rgb_transfer_data_t *)); //wrong? --vluzacn
	#endif
                    hlassume(patch->tRGBData != NULL, assume_NoMemory);
	#ifdef HLRAD_TRANSFERDATA_COMPRESS
                    amtread = fread(patch->tRGBData, vector_size[g_rgbtransfer_compress_type], patch->iData, file);		    
	#else
                    amtread = fread(patch->tRGBData, sizeof(rgb_transfer_data_t), patch->iData, file);		    
	#endif
				}
				else
				{
	#ifdef HLRAD_TRANSFERDATA_COMPRESS
                    patch->tData = (transfer_data_t*)AllocBlock(patch->iData * float_size[g_transfer_compress_type] + unused_size);
	#else
                    patch->tData = (transfer_data_t*)AllocBlock(patch->iData * sizeof(transfer_data_t *));
	#endif
                    hlassume(patch->tData != NULL, assume_NoMemory);
	#ifdef HLRAD_TRANSFERDATA_COMPRESS
                    amtread = fread(patch->tData, float_size[g_transfer_compress_type], patch->iData, file);		    
	#else
                    amtread = fread(patch->tData, sizeof(transfer_data_t), patch->iData, file);		    
	#endif
				}
#else
	#ifdef HLRAD_TRANSFERDATA_COMPRESS
                patch->tData = (transfer_data_t*)AllocBlock(patch->iData * float_size[g_transfer_compress_type] + unused_size);
	#else
                patch->tData = (transfer_data_t*)AllocBlock(patch->iData * sizeof(transfer_data_t *));
	#endif
                hlassume(patch->tData != NULL, assume_NoMemory);
	#ifdef HLRAD_TRANSFERDATA_COMPRESS
                amtread = fread(patch->tData, float_size[g_transfer_compress_type], patch->iData, file);
	#else
                amtread = fread(patch->tData, sizeof(transfer_data_t), patch->iData, file);
	#endif
#endif
                if (amtread != patch->iData)
                {
                    goto FailedRead;
                }
            }
        }

        fclose(file);
        //Warning("Finished reading transfers file [%s] %d\n", transferfile);
        Warning("Finished reading transfers file [%s]\n", transferfile); //--vluzacn
        return true;
    }
    Warning("Failed to open transfers file [%s]\n", transferfile);
    return false;

  FailedRead:
    {
        unsigned        x;
        patch_t*        patch = g_patches;

        for (x = 0; x < g_num_patches; x++, patch++)
        {
            FreeBlock(patch->tData);
            FreeBlock(patch->tIndex);
            patch->iData = 0;
            patch->iIndex = 0;
            patch->tData = NULL;
            patch->tIndex = NULL;
        }
    }
    fclose(file);
    unlink(transferfile);
    return false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x3f0e(%rip), %rsi      # 0x338e3
callq 0x4370
testq %rax, %rax
je 0x2fbc7
movq %rax, %r14
leaq 0xa2ef(%rip), %rdi      # 0x39cdc
xorl %ebp, %ebp
movq %rbx, 0x8(%rsp)
movq %rbx, %rsi
xorl %eax, %eax
callq 0x8790
leaq 0x20(%rsp), %rdi
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x44e0
cmpl $0x1, %eax
jne 0x2fb70
cmpq %r15, 0x20(%rsp)
jne 0x2fb70
leaq 0x5384fc4(%rip), %rax   # 0x53b49f0
movq (%rax), %r12
leaq 0x5a4a(%rip), %rax      # 0x35480
movq (%rax), %rax
movq %rax, 0x10(%rsp)
addq $0x38, %r12
pushq $0x4
popq %r13
movq %r14, 0x18(%rsp)
testq %r15, %r15
jle 0x2fbeb
movq %r12, %rdi
movq %r13, %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x44e0
cmpl $0x1, %eax
jne 0x2fb70
movl (%r12), %edi
testq %rdi, %rdi
je 0x2fab4
shlq $0x3, %rdi
callq 0x477c
movq %rax, 0x8(%r12)
xorl %edi, %edi
testq %rax, %rax
setne %dil
pushq $0x1
popq %rsi
callq 0x92cb
movq 0x8(%r12), %rdi
movl (%r12), %edx
movq %r13, %rsi
movq %r14, %rcx
callq 0x44e0
cmpl %eax, (%r12)
jne 0x2fb70
leaq 0x4(%r12), %rbx
movq %rbx, %rdi
movq %r13, %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x44e0
cmpl $0x1, %eax
jne 0x2fb70
movl (%rbx), %edi
testq %rdi, %rdi
je 0x2fb61
leaq 0x54c6f54(%rip), %rax   # 0x54f6a39
movzbl (%rax), %ebx
testl %ebx, %ebx
leaq 0x15923(%rip), %r13     # 0x45414
cmovneq 0x144af(%rip), %r13     # 0x43fa8
movq %r15, %r14
leaq 0x598d(%rip), %r15      # 0x35490
cmovneq 0x144cd(%rip), %r15     # 0x43fd8
movl (%r13), %eax
imulq (%r15,%rax,8), %rdi
addq 0x10(%rsp), %rdi
callq 0x477c
movq %rax, 0x10(%r12,%rbx,8)
xorl %edi, %edi
testq %rax, %rax
setne %dil
pushq $0x1
popq %rsi
callq 0x92cb
movq 0x10(%r12,%rbx,8), %rdi
movl (%r13), %eax
pushq $0x4
popq %r13
movq (%r15,%rax,8), %rsi
movq %r14, %r15
movq 0x18(%rsp), %r14
movl 0x4(%r12), %edx
movq %r14, %rcx
callq 0x44e0
cmpl %eax, 0x4(%r12)
jne 0x2fb70
addq $0x138, %r12            # imm = 0x138
decq %r15
jmp 0x2fa4b
leaq 0x5384e79(%rip), %rax   # 0x53b49f0
movq (%rax), %rbx
addq $0x48, %rbx
leaq 0x5404e73(%rip), %r15   # 0x54349f8
cmpl (%r15), %ebp
jae 0x2fbb1
movq (%rbx), %rdi
callq 0x479f
movq -0x8(%rbx), %rdi
callq 0x479f
incl %ebp
xorps %xmm0, %xmm0
movups %xmm0, -0x10(%rbx)
andq $0x0, (%rbx)
addq $0x138, %rbx            # imm = 0x138
jmp 0x2fb85
movq %r14, %rdi
callq 0x42d0
movq 0x8(%rsp), %rdi
callq 0x4170
xorl %ebp, %ebp
jmp 0x2fbda
leaq 0xa151(%rip), %rdi      # 0x39d1f
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x8ea5
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x42d0
leaq 0xa0ff(%rip), %rdi      # 0x39cf9
movq 0x8(%rsp), %rsi
xorl %eax, %eax
callq 0x8ea5
movb $0x1, %bpl
jmp 0x2fbda
nop