// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %r8d, %r15d
movq %rcx, %r14
movq %rdx, %r13
movq %rsi, %r12
movq %rdi, %rbx
movb $0x0, (%rdx)
leaq 0x1abc(%rdi), %rbp
cmpl $0x0, 0x1abc(%rdi)
jg 0x8490
cmpl $0xc, 0x12a4(%rbx)
jl 0x8423
movq %rbp, 0x8(%rsp)
movq %r14, 0x10(%rsp)
movq 0x2b18(%rbx), %rdi
leaq 0x54(%rsp), %r14
movq %r14, %rsi
movl $0x4, %edx
callq 0x139ac
movl (%r14), %ebp
movl 0x1ab8(%rbx), %r14d
cmpl %r14d, %ebp
jne 0x8430
movq 0x2b18(%rbx), %rdi
leaq 0x12d8(%rbx), %rsi
movl $0x8, %edx
callq 0x139ac
jmp 0x843a
movq $0x0, 0x12d8(%rbx)
jmp 0x8449
movl $0x2, 0x12e0(%rbx)
cmpl %r14d, %ebp
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %rbp
jne 0x8499
movsd 0x12d8(%rbx), %xmm0
unpcklps 0xcbd8(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xcbe0(%rip), %xmm0     # 0x15040
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0xcbf4(%rip), %xmm1     # 0x15068
movsd %xmm1, 0x12d0(%rbx)
movq 0x2b18(%rbx), %rdi
movq %rbp, %rsi
movl $0x4, %edx
callq 0x139ac
cmpl $0x0, 0x12e0(%rbx)
je 0x84aa
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, 0x10(%rsp)
movl (%rbp), %eax
testl %eax, %eax
jle 0x86d0
leaq 0x1(%r12), %rcx
movq %rcx, 0x28(%rsp)
leaq 0x2(%r12), %rcx
movq %rcx, 0x18(%rsp)
leaq 0x3(%r12), %rcx
movq %rcx, 0x40(%rsp)
leaq 0x4(%r12), %r14
leaq 0x8(%r12), %rcx
movq %rcx, 0x38(%rsp)
decl %eax
movl %eax, 0x1abc(%rbx)
movq 0x2b18(%rbx), %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0x139ac
cmpl $0x0, 0x12e0(%rbx)
jne 0x8499
movq 0x2b18(%rbx), %rdi
movq 0x28(%rsp), %rsi
movl $0x1, %edx
callq 0x139ac
cmpl $0x0, 0x12e0(%rbx)
jne 0x8499
movq 0x2b18(%rbx), %rdi
movq 0x18(%rsp), %rsi
movl $0x1, %edx
callq 0x139ac
cmpl $0x0, 0x12e0(%rbx)
jne 0x8499
movq 0x2b18(%rbx), %rdi
movq 0x40(%rsp), %rsi
movl $0x1, %edx
callq 0x139ac
cmpl $0x0, 0x12e0(%rbx)
jne 0x8499
movq 0x2b18(%rbx), %rdi
movq %r14, %rsi
movl $0x4, %edx
callq 0x139ac
cmpl $0x0, 0x12e0(%rbx)
jne 0x8499
movq 0x2b18(%rbx), %rdi
movq 0x38(%rsp), %rsi
movl $0x8, %edx
callq 0x139ac
cmpl $0x0, 0x12e0(%rbx)
jne 0x8499
movq 0x28(%rsp), %rax
cmpb $0xb, (%rax)
jae 0x86c5
movq 0x18(%rsp), %rax
cmpb $0x1, (%rax)
jne 0x869c
movq %rbp, 0x8(%rsp)
movl (%r14), %ebp
shll $0x2, %ebp
cmpl %r15d, %ebp
cmovgel %r15d, %ebp
movq 0x2b18(%rbx), %rdi
leal (,%rbp,2), %edx
movq 0x10(%rsp), %rsi
callq 0x139ac
cmpl $0x0, 0x12e0(%rbx)
jne 0x8499
movq %r13, 0x30(%rsp)
movl %r15d, 0x24(%rsp)
movl (%r14), %eax
shll $0x2, %eax
cmpl %ebp, %eax
setne %r15b
je 0x8682
movq 0x2b18(%rbx), %rdi
leaq 0x48(%rsp), %rsi
movl $0x8, %edx
callq 0x139ac
cmpl $0x0, 0x12e0(%rbx)
jne 0x8679
xorl %r13d, %r13d
incl %r13d
movl (%r14), %eax
shll $0x2, %eax
subl %ebp, %eax
cmpl %eax, %r13d
setb %r15b
jae 0x8682
movq 0x2b18(%rbx), %rdi
leaq 0x48(%rsp), %rsi
movl $0x8, %edx
callq 0x139ac
cmpl $0x0, 0x12e0(%rbx)
je 0x8646
testb %r15b, %r15b
jne 0x8499
sarl $0x2, %ebp
movl %ebp, (%r14)
movl 0x24(%rsp), %r15d
movq 0x30(%rsp), %r13
movq 0x8(%rsp), %rbp
movq 0x18(%rsp), %rax
movzbl (%rax), %eax
leal -0x80(%rax), %ecx
cmpl $0x11, %ecx
ja 0x86b1
movl $0x30001, %edx          # imm = 0x30001
btl %ecx, %edx
jb 0x86bb
cmpl $0x8, %eax
je 0x86bb
cmpl $0x1, %eax
jne 0x86c5
cmpl $0x0, (%rbp)
jg 0x879e
movl (%rbp), %eax
testl %eax, %eax
jg 0x84e7
movb $0x1, (%r13)
movl $0x0, 0x1280(%rbx)
cmpb $0x1, 0x1a8c(%rbx)
jne 0x8797
leaq 0x1280(%rbx), %r14
movq 0x2b18(%rbx), %rdi
movq %r14, %rsi
movl $0x4, %edx
callq 0x139ac
cmpl $0xbc614e, 0x1280(%rbx) # imm = 0xBC614E
jne 0x874c
movq 0x2b18(%rbx), %r12
movq (%r12), %rdi
movq 0x20(%r12), %r15
addq $-0x4, %r15
movq %r15, %rsi
xorl %edx, %edx
callq 0x20c0
testl %eax, %eax
je 0x8740
movl $0x1, 0x8(%r12)
jmp 0x8745
movq %r15, 0x20(%r12)
movl $0x0, (%r14)
movl (%r14), %eax
cmpl $0xfa1, %eax            # imm = 0xFA1
jge 0x87a5
testl %eax, %eax
js 0x87a5
cmpl $0x0, (%r14)
jle 0x8797
xorl %r15d, %r15d
leaq 0x7(%rsp), %r14
movq 0x2b18(%rbx), %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0x139ac
movb 0x7(%rsp), %al
movb %al, 0x2e0(%rbx,%r15)
incq %r15
movslq 0x1280(%rbx), %rax
cmpq %rax, %r15
jl 0x8768
incq 0x260(%rbx)
movb $0x1, %al
jmp 0x849b
movq 0x2b18(%rbx), %rax
movl $0x15, 0x8(%rax)
jmp 0x8499