// File: NichtJens[P]lmf2txt/LMF_IO.cpp
// Function: LMF_IO::ReadNextfADC4packet(ndigo_packet*, bool&, short*, int)
bool LMF_IO::ReadNextfADC4packet(ndigo_packet * packet, bool &bEnd_of_group_detected, __int16 * i16buffer, __int32 buffersize_in_16bit_words)
/////////////////////////////////////////////////////////////////
{
	bEnd_of_group_detected = false;
	if (fADC4.packet_count < 1) {
		if (this->LMF_Version > 11) {
		__int32 event_end_marker;
		*input_lmf >> event_end_marker;
			if (event_end_marker != fADC4.GroupEndMarker) {
			errorflag = 2;
			return false;
		}
		*input_lmf >> ui64_timestamp;
		} else {
			ui64_timestamp = 0;
		}
		DOUBLE_timestamp = ui64_timestamp * 1.e-12; // convert to seconds
		*input_lmf >> fADC4.packet_count;
	}
	
	if (errorflag) return false;

	while (fADC4.packet_count > 0) {
		fADC4.packet_count--;

		*input_lmf >> packet->channel;		if (errorflag) return false;
		*input_lmf >> packet->card;			if (errorflag) return false;
		*input_lmf >> packet->type;			if (errorflag) return false;
		*input_lmf >> packet->flags;		if (errorflag) return false;
		*input_lmf >> packet->length;		if (errorflag) return false;
		*input_lmf >> packet->timestamp;	if (errorflag) return false;
		
		//if (packet->flags) continue;
		
		if (packet->card >10) continue;

		if (packet->type == CRONO_PACKET_TYPE_16_BIT_SIGNED) {
			__int32 length = __int32(packet->length*4) < buffersize_in_16bit_words ? packet->length*4 : buffersize_in_16bit_words;
			input_lmf->read((char*)i16buffer,length * sizeof(__int16));
			if (errorflag) return false;
			for (unsigned __int32 k=0;k<(packet->length*4 - length);k++) {
				__int64 dummy64;
				*input_lmf >> dummy64;
				if (errorflag) return false;
			}
			packet->length = length >> 2;
		}

		if (   packet->type != CRONO_PACKET_TYPE_16_BIT_SIGNED
			&& packet->type != CRONO_PACKET_TYPE_TDC_RISING
			&& packet->type != CRONO_PACKET_TYPE_TDC_FALLING
			&& packet->type != CRONO_PACKET_TYPE_TDC_DATA
			&& packet->type != CRONO_PACKET_TYPE_TIMESTAMP_ONLY) {
					continue;
		}

		if (fADC4.packet_count > 0) return true;
	}

	bEnd_of_group_detected = true;
	number_of_bytes_in_PostEventData = 0;

	if (fADC4.bReadCustomData) {
		*input_lmf >> number_of_bytes_in_PostEventData;
		if (number_of_bytes_in_PostEventData == 12345678) {
			unsigned __int64 pos = input_lmf->tell();
			input_lmf->seek(pos-4);
			number_of_bytes_in_PostEventData = 0;
		}
		if (number_of_bytes_in_PostEventData > MAX_NUMBER_OF_BYTES_IN_POSTEVENTDATA) {
			input_lmf->error = 21;
			return false;
		}
		if (number_of_bytes_in_PostEventData < 0) {
			input_lmf->error = 21;
			return false;
		}
		for (__int32 i=0;i<number_of_bytes_in_PostEventData;i++) {
			unsigned __int8 byte_dummy;
			*input_lmf >> byte_dummy;
			ui8_PostEventData[i] = byte_dummy;
		}
	}

	++uint64_number_of_read_events;

	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rcx
movb $0x0, (%rcx)
cmpl $0x1, 0x1abc(%rax)
jge 0xacb0
movq -0x58(%rbp), %rax
cmpl $0xb, 0x12a4(%rax)
jle 0xac48
movq -0x58(%rbp), %rax
movq 0x2b18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1cf60
movq -0x58(%rbp), %rcx
movl -0x30(%rbp), %eax
cmpl 0x1ab8(%rcx), %eax
je 0xac2f
movq -0x58(%rbp), %rax
movl $0x2, 0x12e0(%rax)
movb $0x0, -0x1(%rbp)
jmp 0xb077
movq -0x58(%rbp), %rsi
movq 0x2b18(%rsi), %rdi
addq $0x12d8, %rsi           # imm = 0x12D8
callq 0x1cbb0
jmp 0xac57
movq -0x58(%rbp), %rax
movq $0x0, 0x12d8(%rax)
movq -0x58(%rbp), %rsi
movq 0x12d8(%rsi), %xmm0
movaps 0x14406(%rip), %xmm1    # 0x1f070
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x1440a(%rip), %xmm1    # 0x1f080
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x143ab(%rip), %xmm1    # 0x1f038
mulsd %xmm1, %xmm0
movsd %xmm0, 0x12d0(%rsi)
movq 0x2b18(%rsi), %rdi
addq $0x1a80, %rsi           # imm = 0x1A80
addq $0x3c, %rsi
callq 0x1cf60
movq -0x58(%rbp), %rax
cmpl $0x0, 0x12e0(%rax)
je 0xacc6
movb $0x0, -0x1(%rbp)
jmp 0xb077
jmp 0xacc8
movq -0x58(%rbp), %rax
cmpl $0x0, 0x1abc(%rax)
jle 0xaf3e
movq -0x58(%rbp), %rax
movl 0x1abc(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x1abc(%rax)
movq 0x2b18(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x1cb20
movq -0x58(%rbp), %rax
cmpl $0x0, 0x12e0(%rax)
je 0xad12
movb $0x0, -0x1(%rbp)
jmp 0xb077
movq -0x58(%rbp), %rax
movq 0x2b18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x1, %rsi
callq 0x1cb20
movq -0x58(%rbp), %rax
cmpl $0x0, 0x12e0(%rax)
je 0xad40
movb $0x0, -0x1(%rbp)
jmp 0xb077
movq -0x58(%rbp), %rax
movq 0x2b18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x2, %rsi
callq 0x1cb20
movq -0x58(%rbp), %rax
cmpl $0x0, 0x12e0(%rax)
je 0xad6e
movb $0x0, -0x1(%rbp)
jmp 0xb077
movq -0x58(%rbp), %rax
movq 0x2b18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x3, %rsi
callq 0x1cb20
movq -0x58(%rbp), %rax
cmpl $0x0, 0x12e0(%rax)
je 0xad9c
movb $0x0, -0x1(%rbp)
jmp 0xb077
movq -0x58(%rbp), %rax
movq 0x2b18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x4, %rsi
callq 0x1cb80
movq -0x58(%rbp), %rax
cmpl $0x0, 0x12e0(%rax)
je 0xadca
movb $0x0, -0x1(%rbp)
jmp 0xb077
movq -0x58(%rbp), %rax
movq 0x2b18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x8, %rsi
callq 0x1cbb0
movq -0x58(%rbp), %rax
cmpl $0x0, 0x12e0(%rax)
je 0xadf8
movb $0x0, -0x1(%rbp)
jmp 0xb077
movq -0x18(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0xa, %eax
jle 0xae0a
jmp 0xacc8
movq -0x18(%rbp), %rax
movzbl 0x2(%rax), %eax
cmpl $0x1, %eax
jne 0xaed7
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
shll $0x2, %eax
cmpl -0x2c(%rbp), %eax
jge 0xae39
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
shll $0x2, %eax
movl %eax, -0x5c(%rbp)
jmp 0xae3f
movl -0x2c(%rbp), %eax
movl %eax, -0x5c(%rbp)
movq -0x58(%rbp), %rax
movl -0x5c(%rbp), %ecx
movl %ecx, -0x34(%rbp)
movq 0x2b18(%rax), %rdi
movq -0x28(%rbp), %rsi
movslq -0x34(%rbp), %rax
shlq %rax
movl %eax, %edx
callq 0x1cbe0
movq -0x58(%rbp), %rax
cmpl $0x0, 0x12e0(%rax)
je 0xae78
movb $0x0, -0x1(%rbp)
jmp 0xb077
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
movq -0x18(%rbp), %rcx
movl 0x4(%rcx), %ecx
shll $0x2, %ecx
subl -0x34(%rbp), %ecx
cmpl %ecx, %eax
jae 0xaeca
movq -0x58(%rbp), %rax
movq 0x2b18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x1d220
movq -0x58(%rbp), %rax
cmpl $0x0, 0x12e0(%rax)
je 0xaebd
movb $0x0, -0x1(%rbp)
jmp 0xb077
jmp 0xaebf
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xae7f
movl -0x34(%rbp), %ecx
sarl $0x2, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
movzbl 0x2(%rax), %eax
cmpl $0x1, %eax
je 0xaf23
movq -0x18(%rbp), %rax
movzbl 0x2(%rax), %eax
cmpl $0x90, %eax
je 0xaf23
movq -0x18(%rbp), %rax
movzbl 0x2(%rax), %eax
cmpl $0x91, %eax
je 0xaf23
movq -0x18(%rbp), %rax
movzbl 0x2(%rax), %eax
cmpl $0x8, %eax
je 0xaf23
movq -0x18(%rbp), %rax
movzbl 0x2(%rax), %eax
cmpl $0x80, %eax
je 0xaf23
jmp 0xacc8
movq -0x58(%rbp), %rax
cmpl $0x0, 0x1abc(%rax)
jle 0xaf39
movb $0x1, -0x1(%rbp)
jmp 0xb077
jmp 0xacc8
movq -0x58(%rbp), %rax
movq -0x20(%rbp), %rcx
movb $0x1, (%rcx)
movl $0x0, 0x1280(%rax)
testb $0x1, 0x1a8c(%rax)
je 0xb05d
movq -0x58(%rbp), %rsi
movq 0x2b18(%rsi), %rdi
addq $0x1280, %rsi           # imm = 0x1280
callq 0x1cf60
movq -0x58(%rbp), %rax
cmpl $0xbc614e, 0x1280(%rax) # imm = 0xBC614E
jne 0xafc4
movq -0x58(%rbp), %rax
movq 0x2b18(%rax), %rdi
callq 0x1d110
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, -0x48(%rbp)
movq 0x2b18(%rax), %rdi
movq -0x48(%rbp), %rsi
subq $0x4, %rsi
callq 0x2440
movq -0x58(%rbp), %rax
movl $0x0, 0x1280(%rax)
movq -0x58(%rbp), %rax
cmpl $0xfa0, 0x1280(%rax)    # imm = 0xFA0
jle 0xafef
movq -0x58(%rbp), %rax
movq 0x2b18(%rax), %rax
movl $0x15, 0x8(%rax)
movb $0x0, -0x1(%rbp)
jmp 0xb077
movq -0x58(%rbp), %rax
cmpl $0x0, 0x1280(%rax)
jge 0xb014
movq -0x58(%rbp), %rax
movq 0x2b18(%rax), %rax
movl $0x15, 0x8(%rax)
movb $0x0, -0x1(%rbp)
jmp 0xb077
movl $0x0, -0x4c(%rbp)
movq -0x58(%rbp), %rcx
movl -0x4c(%rbp), %eax
cmpl 0x1280(%rcx), %eax
jge 0xb05b
movq -0x58(%rbp), %rax
movq 0x2b18(%rax), %rdi
leaq -0x4d(%rbp), %rsi
callq 0x1cb20
movq -0x58(%rbp), %rax
movb -0x4d(%rbp), %dl
movslq -0x4c(%rbp), %rcx
movb %dl, 0x2e0(%rax,%rcx)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0xb01b
jmp 0xb05d
movq -0x58(%rbp), %rax
movq 0x260(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x260(%rax)
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)