// File: /workspace/github_cpp_makefile_star2_O1/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 0x1120
movq %rax, 0x8(%rsp)
movq %rax, %rdi
movl $0x50, %esi
movl $0x20, %edx
callq 0x15fe
movq (%r14), %rsi
leaq 0x20(%rsp), %r15
movq %r15, %rdi
callq 0x1100
movq %r15, %rdi
callq 0x1080
testq %rax, %rax
je 0x12dc8
movzbl -0x1(%rax,%r15), %ecx
cmpl $0x2f, %ecx
je 0x12dc8
cmpl $0x5c, %ecx
je 0x12dc8
movb $0x0, 0x1f(%rsp,%rax)
jmp 0x12da4
movups 0x16e4(%rip), %xmm0     # 0x144b3
movups %xmm0, 0x40(%rsp,%rax)
movups 0x16c8(%rip), %xmm0     # 0x144a3
movups %xmm0, 0x30(%rsp,%rax)
movupd 0x16ab(%rip), %xmm0     # 0x14493
movupd %xmm0, 0x20(%rsp,%rax)
leaq 0x20(%rsp), %rsi
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x28f6
testb %al, %al
je 0x12e1a
movq %rbx, %rdi
callq 0x252a
cmpl $0x1, %ebp
jg 0x12e3e
leaq 0x16d7(%rip), %rdi      # 0x144ef
jmp 0x12e42
leaq 0x16a2(%rip), %rdi      # 0x144c3
leaq 0x20(%rsp), %rsi
xorl %eax, %eax
callq 0x1050
leaq 0x16e4(%rip), %rdi      # 0x14518
callq 0x11a0
jmp 0x1306e
movq 0x8(%r14), %rdi
leaq 0x16ba(%rip), %rsi      # 0x14503
callq 0x1150
movq %rax, %r14
movb $0x1, %al
testq %r14, %r14
jne 0x12e63
callq 0x1040
cmpl $0x0, (%rax)
sete %al
testq %r14, %r14
movq 0x8(%rsp), %rbp
je 0x1301e
testb %al, %al
je 0x1301e
movl $0x10, 0x2c4(%rbp)
movl $0xa, 0x2cc(%rbp)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x12c8(%rbp)
movabsq $0x3f90624dd2f1a9fc, %rax # imm = 0x3F90624DD2F1A9FC
movq %rax, 0x12c0(%rbp)
movl $0x0, 0x1420(%rbp)
movabsq $-0x3fa7000000000000, %rax # imm = 0xC059000000000000
movq %rax, 0x1430(%rbp)
movabsq $0x408f400000000000, %rax # imm = 0x408F400000000000
movq %rax, 0x1438(%rbp)
leaq 0x1624(%rip), %rsi      # 0x14506
movq %rbp, %rdi
callq 0xca32
xorpd %xmm0, %xmm0
leaq 0x130(%rsp), %rbx
leaq 0x270(%rsp), %r12
leaq 0x18(%rsp), %r15
movsd %xmm0, 0x10(%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 0x12fcf
leaq 0x270(%rsp), %r13
xorl %ebx, %ebx
leaq (%rsp,%rbx,4), %r12
addq $0x130, %r12            # imm = 0x130
movq %r14, %rdi
leaq 0x15be(%rip), %rsi      # 0x14511
movq %r12, %rdx
xorl %eax, %eax
callq 0x11f0
testl %eax, %eax
je 0x12fcf
cmpl $0x0, (%r12)
je 0x12fb3
xorl %ebp, %ebp
movq $0x0, 0x18(%rsp)
movq %r14, %rdi
leaq 0x1597(%rip), %rsi      # 0x14514
movq %r15, %rdx
xorl %eax, %eax
callq 0x11f0
testl %eax, %eax
je 0x12fb3
movsd 0x18(%rsp), %xmm0
movq 0x8(%rsp), %rax
divsd 0x12c0(%rax), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, (%r13,%rbp,4)
incq %rbp
movl (%r12), %eax
cmpq %rax, %rbp
jb 0x12f6a
incq %rbx
movq 0x8(%rsp), %rbp
movslq 0x2c4(%rbp), %rax
subq $-0x80, %r13
cmpq %rax, %rbx
jl 0x12f3e
movq %r14, %rdi
callq 0x11c0
testl %eax, %eax
movsd 0x10(%rsp), %xmm0
jne 0x13049
addsd 0x1467(%rip), %xmm0     # 0x14450
movsd %xmm0, 0x10(%rsp)
movq %rbp, %rdi
movsd 0x10(%rsp), %xmm0
leaq 0x130(%rsp), %rbx
movq %rbx, %rsi
leaq 0x270(%rsp), %r12
movq %r12, %rdx
callq 0xdcd8
movsd 0x10(%rsp), %xmm0
jmp 0x12f03
leaq 0x151d(%rip), %rdi      # 0x14542
callq 0x11a0
movq %rbp, %rdi
callq 0x25a6
movq %rbp, %rdi
callq 0x1f48
movl $0x2b80, %esi           # imm = 0x2B80
movq %rbp, %rdi
callq 0x1130
jmp 0x1306e
movq %rbp, %rdi
callq 0x25a6
movq %rbp, %rdi
callq 0x1f48
movl $0x2b80, %esi           # imm = 0x2B80
movq %rbp, %rdi
callq 0x1130
movq %r14, %rdi
callq 0x1110
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 0x8(%rsp), %rdi
callq 0x1130
movq %r14, %rdi
callq 0x11e0