// File: /workspace/github_cpp_makefile_star2_O3/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, %rbp
movq %rdx, %r13
movq %rsi, %r12
movq %rdi, %rbx
movb $0x0, (%rdx)
leaq 0x1abc(%rdi), %rax
movq %rax, 0x10(%rsp)
cmpl $0x0, 0x1abc(%rdi)
jg 0x8344
cmpl $0xc, 0x12a4(%rbx)
jl 0x8317
movq 0x2b18(%rbx), %rdi
leaq 0x4c(%rsp), %r14
movq %r14, %rsi
movl $0x4, %edx
callq 0x133bc
movl (%r14), %eax
cmpl 0x1ab8(%rbx), %eax
jne 0x854f
movq 0x2b18(%rbx), %rdi
leaq 0x12d8(%rbx), %rsi
movl $0x8, %edx
callq 0x133bc
movsd 0x12d8(%rbx), %xmm1
unpcklps 0xbd87(%rip), %xmm1     # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0xbd8f(%rip), %xmm1     # 0x14090
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
mulsd 0xbda3(%rip), %xmm0     # 0x140b8
jmp 0x8326
movq $0x0, 0x12d8(%rbx)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x12d0(%rbx)
movq 0x2b18(%rbx), %rdi
movq 0x10(%rsp), %rsi
movl $0x4, %edx
callq 0x133bc
cmpl $0x0, 0x12e0(%rbx)
je 0x835e
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbp, 0x30(%rsp)
movq %r13, 0x20(%rsp)
leaq 0x1(%r12), %r13
leaq 0x2(%r12), %rbp
leaq 0x3(%r12), %rax
movq %rax, 0x40(%rsp)
leaq 0x4(%r12), %r14
leaq 0x8(%r12), %rax
movq %rax, 0x38(%rsp)
movl %r15d, 0x1c(%rsp)
movq %r12, 0x28(%rsp)
movq 0x10(%rsp), %rax
movl (%rax), %eax
testl %eax, %eax
jle 0x855e
decl %eax
movl %eax, 0x1abc(%rbx)
movq 0x2b18(%rbx), %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0x133bc
cmpl $0x0, 0x12e0(%rbx)
jne 0x834d
movq 0x2b18(%rbx), %rdi
movq %r13, %rsi
movl $0x1, %edx
callq 0x133bc
cmpl $0x0, 0x12e0(%rbx)
jne 0x834d
movq 0x2b18(%rbx), %rdi
movq %rbp, %rsi
movl $0x1, %edx
callq 0x133bc
cmpl $0x0, 0x12e0(%rbx)
jne 0x834d
movq 0x2b18(%rbx), %rdi
movq 0x40(%rsp), %rsi
movl $0x1, %edx
callq 0x133bc
cmpl $0x0, 0x12e0(%rbx)
jne 0x834d
movq 0x2b18(%rbx), %rdi
movq %r14, %rsi
movl $0x4, %edx
callq 0x133bc
cmpl $0x0, 0x12e0(%rbx)
jne 0x834d
movq 0x2b18(%rbx), %rdi
movq 0x38(%rsp), %rsi
movl $0x8, %edx
callq 0x133bc
cmpl $0x0, 0x12e0(%rbx)
jne 0x834d
cmpb $0xa, (%r13)
ja 0x8395
movb (%rbp), %al
cmpb $0x1, %al
jne 0x8518
movq %r13, %r12
movl (%r14), %r13d
shll $0x2, %r13d
cmpl %r15d, %r13d
cmovgel %r15d, %r13d
movq 0x2b18(%rbx), %rdi
leal (,%r13,2), %edx
movq 0x30(%rsp), %rsi
callq 0x133bc
cmpl $0x0, 0x12e0(%rbx)
jne 0x834d
movl (%r14), %eax
shll $0x2, %eax
cmpl %r13d, %eax
je 0x8501
xorl %r15d, %r15d
movq 0x2b18(%rbx), %rdi
leaq 0x50(%rsp), %rsi
movl $0x8, %edx
callq 0x133bc
cmpl $0x0, 0x12e0(%rbx)
jne 0x834d
incl %r15d
movl (%r14), %eax
shll $0x2, %eax
subl %r13d, %eax
cmpl %eax, %r15d
jb 0x84cd
sarl $0x2, %r13d
movl %r13d, (%r14)
movb (%rbp), %al
movl 0x1c(%rsp), %r15d
movq %r12, %r13
movq 0x28(%rsp), %r12
movzbl %al, %eax
leal -0x80(%rax), %ecx
cmpl $0x11, %ecx
ja 0x852d
movl $0x30001, %edx          # imm = 0x30001
btl %ecx, %edx
jb 0x853b
cmpl $0x8, %eax
je 0x853b
cmpl $0x1, %eax
jne 0x8395
movq 0x10(%rsp), %rax
movl (%rax), %eax
testl %eax, %eax
jle 0x839c
jmp 0x863e
movl $0x2, 0x12e0(%rbx)
jmp 0x834d
movq 0x20(%rsp), %rax
movb $0x1, (%rax)
movl $0x0, 0x1280(%rbx)
cmpb $0x1, 0x1a8c(%rbx)
jne 0x8637
leaq 0x1280(%rbx), %r14
movq 0x2b18(%rbx), %rdi
movq %r14, %rsi
movl $0x4, %edx
callq 0x133bc
movl 0x1280(%rbx), %eax
cmpl $0xbc614e, %eax         # imm = 0xBC614E
jne 0x85d2
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 0x862b
movl $0x1, 0x8(%r12)
jmp 0x8630
cmpl $0xfa1, %eax            # imm = 0xFA1
jge 0x8618
testl %eax, %eax
js 0x8618
je 0x8637
xorl %r15d, %r15d
leaq 0xf(%rsp), %r14
movq 0x2b18(%rbx), %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0x133bc
movb 0xf(%rsp), %al
movb %al, 0x2e0(%rbx,%r15)
incq %r15
movslq 0x1280(%rbx), %rax
cmpq %rax, %r15
jl 0x85e7
jmp 0x8637
movq 0x2b18(%rbx), %rax
movl $0x15, 0x8(%rax)
jmp 0x834d
movq %r15, 0x20(%r12)
movl $0x0, (%r14)
incq 0x260(%rbx)
movb $0x1, %al
jmp 0x834f
nop