// File: /workspace/github_cpp_makefile_star2_O3/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), %r10d
notl %r10d
movl %r10d, (%rdi)
movzbl 0x18(%rsi), %r11d
xorl %ebx, %ebx
movl $0x0, %eax
cmpl $0x8, %r11d
je 0x12fc3
cmpl $0xf, %r11d
je 0x12fc3
movl 0x28(%rsi), %esi
leal -0x4(%rsi), %ebx
leal 0x4(%rcx), %eax
cmpl %ecx, %ebx
cmovll %esi, %eax
cmpl $0x4, %r11d
jae 0x12d67
cmpl $0x5, %eax
jb 0x12fbb
movl $0x4, %ecx
movl $0x3ff, %r11d           # imm = 0x3FF
xorl %ebx, %ebx
movl %r10d, %esi
leal 0xd(%rsi), %r10d
cmpl %r9d, %r10d
jg 0x12faf
movl %ecx, %r10d
leaq (%rdx,%r10,4), %r10
movl %esi, %r15d
movq $-0xc, %r14
leal (%r15,%r14), %ebp
addl $0xc, %ebp
movl (%r10), %r12d
shrl $0x14, %r12d
andl %r11d, %r12d
leal (%r15,%r14), %r13d
addl $0xd, %r13d
movl %r13d, (%rdi)
testl %ebp, %ebp
cmovsl %ebx, %r13d
movw %r12w, (%r8,%r13,2)
movl (%r10), %r12d
shrl $0xa, %r12d
andl %r11d, %r12d
leal (%r15,%r14), %r13d
addl $0xe, %r13d
movl %r13d, (%rdi)
cmpl $-0x1, %ebp
cmovll %ebx, %r13d
movw %r12w, (%r8,%r13,2)
movzwl (%r10), %r12d
andl %r11d, %r12d
leal (%r15,%r14), %r13d
addl $0xf, %r13d
movl %r13d, (%rdi)
cmpl $-0x2, %ebp
cmovll %ebx, %r13d
movw %r12w, (%r8,%r13,2)
addq $0x4, %r10
addq $0x3, %r14
jne 0x12cd7
addl $0x4, %ecx
leal (%rsi,%r14), %r10d
addl $0xc, %r10d
cmpl %eax, %ecx
jb 0x12cb6
leal (%rsi,%r14), %r10d
addl $0xc, %r10d
jmp 0x12fbb
leal -0x4(%r11), %ecx
cmpl $0x2, %ecx
movl %r9d, -0x4(%rsp)
movl %eax, -0x8(%rsp)
jae 0x12ec4
cmpl $0x5, %eax
jb 0x12fbb
movl $0x4, %ecx
movl $0x8, %esi
xorl %r11d, %r11d
movl $0x3ff, %ebx            # imm = 0x3FF
xorl %ebp, %ebp
movl %r10d, %r14d
leal 0x19(%r14), %r10d
cmpl %r9d, %r10d
jg 0x12fb8
movl %esi, %esi
movl %r11d, %r11d
movl %r14d, %r10d
movq $-0x18, %r15
movq %rdx, %r12
leal (%r10,%r15), %r13d
addl $0x18, %r13d
movl 0x10(%r12,%r11,4), %eax
shrl $0x14, %eax
andl %ebx, %eax
leal (%r10,%r15), %r9d
addl $0x19, %r9d
movl %r9d, (%rdi)
testl %r13d, %r13d
cmovsl %ebp, %r9d
movw %ax, (%r8,%r9,2)
movl (%r12,%rsi,4), %eax
shrl $0x14, %eax
andl %ebx, %eax
leal (%r10,%r15), %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 (%r10,%r15), %r9d
addl $0x1b, %r9d
movl %r9d, (%rdi)
cmpl $-0x2, %r13d
cmovll %ebp, %r9d
movw %ax, (%r8,%r9,2)
movl (%r12,%rsi,4), %eax
shrl $0xa, %eax
andl %ebx, %eax
leal (%r10,%r15), %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 (%r10,%r15), %r9d
addl $0x1d, %r9d
movl %r9d, (%rdi)
cmpl $-0x4, %r13d
cmovll %ebp, %r9d
movw %ax, (%r8,%r9,2)
movzwl (%r12,%rsi,4), %eax
andl %ebx, %eax
leal (%r10,%r15), %r9d
addl $0x1e, %r9d
movl %r9d, (%rdi)
cmpl $-0x5, %r13d
cmovll %ebp, %r9d
movw %ax, (%r8,%r9,2)
addq $0x4, %r12
addq $0x6, %r15
jne 0x12dbc
addl $0x8, %ecx
addl $0x8, %esi
addl $0x8, %r11d
leal (%r14,%r15), %r10d
addl $0x18, %r10d
cmpl -0x8(%rsp), %ecx
movl -0x4(%rsp), %r9d
jb 0x12d9a
leal (%r14,%r15), %r10d
addl $0x18, %r10d
jmp 0x12fbb
cmpl $0x7, %r11d
jne 0x12fb4
cmpl $0x5, %eax
jb 0x12fbb
movl $0x4, %esi
xorl %r11d, %r11d
movl $0x3ff, %ebx            # imm = 0x3FF
leal 0x31(%r10), %eax
cmpl %r9d, %eax
jg 0x12fbb
leal 0xc(%rsi), %eax
movl %esi, %r14d
leaq (%rdx,%r14,4), %r15
addq $0x20, %r15
leaq (%rdx,%rax,4), %r12
xorl %r13d, %r13d
movq %r13, %rbp
orq %r14, %rbp
movl $0x1e, %ecx
addl $-0xa, %ecx
movl (%rdx,%rbp,4), %eax
shrl %cl, %eax
andl %ebx, %eax
leal 0x1(%r10), %r9d
movl %r9d, (%rdi)
testl %r10d, %r10d
cmovsl %r11d, %r9d
movw %ax, (%r8,%r9,2)
movl (%r15,%r13,4), %eax
shrl %cl, %eax
andl %ebx, %eax
leal 0x2(%r10), %r9d
movl %r9d, (%rdi)
cmpl $-0x1, %r10d
cmovll %r11d, %r9d
movw %ax, (%r8,%r9,2)
movl -0x10(%r15,%r13,4), %eax
shrl %cl, %eax
andl %ebx, %eax
leal 0x3(%r10), %r9d
movl %r9d, (%rdi)
cmpl $-0x2, %r10d
cmovll %r11d, %r9d
movw %ax, (%r8,%r9,2)
movl (%r12,%r13,4), %eax
shrl %cl, %eax
andl %ebx, %eax
cmpl $-0x3, %r10d
leal 0x4(%r10), %r10d
movl %r10d, (%rdi)
movl $0x0, %r9d
cmovgel %r10d, %r9d
movw %ax, (%r8,%r9,2)
cmpl $0x9, %ecx
ja 0x12f11
incq %r13
cmpq $0x4, %r13
jne 0x12f06
addl $0x10, %esi
cmpl -0x8(%rsp), %esi
movl -0x4(%rsp), %r9d
jb 0x12ee4
jmp 0x12fbb
movl %esi, %r10d
jmp 0x12fbb
xorl %eax, %eax
jmp 0x12fc5
movl %r14d, %r10d
incl %r10d
movb $0x1, %al
movl %r10d, %ebx
movl %ebx, (%rdi)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq