// File: FreeSlave[P]vhlt/hlrad/transparency.cpp
// Function: CreateFinalTransparencyArrays(char const*)
void	CreateFinalTransparencyArrays(const char *print_name)
{
	if( s_raw_count == 0 )
	{
		s_raw_list = NULL;
		s_raw_count = s_max_raw_count = 0;
		return;
	}

	//double sized (faster find function for sorted list)
	s_sorted_count = s_raw_count * 2;
	s_sorted_list = (transList_t *)malloc( sizeof(transList_t) * s_sorted_count );
#ifdef HLRAD_HLASSUMENOMEMORY
	hlassume (s_sorted_list != NULL, assume_NoMemory);
#endif
	
	//First half have p1>p2
	for( unsigned int i = 0; i < s_raw_count; i++ )
	{
		s_sorted_list[i].p1 		= s_raw_list[i].p2;
		s_sorted_list[i].p2 		= s_raw_list[i].p1;
		s_sorted_list[i].data_index	= s_raw_list[i].data_index;
	}
	//Second half have p1<p2
	memcpy( &s_sorted_list[s_raw_count], s_raw_list, sizeof(transList_t) * s_raw_count );
	
	//free old array
	free( s_raw_list );
	s_raw_list = NULL;
	s_raw_count = s_max_raw_count = 0;
	
	//need to sorted for fast search function
	qsort( s_sorted_list, s_sorted_count, sizeof(transList_t), SortList );
	
#ifdef ZHLT_64BIT_FIX
	size_t size = s_sorted_count * sizeof(transList_t) + s_max_trans_count * sizeof(vec3_t);
#else
	unsigned size = s_sorted_count * sizeof(transList_t) + s_max_trans_count * sizeof(vec3_t);
#endif
	if ( size > 1024 * 1024 )
        	Log("%-20s: %5.1f megs \n", print_name, (double)size / (1024.0 * 1024.0));
        else if ( size > 1024 )
        	Log("%-20s: %5.1f kilos\n", print_name, (double)size / 1024.0);
        else
        	Log("%-20s: %5.1f bytes\n", print_name, (double)size); //--vluzacn
	Developer (DEVELOPER_LEVEL_MESSAGE, "\ts_trans_count=%d\ts_sorted_count=%d\n", s_trans_count, s_sorted_count); //--vluzacn
        	
#if 0
        int total_1 = 0;
        for(int i = 0; i < s_sorted_count; i++)
        {
        	Log("a: %7i b: %7i di: %10i r: %3.1f g: %3.1f b: %3.1f\n", 
        		s_sorted_list[i].p1, 
        		s_sorted_list[i].p2, 
        		s_sorted_list[i].data_index,
        		s_trans_list[s_sorted_list[i].data_index][0],
        		s_trans_list[s_sorted_list[i].data_index][1],
        		s_trans_list[s_sorted_list[i].data_index][2]
        	);
        	total_1++;
        }
        
        vec3_t rgb;
        int total_2 = 0;
        for(unsigned int next_index = 0, a = 0; a < g_num_patches; a++)
        {
        	for(unsigned int b = 0; b < g_num_patches; b++)
        	{
        		GetTransparency(a, b, rgb, next_index);
        		
        		if(!VectorCompare(rgb,vec3_one))
        		{
        			Log("a: %7i b: %7i ni: %10i r: %3.1f g: %3.1f b: %3.1f\n", 
        				a, 
        				b, 
        				next_index,
        				rgb[0],
        				rgb[1],
        				rgb[2]
        			);
        			total_2++;
        		}
        	}
        }
        
        Log("total1: %i\ntotal2: %i\n",total_1,total_2);
#endif
}

// Assembly:
subq $0x18, %rsp
movq %rdi, 0x10(%rsp)
cmpl $0x0, 0x66c9bd0(%rip)   # 0x672fb10
jne 0x65f66
movq $0x0, 0x66c9bcb(%rip) # 0x672fb18
movl $0x0, 0x66c9bbd(%rip)   # 0x672fb14
movl $0x0, 0x66c9baf(%rip)   # 0x672fb10
jmp 0x661db
movl 0x66c9ba4(%rip), %eax   # 0x672fb10
shll %eax
movl %eax, 0x66c9bac(%rip)   # 0x672fb20
movl 0x66c9ba6(%rip), %eax   # 0x672fb20
imulq $0xc, %rax, %rdi
callq 0x4430
movq %rax, 0x66c9b9e(%rip)   # 0x672fb28
cmpq $0x0, 0x66c9b96(%rip)   # 0x672fb28
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xbf00
movl $0x0, 0xc(%rsp)
movl 0xc(%rsp), %eax
cmpl 0x66c9b59(%rip), %eax   # 0x672fb10
jae 0x66049
movq 0x66c9b54(%rip), %rax   # 0x672fb18
movl 0xc(%rsp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %ecx
movq 0x66c9b4f(%rip), %rax   # 0x672fb28
movl 0xc(%rsp), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movq 0x66c9b2b(%rip), %rax   # 0x672fb18
movl 0xc(%rsp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %ecx
movq 0x66c9b27(%rip), %rax   # 0x672fb28
movl 0xc(%rsp), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movq 0x66c9b02(%rip), %rax   # 0x672fb18
movl 0xc(%rsp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %ecx
movq 0x66c9afd(%rip), %rax   # 0x672fb28
movl 0xc(%rsp), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x65fad
movq 0x66c9ad8(%rip), %rdi   # 0x672fb28
movl 0x66c9aba(%rip), %eax   # 0x672fb10
imulq $0xc, %rax, %rax
addq %rax, %rdi
movq 0x66c9ab4(%rip), %rsi   # 0x672fb18
movl 0x66c9aa6(%rip), %eax   # 0x672fb10
imulq $0xc, %rax, %rdx
callq 0x4280
movq 0x66c9a9e(%rip), %rdi   # 0x672fb18
callq 0x43a0
movq $0x0, 0x66c9a8e(%rip) # 0x672fb18
movl $0x0, 0x66c9a80(%rip)   # 0x672fb14
movl $0x0, 0x66c9a72(%rip)   # 0x672fb10
movq 0x66c9a83(%rip), %rdi   # 0x672fb28
movl 0x66c9a75(%rip), %eax   # 0x672fb20
movl %eax, %esi
movl $0xc, %edx
leaq 0x127(%rip), %rcx       # 0x661e0
callq 0x4630
movl 0x66c9a5c(%rip), %eax   # 0x672fb20
imulq $0xc, %rax, %rax
movl 0x66c9a62(%rip), %ecx   # 0x672fb30
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, (%rsp)
cmpq $0x100000, (%rsp)       # imm = 0x100000
jbe 0x6612e
movq 0x10(%rsp), %rsi
movaps 0xb9a1(%rip), %xmm1     # 0x71a90
movq (%rsp), %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0xb9a0(%rip), %xmm1     # 0x71aa0
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x5749(%rip), %xmm1     # 0x6b860
divsd %xmm1, %xmm0
leaq 0xbd21(%rip), %rdi      # 0x71e43
movb $0x1, %al
callq 0xb0d0
jmp 0x661bc
cmpq $0x400, (%rsp)          # imm = 0x400
jbe 0x66180
movq 0x10(%rsp), %rsi
movaps 0xb94c(%rip), %xmm1     # 0x71a90
movq (%rsp), %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0xb94b(%rip), %xmm1     # 0x71aa0
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0xbc8c(%rip), %xmm1     # 0x71df8
divsd %xmm1, %xmm0
leaq 0xbce0(%rip), %rdi      # 0x71e57
movb $0x1, %al
callq 0xb0d0
jmp 0x661ba
movq 0x10(%rsp), %rsi
movaps 0xb904(%rip), %xmm1     # 0x71a90
movq (%rsp), %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0xb903(%rip), %xmm1     # 0x71aa0
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
leaq 0xbcb8(%rip), %rdi      # 0x71e6b
movb $0x1, %al
callq 0xb0d0
jmp 0x661bc
movl 0x66c9972(%rip), %edx   # 0x672fb34
movl 0x66c9958(%rip), %ecx   # 0x672fb20
movl $0x3, %edi
leaq 0xbcab(%rip), %rsi      # 0x71e7f
movb $0x0, %al
callq 0xbbf0
addq $0x18, %rsp
retq