// File: 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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rsi
movq %rsi, -0x50(%rbp)
movl $0x0, -0x14(%rbp)
movq 0x2b18(%rsi), %rdi
addq $0x12c8, %rsi           # imm = 0x12C8
callq 0x1d1f0
movq -0x50(%rbp), %rsi
movslq -0x14(%rbp), %rax
addq $0x8, %rax
movl %eax, -0x14(%rbp)
movq 0x2b18(%rsi), %rdi
addq $0x2a4, %rsi            # imm = 0x2A4
callq 0x1cf60
movq -0x50(%rbp), %rsi
movslq -0x14(%rbp), %rax
addq $0x4, %rax
movl %eax, -0x14(%rbp)
movq 0x2b18(%rsi), %rdi
addq $0x2a8, %rsi            # imm = 0x2A8
callq 0x1cb80
movq -0x50(%rbp), %rsi
movslq -0x14(%rbp), %rax
addq $0x4, %rax
movl %eax, -0x14(%rbp)
movq 0x2b18(%rsi), %rdi
addq $0x129c, %rsi           # imm = 0x129C
callq 0x1cf60
movq -0x50(%rbp), %rax
movslq -0x14(%rbp), %rcx
addq $0x4, %rcx
movl %ecx, -0x14(%rbp)
movl 0x129c(%rax), %eax
addl $0x1, %eax
movslq %eax, %rdi
callq 0x2030
movq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, -0x20(%rbp)
movq 0x2b18(%rax), %rdi
movq -0x20(%rbp), %rsi
movl 0x129c(%rax), %edx
callq 0x1cbe0
movq -0x50(%rbp), %rdi
movq -0x20(%rbp), %rax
movslq 0x129c(%rdi), %rcx
movb $0x0, (%rax,%rcx)
movq -0x20(%rbp), %rsi
addq $0xa0, %rdi
callq 0x2250
movq -0x20(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
je 0x527f
movq -0x48(%rbp), %rdi
callq 0x2240
movq -0x50(%rbp), %rax
movq $0x0, -0x20(%rbp)
movl 0x129c(%rax), %ecx
addl -0x14(%rbp), %ecx
movl %ecx, -0x14(%rbp)
movq 0x2b18(%rax), %rdi
callq 0x1d110
movq %rax, -0x28(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x2c(%rbp)
movq -0x50(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl %ecx, -0x14(%rbp)
cmpl $0x1317cb8, 0x280(%rax) # imm = 0x1317CB8
jb 0x52f2
movq -0x50(%rbp), %rax
testb $0x1, 0x1574(%rax)
je 0x52f2
movq -0x50(%rbp), %rsi
movq 0x2b18(%rsi), %rdi
addq $0x12a4, %rsi           # imm = 0x12A4
callq 0x1cf60
movslq -0x14(%rbp), %rax
addq $0x4, %rax
movl %eax, -0x14(%rbp)
movq -0x50(%rbp), %rax
cmpl $0x132677b, 0x280(%rax) # imm = 0x132677B
jb 0x5494
movq -0x50(%rbp), %rax
cmpl $0x8, 0x12a4(%rax)
jl 0x532e
movq -0x50(%rbp), %rax
cmpl $-0x1, 0x290(%rax)
jne 0x532e
movq -0x50(%rbp), %rax
movl $0x1, 0x159c(%rax)
movq -0x50(%rbp), %rax
cmpl $0x9, 0x12a4(%rax)
jl 0x5492
movq -0x50(%rbp), %rsi
movq 0x2b18(%rsi), %rdi
addq $0x1328, %rsi           # imm = 0x1328
callq 0x1cf60
movq -0x50(%rbp), %rax
movslq -0x14(%rbp), %rcx
addq $0x4, %rcx
movl %ecx, -0x14(%rbp)
movslq 0x1328(%rax), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x1330(%rax)
movq 0x1330(%rax), %rdi
movslq 0x1328(%rax), %rdx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x20d0
movl $0x0, -0x30(%rbp)
movq -0x50(%rbp), %rcx
movl -0x30(%rbp), %eax
cmpl 0x1328(%rcx), %eax
jge 0x5490
movq -0x50(%rbp), %rax
movq 0x2b18(%rax), %rdi
leaq -0x34(%rbp), %rsi
callq 0x1cf60
movslq -0x14(%rbp), %rax
addq $0x4, %rax
movl %eax, -0x14(%rbp)
movl $0x20, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, -0x58(%rbp)
callq 0x2260
movq -0x58(%rbp), %rsi
movq -0x50(%rbp), %rax
movq 0x1330(%rax), %rcx
movslq -0x30(%rbp), %rdx
movq %rsi, (%rcx,%rdx,8)
movq 0x1330(%rax), %rax
movslq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movslq -0x34(%rbp), %rsi
callq 0x22d0
cmpl $0x0, -0x34(%rbp)
jle 0x5480
movq -0x50(%rbp), %rax
movq 0x2b18(%rax), %rdi
leaq -0x35(%rbp), %rsi
callq 0x1d0e0
movq -0x50(%rbp), %rcx
movslq -0x14(%rbp), %rax
addq $0x1, %rax
movl %eax, -0x14(%rbp)
movb -0x35(%rbp), %al
movq 0x1330(%rcx), %rcx
movslq -0x30(%rbp), %rdx
movq (%rcx,%rdx,8), %rdi
movsbl %al, %esi
callq 0x2100
movl -0x34(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x5432
jmp 0x5482
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x53bb
jmp 0x5492
jmp 0x5494
movq -0x50(%rbp), %rsi
movq 0x2b18(%rsi), %rdi
addq $0x128c, %rsi           # imm = 0x128C
callq 0x1cf60
movq -0x50(%rbp), %rsi
movslq -0x14(%rbp), %rax
addq $0x4, %rax
movl %eax, -0x14(%rbp)
movq 0x2b18(%rsi), %rdi
addq $0x240, %rsi            # imm = 0x240
callq 0x1cf60
movq -0x50(%rbp), %rsi
movslq -0x14(%rbp), %rax
addq $0x4, %rax
movl %eax, -0x14(%rbp)
movq 0x2b18(%rsi), %rdi
addq $0x1294, %rsi           # imm = 0x1294
callq 0x1cf60
movq -0x50(%rbp), %rsi
movslq -0x14(%rbp), %rax
addq $0x4, %rax
movl %eax, -0x14(%rbp)
movq 0x2b18(%rsi), %rdi
addq $0x12b8, %rsi           # imm = 0x12B8
callq 0x1d1f0
movq -0x50(%rbp), %rax
movslq -0x14(%rbp), %rcx
addq $0x8, %rcx
movl %ecx, -0x14(%rbp)
movl $0x1, 0x12ac(%rax)
cmpl $0x1317cb8, 0x280(%rax) # imm = 0x1317CB8
jb 0x5565
movq -0x50(%rbp), %rax
testb $0x1, 0x1574(%rax)
je 0x5565
movq -0x50(%rbp), %rsi
movq 0x2b18(%rsi), %rdi
addq $0x12ac, %rsi           # imm = 0x12AC
callq 0x1cf60
movslq -0x14(%rbp), %rax
addq $0x4, %rax
movl %eax, -0x14(%rbp)
movq -0x50(%rbp), %rsi
movq 0x2b18(%rsi), %rdi
addq $0x2b0, %rsi            # imm = 0x2B0
callq 0x1cf60
movq -0x50(%rbp), %rax
movslq -0x14(%rbp), %rcx
addq $0x4, %rcx
movl %ecx, -0x14(%rbp)
cmpl $0x132677b, 0x280(%rax) # imm = 0x132677B
jae 0x55dd
movq -0x50(%rbp), %rsi
movq 0x2b18(%rsi), %rdi
addq $0x2b4, %rsi            # imm = 0x2B4
callq 0x1cb80
movq -0x50(%rbp), %rsi
movslq -0x14(%rbp), %rax
addq $0x4, %rax
movl %eax, -0x14(%rbp)
movq 0x2b18(%rsi), %rdi
addq $0x2bc, %rsi            # imm = 0x2BC
callq 0x1cb80
movslq -0x14(%rbp), %rax
addq $0x4, %rax
movl %eax, -0x14(%rbp)
jmp 0x5633
movq -0x50(%rbp), %rax
movq 0x2b18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x1d220
movq -0x50(%rbp), %rax
movq -0x40(%rbp), %rcx
movl %ecx, 0x2b4(%rax)
movslq -0x14(%rbp), %rcx
addq $0x8, %rcx
movl %ecx, -0x14(%rbp)
movq 0x2b18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x1d220
movq -0x50(%rbp), %rax
movq -0x40(%rbp), %rcx
movl %ecx, 0x2bc(%rax)
movslq -0x14(%rbp), %rax
addq $0x8, %rax
movl %eax, -0x14(%rbp)
movq -0x50(%rbp), %rcx
movl 0x2b4(%rcx), %eax
cmpl 0x2b70(%rcx), %eax
jle 0x565f
movq -0x50(%rbp), %rax
movl $0x13, 0x12e0(%rax)
movl $0xfffe7960, -0x4(%rbp) # imm = 0xFFFE7960
jmp 0x58c6
movq -0x50(%rbp), %rcx
movl 0x2bc(%rcx), %eax
cmpl 0x2b74(%rcx), %eax
jle 0x568b
movq -0x50(%rbp), %rax
movl $0x14, 0x12e0(%rax)
movl $0xfffe7960, -0x4(%rbp) # imm = 0xFFFE7960
jmp 0x58c6
movq -0x50(%rbp), %rsi
movq 0x2b18(%rsi), %rdi
addq $0x290, %rsi            # imm = 0x290
callq 0x1cf60
movq -0x50(%rbp), %rsi
movslq -0x14(%rbp), %rax
addq $0x4, %rax
movl %eax, -0x14(%rbp)
movq 0x2b18(%rsi), %rdi
addq $0x1288, %rsi           # imm = 0x1288
callq 0x1cf60
movq -0x50(%rbp), %rcx
movslq -0x14(%rbp), %rax
addq $0x4, %rax
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %eax
movl 0x29c(%rcx), %ecx
subl $0xc, %ecx
cmpl %ecx, %eax
jne 0x56ee
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x58c6
movq -0x50(%rbp), %rax
cmpl $0x1317cb8, 0x280(%rax) # imm = 0x1317CB8
jb 0x5819
movq -0x50(%rbp), %rax
testb $0x1, 0x1574(%rax)
je 0x5819
movq -0x50(%rbp), %rsi
movq 0x2b18(%rsi), %rdi
addq $0x1558, %rsi           # imm = 0x1558
callq 0x1cf60
movq -0x50(%rbp), %rsi
movslq -0x14(%rbp), %rax
addq $0x4, %rax
movl %eax, -0x14(%rbp)
movq 0x2b18(%rsi), %rdi
addq $0x1558, %rsi           # imm = 0x1558
addq $0x4, %rsi
callq 0x1cf60
movq -0x50(%rbp), %rsi
movslq -0x14(%rbp), %rax
addq $0x4, %rax
movl %eax, -0x14(%rbp)
movq 0x2b18(%rsi), %rdi
addq $0x1558, %rsi           # imm = 0x1558
addq $0x8, %rsi
callq 0x1cf60
movq -0x50(%rbp), %rsi
movslq -0x14(%rbp), %rax
addq $0x4, %rax
movl %eax, -0x14(%rbp)
movq 0x2b18(%rsi), %rdi
addq $0x1558, %rsi           # imm = 0x1558
addq $0xc, %rsi
callq 0x1cf60
movq -0x50(%rbp), %rsi
movslq -0x14(%rbp), %rax
addq $0x4, %rax
movl %eax, -0x14(%rbp)
movq 0x2b18(%rsi), %rdi
addq $0x1558, %rsi           # imm = 0x1558
addq $0x10, %rsi
callq 0x1cf60
movq -0x50(%rbp), %rsi
movslq -0x14(%rbp), %rax
addq $0x4, %rax
movl %eax, -0x14(%rbp)
movq 0x2b18(%rsi), %rdi
addq $0x1558, %rsi           # imm = 0x1558
addq $0x14, %rsi
callq 0x1cf60
movq -0x50(%rbp), %rsi
movslq -0x14(%rbp), %rax
addq $0x4, %rax
movl %eax, -0x14(%rbp)
movq 0x2b18(%rsi), %rdi
addq $0x1558, %rsi           # imm = 0x1558
addq $0x18, %rsi
callq 0x1cf60
movslq -0x14(%rbp), %rax
addq $0x4, %rax
movl %eax, -0x14(%rbp)
movq -0x50(%rbp), %rcx
movl -0x14(%rbp), %eax
movl 0x29c(%rcx), %ecx
subl $0xc, %ecx
cmpl %ecx, %eax
je 0x5873
movq -0x50(%rbp), %rax
cmpl $0x132677b, 0x280(%rax) # imm = 0x132677B
jae 0x5873
movq -0x50(%rbp), %rax
testb $0x1, 0x1574(%rax)
jne 0x5853
movl $0x0, -0x4(%rbp)
jmp 0x58c6
movq -0x50(%rbp), %rax
movb $0x0, 0x1574(%rax)
movq 0x2b18(%rax), %rdi
movq -0x28(%rbp), %rsi
callq 0x2440
jmp 0x52ad
movq -0x50(%rbp), %rax
cmpl $0x8, 0x12a4(%rax)
jne 0x58c0
movq -0x50(%rbp), %rax
movq 0x2b18(%rax), %rdi
callq 0x1d1d0
movq -0x50(%rbp), %rcx
movq 0x2b18(%rcx), %rdi
movl 0x298(%rcx), %eax
addl 0x29c(%rcx), %eax
movl %eax, %eax
movl %eax, %esi
callq 0x2440
movq -0x50(%rbp), %rax
movq 0x2b18(%rax), %rdi
callq 0x1d1d0
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nop