// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movq %rdi, %r15
cmpq $0x0, 0x2b20(%rdi)
je 0xf102
cmpb $0x0, 0x2b29(%r15)
je 0xf102
movq %rcx, %r13
incq 0x2b10(%r15)
movq %r15, %rdi
movq %rdx, 0x20(%rsp)
callq 0xe480
xorl %ebp, %ebp
cmpl $0x65, 0x28c(%r15)
movq %r13, 0x18(%rsp)
je 0xf1b8
cmpl $0x0, 0x2c4(%r15)
jle 0xf11c
xorl %ecx, %ecx
xorl %ebp, %ebp
movq 0x20(%rsp), %rax
movslq (%rax,%rcx,4), %rdx
movslq 0x2cc(%r15), %rax
cmpl %eax, %edx
movq %rax, (%rsp)
movl %eax, %r12d
movq %rdx, 0x8(%rsp)
cmovll %edx, %r12d
cmpl $0x2, 0x294(%r15)
movq %rcx, 0x30(%rsp)
movslq %ecx, %r14
movl %r12d, 0x10(%rsp)
jne 0xeebc
movq 0x2b20(%r15), %rdi
movzwl %r12w, %esi
callq 0x139f4
testl %r12d, %r12d
movq %r14, %rbx
movq %r13, %r14
movsd 0x6247(%rip), %xmm1     # 0x15080
jle 0xee89
movq (%rsp), %r12
movq 0x8(%rsp), %rax
cmpq %r12, %rax
cmovlq %rax, %r12
xorl %r13d, %r13d
movslq 0x2b74(%r15), %rax
imulq %rbx, %rax
addq %r13, %rax
movsd (%r14,%rax,8), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movq 0x2b20(%r15), %rdi
movzwl %ax, %esi
callq 0x139f4
movsd 0x61ff(%rip), %xmm1     # 0x15080
incq %r13
cmpl %r13d, %r12d
jne 0xee4e
movl 0x10(%rsp), %r12d
movq %rbx, %rax
movl %r12d, %ebx
cmpl 0x2cc(%r15), %r12d
movq %r14, %r13
movq %rax, %r14
jge 0xeebc
movq 0x2b20(%r15), %rdi
xorl %esi, %esi
callq 0x139f4
incl %ebx
cmpl 0x2cc(%r15), %ebx
jl 0xeea3
cmpl $0x5, 0x294(%r15)
jne 0xef4b
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movq 0x2b20(%r15), %rdi
callq 0x13c3c
testl %r12d, %r12d
jle 0xef1f
movq (%rsp), %rbx
movq 0x8(%rsp), %rax
cmpq %rbx, %rax
cmovlq %rax, %rbx
xorl %r12d, %r12d
movq 0x2b20(%r15), %rdi
movslq 0x2b74(%r15), %rax
imulq %r14, %rax
addq %r12, %rax
movsd (%r13,%rax,8), %xmm0
callq 0x13c3c
incq %r12
cmpl %r12d, %ebx
jne 0xeef6
movl 0x10(%rsp), %r12d
movl %r12d, %ebx
cmpl 0x2cc(%r15), %r12d
jge 0xef4b
movq 0x2b20(%r15), %rdi
xorpd %xmm0, %xmm0
callq 0x13c3c
incl %ebx
cmpl 0x2cc(%r15), %ebx
jl 0xef30
cmpl $0xa, 0x294(%r15)
jne 0xf00b
movq 0x2b20(%r15), %rdi
movl %r12d, %esi
callq 0x13c82
testl %r12d, %r12d
jle 0xefdc
movq (%rsp), %r13
movq 0x8(%rsp), %rax
cmpq %r13, %rax
cmovlq %rax, %r13
xorl %r12d, %r12d
movq 0x18(%rsp), %rbx
movslq 0x2b74(%r15), %rax
imulq %r14, %rax
addq %r12, %rax
movsd (%rbx,%rax,8), %xmm0
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
movapd %xmm0, %xmm1
addsd 0x60d4(%rip), %xmm1     # 0x15080
cvttsd2si %xmm1, %eax
cmovbl %ebp, %eax
ucomisd %xmm0, %xmm2
addsd 0x60c9(%rip), %xmm0     # 0x15088
cvttsd2si %xmm0, %ebp
cmovbel %eax, %ebp
movq 0x2b20(%r15), %rdi
movl %ebp, %esi
callq 0x13c82
incq %r12
cmpl %r12d, %r13d
jne 0xef85
movl 0x10(%rsp), %r12d
movl %r12d, %ebx
cmpl 0x2cc(%r15), %r12d
jge 0xf006
movq 0x2b20(%r15), %rdi
xorl %esi, %esi
callq 0x13c82
incl %ebx
cmpl 0x2cc(%r15), %ebx
jl 0xefed
movq 0x18(%rsp), %r13
cmpl $-0x1, 0x294(%r15)
jne 0xf0e8
movq 0x2b20(%r15), %rdi
movzwl %r12w, %esi
callq 0x139f4
testl %r12d, %r12d
jle 0xf0e8
movq %r14, %rbx
movq (%rsp), %r14
movq 0x8(%rsp), %rax
cmpq %r14, %rax
cmovlq %rax, %r14
xorl %r12d, %r12d
movsd 0x6030(%rip), %xmm2     # 0x15080
xorpd %xmm3, %xmm3
movsd 0x602c(%rip), %xmm4     # 0x15088
movslq 0x2b74(%r15), %rax
imulq %rbx, %rax
addq %r12, %rax
movsd (%r13,%rax,8), %xmm0
ucomisd %xmm3, %xmm0
movapd %xmm0, %xmm1
addsd %xmm2, %xmm1
cvttsd2si %xmm1, %eax
cmovbl %ebp, %eax
ucomisd %xmm0, %xmm3
addsd %xmm4, %xmm0
cvttsd2si %xmm0, %ebp
cmovbel %eax, %ebp
movl 0x28c(%r15), %eax
cmpl $0x5, %eax
ja 0xf0ba
movl $0x26, %ecx
btl %eax, %ecx
jae 0xf0ba
movq 0x2b20(%r15), %rdi
movzwl %bp, %esi
callq 0x139f4
jmp 0xf0c8
movq 0x2b20(%r15), %rdi
movl %ebp, %esi
callq 0x13c82
incq %r12
cmpl %r12d, %r14d
movsd 0x5faa(%rip), %xmm2     # 0x15080
xorpd %xmm3, %xmm3
movsd 0x5fa6(%rip), %xmm4     # 0x15088
jne 0xf05c
movq 0x30(%rsp), %rcx
incq %rcx
movslq 0x2c4(%r15), %rax
cmpq %rax, %rcx
jl 0xeddb
jmp 0xf11e
movl $0xa, 0x12e0(%r15)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
cmpl $0x8, 0x28c(%r15)
jne 0xf145
cmpl $0x9, 0x12a8(%r15)
jl 0xf145
movq 0x2b20(%r15), %rdi
movq 0x2d8(%r15), %rsi
callq 0x13a38
cmpl $0x8, 0x28c(%r15)
jne 0xf1b8
cmpl $0xa, 0x12a8(%r15)
jl 0xf1b8
movq 0x2b20(%r15), %rdi
movl 0x1280(%r15), %esi
callq 0x13c82
cmpl $0x0, 0x1280(%r15)
jle 0xf1b8
xorl %r14d, %r14d
movq 0x2b20(%r15), %rbx
movsbl 0x2e0(%r15,%r14), %edi
movq (%rbx), %rsi
callq 0x2190
movq 0x10(%rbx), %rcx
movq 0x20(%rbx), %rax
incq %rax
movq %rax, 0x20(%rbx)
cmpq %rax, %rcx
jae 0xf1a9
movq %rax, 0x10(%rbx)
incq %r14
movslq 0x1280(%r15), %rax
cmpq %rax, %r14
jl 0xf179
cmpl $0x5, 0x28c(%r15)
jne 0xf452
cmpl $0x0, 0x2c8(%r15)
jle 0xf452
cmpl $0x0, 0x2c8(%r15)
jle 0xf452
movslq 0x2c4(%r15), %rdx
movq 0x20(%rsp), %rax
movslq (%rax,%rdx,4), %rax
movslq 0x2d0(%r15), %r14
cmpl %r14d, %eax
movl %r14d, %r12d
movq %rax, 0x28(%rsp)
cmovll %eax, %r12d
cmpl $0x2, 0x294(%r15)
movq %rdx, 0x30(%rsp)
movslq %edx, %rax
movq %rax, 0x8(%rsp)
movq %r14, (%rsp)
jne 0xf2d1
movq 0x2b20(%r15), %rdi
movzwl %r12w, %esi
callq 0x139f4
testl %r12d, %r12d
movq %r13, %r14
movsd 0x5e3b(%rip), %xmm1     # 0x15080
movq 0x8(%rsp), %rdx
jle 0xf2a5
movq (%rsp), %r13
movq 0x28(%rsp), %rax
cmpq %r13, %rax
cmovlq %rax, %r13
movq %r13, 0x10(%rsp)
xorl %ebx, %ebx
movslq 0x2b74(%r15), %rax
imulq %rdx, %rax
addq %rbx, %rax
movsd (%r14,%rax,8), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movq 0x2b20(%r15), %rdi
movzwl %ax, %esi
movq %rdx, %r13
callq 0x139f4
movq %r13, %rdx
movsd 0x5de4(%rip), %xmm1     # 0x15080
incq %rbx
cmpl %ebx, 0x10(%rsp)
jne 0xf263
movl %r12d, %ebx
cmpl 0x2d0(%r15), %r12d
movq %r14, %r13
movq (%rsp), %r14
jge 0xf2d1
movq 0x2b20(%r15), %rdi
xorl %esi, %esi
callq 0x139f4
incl %ebx
cmpl 0x2d0(%r15), %ebx
jl 0xf2b8
cmpl $0x5, 0x294(%r15)
jne 0xf376
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movq 0x2b20(%r15), %rdi
callq 0x13c3c
testl %r12d, %r12d
movq %r13, %r14
movq 0x8(%rsp), %rdx
jle 0xf348
movq (%rsp), %rbx
movq 0x28(%rsp), %rax
cmpq %rbx, %rax
cmovlq %rax, %rbx
movq %rbx, 0x10(%rsp)
xorl %r13d, %r13d
movq 0x2b20(%r15), %rdi
movslq 0x2b74(%r15), %rax
imulq %rdx, %rax
addq %r13, %rax
movsd (%r14,%rax,8), %xmm0
movq %rdx, %rbx
callq 0x13c3c
movq %rbx, %rdx
incq %r13
cmpl %r13d, 0x10(%rsp)
jne 0xf318
movl %r12d, %ebx
cmpl 0x2d0(%r15), %r12d
movq %r14, %r13
movq (%rsp), %r14
jge 0xf376
movq 0x2b20(%r15), %rdi
xorpd %xmm0, %xmm0
callq 0x13c3c
incl %ebx
cmpl 0x2d0(%r15), %ebx
jl 0xf35b
cmpl $0xa, 0x294(%r15)
jne 0xf430
movq 0x2b20(%r15), %rdi
movl %r12d, %esi
callq 0x13c82
testl %r12d, %r12d
jle 0xf422
movq 0x28(%rsp), %rax
cmpq %r14, %rax
cmovlq %rax, %r14
xorl %r13d, %r13d
movq %r14, %rbx
movq 0x8(%rsp), %r14
movslq 0x2b74(%r15), %rax
imulq %r14, %rax
addq %r13, %rax
movq 0x18(%rsp), %rcx
movsd (%rcx,%rax,8), %xmm0
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
movapd %xmm0, %xmm1
addsd 0x5ca1(%rip), %xmm1     # 0x15080
cvttsd2si %xmm1, %eax
cmovbl %ebp, %eax
ucomisd %xmm0, %xmm2
addsd 0x5c96(%rip), %xmm0     # 0x15088
cvttsd2si %xmm0, %ebp
cmovbel %eax, %ebp
movq 0x2b20(%r15), %rdi
movl %ebp, %esi
callq 0x13c82
incq %r13
cmpl %r13d, %ebx
jne 0xf3b3
jmp 0xf422
movq 0x2b20(%r15), %rdi
xorl %esi, %esi
callq 0x13c82
incl %r12d
cmpl 0x2d0(%r15), %r12d
jl 0xf411
movq 0x18(%rsp), %r13
movq 0x30(%rsp), %rdx
incq %rdx
movslq 0x2c4(%r15), %rax
movslq 0x2c8(%r15), %rcx
addq %rax, %rcx
cmpq %rcx, %rdx
jl 0xf1e9
cmpl $0x65, 0x28c(%r15)
jne 0xf10d
movl 0x2c4(%r15), %eax
testq %rax, %rax
je 0xf48a
xorl %ecx, %ecx
xorl %ebp, %ebp
movq 0x20(%rsp), %rsi
movl (%rsi,%rcx,4), %edx
addl %edx, %ebp
cmpl $0x1, %edx
sbbl $-0x1, %ebp
incq %rcx
cmpq %rcx, %rax
jne 0xf475
jmp 0xf48c
xorl %ebp, %ebp
cmpl $0x2, 0x294(%r15)
jne 0xf4a5
movq 0x2b20(%r15), %rdi
movzwl %bp, %esi
callq 0x139f4
cmpl $0xa, 0x294(%r15)
jne 0xf4bd
movq 0x2b20(%r15), %rdi
movl %ebp, %esi
callq 0x13968
cmpl $0x2, 0x294(%r15)
movq 0x20(%rsp), %r12
jne 0xf53b
cmpl $0x0, 0x2c4(%r15)
je 0xf53b
xorl %r14d, %r14d
movl (%r12,%r14,4), %eax
testl %eax, %eax
je 0xf52c
movl %r14d, %ecx
shll $0x8, %ecx
addl %ecx, %eax
movq 0x2b20(%r15), %rdi
movzwl %ax, %esi
callq 0x139f4
cmpl $0x0, (%r12,%r14,4)
je 0xf52c
xorl %ebx, %ebx
movl 0x2b74(%r15), %eax
imull %r14d, %eax
addl %ebx, %eax
cvttsd2si (%r13,%rax,8), %eax
movq 0x2b20(%r15), %rdi
movzwl %ax, %esi
callq 0x139f4
incl %ebx
cmpl (%r12,%r14,4), %ebx
jb 0xf501
incq %r14
movl 0x2c4(%r15), %eax
cmpq %rax, %r14
jb 0xf4d9
cmpl $0xa, 0x294(%r15)
jne 0xf10d
cmpl $0x0, 0x2c4(%r15)
je 0xf10d
xorl %r14d, %r14d
movl (%r12,%r14,4), %esi
testl %esi, %esi
je 0xf5a7
movl %r14d, %eax
shll $0x18, %eax
addl %eax, %esi
movq 0x2b20(%r15), %rdi
callq 0x13c82
cmpl $0x0, (%r12,%r14,4)
je 0xf5a7
xorl %ebx, %ebx
movl 0x2b74(%r15), %eax
imull %r14d, %eax
addl %ebx, %eax
cvttsd2si (%r13,%rax,8), %esi
movq 0x2b20(%r15), %rdi
callq 0x13c82
incl %ebx
cmpl (%r12,%r14,4), %ebx
jb 0xf57f
incq %r14
movl 0x2c4(%r15), %eax
cmpq %rax, %r14
jb 0xf55a
jmp 0xf10d
nop