// File: /workspace/github_cpp_makefile_star2_O1/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 0x139ac
movq 0x2b18(%rbx), %rdi
leaq 0x2a4(%rbx), %rsi
movl $0x4, %edx
callq 0x139ac
movq 0x2b18(%rbx), %rdi
leaq 0x2a8(%rbx), %rsi
movl $0x4, %edx
callq 0x139ac
movq 0x2b18(%rbx), %rdi
leaq 0x129c(%rbx), %rsi
movl $0x4, %edx
callq 0x139ac
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 0x139ac
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, 0x78(%rsp)
leaq 0x12a4(%rbx), %rax
movq %rax, 0x58(%rsp)
addl $0x18, %ecx
movq %rcx, 0x60(%rsp)
leaq 0x1328(%rbx), %r12
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, 0x50(%rsp)
leaq 0x2b0(%rbx), %rax
movq %rax, 0x90(%rsp)
leaq 0x2b4(%rbx), %rax
movq %rax, 0x70(%rsp)
leaq 0x2bc(%rbx), %rax
movq %rax, 0x68(%rsp)
leaq 0x290(%rbx), %rax
movq %rax, 0x88(%rsp)
leaq 0x1288(%rbx), %rax
movq %rax, 0x80(%rsp)
leaq 0x1558(%rbx), %rax
movq %rax, 0x48(%rsp)
leaq 0x155c(%rbx), %rax
movq %rax, 0x40(%rsp)
leaq 0x1560(%rbx), %rax
movq %rax, 0x38(%rsp)
leaq 0x1564(%rbx), %rax
movq %rax, 0x30(%rsp)
leaq 0x1568(%rbx), %rax
movq %rax, 0x28(%rsp)
leaq 0x156c(%rbx), %rax
movq %rax, 0x20(%rsp)
leaq 0x1570(%rbx), %rax
movq %rax, 0x18(%rsp)
leaq 0x7(%rsp), %r13
cmpl $0x1317cb8, 0x280(%rbx) # imm = 0x1317CB8
movl 0xc(%rsp), %eax
movl %eax, %r14d
jb 0x44da
cmpb $0x1, 0x1574(%rbx)
movl 0xc(%rsp), %eax
movl %eax, %r14d
jne 0x44da
movq 0x2b18(%rbx), %rdi
movq 0x58(%rsp), %rsi
movl $0x4, %edx
callq 0x139ac
movq 0x60(%rsp), %r14
cmpl $0x132677b, 0x280(%rbx) # imm = 0x132677B
jb 0x461f
movl 0x12a4(%rbx), %eax
cmpl $0x8, %eax
jl 0x4508
cmpl $-0x1, 0x290(%rbx)
jne 0x4508
movl $0x1, 0x159c(%rbx)
cmpl $0x9, %eax
jl 0x461f
movq 0x2b18(%rbx), %rdi
movq %r12, %rsi
movl $0x4, %edx
callq 0x139ac
addl $0x4, %r14d
movslq 0x1328(%rbx), %rax
leaq (,%rax,8), %rbp
testq %rax, %rax
movq %rbp, %rdi
movq $-0x1, %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, 0x1330(%rbx)
movq %rax, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x20b0
cmpl $0x0, 0x1328(%rbx)
jle 0x461f
xorl %r15d, %r15d
leaq 0x8(%rsp), %rbp
movq 0x2b18(%rbx), %rdi
movq %rbp, %rsi
movl $0x4, %edx
callq 0x139ac
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 0x460f
movq 0x2b18(%rbx), %rdi
movq %r13, %rsi
movl $0x1, %edx
callq 0x139ac
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 0x45d3
incq %r15
movslq (%r12), %rax
cmpq %rax, %r15
jl 0x4577
movq 0x2b18(%rbx), %rdi
movq 0xb0(%rsp), %rsi
movl $0x4, %edx
callq 0x139ac
movq 0x2b18(%rbx), %rdi
movq 0xa8(%rsp), %rsi
movl $0x4, %edx
callq 0x139ac
movq 0x2b18(%rbx), %rdi
movq 0xa0(%rsp), %rsi
movl $0x4, %edx
callq 0x139ac
movq 0x2b18(%rbx), %rdi
movq 0x98(%rsp), %rsi
movl $0x8, %edx
callq 0x139ac
leal 0x14(%r14), %r15d
movl $0x1, 0x12ac(%rbx)
cmpl $0x1317cb8, 0x280(%rbx) # imm = 0x1317CB8
jb 0x46c3
cmpb $0x1, 0x1574(%rbx)
jne 0x46c3
movq 0x2b18(%rbx), %rdi
movq 0x50(%rsp), %rsi
movl $0x4, %edx
callq 0x139ac
addl $0x18, %r14d
movl %r14d, %r15d
movq 0x2b18(%rbx), %rdi
movq 0x90(%rsp), %rsi
movl $0x4, %edx
callq 0x139ac
cmpl $0x132677a, 0x280(%rbx) # imm = 0x132677A
ja 0x471a
movq 0x2b18(%rbx), %rdi
movq 0x70(%rsp), %rsi
movl $0x4, %edx
callq 0x139ac
movq 0x2b18(%rbx), %rdi
movq 0x68(%rsp), %rsi
movl $0x4, %edx
callq 0x139ac
addl $0xc, %r15d
jmp 0x475f
movq 0x2b18(%rbx), %rdi
leaq 0x10(%rsp), %r14
movq %r14, %rsi
movl $0x8, %edx
callq 0x139ac
movl 0x10(%rsp), %eax
movl %eax, 0x2b4(%rbx)
movq 0x2b18(%rbx), %rdi
movq %r14, %rsi
movl $0x8, %edx
callq 0x139ac
movl 0x10(%rsp), %eax
movl %eax, 0x2bc(%rbx)
addl $0x14, %r15d
movl 0x2b4(%rbx), %eax
cmpl 0x2b70(%rbx), %eax
jg 0x48e9
movl 0x2bc(%rbx), %eax
cmpl 0x2b74(%rbx), %eax
jg 0x48f5
movq 0x2b18(%rbx), %rdi
movq 0x88(%rsp), %rsi
movl $0x4, %edx
callq 0x139ac
movq 0x2b18(%rbx), %rdi
movq 0x80(%rsp), %rsi
movl $0x4, %edx
callq 0x139ac
leal 0x8(%r15), %ebp
movl 0x29c(%rbx), %eax
addl $-0x14, %eax
cmpl %eax, %r15d
je 0x4965
cmpl $0x1317cb8, 0x280(%rbx) # imm = 0x1317CB8
jb 0x4889
cmpb $0x1, 0x1574(%rbx)
jne 0x4889
movq 0x2b18(%rbx), %rdi
movq 0x48(%rsp), %rsi
movl $0x4, %edx
callq 0x139ac
movq 0x2b18(%rbx), %rdi
movq 0x40(%rsp), %rsi
movl $0x4, %edx
callq 0x139ac
movq 0x2b18(%rbx), %rdi
movq 0x38(%rsp), %rsi
movl $0x4, %edx
callq 0x139ac
movq 0x2b18(%rbx), %rdi
movq 0x30(%rsp), %rsi
movl $0x4, %edx
callq 0x139ac
movq 0x2b18(%rbx), %rdi
movq 0x28(%rsp), %rsi
movl $0x4, %edx
callq 0x139ac
movq 0x2b18(%rbx), %rdi
movq 0x20(%rsp), %rsi
movl $0x4, %edx
callq 0x139ac
movq 0x2b18(%rbx), %rdi
movq 0x18(%rsp), %rsi
movl $0x4, %edx
callq 0x139ac
addl $0x24, %r15d
movl %r15d, %ebp
movl 0x29c(%rbx), %eax
addl $-0xc, %eax
cmpl %eax, %ebp
je 0x4906
cmpl $0x132677a, 0x280(%rbx) # imm = 0x132677A
ja 0x4906
cmpb $0x1, 0x1574(%rbx)
jne 0x494e
movb $0x0, 0x1574(%rbx)
movq 0x2b18(%rbx), %r14
movq (%r14), %rdi
movq 0x78(%rsp), %r15
movq %r15, %rsi
xorl %edx, %edx
callq 0x20c0
testl %eax, %eax
je 0x48e0
movl $0x1, 0x8(%r14)
jmp 0x449c
movq %r15, 0x20(%r14)
jmp 0x449c
movl $0x13, 0x12e0(%rbx)
jmp 0x48ff
movl $0x14, 0x12e0(%rbx)
movl $0xfffe7960, %ebp       # imm = 0xFFFE7960
jmp 0x4965
cmpl $0x8, 0x12a4(%rbx)
jne 0x4965
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 0x4952
movl $0x1, 0x8(%r15)
jmp 0x4956
xorl %ebp, %ebp
jmp 0x4965
movq %r14, 0x20(%r15)
movq 0x2b18(%rbx), %rax
movq (%rax), %rdi
callq 0x2160
movl %ebp, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop