// File: /workspace/github_cpp_makefile_star2_O3/NichtJens[P]lmf2txt/WriteLMF.cpp
// Function: main
int main(int argc, char* argv[], char* envp[])
//////////////////////////////////////////////////////////////////////////////////////////
{
	#ifdef _DEBUG
		printf("\n***********************\n    SLOW DEBUG VERSION USED\n***********************\n");
	#endif

	unsigned int	number_of_hits[NUM_CHANNELS];
	int				TDC[NUM_CHANNELS][NUM_IONS];


//  --------------------------------------------------------
//	read Header information of template LMF-file (ignore this part)
	LMF_IO * LMF = new LMF_IO(NUM_CHANNELS, NUM_IONS);
	char template_name[270];
	sprintf(template_name,"%s",argv[0]);
	while (strlen(template_name) > 0) {
		if (template_name[strlen(template_name)-1] == '\\' || template_name[strlen(template_name)-1] == '/') {
			break;
		}
		template_name[strlen(template_name)-1] = 0;
	}

	sprintf(template_name+strlen(template_name),"%s","data/Cobold2011R5-2_Header-Template_1TDC8HP.lmf");

	if (!LMF->OpenInputLMF(template_name)) {
		printf("error: could not open the template file\n%s\n",template_name);
		printf("It must be in the same folder as the exe.\n");
		return false;
	}
	LMF->CloseInputLMF();
//  end of read Header information ----------------------------

	FILE * in_file = 0;
	int result = 0;
	if (argc < 2)
		result = fopen_s(&in_file, "data/input_file.dat", "rt");
	else
		result = fopen_s(&in_file, argv[1], "rt");

	if (result || (!in_file)) {
		printf("input file not found.\n");
		LMF->CloseOutputLMF();
		if (LMF) delete LMF;
		return 0;
	}

//	Open and prepare output file
	LMF->number_of_channels_output = 16;
	LMF->max_number_of_hits_output = 10;
	LMF->frequency = 1.;
	LMF->tdcresolution_output				= 0.016; // can be ignored
	LMF->TDC8HP.GroupingEnable_p66_output	= 0;	 // can be ignored
	LMF->TDC8HP.GroupRangeStart_p69			= -100.; // can be ignored
	LMF->TDC8HP.GroupRangeEnd_p70			= 1000.; // can be ignored
	LMF->OpenOutputLMF((char*) "output.lmf");

	double dTimeStamp = 0.; // timestamp in seconds


//  Write Events into new LMF file:
	while (true) {
		memset(number_of_hits,   0,sizeof(number_of_hits)   );	// clear number_of_hits[] array
		memset(TDC,   0,sizeof(TDC)   );	// clear TDC[] array

		for (int channel = 0;channel < LMF->number_of_channels_output; channel++)
		{
			if (!fscanf_s(in_file,"%i",&number_of_hits[channel])) break;
			for (unsigned int hit = 0;hit < number_of_hits[channel]; hit++)
			{
				double time_in_ns = 0.;
				if (!fscanf_s(in_file, "%lf", &time_in_ns)) break;
				TDC[channel][hit] = int(time_in_ns / LMF->tdcresolution_output); // convert nanoseconds to TDC-bins
			}
		}
		
		if (feof(in_file)) break;

		dTimeStamp += 1.; // here you can set your own event timestamp (optionally)

		LMF->WriteTDCData(dTimeStamp, number_of_hits,&TDC[0][0]);
	}


	LMF->CloseOutputLMF();

	if (LMF) delete LMF;
	if (in_file) fclose(in_file);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2a68, %rsp           # imm = 0x2A68
movq %rsi, %r14
movl %edi, %ebp
movl $0x2b80, %edi           # imm = 0x2B80
callq 0x1120
movq %rax, %r15
movq %rax, %rdi
movl $0x50, %esi
movl $0x20, %edx
callq 0x1608
movq (%r14), %rsi
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x1100
movq %rbx, %rdi
callq 0x1080
testq %rax, %rax
je 0x127a1
movzbl -0x1(%rax,%rbx), %ecx
cmpl $0x2f, %ecx
je 0x127a3
cmpl $0x5c, %ecx
je 0x127a3
movb $0x0, 0xf(%rsp,%rax)
jmp 0x1277e
xorl %eax, %eax
movups 0xd89(%rip), %xmm0      # 0x13533
movups %xmm0, 0x30(%rsp,%rax)
movups 0xd6d(%rip), %xmm0      # 0x13523
movups %xmm0, 0x20(%rsp,%rax)
movupd 0xd50(%rip), %xmm0      # 0x13513
movupd %xmm0, 0x10(%rsp,%rax)
leaq 0x10(%rsp), %rsi
movq %r15, %rbx
movq %r15, %rdi
callq 0x2994
testb %al, %al
je 0x127f3
movq %rbx, %rdi
callq 0x2556
cmpl $0x1, %ebp
jg 0x12817
leaq 0xd7e(%rip), %rdi       # 0x1356f
jmp 0x1281b
leaq 0xd49(%rip), %rdi       # 0x13543
leaq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x1050
leaq 0xd8b(%rip), %rdi       # 0x13598
callq 0x11a0
jmp 0x129fc
movq 0x8(%r14), %rdi
leaq 0xd61(%rip), %rsi       # 0x13583
callq 0x1150
testq %rax, %rax
je 0x129d3
movq %rax, %r14
movq %r15, %rbp
movl $0x10, 0x2c4(%r15)
movl $0xa, 0x2cc(%r15)
movaps 0xc5d(%rip), %xmm0      # 0x134b0
movups %xmm0, 0x12c0(%r15)
movl $0x0, 0x1420(%r15)
movapd 0xc52(%rip), %xmm0      # 0x134c0
movupd %xmm0, 0x1430(%r15)
leaq 0xd08(%rip), %rsi       # 0x13586
movq %r15, %rdi
callq 0xc8aa
xorpd %xmm0, %xmm0
leaq 0x120(%rsp), %rbx
leaq 0x260(%rsp), %r12
movsd %xmm0, (%rsp)
movl $0x140, %edx            # imm = 0x140
movq %rbx, %rdi
xorl %esi, %esi
callq 0x10a0
movl $0x2800, %edx           # imm = 0x2800
movq %r12, %rdi
xorl %esi, %esi
callq 0x10a0
cmpl $0x0, 0x2c4(%rbp)
jle 0x12961
leaq 0x260(%rsp), %r13
xorl %ebx, %ebx
leaq (%rsp,%rbx,4), %r12
addq $0x120, %r12            # imm = 0x120
movq %r14, %rdi
leaq 0xca8(%rip), %rsi       # 0x13591
movq %r12, %rdx
xorl %eax, %eax
callq 0x11f0
testl %eax, %eax
je 0x12961
cmpl $0x0, (%r12)
je 0x12947
xorl %ebp, %ebp
movq $0x0, 0x8(%rsp)
movq %r14, %rdi
leaq 0xc81(%rip), %rsi       # 0x13594
leaq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x11f0
testl %eax, %eax
je 0x12947
movsd 0x8(%rsp), %xmm0
divsd 0x12c0(%r15), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, (%r13,%rbp,4)
incq %rbp
movl (%r12), %eax
cmpq %rax, %rbp
jb 0x12900
incq %rbx
movq %r15, %rbp
movslq 0x2c4(%r15), %rax
subq $-0x80, %r13
cmpq %rax, %rbx
jl 0x128d4
movq %r14, %rdi
callq 0x11c0
testl %eax, %eax
movsd (%rsp), %xmm0
jne 0x129ac
addsd 0xb56(%rip), %xmm0      # 0x134d0
movsd %xmm0, (%rsp)
movq %rbp, %rdi
movsd (%rsp), %xmm0
leaq 0x120(%rsp), %rbx
movq %rbx, %rsi
leaq 0x260(%rsp), %r12
movq %r12, %rdx
callq 0xdac8
movsd (%rsp), %xmm0
jmp 0x1289a
movq %rbp, %rdi
callq 0x2606
movq %rbp, %rdi
callq 0x1f50
movl $0x2b80, %esi           # imm = 0x2B80
movq %rbp, %rdi
callq 0x1130
movq %r14, %rdi
callq 0x1110
jmp 0x129fc
leaq 0xbe8(%rip), %rdi       # 0x135c2
callq 0x11a0
movq %r15, %rdi
callq 0x2606
movq %r15, %rdi
callq 0x1f50
movl $0x2b80, %esi           # imm = 0x2B80
movq %r15, %rdi
callq 0x1130
xorl %eax, %eax
addq $0x2a68, %rsp           # imm = 0x2A68
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movl $0x2b80, %esi           # imm = 0x2B80
movq %r15, %rdi
callq 0x1130
movq %r14, %rdi
callq 0x11e0