// File: 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:
subq $0x58, %rsp
movq %rdi, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movq 0x48(%rsp), %rdi
leaq 0x6149(%rip), %rsi      # 0x6b8d3
callq 0x4390
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
je 0x65a91
movq 0x48(%rsp), %rsi
leaq 0xc5d8(%rip), %rdi      # 0x71d84
movb $0x0, %al
callq 0xb0d0
movq 0x38(%rsp), %rcx
leaq 0x30(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x4500
movl %eax, 0x2c(%rsp)
cmpl $0x1, 0x2c(%rsp)
je 0x657dc
jmp 0x65aae
movq 0x30(%rsp), %rax
cmpq 0x40(%rsp), %rax
je 0x657ed
jmp 0x65aae
movq 0x30(%rsp), %rax
movq %rax, 0x18(%rsp)
leaq 0x5388202(%rip), %rax   # 0x53eda00
movq (%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x18(%rsp), %rax
movq %rax, %rcx
addq $-0x1, %rcx
movq %rcx, 0x18(%rsp)
cmpq $0x0, %rax
jle 0x65a6a
movq 0x20(%rsp), %rdi
addq $0x38, %rdi
movq 0x38(%rsp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x4500
movl %eax, 0x2c(%rsp)
cmpl $0x1, 0x2c(%rsp)
je 0x6584e
jmp 0x65aae
movq 0x20(%rsp), %rax
cmpl $0x0, 0x38(%rax)
je 0x658d0
movq 0x20(%rsp), %rax
movl 0x38(%rax), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x47a0
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x40(%rax)
movq 0x20(%rsp), %rax
cmpq $0x0, 0x40(%rax)
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xbf00
movq 0x20(%rsp), %rax
movq 0x40(%rax), %rdi
movq 0x20(%rsp), %rax
movl 0x38(%rax), %eax
movl %eax, %edx
movq 0x38(%rsp), %rcx
movl $0x4, %esi
callq 0x4500
movl %eax, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movq 0x20(%rsp), %rcx
cmpl 0x38(%rcx), %eax
je 0x658ce
jmp 0x65aae
jmp 0x658d0
movq 0x20(%rsp), %rdi
addq $0x3c, %rdi
movq 0x38(%rsp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x4500
movl %eax, 0x2c(%rsp)
cmpl $0x1, 0x2c(%rsp)
je 0x658fd
jmp 0x65aae
movq 0x20(%rsp), %rax
cmpl $0x0, 0x3c(%rax)
je 0x65a53
leaq 0x54ca136(%rip), %rax   # 0x552fa49
testb $0x1, (%rax)
je 0x659b1
movq 0x20(%rsp), %rax
movl 0x3c(%rax), %eax
movl %eax, %edi
leaq 0x18aeb(%rip), %rax     # 0x7e418
movl (%rax), %eax
movl %eax, %ecx
leaq 0x7db8(%rip), %rax      # 0x6d6f0
imulq (%rax,%rcx,8), %rdi
leaq 0x7d7c(%rip), %rax      # 0x6d6c0
addq (%rax), %rdi
callq 0x47a0
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x50(%rax)
movq 0x20(%rsp), %rax
cmpq $0x0, 0x50(%rax)
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xbf00
movq 0x20(%rsp), %rax
movq 0x50(%rax), %rdi
leaq 0x18a93(%rip), %rax     # 0x7e418
movl (%rax), %eax
movl %eax, %ecx
leaq 0x7d60(%rip), %rax      # 0x6d6f0
movq (%rax,%rcx,8), %rsi
movq 0x20(%rsp), %rax
movl 0x3c(%rax), %eax
movl %eax, %edx
movq 0x38(%rsp), %rcx
callq 0x4500
movl %eax, 0x2c(%rsp)
jmp 0x65a41
movq 0x20(%rsp), %rax
movl 0x3c(%rax), %eax
movl %eax, %edi
leaq 0x18a52(%rip), %rax     # 0x7e414
movl (%rax), %eax
movl %eax, %ecx
leaq 0x7d03(%rip), %rax      # 0x6d6d0
imulq (%rax,%rcx,8), %rdi
leaq 0x7ce7(%rip), %rax      # 0x6d6c0
addq (%rax), %rdi
callq 0x47a0
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x48(%rax)
movq 0x20(%rsp), %rax
cmpq $0x0, 0x48(%rax)
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xbf00
movq 0x20(%rsp), %rax
movq 0x48(%rax), %rdi
leaq 0x189fa(%rip), %rax     # 0x7e414
movl (%rax), %eax
movl %eax, %ecx
leaq 0x7cab(%rip), %rax      # 0x6d6d0
movq (%rax,%rcx,8), %rsi
movq 0x20(%rsp), %rax
movl 0x3c(%rax), %eax
movl %eax, %edx
movq 0x38(%rsp), %rcx
callq 0x4500
movl %eax, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movq 0x20(%rsp), %rcx
cmpl 0x3c(%rcx), %eax
je 0x65a51
jmp 0x65aae
jmp 0x65a53
jmp 0x65a55
movq 0x20(%rsp), %rax
addq $0x138, %rax            # imm = 0x138
movq %rax, 0x20(%rsp)
jmp 0x65806
movq 0x38(%rsp), %rdi
callq 0x4310
movq 0x48(%rsp), %rsi
leaq 0xc321(%rip), %rdi      # 0x71da1
movb $0x0, %al
callq 0xb9f0
movb $0x1, 0x57(%rsp)
jmp 0x65b58
movq 0x48(%rsp), %rsi
leaq 0xc32a(%rip), %rdi      # 0x71dc7
movb $0x0, %al
callq 0xb9f0
movb $0x0, 0x57(%rsp)
jmp 0x65b58
leaq 0x5387f4b(%rip), %rax   # 0x53eda00
movq (%rax), %rax
movq %rax, 0x8(%rsp)
movl $0x0, 0x14(%rsp)
movl 0x14(%rsp), %eax
leaq 0x5407f38(%rip), %rcx   # 0x546da08
cmpl (%rcx), %eax
jae 0x65b3f
movq 0x8(%rsp), %rax
movq 0x48(%rax), %rdi
callq 0x47d0
movq 0x8(%rsp), %rax
movq 0x40(%rax), %rdi
callq 0x47d0
movq 0x8(%rsp), %rax
movl $0x0, 0x3c(%rax)
movq 0x8(%rsp), %rax
movl $0x0, 0x38(%rax)
movq 0x8(%rsp), %rax
movq $0x0, 0x48(%rax)
movq 0x8(%rsp), %rax
movq $0x0, 0x40(%rax)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
movq 0x8(%rsp), %rax
addq $0x138, %rax            # imm = 0x138
movq %rax, 0x8(%rsp)
jmp 0x65ac5
movq 0x38(%rsp), %rdi
callq 0x4310
movq 0x48(%rsp), %rdi
callq 0x4180
movb $0x0, 0x57(%rsp)
movb 0x57(%rsp), %al
andb $0x1, %al
addq $0x58, %rsp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)