// File: /workspace/github_cpp_makefile_star2_O3/NichtJens[P]lmf2txt/LMF_IO.cpp
// Function: LMF_IO::ReadTDC8PCI2Header()
__int32	LMF_IO::ReadTDC8PCI2Header()
/////////////////////////////////////////////////////////////////
{
	__int32 byte_counter;
	byte_counter = 0;



	*input_lmf >> frequency;	byte_counter += sizeof(double);		// frequency is always 4th value
	*input_lmf >> IOaddress;	byte_counter += sizeof(__int32);		// IO address (parameter 1) always 5th value
	*input_lmf >> timestamp_format;	byte_counter += sizeof(__int32);	// TimeInfo (parameter 2) always 6th value  (0,1,2)*32Bit
	
	*input_lmf >> DAQ_info_Length;	byte_counter += sizeof(__int32);		// Length of DAQInfo always 7th value
	__int8 * __int8_temp = new __int8[DAQ_info_Length+1];
	input_lmf->read(__int8_temp,DAQ_info_Length);	// DAQInfo always 8th value
	__int8_temp[DAQ_info_Length] = 0;
	DAQ_info = __int8_temp;
	delete[] __int8_temp; __int8_temp = 0;
	byte_counter += DAQ_info_Length;

//	input_lmf->flush();
	unsigned __int64 StartPosition = input_lmf->tell();
	__int32 old_byte_counter = byte_counter;
L50:
	byte_counter = old_byte_counter;

//	TRY
	if (DAQVersion >= 20020408 && TDC8PCI2.use_normal_method) {
		*input_lmf >> LMF_Version;
		byte_counter += sizeof(__int32);
	}

	if (DAQVersion >= 20080507) {
		if (LMF_Version >= 0x8 && data_format_in_userheader == -1) TDC8PCI2.variable_event_length = 1;
		if (LMF_Version >= 0x9) {
			*input_lmf >> number_of_DAQ_source_strings;    byte_counter += sizeof(__int32);
			DAQ_source_strings = new std::string*[number_of_DAQ_source_strings];
			memset(DAQ_source_strings,0,sizeof(std::string*)*number_of_DAQ_source_strings);
			for (__int32 i=0;i<number_of_DAQ_source_strings;++i) {
				__int32 string_len;
				*input_lmf >> string_len;    byte_counter += sizeof(__int32);
				DAQ_source_strings[i] = new std::string();
				DAQ_source_strings[i]->reserve(string_len);
				while (string_len > 0) {
					__int8 c;
					*input_lmf >> c;     byte_counter += sizeof(__int8);
					*DAQ_source_strings[i] += c;
					--string_len;
				}
			}
		}
	}

	*input_lmf >> system_timeout;	byte_counter += sizeof(__int32);		//   system time-out
	*input_lmf >> time_reference;	byte_counter += sizeof(__int32);
	*input_lmf >> common_mode;		byte_counter += sizeof(__int32);			//   0 common start    1 common stop
	*input_lmf >> tdcresolution;	byte_counter += sizeof(double);	// tdc resolution in ns


	TDCDataType = 1;
	if (DAQVersion >= 20020408 && TDC8PCI2.use_normal_method) {*input_lmf >> TDCDataType; byte_counter += sizeof(__int32);}
	*input_lmf >> timerange; byte_counter += sizeof(__int32);			// time range of the tdc in microseconds

	if (this->DAQVersion < 20080507) {
		*input_lmf >> number_of_channels; byte_counter += sizeof(__int32);	// number of channels
		*input_lmf >> max_number_of_hits; byte_counter += sizeof(__int32);	// number of hits
	} else {
		__int64 i64_temp;
		*input_lmf >> i64_temp; number_of_channels = (unsigned __int32)(i64_temp); byte_counter += sizeof(__int64);	// number of channels
		*input_lmf >> i64_temp; max_number_of_hits = (unsigned __int32)(i64_temp); byte_counter += sizeof(__int64);	// number of hits
	}

	if (int(number_of_channels) > num_channels)	{errorflag = 19;	return -100000;}
	if (int(max_number_of_hits) > num_ions)		{errorflag = 20;	return -100000;}
	*input_lmf >> data_format_in_userheader;	byte_counter += sizeof(__int32);				// data format (2=short integer)

	*input_lmf >> module_2nd;	byte_counter += sizeof(__int32);		// indicator for 2nd module data
	
	if ((unsigned) byte_counter == User_header_size - 12) return byte_counter;

	if (DAQVersion >= 20020408 && TDC8PCI2.use_normal_method) {
		*input_lmf >> TDC8PCI2.GateDelay_1st_card;			byte_counter += sizeof(__int32); // gate delay 1st card
		*input_lmf >> TDC8PCI2.OpenTime_1st_card;			byte_counter += sizeof(__int32); // open time 1st card
		*input_lmf >> TDC8PCI2.WriteEmptyEvents_1st_card;	byte_counter += sizeof(__int32); // write empty events 1st card
		*input_lmf >> TDC8PCI2.TriggerFalling_1st_card;		byte_counter += sizeof(__int32); // trigger falling edge 1st card
		*input_lmf >> TDC8PCI2.TriggerRising_1st_card;		byte_counter += sizeof(__int32); // trigger rising edge 1st card
		*input_lmf >> TDC8PCI2.EmptyCounter_1st_card;		byte_counter += sizeof(__int32); // EmptyCounter 1st card
		*input_lmf >> TDC8PCI2.EmptyCounter_since_last_Event_1st_card;	byte_counter += sizeof(__int32); // Empty Counter since last event 1st card
	}

/*
	CATCH( CArchiveException, e )
		if (!TDC8PCI2.use_normal_method) return 0;
		TDC8PCI2.use_normal_method = false;
		in_ar->Close(); delete in_ar; in_ar = 0;
		input_lmf->seek(StartPosition);
		in_ar = new CArchive(input_lmf,CArchive::load);
		goto L50;
	END_CATCH
*/

	if ((unsigned) byte_counter != User_header_size - 12 && DAQVersion < 20080507) {
		if (!TDC8PCI2.use_normal_method) return 0;
		TDC8PCI2.use_normal_method = false;
		input_lmf->seek(StartPosition);
		goto L50;
	}

	if (LMF_Version == 0x8) {
		input_lmf->flush();
		input_lmf->seek((unsigned __int64)(this->Headersize+this->User_header_size));
		input_lmf->flush();
	}

	return byte_counter;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rdi, %rbx
movq 0x2b18(%rdi), %rdi
leaq 0x12c8(%rbx), %rsi
movl $0x8, %edx
callq 0x133bc
movq 0x2b18(%rbx), %rdi
leaq 0x2a4(%rbx), %rsi
movl $0x4, %edx
callq 0x133bc
movq 0x2b18(%rbx), %rdi
leaq 0x2a8(%rbx), %rsi
movl $0x4, %edx
callq 0x133bc
movq 0x2b18(%rbx), %rdi
leaq 0x129c(%rbx), %rsi
movl $0x4, %edx
callq 0x133bc
movslq 0x129c(%rbx), %r14
movq %r14, %rdi
incq %rdi
callq 0x2030
movq %rax, %r15
movq 0x2b18(%rbx), %rdi
movq %rax, %rsi
movl %r14d, %edx
callq 0x133bc
movslq 0x129c(%rbx), %rax
movb $0x0, (%r15,%rax)
leaq 0xa0(%rbx), %r14
movq 0xa8(%rbx), %r12
movq %r15, %rdi
callq 0x2090
movq %r14, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r15, %rcx
movq %rax, %r8
callq 0x21f0
movq %r15, %rdi
callq 0x21a0
movl 0x129c(%rbx), %ecx
leal 0x14(%rcx), %eax
movl %eax, 0xc(%rsp)
movq 0x2b18(%rbx), %rax
movq 0x20(%rax), %rax
movq %rax, 0x80(%rsp)
leaq 0x12a4(%rbx), %rax
movq %rax, 0x10(%rsp)
addl $0x18, %ecx
movq %rcx, 0x68(%rsp)
leaq 0x290(%rbx), %rax
movq %rax, 0x18(%rsp)
leaq 0x1328(%rbx), %r13
leaq 0x128c(%rbx), %rax
movq %rax, 0xb0(%rsp)
leaq 0x240(%rbx), %rax
movq %rax, 0xa8(%rsp)
leaq 0x1294(%rbx), %rax
movq %rax, 0xa0(%rsp)
leaq 0x12b8(%rbx), %rax
movq %rax, 0x98(%rsp)
leaq 0x12ac(%rbx), %rax
movq %rax, 0x60(%rsp)
leaq 0x2b0(%rbx), %rax
movq %rax, 0x90(%rsp)
leaq 0x2b4(%rbx), %rax
movq %rax, 0x78(%rsp)
leaq 0x2bc(%rbx), %rax
movq %rax, 0x70(%rsp)
leaq 0x1288(%rbx), %rax
movq %rax, 0x88(%rsp)
leaq 0x1558(%rbx), %rax
movq %rax, 0x58(%rsp)
leaq 0x155c(%rbx), %rax
movq %rax, 0x50(%rsp)
leaq 0x1560(%rbx), %rax
movq %rax, 0x48(%rsp)
leaq 0x1564(%rbx), %rax
movq %rax, 0x40(%rsp)
leaq 0x1568(%rbx), %rax
movq %rax, 0x38(%rsp)
leaq 0x156c(%rbx), %rax
movq %rax, 0x30(%rsp)
leaq 0x1570(%rbx), %rax
movq %rax, 0x28(%rsp)
leaq 0x7(%rsp), %rbp
movl 0x280(%rbx), %eax
movl 0xc(%rsp), %ecx
movl %ecx, %r14d
cmpl $0x1317cb8, %eax        # imm = 0x1317CB8
jb 0x46dd
cmpb $0x1, 0x1574(%rbx)
movl 0xc(%rsp), %ecx
movl %ecx, %r14d
jne 0x459f
movq 0x2b18(%rbx), %rdi
movq 0x10(%rsp), %rsi
movl $0x4, %edx
callq 0x133bc
movl 0x280(%rbx), %eax
movq 0x68(%rsp), %r14
cmpl $0x132677b, %eax        # imm = 0x132677B
jb 0x46dd
movq 0x10(%rsp), %rax
movl (%rax), %eax
cmpl $0x8, %eax
jl 0x45ca
movq 0x18(%rsp), %rcx
cmpl $-0x1, (%rcx)
jne 0x45ca
movl $0x1, 0x159c(%rbx)
cmpl $0x9, %eax
jl 0x46dd
movq 0x2b18(%rbx), %rdi
movq %r13, %rsi
movl $0x4, %edx
callq 0x133bc
addl $0x4, %r14d
movslq 0x1328(%rbx), %r15
leaq (,%r15,8), %r12
testq %r15, %r15
movq %r12, %rdi
movq $-0x1, %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, 0x1330(%rbx)
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x20b0
testq %r15, %r15
jle 0x46dd
xorl %r15d, %r15d
leaq 0x8(%rsp), %r12
movq 0x2b18(%rbx), %rdi
movq %r12, %rsi
movl $0x4, %edx
callq 0x133bc
addl $0x4, %r14d
movl $0x20, %edi
callq 0x2130
leaq 0x10(%rax), %rcx
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x1330(%rbx), %rcx
movq %rax, (%rcx,%r15,8)
movq 0x1330(%rbx), %rax
movq (%rax,%r15,8), %rdi
movslq 0x8(%rsp), %rsi
callq 0x2210
cmpl $0x0, 0x8(%rsp)
jle 0x46cd
movq 0x2b18(%rbx), %rdi
movq %rbp, %rsi
movl $0x1, %edx
callq 0x133bc
incl %r14d
movsbl 0x7(%rsp), %esi
movq 0x1330(%rbx), %rax
movq (%rax,%r15,8), %rdi
callq 0x20f0
movl 0x8(%rsp), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x8(%rsp)
cmpl $0x1, %eax
jg 0x4691
incq %r15
movslq (%r13), %rax
cmpq %rax, %r15
jl 0x4635
movq 0x2b18(%rbx), %rdi
movq 0xb0(%rsp), %rsi
movl $0x4, %edx
callq 0x133bc
movq 0x2b18(%rbx), %rdi
movq 0xa8(%rsp), %rsi
movl $0x4, %edx
callq 0x133bc
movq 0x2b18(%rbx), %rdi
movq 0xa0(%rsp), %rsi
movl $0x4, %edx
callq 0x133bc
movq 0x2b18(%rbx), %rdi
movq 0x98(%rsp), %rsi
movl $0x8, %edx
callq 0x133bc
leal 0x14(%r14), %r15d
movl $0x1, 0x12ac(%rbx)
cmpl $0x1317cb8, 0x280(%rbx) # imm = 0x1317CB8
jb 0x4781
cmpb $0x1, 0x1574(%rbx)
jne 0x4781
movq 0x2b18(%rbx), %rdi
movq 0x60(%rsp), %rsi
movl $0x4, %edx
callq 0x133bc
addl $0x18, %r14d
movl %r14d, %r15d
movq 0x2b18(%rbx), %rdi
movq 0x90(%rsp), %rsi
movl $0x4, %edx
callq 0x133bc
cmpl $0x132677a, 0x280(%rbx) # imm = 0x132677A
ja 0x47d8
movq 0x2b18(%rbx), %rdi
movq 0x78(%rsp), %rsi
movl $0x4, %edx
callq 0x133bc
movq 0x2b18(%rbx), %rdi
movq 0x70(%rsp), %rsi
movl $0x4, %edx
callq 0x133bc
addl $0xc, %r15d
jmp 0x481d
movq 0x2b18(%rbx), %rdi
leaq 0x20(%rsp), %r14
movq %r14, %rsi
movl $0x8, %edx
callq 0x133bc
movl 0x20(%rsp), %eax
movl %eax, 0x2b4(%rbx)
movq 0x2b18(%rbx), %rdi
movq %r14, %rsi
movl $0x8, %edx
callq 0x133bc
movl 0x20(%rsp), %eax
movl %eax, 0x2bc(%rbx)
addl $0x14, %r15d
movl 0x2b4(%rbx), %eax
cmpl 0x2b70(%rbx), %eax
jg 0x49ac
movl 0x2bc(%rbx), %eax
cmpl 0x2b74(%rbx), %eax
jg 0x49b8
movq 0x2b18(%rbx), %rdi
movq 0x18(%rsp), %rsi
movl $0x4, %edx
callq 0x133bc
movq 0x2b18(%rbx), %rdi
movq 0x88(%rsp), %rsi
movl $0x4, %edx
callq 0x133bc
leal 0x8(%r15), %r12d
movl 0x29c(%rbx), %eax
leal -0x14(%rax), %ecx
cmpl %ecx, %r15d
je 0x4a2b
movl 0x280(%rbx), %ecx
cmpl $0x1317cb8, %ecx        # imm = 0x1317CB8
jb 0x4952
cmpb $0x1, 0x1574(%rbx)
jne 0x4952
movq 0x2b18(%rbx), %rdi
movq 0x58(%rsp), %rsi
movl $0x4, %edx
callq 0x133bc
movq 0x2b18(%rbx), %rdi
movq 0x50(%rsp), %rsi
movl $0x4, %edx
callq 0x133bc
movq 0x2b18(%rbx), %rdi
movq 0x48(%rsp), %rsi
movl $0x4, %edx
callq 0x133bc
movq 0x2b18(%rbx), %rdi
movq 0x40(%rsp), %rsi
movl $0x4, %edx
callq 0x133bc
movq 0x2b18(%rbx), %rdi
movq 0x38(%rsp), %rsi
movl $0x4, %edx
callq 0x133bc
movq 0x2b18(%rbx), %rdi
movq 0x30(%rsp), %rsi
movl $0x4, %edx
callq 0x133bc
movq 0x2b18(%rbx), %rdi
movq 0x28(%rsp), %rsi
movl $0x4, %edx
callq 0x133bc
addl $0x24, %r15d
movl 0x280(%rbx), %ecx
movl 0x29c(%rbx), %eax
movl %r15d, %r12d
addl $-0xc, %eax
cmpl %eax, %r12d
je 0x49ca
cmpl $0x132677a, %ecx        # imm = 0x132677A
ja 0x49ca
cmpb $0x1, 0x1574(%rbx)
jne 0x4a13
movb $0x0, 0x1574(%rbx)
movq 0x2b18(%rbx), %r14
movq (%r14), %rdi
movq 0x80(%rsp), %r15
movq %r15, %rsi
xorl %edx, %edx
callq 0x20c0
testl %eax, %eax
je 0x49a3
movl $0x1, 0x8(%r14)
jmp 0x4556
movq %r15, 0x20(%r14)
jmp 0x4556
movl $0x13, 0x12e0(%rbx)
jmp 0x49c2
movl $0x14, 0x12e0(%rbx)
movl $0xfffe7960, %r12d      # imm = 0xFFFE7960
jmp 0x4a2b
movq 0x10(%rsp), %rax
cmpl $0x8, (%rax)
jne 0x4a2b
movq 0x2b18(%rbx), %rax
movq (%rax), %rdi
callq 0x2160
movq 0x2b18(%rbx), %r15
movl 0x29c(%rbx), %r14d
addl 0x298(%rbx), %r14d
movq (%r15), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x20c0
testl %eax, %eax
je 0x4a18
movl $0x1, 0x8(%r15)
jmp 0x4a1c
xorl %r12d, %r12d
jmp 0x4a2b
movq %r14, 0x20(%r15)
movq 0x2b18(%rbx), %rax
movq (%rax), %rdi
callq 0x2160
movl %r12d, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq