// File: /workspace/github_cpp_makefile_star2_O3/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 $0x18, %rsp
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x4b50(%rip), %rsi      # 0x38983
callq 0x4360
testq %rax, %rax
je 0x3404e
movq %rax, %r14
leaq 0xaf49(%rip), %rdi      # 0x3ed94
movq %rbx, (%rsp)
movq %rbx, %rsi
xorl %eax, %eax
callq 0x9c46
leaq 0x10(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x44b0
cmpl $0x1, %eax
jne 0x33fea
cmpq %r15, 0x10(%rsp)
jne 0x33fea
testq %r15, %r15
jle 0x33fcb
leaq 0x5383b4c(%rip), %rax   # 0x53b79e0
movq (%rax), %r12
leaq 0x6662(%rip), %rax      # 0x3a500
movq (%rax), %rax
movq %rax, 0x8(%rsp)
incq %r15
addq $0x38, %r12
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x44b0
cmpl $0x1, %eax
jne 0x33fea
movl (%r12), %edi
testq %rdi, %rdi
je 0x33f15
shlq $0x3, %rdi
callq 0x491c
movq %rax, 0x8(%r12)
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xa6ed
movq 0x8(%r12), %rdi
movl (%r12), %edx
movl $0x4, %esi
movq %r14, %rcx
callq 0x44b0
cmpl %eax, (%r12)
jne 0x33fea
leaq 0x4(%r12), %r13
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r14, %rcx
callq 0x44b0
cmpl $0x1, %eax
jne 0x33fea
movl (%r13), %edi
testq %rdi, %rdi
je 0x33fb7
leaq 0x54c5ae1(%rip), %rax   # 0x54f9a29
movzbl (%rax), %r13d
testl %r13d, %r13d
leaq 0x144ae(%rip), %rbp     # 0x48404
cmovneq 0x1304a(%rip), %rbp     # 0x46fa8
leaq 0x65ab(%rip), %rbx      # 0x3a510
cmovneq 0x1306b(%rip), %rbx     # 0x46fd8
movl (%rbp), %eax
imulq (%rbx,%rax,8), %rdi
addq 0x8(%rsp), %rdi
callq 0x491c
movq %rax, 0x10(%r12,%r13,8)
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xa6ed
movq 0x10(%r12,%r13,8), %rdi
movl (%rbp), %eax
movq (%rbx,%rax,8), %rsi
movl 0x4(%r12), %edx
movq %r14, %rcx
callq 0x44b0
cmpl %eax, 0x4(%r12)
jne 0x33fea
decq %r15
addq $0x138, %r12            # imm = 0x138
cmpq $0x1, %r15
ja 0x33ead
movq %r14, %rdi
callq 0x42d0
leaq 0xadd7(%rip), %rdi      # 0x3edb1
movq (%rsp), %rsi
xorl %eax, %eax
callq 0xa2a6
movb $0x1, %bpl
jmp 0x34061
leaq 0x54039f7(%rip), %r15   # 0x54379e8
cmpl $0x0, (%r15)
je 0x34039
leaq 0x53839e2(%rip), %rax   # 0x53b79e0
movq (%rax), %r12
addq $0x48, %r12
xorl %ebp, %ebp
movq (%r12), %rdi
callq 0x4941
movq -0x8(%r12), %rdi
callq 0x4941
incl %ebp
xorps %xmm0, %xmm0
movups %xmm0, -0x10(%r12)
movq $0x0, (%r12)
addq $0x138, %r12            # imm = 0x138
cmpl (%r15), %ebp
jb 0x34007
movq %r14, %rdi
callq 0x42d0
movq (%rsp), %rdi
callq 0x4160
xorl %ebp, %ebp
jmp 0x34061
leaq 0xad82(%rip), %rdi      # 0x3edd7
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0xa2a6
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop