// File: /workspace/github_cpp_makefile_star2_O2/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 $0x2a78, %rsp           # imm = 0x2A78
movq %rsi, %r14
movl %edi, %ebp
movl $0x2b80, %edi           # imm = 0x2B80
callq 0x1130
movq %rax, %rbx
pushq $0x50
popq %rsi
pushq $0x20
popq %rdx
movq %rax, %rdi
callq 0x1568
movq (%r14), %rsi
leaq 0x20(%rsp), %r15
movq %r15, %rdi
callq 0x1110
movq %r15, %rdi
callq 0x1080
testq %rax, %rax
je 0x101f4
movzbl -0x1(%rax,%r15), %ecx
cmpl $0x2f, %ecx
je 0x101f4
cmpl $0x5c, %ecx
je 0x101f4
movb $0x0, 0x1f(%rsp,%rax)
jmp 0x101d0
movups 0x1308(%rip), %xmm0     # 0x11503
movups %xmm0, 0x40(%rsp,%rax)
movups 0x12ec(%rip), %xmm0     # 0x114f3
movups %xmm0, 0x30(%rsp,%rax)
movupd 0x12cf(%rip), %xmm0     # 0x114e3
movupd %xmm0, 0x20(%rsp,%rax)
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x242c
testb %al, %al
je 0x10246
movq %rbx, %rdi
callq 0x2156
andq $0x0, (%rsp)
cmpl $0x1, %ebp
jg 0x1026a
leaq 0x12fb(%rip), %rsi      # 0x1153f
jmp 0x1026e
leaq 0x12c6(%rip), %rdi      # 0x11513
leaq 0x20(%rsp), %rsi
xorl %eax, %eax
callq 0x1050
leaq 0x1308(%rip), %rdi      # 0x11568
callq 0x11a0
jmp 0x1044c
movq 0x8(%r14), %rsi
leaq 0x12de(%rip), %rdx      # 0x11553
movq %rsp, %rdi
callq 0x10148
testl %eax, %eax
jne 0x103f6
cmpq $0x0, (%rsp)
je 0x103f6
movl $0x10, 0x2c4(%rbx)
movl $0xa, 0x2cc(%rbx)
movaps 0x11d5(%rip), %xmm0     # 0x11480
movups %xmm0, 0x12c0(%rbx)
andl $0x0, 0x1420(%rbx)
movapd 0x11cf(%rip), %xmm0     # 0x11490
movupd %xmm0, 0x1430(%rbx)
leaq 0x1286(%rip), %rsi      # 0x11556
movq %rbx, %rdi
callq 0xa9fe
xorpd %xmm0, %xmm0
leaq 0x130(%rsp), %r14
leaq 0x270(%rsp), %r15
leaq 0x1271(%rip), %r13      # 0x11564
leaq 0x10(%rsp), %rbp
movsd %xmm0, 0x8(%rsp)
movl $0x140, %edx            # imm = 0x140
movq %r14, %rdi
xorl %esi, %esi
callq 0x10b0
movl $0x2800, %edx           # imm = 0x2800
movq %r15, %rdi
xorl %esi, %esi
callq 0x10b0
xorl %r12d, %r12d
movslq 0x2c4(%rbx), %rax
cmpq %rax, %r12
jge 0x103a6
movq (%rsp), %rdi
leaq (%rsp,%r12,4), %r14
addq $0x130, %r14            # imm = 0x130
leaq 0x1220(%rip), %rsi      # 0x11561
movq %r14, %rdx
xorl %eax, %eax
callq 0x11e0
testl %eax, %eax
je 0x103a6
movq %r12, 0x18(%rsp)
xorl %r12d, %r12d
movl (%r14), %eax
cmpq %rax, %r12
jae 0x10395
andq $0x0, 0x10(%rsp)
movq (%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x11e0
testl %eax, %eax
je 0x10395
movsd 0x10(%rsp), %xmm0
divsd 0x12c0(%rbx), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, (%r15,%r12,4)
incq %r12
jmp 0x10357
movq 0x18(%rsp), %r12
incq %r12
subq $-0x80, %r15
jmp 0x1031f
movq (%rsp), %rdi
callq 0x11b0
testl %eax, %eax
movsd 0x8(%rsp), %xmm0
jne 0x10421
addsd 0x10df(%rip), %xmm0     # 0x114a0
movsd %xmm0, 0x8(%rsp)
movq %rbx, %rdi
movsd 0x8(%rsp), %xmm0
leaq 0x130(%rsp), %r14
movq %r14, %rsi
leaq 0x270(%rsp), %r15
movq %r15, %rdx
callq 0xbaf0
movsd 0x8(%rsp), %xmm0
jmp 0x102f8
leaq 0x1195(%rip), %rdi      # 0x11592
callq 0x11a0
movq %rbx, %rdi
callq 0x21a4
movq %rbx, %rdi
callq 0x1d58
movl $0x2b80, %esi           # imm = 0x2B80
movq %rbx, %rdi
callq 0x1140
jmp 0x1044c
movq %rbx, %rdi
callq 0x21a4
movq %rbx, %rdi
callq 0x1d58
movl $0x2b80, %esi           # imm = 0x2B80
movq %rbx, %rdi
callq 0x1140
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x1044c
callq 0x1120
xorl %eax, %eax
addq $0x2a78, %rsp           # imm = 0x2A78
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movl $0x2b80, %esi           # imm = 0x2B80
movq %rbx, %rdi
callq 0x1140
movq %r14, %rdi
callq 0x11d0