// File: /workspace/github_cpp_makefile_star2_O2/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
callq 0x110e4
movq 0x2b18(%rbx), %rdi
leaq 0x2a4(%rbx), %rsi
callq 0x10fca
movq 0x2b18(%rbx), %rdi
leaq 0x2a8(%rbx), %rsi
callq 0x10d66
movq 0x2b18(%rbx), %rdi
leaq 0x129c(%rbx), %rsi
callq 0x10fca
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 0x10d8a
movslq 0x129c(%rbx), %rax
movb $0x0, (%r15,%rax)
leaq 0xa0(%rbx), %rdi
movq %r15, %rsi
callq 0x2230
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), %r14
movl 0x280(%rbx), %eax
movl 0xc(%rsp), %ecx
movl %ecx, %r15d
cmpl $0x1317cb8, %eax        # imm = 0x1317CB8
jb 0x4039
cmpb $0x1, 0x1574(%rbx)
movl 0xc(%rsp), %ecx
movl %ecx, %r15d
jne 0x3f1a
movq 0x2b18(%rbx), %rdi
movq 0x10(%rsp), %rsi
callq 0x10fca
movl 0x280(%rbx), %eax
movq 0x68(%rsp), %r15
cmpl $0x132677b, %eax        # imm = 0x132677B
jb 0x4039
movq 0x10(%rsp), %rax
movl (%rax), %eax
cmpl $0x8, %eax
jl 0x3f45
movq 0x18(%rsp), %rcx
cmpl $-0x1, (%rcx)
jne 0x3f45
movl $0x1, 0x159c(%rbx)
cmpl $0x9, %eax
jl 0x4039
movq 0x2b18(%rbx), %rdi
movq %r13, %rsi
callq 0x10fca
addl $0x4, %r15d
movslq 0x1328(%rbx), %rbp
leaq (,%rbp,8), %r12
testq %rbp, %rbp
movq %r12, %rdi
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, 0x1330(%rbx)
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x20b0
xorl %r12d, %r12d
movslq %ebp, %rax
cmpq %rax, %r12
jge 0x4039
movq 0x2b18(%rbx), %rdi
leaq 0x8(%rsp), %rsi
callq 0x10fca
addl $0x4, %r15d
pushq $0x20
popq %rdi
callq 0x2130
leaq 0x10(%rax), %rcx
movq %rcx, (%rax)
andq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x1330(%rbx), %rcx
movq %rax, (%rcx,%r12,8)
movq 0x1330(%rbx), %rax
movq (%rax,%r12,8), %rdi
movslq 0x8(%rsp), %rsi
callq 0x2200
movl 0x8(%rsp), %eax
testl %eax, %eax
jle 0x402d
movq 0x2b18(%rbx), %rdi
movq %r14, %rsi
callq 0x11060
incl %r15d
movsbl 0x7(%rsp), %esi
movq 0x1330(%rbx), %rax
movq (%rax,%r12,8), %rdi
callq 0x20f0
movl 0x8(%rsp), %eax
decl %eax
movl %eax, 0x8(%rsp)
jmp 0x3ff6
incq %r12
movl (%r13), %ebp
jmp 0x3f99
movq 0x2b18(%rbx), %rdi
movq 0xb0(%rsp), %rsi
callq 0x10fca
movq 0x2b18(%rbx), %rdi
movq 0xa8(%rsp), %rsi
callq 0x10fca
movq 0x2b18(%rbx), %rdi
movq 0xa0(%rsp), %rsi
callq 0x10fca
movq 0x2b18(%rbx), %rdi
movq 0x98(%rsp), %rsi
callq 0x110e4
leal 0x14(%r15), %ebp
movl $0x1, 0x12ac(%rbx)
cmpl $0x1317cb8, 0x280(%rbx) # imm = 0x1317CB8
jb 0x40c4
cmpb $0x1, 0x1574(%rbx)
jne 0x40c4
movq 0x2b18(%rbx), %rdi
movq 0x60(%rsp), %rsi
callq 0x10fca
addl $0x18, %r15d
movl %r15d, %ebp
movq 0x2b18(%rbx), %rdi
movq 0x90(%rsp), %rsi
callq 0x10fca
cmpl $0x132677a, 0x280(%rbx) # imm = 0x132677A
ja 0x410b
movq 0x2b18(%rbx), %rdi
movq 0x78(%rsp), %rsi
callq 0x10d66
movq 0x2b18(%rbx), %rdi
movq 0x70(%rsp), %rsi
callq 0x10d66
addl $0xc, %ebp
jmp 0x4145
movq 0x2b18(%rbx), %rdi
leaq 0x20(%rsp), %r15
movq %r15, %rsi
callq 0x110f6
movl 0x20(%rsp), %eax
movl %eax, 0x2b4(%rbx)
movq 0x2b18(%rbx), %rdi
movq %r15, %rsi
callq 0x110f6
movl 0x20(%rsp), %eax
movl %eax, 0x2bc(%rbx)
addl $0x14, %ebp
movl 0x2b4(%rbx), %eax
cmpl 0x2b70(%rbx), %eax
jg 0x4284
movl 0x2bc(%rbx), %eax
cmpl 0x2b74(%rbx), %eax
jg 0x4290
movq 0x2b18(%rbx), %rdi
movq 0x18(%rsp), %rsi
callq 0x10fca
movq 0x2b18(%rbx), %rdi
movq 0x88(%rsp), %rsi
callq 0x10fca
leal 0x8(%rbp), %r12d
movl 0x29c(%rbx), %eax
leal -0x14(%rax), %ecx
cmpl %ecx, %ebp
je 0x42e1
movl 0x280(%rbx), %ecx
cmpl $0x1317cb8, %ecx        # imm = 0x1317CB8
jb 0x424b
cmpb $0x1, 0x1574(%rbx)
jne 0x424b
movq 0x2b18(%rbx), %rdi
movq 0x58(%rsp), %rsi
callq 0x10fca
movq 0x2b18(%rbx), %rdi
movq 0x50(%rsp), %rsi
callq 0x10fca
movq 0x2b18(%rbx), %rdi
movq 0x48(%rsp), %rsi
callq 0x10fca
movq 0x2b18(%rbx), %rdi
movq 0x40(%rsp), %rsi
callq 0x10fca
movq 0x2b18(%rbx), %rdi
movq 0x38(%rsp), %rsi
callq 0x10fca
movq 0x2b18(%rbx), %rdi
movq 0x30(%rsp), %rsi
callq 0x10fca
movq 0x2b18(%rbx), %rdi
movq 0x28(%rsp), %rsi
callq 0x10fca
addl $0x24, %ebp
movl 0x280(%rbx), %ecx
movl 0x29c(%rbx), %eax
movl %ebp, %r12d
addl $-0xc, %eax
cmpl %eax, %r12d
je 0x42a2
cmpl $0x132677a, %ecx        # imm = 0x132677A
ja 0x42a2
cmpb $0x1, 0x1574(%rbx)
jne 0x42de
movb $0x0, 0x1574(%rbx)
movq 0x2b18(%rbx), %rdi
movq 0x80(%rsp), %rsi
callq 0x235c
jmp 0x3ed6
movl $0x13, 0x12e0(%rbx)
jmp 0x429a
movl $0x14, 0x12e0(%rbx)
movl $0xfffe7960, %r12d      # imm = 0xFFFE7960
jmp 0x42e1
movq 0x10(%rsp), %rax
cmpl $0x8, (%rax)
jne 0x42e1
movq 0x2b18(%rbx), %rdi
callq 0x110dc
movq 0x2b18(%rbx), %rdi
movl 0x29c(%rbx), %esi
addl 0x298(%rbx), %esi
callq 0x235c
movq 0x2b18(%rbx), %rdi
callq 0x110dc
jmp 0x42e1
xorl %r12d, %r12d
movl %r12d, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq