// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %rsp
movq %r8, %r15
movq %rcx, 0x18(%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 0x52f0
movl %eax, 0x8(%rsp)
testl %eax, %eax
jle 0xf4e4
movq %r15, %rdi
callq 0xe08a
movslq 0xc8(%rbx), %r14
leaq 0x30(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x1a269(%rip), %rdx     # 0x29459
leaq 0x20(%rsp), %r12
movq %r12, %rdi
movq %rdx, %rsi
callq 0xfa70
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0xf580
movq %r15, 0x50(%rsp)
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0xf222
callq 0x53d0
movslq 0xc8(%rbx), %rax
movslq %ebp, %rcx
imulq %rax, %rcx
movslq 0xcc(%rbx), %r14
imulq %rcx, %r14
callq 0x5080
movq %rax, %r12
callq 0x56e0
movslq %eax, %rsi
leaq 0x10(%rsp), %r13
movl $0x1000, %edx           # imm = 0x1000
movq %r13, %rdi
callq 0x5200
movq (%r13), %rdi
xorl %r15d, %r15d
movl $0x1000, %edx           # imm = 0x1000
xorl %esi, %esi
callq 0x5240
addq $0x1000, %r14           # imm = 0x1000
movl 0x8(%rsp), %ebp
movl %ebp, %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x5710
movq (%r13), %r14
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
xorl %esi, %esi
callq 0x5240
movl $0x1000, %edx           # imm = 0x1000
movl %ebp, %edi
movq %r14, %rsi
callq 0x5590
callq 0x5080
subq %r12, %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %rdx
addq %rax, %rdx
cvtsi2ss %rdx, %xmm0
divss 0x19d2b(%rip), %xmm0    # 0x29004
addss 0x18c(%rbx), %xmm0
movss %xmm0, 0x18c(%rbx)
movslq 0xcc(%rbx), %r14
leaq 0x1(%r14), %rdi
callq 0x5530
movq %rax, %r12
movb $0x0, (%rax,%r14)
cmpl $0x0, 0xc8(%rbx)
jle 0xf452
movslq 0x18(%rsp), %rax
movq %rax, 0x48(%rsp)
xorl %r13d, %r13d
xorl %ebp, %ebp
movq 0x18(%rsp), %rax
addl %r15d, %eax
cmpl $0x1000, %eax           # imm = 0x1000
jl 0xf3d3
xorl %r14d, %r14d
cmpl $0xfff, %r15d           # imm = 0xFFF
jg 0xf358
movslq %r15d, %rsi
addq 0x10(%rsp), %rsi
movl $0x1000, %r14d          # imm = 0x1000
subl %r15d, %r14d
movq %r12, %rdi
movq %r14, %rdx
callq 0x5330
callq 0x5080
movq %rax, %r15
movq 0x10(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movl 0x8(%rsp), %edi
callq 0x5590
callq 0x5080
subq %r15, %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %rdx
addq %rax, %rdx
xorps %xmm0, %xmm0
cvtsi2ss %rdx, %xmm0
divss 0x19c5e(%rip), %xmm0    # 0x29004
addss 0x18c(%rbx), %xmm0
movss %xmm0, 0x18c(%rbx)
movq 0x18(%rsp), %r15
subl %r14d, %r15d
movl %r14d, %edi
addq %r12, %rdi
movq 0x10(%rsp), %rsi
movslq %r15d, %rdx
callq 0x5330
jmp 0xf3f0
movslq %r15d, %rsi
addq 0x10(%rsp), %rsi
movslq 0xcc(%rbx), %r14
movq %r12, %rdi
movq %r14, %rdx
callq 0x5330
addl %r14d, %r15d
leaq 0x20(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
leaq 0xf(%rsp), %rdx
callq 0x51b0
movq 0x50(%rsp), %rax
movq (%rax), %rdi
addq %r13, %rdi
movq %r14, %rsi
callq 0x54b0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0xf42c
callq 0x53d0
movq %r12, %rdi
xorl %esi, %esi
movq 0x48(%rsp), %rdx
callq 0x5240
incq %rbp
movslq 0xc8(%rbx), %rax
addq $0x20, %r13
cmpq %rax, %rbp
jl 0xf31d
movq %r12, %rdi
callq 0x5490
movq 0x10(%rsp), %rdi
callq 0x5490
movl 0x8(%rsp), %edi
callq 0x5740
cmpl $0x0, 0x234(%rbx)
jle 0xf555
callq 0x5080
movq %rax, %r14
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rdi
movaps %xmm0, (%rdi)
movslq 0x234(%rbx), %rax
movq %rax, 0x8(%rdi)
xorl %esi, %esi
callq 0x5130
callq 0x5080
subq %r14, %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %rdx
addq %rax, %rdx
xorps %xmm0, %xmm0
cvtsi2ss %rdx, %xmm0
divss 0x19b32(%rip), %xmm0    # 0x29004
addss 0x18c(%rbx), %xmm0
movss %xmm0, 0x18c(%rbx)
jmp 0xf555
callq 0x5060
movl (%rax), %edi
callq 0x5170
leaq 0x1a07e(%rip), %rdi     # 0x29575
movq %rax, %rsi
xorl %eax, %eax
callq 0x5070
movq 0x29ac8(%rip), %rbx     # 0x38fd0
leaq 0x1a070(%rip), %rsi     # 0x2957f
movl $0x1a, %edx
movq %rbx, %rdi
callq 0x5470
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %rbx, %rdi
callq 0x5470
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x53f0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x52a0
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0xf578
callq 0x53d0
movq %rbx, %rdi
callq 0x56b0