// File: /workspace/github_cpp_makefile_star2_O2/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, %ebp
movq %rcx, %r13
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbx
movb $0x0, (%rdx)
leaq 0x1abc(%rdi), %rax
movq %rax, 0x10(%rsp)
cmpl $0x0, 0x1abc(%rdi)
jg 0x71aa
cmpl $0xc, 0x12a4(%rbx)
jl 0x7185
movq 0x2b18(%rbx), %rdi
leaq 0x4c(%rsp), %r14
movq %r14, %rsi
callq 0x10fca
movl (%r14), %eax
cmpl 0x1ab8(%rbx), %eax
jne 0x7383
movq 0x2b18(%rbx), %rdi
leaq 0x12d8(%rbx), %rsi
callq 0x10d78
movsd 0x12d8(%rbx), %xmm1
unpcklps 0xaee9(%rip), %xmm1     # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0xaef1(%rip), %xmm1     # 0x12060
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
mulsd 0xaf05(%rip), %xmm0     # 0x12088
jmp 0x7191
andq $0x0, 0x12d8(%rbx)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x12d0(%rbx)
movq 0x2b18(%rbx), %rdi
movq 0x10(%rsp), %rsi
callq 0x10fca
cmpl $0x0, 0x12e0(%rbx)
je 0x71c4
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, 0x30(%rsp)
movq %r15, 0x20(%rsp)
leaq 0x1(%r12), %r15
leaq 0x2(%r12), %r13
leaq 0x3(%r12), %rax
movq %rax, 0x40(%rsp)
leaq 0x4(%r12), %r14
leaq 0x8(%r12), %rax
movq %rax, 0x38(%rsp)
movq 0x10(%rsp), %rax
movl (%rax), %ecx
testl %ecx, %ecx
jle 0x7392
decl %ecx
movl %ecx, 0x1abc(%rbx)
movq 0x2b18(%rbx), %rdi
movq %r12, %rsi
callq 0x10d42
cmpl $0x0, 0x12e0(%rbx)
jne 0x71b3
movq 0x2b18(%rbx), %rdi
movq %r15, %rsi
callq 0x10d42
cmpl $0x0, 0x12e0(%rbx)
jne 0x71b3
movq 0x2b18(%rbx), %rdi
movq %r13, %rsi
callq 0x10d42
cmpl $0x0, 0x12e0(%rbx)
jne 0x71b3
movq 0x2b18(%rbx), %rdi
movq 0x40(%rsp), %rsi
callq 0x10d42
cmpl $0x0, 0x12e0(%rbx)
jne 0x71b3
movq 0x2b18(%rbx), %rdi
movq %r14, %rsi
callq 0x10d66
cmpl $0x0, 0x12e0(%rbx)
jne 0x71b3
movq 0x2b18(%rbx), %rdi
movq 0x38(%rsp), %rsi
callq 0x10d78
cmpl $0x0, 0x12e0(%rbx)
jne 0x71b3
cmpb $0xa, (%r15)
ja 0x71f1
movb (%r13), %al
cmpb $0x1, %al
jne 0x734a
movq %r12, 0x28(%rsp)
movq %r15, %r12
movl (%r14), %r15d
shll $0x2, %r15d
cmpl %ebp, %r15d
cmovgel %ebp, %r15d
movq 0x2b18(%rbx), %rdi
leal (%r15,%r15), %edx
movq 0x30(%rsp), %rsi
callq 0x10d8a
cmpl $0x0, 0x12e0(%rbx)
jne 0x71b3
movl %ebp, 0x1c(%rsp)
pushq $-0x1
popq %rbp
movl (%r14), %eax
shll $0x2, %eax
subl %r15d, %eax
incl %ebp
cmpl %eax, %ebp
jae 0x7333
movq 0x2b18(%rbx), %rdi
leaq 0x50(%rsp), %rsi
callq 0x110f6
cmpl $0x0, 0x12e0(%rbx)
je 0x7305
jmp 0x71b3
sarl $0x2, %r15d
movl %r15d, (%r14)
movb (%r13), %al
movl 0x1c(%rsp), %ebp
movq %r12, %r15
movq 0x28(%rsp), %r12
movzbl %al, %eax
leal -0x80(%rax), %ecx
cmpl $0x11, %ecx
ja 0x735f
movl $0x30001, %edx          # imm = 0x30001
btl %ecx, %edx
jb 0x736d
cmpl $0x8, %eax
je 0x736d
cmpl $0x1, %eax
jne 0x71f1
movq 0x10(%rsp), %rax
movl (%rax), %ecx
movb $0x1, %al
testl %ecx, %ecx
jle 0x71f8
jmp 0x71b5
movl $0x2, 0x12e0(%rbx)
jmp 0x71b3
movq 0x20(%rsp), %rax
movb $0x1, (%rax)
andl $0x0, 0x1280(%rbx)
cmpb $0x1, 0x1a8c(%rbx)
jne 0x7429
leaq 0x1280(%rbx), %rsi
movq 0x2b18(%rbx), %rdi
callq 0x10fca
movl 0x1280(%rbx), %eax
cmpl $0xbc614e, %eax         # imm = 0xBC614E
jne 0x73e9
movq 0x2b18(%rbx), %rdi
movq 0x20(%rdi), %rsi
addq $-0x4, %rsi
callq 0x235c
andl $0x0, 0x1280(%rbx)
xorl %eax, %eax
jmp 0x73f4
cmpl $0xfa1, %eax            # imm = 0xFA1
jge 0x7437
testl %eax, %eax
js 0x7437
xorl %r15d, %r15d
leaq 0xf(%rsp), %r14
cltq
cmpq %rax, %r15
jge 0x7429
movq 0x2b18(%rbx), %rdi
movq %r14, %rsi
callq 0x10d42
movb 0xf(%rsp), %al
movb %al, 0x2e0(%rbx,%r15)
incq %r15
movl 0x1280(%rbx), %eax
jmp 0x73fc
incq 0x260(%rbx)
movb $0x1, %al
jmp 0x71b5
movq 0x2b18(%rbx), %rax
movl $0x15, 0x8(%rax)
jmp 0x71b3