// File: /workspace/github_cpp_makefile_star2_O1/NichtJens[P]lmf2txt/LMF_IO.cpp
// Function: LMF_IO::decompress_asynchronous_fADC8_signal(fADC8_signal_info_struct&, unsigned int*, unsigned int, short*, int, int&)
bool LMF_IO::decompress_asynchronous_fADC8_signal(fADC8_signal_info_struct &signal_info, unsigned __int32 source_buffer[], unsigned __int32 source_buffer_size_in_32bit_words, __int16 i16bit_target_buffer[], __int32 target_buffer_size_in_16bit_words, __int32 &number_of_filled_16bit_words)
{
	number_of_filled_16bit_words = -signal_info.timestamp_subindex - 1;

	if ((signal_info.signal_type == FADC8_HEADER_EVENT_ID_TDC) || (signal_info.signal_type == FADC8_HEADER_EVENT_ID_END_MARKER)) {
		number_of_filled_16bit_words = 0;
		return false;
	}

	unsigned __int32 limit_;
	if (signal_info.signallength_including_header_in_32bit_words - 4 < __int32(source_buffer_size_in_32bit_words)) {
		limit_ = signal_info.signallength_including_header_in_32bit_words - 4;
	} else {
		limit_ = source_buffer_size_in_32bit_words;
	}

	if (   (signal_info.signal_type == FADC8_HEADER_EVENT_ID_1_25G_ADC1)
		|| (signal_info.signal_type == FADC8_HEADER_EVENT_ID_1_25G_ADC2)
		|| (signal_info.signal_type == FADC8_HEADER_EVENT_ID_1_25G_ADC3)
		|| (signal_info.signal_type == FADC8_HEADER_EVENT_ID_1_25G_ADC4)) {

		for (unsigned __int32 i=4;i<limit_ + 4;i+=4)  {
			if (target_buffer_size_in_16bit_words < number_of_filled_16bit_words +1 + 4*3) {
				break;
			}
			for (__int32 j=0; j<4; j++) {
				i16bit_target_buffer[(++number_of_filled_16bit_words) > 0 ? number_of_filled_16bit_words : 0] = (source_buffer[i+j] >> 20) & 0x3ff;
				i16bit_target_buffer[(++number_of_filled_16bit_words) > 0 ? number_of_filled_16bit_words : 0] = (source_buffer[i+j] >> 10) & 0x3ff;
				i16bit_target_buffer[(++number_of_filled_16bit_words) > 0 ? number_of_filled_16bit_words : 0] = (source_buffer[i+j]      ) & 0x3ff;
			}
		}

		number_of_filled_16bit_words++;
		//trace_filter(i16bit_target_buffer,number_of_filled_16bit_words, signal_info);
		return true;
	} // endif 1.25 GS mode


	if (   (signal_info.signal_type == FADC8_HEADER_EVENT_ID_2_5G_ADC12)
		|| (signal_info.signal_type == FADC8_HEADER_EVENT_ID_2_5G_ADC34)) {
	
		for (unsigned __int32 i=4;i<limit_ + 4;i+=8)  {
			if (target_buffer_size_in_16bit_words < number_of_filled_16bit_words +1 + 2*4*3) {
				break;
			}

			for (__int32 j=0; j<4; j++) {
				i16bit_target_buffer[(++number_of_filled_16bit_words) > 0 ? number_of_filled_16bit_words : 0] = (source_buffer[i+0+j] >> 20) & 0x3ff;
				i16bit_target_buffer[(++number_of_filled_16bit_words) > 0 ? number_of_filled_16bit_words : 0] = (source_buffer[i+4+j] >> 20) & 0x3ff;
				i16bit_target_buffer[(++number_of_filled_16bit_words) > 0 ? number_of_filled_16bit_words : 0] = (source_buffer[i+0+j] >> 10) & 0x3ff;
				i16bit_target_buffer[(++number_of_filled_16bit_words) > 0 ? number_of_filled_16bit_words : 0] = (source_buffer[i+4+j] >> 10) & 0x3ff;
				i16bit_target_buffer[(++number_of_filled_16bit_words) > 0 ? number_of_filled_16bit_words : 0] = (source_buffer[i+0+j]      ) & 0x3ff;
				i16bit_target_buffer[(++number_of_filled_16bit_words) > 0 ? number_of_filled_16bit_words : 0] = (source_buffer[i+4+j]      ) & 0x3ff;
			}
			
		}

		number_of_filled_16bit_words++;
		//trace_filter(i16bit_target_buffer,number_of_filled_16bit_words, signal_info);
		return true;
	} // endif 2.5 Gs mode

	if (signal_info.signal_type == FADC8_HEADER_EVENT_ID_5G_ADC1234) {

		for (unsigned __int32 i=4;i<limit_ + 4;i+=16)  {
			if (target_buffer_size_in_16bit_words < number_of_filled_16bit_words +1 + 4*4*3) {
				break;
			}

			for (__int32 j=0; j<4; j++) {
				for (__int32 shift_ = 20;shift_ >= 0;shift_-=10) {
					i16bit_target_buffer[(++number_of_filled_16bit_words) > 0 ? number_of_filled_16bit_words : 0] = (source_buffer[i+0 +j] >> shift_) & 0x3ff;
					i16bit_target_buffer[(++number_of_filled_16bit_words) > 0 ? number_of_filled_16bit_words : 0] = (source_buffer[i+8 +j] >> shift_) & 0x3ff;
					i16bit_target_buffer[(++number_of_filled_16bit_words) > 0 ? number_of_filled_16bit_words : 0] = (source_buffer[i+4 +j] >> shift_) & 0x3ff;
					i16bit_target_buffer[(++number_of_filled_16bit_words) > 0 ? number_of_filled_16bit_words : 0] = (source_buffer[i+12+j] >> shift_) & 0x3ff;
				}
			}

		}

		number_of_filled_16bit_words++;
		//trace_filter(i16bit_target_buffer,number_of_filled_16bit_words, signal_info);
		return true;
	} // endif 5 Gs mode

	return false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq 0x38(%rsp), %rdi
movsbl 0x2c(%rsi), %eax
notl %eax
movl %eax, (%rdi)
movzbl 0x18(%rsi), %r11d
xorl %r10d, %r10d
movl $0x0, %eax
cmpl $0x8, %r11d
je 0x1360a
cmpl $0xf, %r11d
je 0x1360a
movl 0x28(%rsi), %esi
leal -0x4(%rsi), %r10d
leal 0x4(%rcx), %eax
cmpl %ecx, %r10d
cmovll %esi, %eax
cmpl $0x4, %r11d
jae 0x133c4
cmpl $0x5, %eax
jb 0x13602
movl (%rdi), %ecx
movl $0x4, %esi
movl $0x3ff, %r10d           # imm = 0x3FF
xorl %r11d, %r11d
leal 0xd(%rcx), %ebx
cmpl %r9d, %ebx
jg 0x13602
movl %esi, %ebx
leaq (%rdx,%rbx,4), %r14
movl %ecx, %r15d
movq $-0xc, %rbx
leal (%r15,%rbx), %ebp
addl $0xc, %ebp
movl (%r14), %r12d
shrl $0x14, %r12d
andl %r10d, %r12d
leal (%r15,%rbx), %r13d
addl $0xd, %r13d
movl %r13d, (%rdi)
testl %ebp, %ebp
cmovsl %r11d, %r13d
movw %r12w, (%r8,%r13,2)
movl (%r14), %r12d
shrl $0xa, %r12d
andl %r10d, %r12d
leal (%r15,%rbx), %r13d
addl $0xe, %r13d
movl %r13d, (%rdi)
cmpl $-0x1, %ebp
cmovll %r11d, %r13d
movw %r12w, (%r8,%r13,2)
movzwl (%r14), %r12d
andl %r10d, %r12d
leal (%r15,%rbx), %r13d
addl $0xf, %r13d
movl %r13d, (%rdi)
cmpl $-0x2, %ebp
cmovll %r11d, %r13d
movw %r12w, (%r8,%r13,2)
addq $0x4, %r14
addq $0x3, %rbx
jne 0x1333f
addl $0x4, %esi
addl %ebx, %ecx
addl $0xc, %ecx
cmpl %eax, %esi
jb 0x13323
jmp 0x13602
leal -0x4(%r11), %ecx
cmpl $0x2, %ecx
movl %r9d, -0x4(%rsp)
movl %eax, -0x8(%rsp)
jae 0x13519
cmpl $0x5, %eax
jb 0x13602
movl (%rdi), %ecx
movl $0x4, %esi
movl $0x8, %r10d
xorl %r11d, %r11d
movl $0x3ff, %ebx            # imm = 0x3FF
xorl %ebp, %ebp
leal 0x19(%rcx), %r14d
cmpl %r9d, %r14d
jg 0x13602
movl %r10d, %r10d
movl %r11d, %r11d
movl %ecx, %r15d
movq $-0x18, %r14
movq %rdx, %r12
leal (%r15,%r14), %r13d
addl $0x18, %r13d
movl 0x10(%r12,%r11,4), %eax
shrl $0x14, %eax
andl %ebx, %eax
leal (%r15,%r14), %r9d
addl $0x19, %r9d
movl %r9d, (%rdi)
testl %r13d, %r13d
cmovsl %ebp, %r9d
movw %ax, (%r8,%r9,2)
movl (%r12,%r10,4), %eax
shrl $0x14, %eax
andl %ebx, %eax
leal (%r15,%r14), %r9d
addl $0x1a, %r9d
movl %r9d, (%rdi)
cmpl $-0x1, %r13d
cmovll %ebp, %r9d
movw %ax, (%r8,%r9,2)
movl 0x10(%r12,%r11,4), %eax
shrl $0xa, %eax
andl %ebx, %eax
leal (%r15,%r14), %r9d
addl $0x1b, %r9d
movl %r9d, (%rdi)
cmpl $-0x2, %r13d
cmovll %ebp, %r9d
movw %ax, (%r8,%r9,2)
movl (%r12,%r10,4), %eax
shrl $0xa, %eax
andl %ebx, %eax
leal (%r15,%r14), %r9d
addl $0x1c, %r9d
movl %r9d, (%rdi)
cmpl $-0x3, %r13d
cmovll %ebp, %r9d
movw %ax, (%r8,%r9,2)
movzwl 0x10(%r12,%r11,4), %eax
andl %ebx, %eax
leal (%r15,%r14), %r9d
addl $0x1d, %r9d
movl %r9d, (%rdi)
cmpl $-0x4, %r13d
cmovll %ebp, %r9d
movw %ax, (%r8,%r9,2)
movzwl (%r12,%r10,4), %eax
andl %ebx, %eax
leal (%r15,%r14), %r9d
addl $0x1e, %r9d
movl %r9d, (%rdi)
cmpl $-0x5, %r13d
cmovll %ebp, %r9d
movw %ax, (%r8,%r9,2)
addq $0x4, %r12
addq $0x6, %r14
jne 0x1341a
addl $0x8, %esi
addl $0x8, %r10d
addl $0x8, %r11d
addl %r14d, %ecx
addl $0x18, %ecx
cmpl -0x8(%rsp), %esi
movl -0x4(%rsp), %r9d
jb 0x133fa
jmp 0x13602
cmpl $0x7, %r11d
jne 0x13618
cmpl $0x5, %eax
jb 0x13602
movl (%rdi), %ebp
movl $0x4, %esi
xorl %r10d, %r10d
movl $0x3ff, %r11d           # imm = 0x3FF
leal 0x31(%rbp), %eax
cmpl %r9d, %eax
jg 0x13602
leal 0xc(%rsi), %eax
movl %esi, %ebx
leaq (%rdx,%rbx,4), %r14
addq $0x20, %r14
leaq (%rdx,%rax,4), %r15
xorl %r12d, %r12d
movq %r12, %r13
orq %rbx, %r13
movl $0x1e, %ecx
addl $-0xa, %ecx
movl (%rdx,%r13,4), %eax
shrl %cl, %eax
andl %r11d, %eax
leal 0x1(%rbp), %r9d
movl %r9d, (%rdi)
testl %ebp, %ebp
cmovsl %r10d, %r9d
movw %ax, (%r8,%r9,2)
movl (%r14,%r12,4), %eax
shrl %cl, %eax
andl %r11d, %eax
leal 0x2(%rbp), %r9d
movl %r9d, (%rdi)
cmpl $-0x1, %ebp
cmovll %r10d, %r9d
movw %ax, (%r8,%r9,2)
movl -0x10(%r14,%r12,4), %eax
shrl %cl, %eax
andl %r11d, %eax
leal 0x3(%rbp), %r9d
movl %r9d, (%rdi)
cmpl $-0x2, %ebp
cmovll %r10d, %r9d
movw %ax, (%r8,%r9,2)
movl (%r15,%r12,4), %eax
shrl %cl, %eax
andl %r11d, %eax
cmpl $-0x3, %ebp
leal 0x4(%rbp), %ebp
movl %ebp, (%rdi)
movl $0x0, %r9d
cmovgel %ebp, %r9d
movw %ax, (%r8,%r9,2)
cmpl $0x9, %ecx
ja 0x13567
incq %r12
cmpq $0x4, %r12
jne 0x1355c
addl $0x10, %esi
cmpl -0x8(%rsp), %esi
movl -0x4(%rsp), %r9d
jb 0x1353c
movl (%rdi), %r10d
incl %r10d
movb $0x1, %al
movl %r10d, (%rdi)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0x1360d