// File: /workspace/github_cpp_makefile_star2_O1/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 0x4ad0(%rip), %rsi      # 0x39983
callq 0x4370
testq %rax, %rax
je 0x350a8
movq %rax, %r14
leaq 0xaf49(%rip), %rdi      # 0x3fe14
movq %rbx, %rsi
xorl %eax, %eax
callq 0x9d2e
leaq 0x20(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x44c0
cmpl $0x1, %eax
jne 0x350e2
movq 0x20(%rsp), %rbp
cmpq %r15, %rbp
jne 0x350e2
testq %rbp, %rbp
setg %r13b
jle 0x350c0
leaq 0x5383ac9(%rip), %rax   # 0x53b89e0
movq (%rax), %r15
leaq 0x38(%r15), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x44c0
cmpl $0x1, %eax
jne 0x350d9
leaq 0x65e0(%rip), %rax      # 0x3b520
movq (%rax), %rax
movq %rax, 0x10(%rsp)
addq $0x170, %r15            # imm = 0x170
incq %rbp
movq %rbx, 0x18(%rsp)
movl -0x138(%r15), %edi
testq %rdi, %rdi
je 0x34fae
shlq $0x3, %rdi
callq 0x492c
movq %rax, -0x130(%r15)
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xa7c9
movq -0x130(%r15), %rdi
movl -0x138(%r15), %edx
movl $0x4, %esi
movq %r14, %rcx
callq 0x44c0
cmpl %eax, -0x138(%r15)
jne 0x350d9
leaq -0x134(%r15), %r12
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x44c0
cmpl $0x1, %eax
jne 0x350d9
movl (%r12), %edi
testq %rdi, %rdi
je 0x35071
leaq 0x54c5a42(%rip), %rax   # 0x54faa29
movzbl (%rax), %r12d
testl %r12d, %r12d
movb %r13b, 0xf(%rsp)
leaq 0x1440a(%rip), %r13     # 0x49404
cmovneq 0x12fa6(%rip), %r13     # 0x47fa8
leaq 0x6527(%rip), %rbx      # 0x3b530
cmovneq 0x12fbf(%rip), %rbx     # 0x47fd0
movl (%r13), %eax
imulq (%rbx,%rax,8), %rdi
addq 0x10(%rsp), %rdi
callq 0x492c
movq %rax, -0x128(%r15,%r12,8)
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xa7c9
movq -0x128(%r15,%r12,8), %rdi
movl (%r13), %eax
movb 0xf(%rsp), %r13b
movq (%rbx,%rax,8), %rsi
movq 0x18(%rsp), %rbx
movl -0x134(%r15), %edx
movq %r14, %rcx
callq 0x44c0
cmpl %eax, -0x134(%r15)
jne 0x350d9
decq %rbp
cmpq $0x2, %rbp
setge %r13b
jl 0x350c0
leaq 0x138(%r15), %r12
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x44c0
movq %r12, %r15
cmpl $0x1, %eax
je 0x34f57
jmp 0x350d9
leaq 0xada8(%rip), %rdi      # 0x3fe57
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0xa38e
jmp 0x35143
movq %r14, %rdi
callq 0x42e0
leaq 0xad62(%rip), %rdi      # 0x3fe31
movq %rbx, %rsi
xorl %eax, %eax
callq 0xa38e
movb $0x1, %bpl
testb $0x1, %r13b
je 0x35143
leaq 0x54038ff(%rip), %r15   # 0x54389e8
cmpl $0x0, (%r15)
je 0x35131
leaq 0x53838ea(%rip), %rax   # 0x53b89e0
movq (%rax), %r12
addq $0x48, %r12
xorl %ebp, %ebp
movq (%r12), %rdi
callq 0x4951
movq -0x8(%r12), %rdi
callq 0x4951
incl %ebp
xorps %xmm0, %xmm0
movups %xmm0, -0x10(%r12)
movq $0x0, (%r12)
addq $0x138, %r12            # imm = 0x138
cmpl (%r15), %ebp
jb 0x350ff
movq %r14, %rdi
callq 0x42e0
movq %rbx, %rdi
callq 0x4160
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq