// File: NichtJens[P]lmf2txt/LMF_IO.cpp
// Function: LMF_IO::SeekToEventNumber(unsigned long long)
bool LMF_IO::SeekToEventNumber(unsigned __int64 target_number)
/////////////////////////////////////////////////////////////////
{
	if (DAQ_ID == DAQ_ID_SIMPLE) return false;

	if (target_number == 0) {
		input_lmf->seek((unsigned __int64)(Headersize + User_header_size));
		must_read_first = true;
		errorflag = 0;
		input_lmf->error = 0;
		return true;
	}

	if (data_format_in_userheader == LM_USERDEF) {errorflag = 16; return false;}

	if (!input_lmf) {
		errorflag = 9;
		return false;
	}


/*	unsigned __int64 filesize;
	unsigned __int64 pos = input_lmf->tell();
	input_lmf->seek_to_end();
	filesize = input_lmf->tell();
	input_lmf->seek(pos);
*/

	

	if (target_number < 0) return false;
	if (target_number > uint64_Numberofevents) return false;
	__int32 eventsize = 0;
	if (data_format_in_userheader == 2 ) eventsize = 2 * Numberofcoordinates;
	if (data_format_in_userheader == 5 ) eventsize = 8 * Numberofcoordinates;
	if (data_format_in_userheader == 10) eventsize = 4 * Numberofcoordinates;

	if (DAQ_ID == DAQ_ID_RAW32BIT) eventsize = 4 * number_of_channels * (max_number_of_hits+1);

	if (input_lmf->filesize < (unsigned __int64)(eventsize)*target_number + (unsigned  __int64)(Headersize + User_header_size)) return false;

	unsigned __int64 new_position = (unsigned __int64)(eventsize)*target_number + (unsigned __int64)(Headersize + User_header_size);

	input_lmf->seek(new_position);



	uint64_number_of_read_events = target_number;
	must_read_first = true;
	errorflag = 0;
	input_lmf->error = 0;
	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpl $0x65, 0x288(%rax)
jne 0x168da
movb $0x0, -0x1(%rbp)
jmp 0x16abe
cmpq $0x0, -0x18(%rbp)
jne 0x1692d
movq -0x30(%rbp), %rcx
movq 0x2b18(%rcx), %rdi
movl 0x298(%rcx), %eax
addl 0x29c(%rcx), %eax
movl %eax, %eax
movl %eax, %esi
callq 0x2440
movq -0x30(%rbp), %rax
movb $0x1, 0x2b4c(%rax)
movl $0x0, 0x12e0(%rax)
movq 0x2b18(%rax), %rax
movl $0x0, 0x8(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x16abe
movq -0x30(%rbp), %rax
cmpl $-0x1, 0x290(%rax)
jne 0x16951
movq -0x30(%rbp), %rax
movl $0x10, 0x12e0(%rax)
movb $0x0, -0x1(%rbp)
jmp 0x16abe
movq -0x30(%rbp), %rax
cmpq $0x0, 0x2b18(%rax)
jne 0x16976
movq -0x30(%rbp), %rax
movl $0x9, 0x12e0(%rax)
movb $0x0, -0x1(%rbp)
jmp 0x16abe
cmpq $0x0, -0x18(%rbp)
jae 0x16986
movb $0x0, -0x1(%rbp)
jmp 0x16abe
movq -0x30(%rbp), %rcx
movq -0x18(%rbp), %rax
cmpq 0x268(%rcx), %rax
jbe 0x169a0
movb $0x0, -0x1(%rbp)
jmp 0x16abe
movq -0x30(%rbp), %rax
movl $0x0, -0x1c(%rbp)
cmpl $0x2, 0x290(%rax)
jne 0x169c3
movq -0x30(%rbp), %rax
movl 0x270(%rax), %eax
shll %eax
movl %eax, -0x1c(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x5, 0x290(%rax)
jne 0x169e0
movq -0x30(%rbp), %rax
movl 0x270(%rax), %eax
shll $0x3, %eax
movl %eax, -0x1c(%rbp)
movq -0x30(%rbp), %rax
cmpl $0xa, 0x290(%rax)
jne 0x169fd
movq -0x30(%rbp), %rax
movl 0x270(%rax), %eax
shll $0x2, %eax
movl %eax, -0x1c(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x64, 0x288(%rax)
jne 0x16a26
movq -0x30(%rbp), %rcx
movl 0x2b4(%rcx), %eax
shll $0x2, %eax
movl 0x2bc(%rcx), %ecx
addl $0x1, %ecx
imull %ecx, %eax
movl %eax, -0x1c(%rbp)
movq -0x30(%rbp), %rsi
movq 0x2b18(%rsi), %rax
movq 0x10(%rax), %rax
movslq -0x1c(%rbp), %rcx
imulq -0x18(%rbp), %rcx
movl 0x298(%rsi), %edx
addl 0x29c(%rsi), %edx
movl %edx, %edx
addq %rdx, %rcx
cmpq %rcx, %rax
jae 0x16a5a
movb $0x0, -0x1(%rbp)
jmp 0x16abe
movq -0x30(%rbp), %rax
movslq -0x1c(%rbp), %rcx
imulq -0x18(%rbp), %rcx
movl 0x298(%rax), %edx
addl 0x29c(%rax), %edx
movl %edx, %edx
addq %rdx, %rcx
movq %rcx, -0x28(%rbp)
movq 0x2b18(%rax), %rdi
movq -0x28(%rbp), %rsi
callq 0x2440
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movq %rcx, 0x260(%rax)
movb $0x1, 0x2b4c(%rax)
movl $0x0, 0x12e0(%rax)
movq 0x2b18(%rax), %rax
movl $0x0, 0x8(%rax)
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)