// File: /workspace/github_cpp_makefile_star2_O2/NichtJens[P]lmf2txt/LMF_IO.cpp
// Function: LMF_IO::WriteTDCData(unsigned long long, unsigned int*, double*)
void LMF_IO::WriteTDCData(unsigned __int64 timestamp, unsigned __int32 cnt[], double * d64TDC)
/////////////////////////////////////////////////////////////////
{
	unsigned __int16 dummy_uint16;
	double dummy_double;
	__int32 dummy_int32;

	if (!output_lmf || !OutputFileIsOpen) {
		errorflag = 10;
		return;
	}
	
	++uint64_number_of_written_events;

	WriteEventHeader(timestamp,cnt);

	__int32 i,j;
	__int32 ii = 0;
	if (DAQ_ID_output != DAQ_ID_SIMPLE) {
		for (i=0;i<number_of_channels_output;++i) {
			__int32 hits = cnt[i];
			if (hits > max_number_of_hits_output) hits = max_number_of_hits_output;
			if (data_format_in_userheader_output == 2) {
				dummy_uint16 = (unsigned __int16)(hits);
				*output_lmf << dummy_uint16;
				for (j=0;j<hits;++j) {
					dummy_uint16 = (unsigned __int16)(d64TDC[i*num_ions+j]+1.e-6);
					*output_lmf << dummy_uint16;
				}
				dummy_uint16 = 0;
				for (j=hits;j<max_number_of_hits_output;++j) *output_lmf << dummy_uint16;
			}
			if (data_format_in_userheader_output == 5) {
				dummy_double = double(hits);
				*output_lmf << dummy_double;
				for (j=0;j<hits;++j) 	*output_lmf << d64TDC[i*num_ions+j];
				dummy_double = 0.;
				for (j=hits;j<max_number_of_hits_output;++j) *output_lmf << dummy_double;
			}
			if (data_format_in_userheader_output == 10) {
				dummy_int32 = __int32(hits);
				*output_lmf << dummy_int32;
				for (j=0;j<hits;++j) {
					if (d64TDC[i*num_ions+j] >= 0.) ii =__int32(d64TDC[i*num_ions+j]+1.e-6);
					if (d64TDC[i*num_ions+j] <  0.) ii =__int32(d64TDC[i*num_ions+j]-1.e-6);
					*output_lmf << ii;
				}
				dummy_int32 = 0;
				for (j=hits;j<max_number_of_hits_output;++j) *output_lmf << dummy_int32;
			}
			if (data_format_in_userheader_output == LM_USERDEF) {
				dummy_uint16 = (unsigned __int16)(hits);
				*output_lmf << dummy_uint16;
				for (j=0;j<hits;++j) {
					if (d64TDC[i*num_ions+j] >= 0.) ii =__int32(d64TDC[i*num_ions+j]+1.e-6);
					if (d64TDC[i*num_ions+j] <  0.) ii =__int32(d64TDC[i*num_ions+j]-1.e-6);
					if (DAQ_ID_output != DAQ_ID_HM1 && DAQ_ID_output != DAQ_ID_TDC8 && DAQ_ID_output != DAQ_ID_2TDC8) *output_lmf << ii;
						else *output_lmf << (unsigned __int16)(ii);
				}
			}
		}
				if (DAQ_ID_output == DAQ_ID_TDC8HP && this->LMF_Version_output >= 9) {
					*output_lmf << ui64LevelInfo;
				}
				if (DAQ_ID_output == DAQ_ID_TDC8HP && this->LMF_Version_output >= 10) {
					*output_lmf << number_of_bytes_in_PostEventData;
					for (__int32 i=0;i<number_of_bytes_in_PostEventData;i++) *output_lmf << ui8_PostEventData[i];
		}
	}
	if (DAQ_ID_output == DAQ_ID_2TDC8 && number_of_channels2_output > 0) {
		for (i=number_of_channels_output;i<number_of_channels_output+number_of_channels2_output;++i) {
			__int32 hits = cnt[i];
			if (hits > max_number_of_hits2_output) hits = max_number_of_hits2_output;
			if (data_format_in_userheader_output == 2) {
				dummy_uint16 = (unsigned __int16)(hits);
				*output_lmf << dummy_uint16;
				for (j=0;j<hits;++j) {
					dummy_uint16 = (unsigned __int16)(d64TDC[i*num_ions+j]+1.e-6);
					*output_lmf << dummy_uint16;
				}
				dummy_uint16 = 0;
				for (j=hits;j<max_number_of_hits2_output;++j) *output_lmf << dummy_uint16;
			}
			if (data_format_in_userheader_output == 5) {
				dummy_double = double(hits);
				*output_lmf << dummy_double;
				for (j=0;j<hits;++j) *output_lmf << d64TDC[i*num_ions+j];
				dummy_double = 0.;
				for (j=hits;j<max_number_of_hits2_output;++j) *output_lmf << dummy_double;
			}
			if (data_format_in_userheader_output == 10) {
				*output_lmf <<__int32(hits);
				for (j=0;j<hits;++j) {
					if (d64TDC[i*num_ions+j] >= 0.) ii =__int32(d64TDC[i*num_ions+j]+1.e-6);
					if (d64TDC[i*num_ions+j] <  0.) ii =__int32(d64TDC[i*num_ions+j]-1.e-6);
					*output_lmf << ii;
				}
				dummy_int32 = 0;
				for (j=hits;j<max_number_of_hits2_output;++j) *output_lmf << dummy_int32;
			}
		}
	}

	if (DAQ_ID_output == DAQ_ID_SIMPLE) {
		unsigned __int32 channel;
		unsigned __int32 i;
		i = 0;
		for (channel=0;channel < (unsigned __int32)number_of_channels_output;++channel) i = i + cnt[channel] + (cnt[channel]>0 ? 1 : 0);
		if (data_format_in_userheader_output == 2)  {
			dummy_uint16 = (unsigned __int16)i;
			*output_lmf << dummy_uint16;
		}
		if (data_format_in_userheader_output == 10) *output_lmf << i;
		if (data_format_in_userheader_output == 2) {
			for (channel=0;channel < (unsigned __int32)number_of_channels_output;++channel) {
				if (cnt[channel]>0) {
					dummy_uint16 = (unsigned __int16)((channel << 8) + cnt[channel]);
					*output_lmf << dummy_uint16;
					for (i=0;i < cnt[channel]; ++i) {
						dummy_uint16 = (unsigned __int16)(d64TDC[channel*num_ions+i]);
						*output_lmf << dummy_uint16;
					}
				}
			}
		}
		if (data_format_in_userheader_output == 10) {
			for (channel=0;channel < (unsigned __int32)number_of_channels_output;++channel) {
				if (cnt[channel]>0) {
					dummy_int32 = __int32((channel << 24) + cnt[channel]);
					*output_lmf << dummy_int32;
					for (i=0;i<cnt[channel];++i) {
						dummy_int32 = __int32(d64TDC[channel*num_ions+i]);
						*output_lmf << dummy_int32;
					}
				}
			}
		}
	} // end if (DAQ_ID_output == DAQ_ID_SIMPLE)

	return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r15
cmpq $0x0, 0x2b20(%rdi)
je 0xcead
cmpb $0x0, 0x2b29(%r15)
je 0xcead
movq %rcx, %rbx
movq %rdx, %r13
incq 0x2b10(%r15)
movq %r15, %rdi
callq 0xc364
cmpl $0x65, 0x28c(%r15)
je 0xd190
xorl %edx, %edx
xorl %ebp, %ebp
movq %r13, 0x10(%rsp)
movslq 0x2c4(%r15), %rax
cmpq %rax, %rdx
jge 0xcec7
movl (%r13,%rdx,4), %ecx
movl 0x294(%r15), %eax
movl 0x2cc(%r15), %esi
cmpl %esi, %ecx
cmovll %ecx, %esi
movq %rsi, (%rsp)
movslq %edx, %r13
cmpl $0x2, %eax
movq %rdx, 0x8(%rsp)
jne 0xccb9
movq 0x2b20(%r15), %rdi
movq (%rsp), %r12
movzwl %r12w, %esi
callq 0x10df4
testl %r12d, %r12d
movl $0x0, %r14d
cmovgl %r12d, %r14d
xorl %r12d, %r12d
cmpl %r12d, %r14d
je 0xcc8a
movslq 0x2b74(%r15), %rax
imulq %r13, %rax
addq %r12, %rax
movsd (%rbx,%rax,8), %xmm0
addsd 0x542e(%rip), %xmm0     # 0x120a0
cvttsd2si %xmm0, %eax
movq 0x2b20(%r15), %rdi
movzwl %ax, %esi
callq 0x10df4
incq %r12
jmp 0xcc52
movq (%rsp), %rax
movl %eax, %r14d
cmpl 0x2cc(%r15), %r14d
jge 0xccad
movq 0x2b20(%r15), %rdi
xorl %esi, %esi
callq 0x10df4
incl %r14d
jmp 0xcc91
movl 0x294(%r15), %eax
movq 0x8(%rsp), %rdx
cmpl $0x5, %eax
jne 0xcd3d
movq (%rsp), %r12
cvtsi2sd %r12d, %xmm0
movq 0x2b20(%r15), %rdi
callq 0x11072
testl %r12d, %r12d
movl $0x0, %r14d
cmovgl %r12d, %r14d
xorl %r12d, %r12d
cmpl %r12d, %r14d
je 0xcd0c
movq 0x2b20(%r15), %rdi
movslq 0x2b74(%r15), %rax
imulq %r13, %rax
addq %r12, %rax
movsd (%rbx,%rax,8), %xmm0
callq 0x11072
incq %r12
jmp 0xcce3
movq (%rsp), %rax
movl %eax, %r14d
cmpl 0x2cc(%r15), %r14d
jge 0xcd31
movq 0x2b20(%r15), %rdi
xorpd %xmm0, %xmm0
callq 0x11072
incl %r14d
jmp 0xcd13
movl 0x294(%r15), %eax
movq 0x8(%rsp), %rdx
cmpl $0xa, %eax
jne 0xcdf1
movq 0x2b20(%r15), %rdi
movq (%rsp), %r12
movl %r12d, %esi
callq 0x11094
testl %r12d, %r12d
movl $0x0, %r14d
cmovgl %r12d, %r14d
xorl %r12d, %r12d
cmpl %r12d, %r14d
je 0xcdc2
movslq 0x2b74(%r15), %rax
imulq %r13, %rax
addq %r12, %rax
movsd (%rbx,%rax,8), %xmm0
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
movapd %xmm0, %xmm1
addsd 0x530b(%rip), %xmm1     # 0x120a0
cvttsd2si %xmm1, %eax
cmovbl %ebp, %eax
ucomisd %xmm0, %xmm2
addsd 0x5300(%rip), %xmm0     # 0x120a8
cvttsd2si %xmm0, %ebp
cmovbel %eax, %ebp
movq 0x2b20(%r15), %rdi
movl %ebp, %esi
callq 0x11094
incq %r12
jmp 0xcd69
movq (%rsp), %rax
movl %eax, %r14d
cmpl 0x2cc(%r15), %r14d
jge 0xcde5
movq 0x2b20(%r15), %rdi
xorl %esi, %esi
callq 0x11094
incl %r14d
jmp 0xcdc9
movl 0x294(%r15), %eax
movq 0x8(%rsp), %rdx
cmpl $-0x1, %eax
jne 0xcea0
movq 0x2b20(%r15), %rdi
movq (%rsp), %r12
movzwl %r12w, %esi
callq 0x10df4
testl %r12d, %r12d
movl $0x0, %eax
cmovlel %eax, %r12d
xorl %r14d, %r14d
cmpl %r14d, %r12d
je 0xce9b
movslq 0x2b74(%r15), %rax
imulq %r13, %rax
addq %r14, %rax
movsd (%rbx,%rax,8), %xmm0
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
movapd %xmm0, %xmm1
addsd 0x5257(%rip), %xmm1     # 0x120a0
cvttsd2si %xmm1, %eax
cmovbl %ebp, %eax
ucomisd %xmm0, %xmm2
addsd 0x524c(%rip), %xmm0     # 0x120a8
cvttsd2si %xmm0, %ebp
cmovbel %eax, %ebp
movl 0x28c(%r15), %eax
cmpl $0x5, %eax
ja 0xce88
pushq $0x26
popq %rcx
btl %eax, %ecx
jae 0xce88
movq 0x2b20(%r15), %rdi
movzwl %bp, %esi
callq 0x10df4
jmp 0xce96
movq 0x2b20(%r15), %rdi
movl %ebp, %esi
callq 0x11094
incq %r14
jmp 0xce1d
movq 0x8(%rsp), %rdx
incq %rdx
movq 0x10(%rsp), %r13
jmp 0xcbf1
movl $0xa, 0x12e0(%r15)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x28c(%r15), %eax
cmpl $0x8, %eax
sete %dl
movl 0x12a8(%r15), %ecx
cmpl $0x9, %ecx
setge %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0xcf0c
movq 0x2b20(%r15), %rdi
movq 0x2d8(%r15), %rsi
callq 0x10e18
movl 0x28c(%r15), %eax
movl 0x12a8(%r15), %ecx
cmpl $0x8, %eax
jne 0xcf59
cmpl $0xa, %ecx
jl 0xcf59
movq 0x2b20(%r15), %rdi
movl 0x1280(%r15), %esi
callq 0x11094
xorl %r14d, %r14d
movslq 0x1280(%r15), %rax
cmpq %rax, %r14
jge 0xcf52
movq 0x2b20(%r15), %rdi
movzbl 0x2e0(%r15,%r14), %esi
callq 0x10dd0
incq %r14
jmp 0xcf2c
movl 0x28c(%r15), %eax
cmpl $0x5, %eax
jne 0xd187
movl 0x2c8(%r15), %eax
testl %eax, %eax
jle 0xceb8
movl 0x2c4(%r15), %ecx
movslq %ecx, %rdx
addl %ecx, %eax
cltq
cmpq %rax, %rdx
jge 0xd180
movl (%r13,%rdx,4), %ecx
movl 0x294(%r15), %eax
movl 0x2d0(%r15), %r12d
cmpl %r12d, %ecx
cmovll %ecx, %r12d
movslq %edx, %rcx
movq %rcx, (%rsp)
cmpl $0x2, %eax
movq %rdx, 0x8(%rsp)
jne 0xd040
movq 0x2b20(%r15), %rdi
movzwl %r12w, %esi
callq 0x10df4
testl %r12d, %r12d
movl $0x0, %r14d
cmovgl %r12d, %r14d
xorl %r13d, %r13d
cmpl %r13d, %r14d
je 0xd010
movslq 0x2b74(%r15), %rax
imulq (%rsp), %rax
addq %r13, %rax
movsd (%rbx,%rax,8), %xmm0
addsd 0x50a8(%rip), %xmm0     # 0x120a0
cvttsd2si %xmm0, %eax
movq 0x2b20(%r15), %rdi
movzwl %ax, %esi
callq 0x10df4
incq %r13
jmp 0xcfd7
movl %r12d, %r14d
movq 0x10(%rsp), %r13
cmpl 0x2d0(%r15), %r14d
jge 0xd034
movq 0x2b20(%r15), %rdi
xorl %esi, %esi
callq 0x10df4
incl %r14d
jmp 0xd018
movl 0x294(%r15), %eax
movq 0x8(%rsp), %rdx
cmpl $0x5, %eax
jne 0xd0c2
cvtsi2sd %r12d, %xmm0
movq 0x2b20(%r15), %rdi
callq 0x11072
testl %r12d, %r12d
movl $0x0, %r14d
cmovgl %r12d, %r14d
xorl %r13d, %r13d
cmpl %r13d, %r14d
je 0xd090
movq 0x2b20(%r15), %rdi
movslq 0x2b74(%r15), %rax
imulq (%rsp), %rax
addq %r13, %rax
movsd (%rbx,%rax,8), %xmm0
callq 0x11072
incq %r13
jmp 0xd066
movl %r12d, %r14d
movq 0x10(%rsp), %r13
cmpl 0x2d0(%r15), %r14d
jge 0xd0b6
movq 0x2b20(%r15), %rdi
xorpd %xmm0, %xmm0
callq 0x11072
incl %r14d
jmp 0xd098
movl 0x294(%r15), %eax
movq 0x8(%rsp), %rdx
cmpl $0xa, %eax
jne 0xd16a
movq 0x2b20(%r15), %rdi
movl %r12d, %esi
callq 0x11094
testl %r12d, %r12d
movl $0x0, %r14d
cmovgl %r12d, %r14d
xorl %r13d, %r13d
cmpl %r13d, %r14d
je 0xd144
movslq 0x2b74(%r15), %rax
imulq (%rsp), %rax
addq %r13, %rax
movsd (%rbx,%rax,8), %xmm0
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
movapd %xmm0, %xmm1
addsd 0x4f89(%rip), %xmm1     # 0x120a0
cvttsd2si %xmm1, %eax
cmovbl %ebp, %eax
ucomisd %xmm0, %xmm2
addsd 0x4f7e(%rip), %xmm0     # 0x120a8
cvttsd2si %xmm0, %ebp
cmovbel %eax, %ebp
movq 0x2b20(%r15), %rdi
movl %ebp, %esi
callq 0x11094
incq %r13
jmp 0xd0ea
movq 0x10(%rsp), %r13
cmpl 0x2d0(%r15), %r12d
jge 0xd165
movq 0x2b20(%r15), %rdi
xorl %esi, %esi
callq 0x11094
incl %r12d
jmp 0xd149
movq 0x8(%rsp), %rdx
incq %rdx
movl 0x2c4(%r15), %ecx
movl 0x2c8(%r15), %eax
jmp 0xcf7b
movl 0x28c(%r15), %eax
cmpl $0x65, %eax
jne 0xceb8
movl 0x2c4(%r15), %eax
xorl %ecx, %ecx
xorl %ebp, %ebp
cmpq %rcx, %rax
je 0xd1b2
movl (%r13,%rcx,4), %edx
addl %edx, %ebp
cmpl $0x1, %edx
sbbl $-0x1, %ebp
incq %rcx
jmp 0xd19b
movl 0x294(%r15), %eax
cmpl $0x2, %eax
jne 0xd1d4
movq 0x2b20(%r15), %rdi
movzwl %bp, %esi
callq 0x10df4
movl 0x294(%r15), %eax
cmpl $0xa, %eax
jne 0xd1ee
movq 0x2b20(%r15), %rdi
movl %ebp, %esi
callq 0x10d1e
movl 0x294(%r15), %eax
cmpl $0x2, %eax
jne 0xd25c
xorl %r12d, %r12d
movl 0x2c4(%r15), %eax
cmpq %rax, %r12
jae 0xd255
movl (%r13,%r12,4), %eax
testl %eax, %eax
je 0xd250
movl %r12d, %ecx
shll $0x8, %ecx
addl %ecx, %eax
movq 0x2b20(%r15), %rdi
movzwl %ax, %esi
callq 0x10df4
xorl %ebp, %ebp
cmpl (%r13,%r12,4), %ebp
jae 0xd250
movl 0x2b74(%r15), %eax
imull %r12d, %eax
addl %ebp, %eax
cvttsd2si (%rbx,%rax,8), %eax
movq 0x2b20(%r15), %rdi
movzwl %ax, %esi
callq 0x10df4
incl %ebp
jmp 0xd224
incq %r12
jmp 0xd1f6
movl 0x294(%r15), %eax
cmpl $0xa, %eax
jne 0xceb8
xorl %r12d, %r12d
movl 0x2c4(%r15), %eax
cmpq %rax, %r12
jae 0xceb8
movl (%r13,%r12,4), %esi
testl %esi, %esi
je 0xd2c0
movl %r12d, %eax
shll $0x18, %eax
addl %eax, %esi
movq 0x2b20(%r15), %rdi
callq 0x11094
xorl %ebp, %ebp
cmpl (%r13,%r12,4), %ebp
jae 0xd2c0
movl 0x2b74(%r15), %eax
imull %r12d, %eax
addl %ebp, %eax
cvttsd2si (%rbx,%rax,8), %esi
movq 0x2b20(%r15), %rdi
callq 0x11094
incl %ebp
jmp 0xd297
incq %r12
jmp 0xd268
nop