// File: /workspace/github_cpp_makefile_star2_O2/BU-DiSC[P]BF-Shared-Hashing/db.cpp
// Function: db::read_data(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, int, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>&)
inline int db::read_data ( string filename, int pos, int key_size, vector<string> & data_block )
{
	//int flags = O_RDWR | O_DIRECT | O_SYNC;
	mode_t mode=S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;//644

	int fd = open(filename.c_str(), file_read_flags, mode );
	if (fd <= 0) {
    	printf("Error %s\n", strerror(errno));
		cout << "Cannot open partiton file " << filename << endl;
		return 0;
	}
      
	data_block.clear();
	data_block.resize(B, "");

	char* buf;
	int offset = pos*B*E;


	auto data_start = high_resolution_clock::now();
	posix_memalign((void**)&buf,getpagesize(),DB_PAGE_SIZE);
	memset(buf, 0, DB_PAGE_SIZE);


	//read( fd, buf, DB_PAGE_SIZE );
	//auto data_end   = high_resolution_clock::now();
	//data_duration += duration_cast<microseconds>(data_end - data_start);

      
    // Moving pointer
	lseek( fd, DB_PAGE_SIZE+offset, SEEK_SET );

	memset(buf, 0, DB_PAGE_SIZE);
	//data_start = high_resolution_clock::now();
	read( fd, buf, DB_PAGE_SIZE );
	auto data_end   = high_resolution_clock::now();
	data_duration += duration_cast<microseconds>(data_end - data_start);

	//int key_size;
	int entry_size = E;
	char* tmp_buf = (char*) malloc(entry_size+1);
	offset = 0;
	tmp_buf[entry_size] = '\0';
	for(int i = 0; i < B; i++){
		if(offset + key_size >= DB_PAGE_SIZE){
			unsigned int inner_key_offset = 0;
			if(offset < DB_PAGE_SIZE){
				memcpy(tmp_buf, buf+offset,DB_PAGE_SIZE-offset);
				inner_key_offset = DB_PAGE_SIZE-offset;
			}
		
			 data_start = high_resolution_clock::now();
			read( fd, buf, DB_PAGE_SIZE );
			data_end   = high_resolution_clock::now();
			data_duration += duration_cast<microseconds>(data_end - data_start);

			offset = key_size - inner_key_offset;	
			memcpy(tmp_buf+inner_key_offset, buf, offset);
		}else{

			memcpy(tmp_buf, buf+offset, E);
			offset += E;
		}

		data_block[i] = string(tmp_buf);

		memset(tmp_buf, 0, key_size);
	}
	free(tmp_buf);

	free( buf );
	close( fd );

        if(delay > 0){
			 data_start = high_resolution_clock::now();
	    struct timespec req = {0}; 
            req.tv_sec = 0;
	    req.tv_nsec = delay;
	    nanosleep(&req, (struct timespec *)NULL);
			data_end   = high_resolution_clock::now();
			data_duration += duration_cast<microseconds>(data_end - data_start);
	}
		
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r8, %r15
movq %rcx, 0x10(%rsp)
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
movq (%rsi), %rdi
movl 0xc0(%rbx), %esi
movl $0x1a4, %edx            # imm = 0x1A4
xorl %eax, %eax
callq 0x6300
movl %eax, (%rsp)
testl %eax, %eax
jle 0xcf95
movq %r15, %rdi
callq 0xbe20
movslq 0xc8(%rbx), %r14
leaq 0x156e3(%rip), %rsi     # 0x223d9
leaq 0x28(%rsp), %r12
leaq 0x8(%rsp), %rdx
movq %r12, %rdi
callq 0x61b0
movq %r15, 0x20(%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0xcffe
leaq 0x28(%rsp), %rdi
callq 0x61f0
movslq 0xc8(%rbx), %rax
movslq %ebp, %rcx
imulq %rax, %rcx
movslq 0xcc(%rbx), %r14
imulq %rcx, %r14
callq 0x6070
movq %rax, %r13
callq 0x66e0
movslq %eax, %rsi
leaq 0x8(%rsp), %rbp
movl $0x1000, %edx           # imm = 0x1000
movq %rbp, %rdi
callq 0x6230
movq (%rbp), %rdi
xorl %r12d, %r12d
movl $0x1000, %edx           # imm = 0x1000
xorl %esi, %esi
callq 0x6260
addq $0x1000, %r14           # imm = 0x1000
movl (%rsp), %r15d
movl %r15d, %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x6700
movq (%rbp), %r14
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
xorl %esi, %esi
callq 0x6260
movl $0x1000, %edx           # imm = 0x1000
movl %r15d, %edi
movq %r14, %rsi
callq 0x65a0
callq 0x6070
subq %r13, %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
cvtsi2ss %rax, %xmm0
divss 0x15237(%rip), %xmm0    # 0x22004
addss 0x18c(%rbx), %xmm0
movss %xmm0, 0x18c(%rbx)
movslq 0xcc(%rbx), %r14
leaq 0x1(%r14), %rdi
callq 0x6550
movq %rax, %r13
movb $0x0, (%rax,%r14)
movslq 0x10(%rsp), %rax
movq %rax, 0x18(%rsp)
xorl %ebp, %ebp
xorl %r15d, %r15d
movslq 0xc8(%rbx), %rax
cmpq %rax, %rbp
jge 0xcf1a
movq 0x10(%rsp), %rax
addl %r15d, %eax
movslq %r15d, %rsi
cmpl $0x1000, %eax           # imm = 0x1000
jl 0xceb5
xorl %r14d, %r14d
cmpl $0xfff, %r15d           # imm = 0xFFF
jg 0xce4f
addq 0x8(%rsp), %rsi
movl $0x1000, %r14d          # imm = 0x1000
subl %r15d, %r14d
movq %r13, %rdi
movq %r14, %rdx
callq 0x6350
callq 0x6070
movq %rax, %r15
movq 0x8(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movl (%rsp), %edi
callq 0x65a0
callq 0x6070
subq %r15, %rax
cqto
movl $0x3e8, %ecx            # imm = 0x3E8
idivq %rcx
cvtsi2ss %rax, %xmm0
divss 0x1517c(%rip), %xmm0    # 0x22004
addss 0x18c(%rbx), %xmm0
movss %xmm0, 0x18c(%rbx)
movq 0x10(%rsp), %r15
subl %r14d, %r15d
movl %r14d, %edi
addq %r13, %rdi
movq 0x8(%rsp), %rsi
movslq %r15d, %rdx
callq 0x6350
jmp 0xcecf
addq 0x8(%rsp), %rsi
movslq 0xcc(%rbx), %r14
movq %r13, %rdi
movq %r14, %rdx
callq 0x6350
addl %r14d, %r15d
leaq 0x28(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
leaq 0x7(%rsp), %rdx
callq 0x61b0
movq 0x20(%rsp), %rax
movq (%rax), %rdi
addq %r12, %rdi
movq %r14, %rsi
callq 0x64c0
movq %r14, %rdi
callq 0x61f0
movq %r13, %rdi
xorl %esi, %esi
movq 0x18(%rsp), %rdx
callq 0x6260
incq %rbp
addq $0x20, %r12
jmp 0xce04
movq %r13, %rdi
callq 0x6490
movq 0x8(%rsp), %rdi
callq 0x6490
movl (%rsp), %edi
callq 0x6750
cmpl $0x0, 0x234(%rbx)
jle 0xcfd8
callq 0x6070
leaq 0x28(%rsp), %rdi
andq $0x0, (%rdi)
movq %rax, %r14
movslq 0x234(%rbx), %rax
movq %rax, 0x8(%rdi)
xorl %esi, %esi
callq 0x6150
callq 0x6070
subq %r14, %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
cvtsi2ss %rax, %xmm0
divss 0x15081(%rip), %xmm0    # 0x22004
addss 0x18c(%rbx), %xmm0
movss %xmm0, 0x18c(%rbx)
jmp 0xcfd8
callq 0x6050
movl (%rax), %edi
callq 0x6170
leaq 0x155c1(%rip), %rdi     # 0x22569
movq %rax, %rsi
xorl %eax, %eax
callq 0x6060
movq 0x2800f(%rip), %rdi     # 0x34fc8
leaq 0x155b3(%rip), %rsi     # 0x22573
callq 0x6410
movq %rax, %rdi
movq %r14, %rsi
callq 0x63b0
movq %rax, %rdi
callq 0x6210
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x61f0
movq %rbx, %rdi
callq 0x66b0