// File: 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:
subq $0x188, %rsp            # imm = 0x188
movq %rsi, 0x30(%rsp)
movq %rdi, %rax
movq 0x30(%rsp), %rdi
movq %rdi, 0x38(%rsp)
movq %rax, 0x178(%rsp)
movq %rdi, 0x170(%rsp)
movl %edx, 0x16c(%rsp)
movl %ecx, 0x168(%rsp)
movq %r8, 0x160(%rsp)
movq 0x178(%rsp), %rax
movq %rax, 0x40(%rsp)
movl $0x1a4, 0x15c(%rsp) # imm = 0x1A4
callq 0x6250
movq %rax, %rdi
movq 0x40(%rsp), %rax
movl 0xc0(%rax), %esi
movl 0x15c(%rsp), %edx
movb $0x0, %al
callq 0x63e0
movl %eax, 0x158(%rsp)
cmpl $0x0, 0x158(%rsp)
jg 0x15ca5
callq 0x6090
movl (%rax), %edi
callq 0x61e0
movq %rax, %rsi
leaq 0x4e8fb(%rip), %rdi     # 0x6455a
movb $0x0, %al
callq 0x60a0
movq 0x6a353(%rip), %rdi     # 0x7ffc0
leaq 0x4e8f0(%rip), %rsi     # 0x64564
callq 0x6500
movq 0x38(%rsp), %rsi
movq %rax, %rdi
callq 0x6490
movq %rax, %rdi
movq 0x6a318(%rip), %rsi     # 0x7ffa8
callq 0x6540
movl $0x0, 0x184(%rsp)
jmp 0x1626b
movq 0x160(%rsp), %rdi
callq 0x12bb0
movq 0x40(%rsp), %rax
movq 0x160(%rsp), %rcx
movq %rcx, 0x18(%rsp)
movslq 0xc8(%rax), %rax
movq %rax, 0x20(%rsp)
leaq 0x137(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x6860
movq 0x28(%rsp), %rdx
leaq 0x4e6dc(%rip), %rsi     # 0x643ca
leaq 0x138(%rsp), %rdi
callq 0x66f0
jmp 0x15cfd
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdi
leaq 0x138(%rsp), %rdx
callq 0x16290
jmp 0x15d16
leaq 0x138(%rsp), %rdi
callq 0x6340
leaq 0x137(%rsp), %rdi
callq 0x6570
movq 0x40(%rsp), %rcx
movl 0x16c(%rsp), %eax
imull 0xc8(%rcx), %eax
imull 0xcc(%rcx), %eax
movl %eax, 0x114(%rsp)
callq 0x60b0
movq %rax, 0x108(%rsp)
callq 0x6890
movslq %eax, %rsi
leaq 0x118(%rsp), %rdi
movl $0x1000, %edx           # imm = 0x1000
callq 0x62c0
movq 0x118(%rsp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x62e0
movl 0x158(%rsp), %edi
movl 0x114(%rsp), %eax
addl $0x1000, %eax           # imm = 0x1000
movslq %eax, %rsi
xorl %edx, %edx
callq 0x68b0
movq 0x118(%rsp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x62e0
movl 0x158(%rsp), %edi
movq 0x118(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x6730
callq 0x60b0
movq %rax, 0x100(%rsp)
leaq 0x100(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x7a60
movq %rax, 0xe8(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x7a30
movq %rax, 0xf0(%rsp)
leaq 0xfc(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x7ab0
movq 0x40(%rsp), %rdi
addq $0x18c, %rdi            # imm = 0x18C
leaq 0xfc(%rsp), %rsi
callq 0x7af0
movq 0x40(%rsp), %rax
movl 0xcc(%rax), %eax
movl %eax, 0xe4(%rsp)
movl 0xe4(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rdi
callq 0x66b0
movq %rax, 0xd8(%rsp)
movl $0x0, 0x114(%rsp)
movq 0xd8(%rsp), %rax
movslq 0xe4(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movl $0x0, 0xd4(%rsp)
movq 0x40(%rsp), %rcx
movl 0xd4(%rsp), %eax
cmpl 0xc8(%rcx), %eax
jge 0x16173
movl 0x114(%rsp), %eax
addl 0x168(%rsp), %eax
cmpl $0x1000, %eax           # imm = 0x1000
jl 0x16064
movl $0x0, 0xd0(%rsp)
cmpl $0x1000, 0x114(%rsp) # imm = 0x1000
jge 0x15f73
movq 0xd8(%rsp), %rdi
movq 0x118(%rsp), %rsi
movslq 0x114(%rsp), %rax
addq %rax, %rsi
movl $0x1000, %eax           # imm = 0x1000
subl 0x114(%rsp), %eax
movslq %eax, %rdx
callq 0x6440
movl $0x1000, %eax           # imm = 0x1000
subl 0x114(%rsp), %eax
movl %eax, 0xd0(%rsp)
jmp 0x15f73
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x128(%rsp)
movl %eax, 0x124(%rsp)
jmp 0x15f61
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x128(%rsp)
movl %eax, 0x124(%rsp)
leaq 0x138(%rsp), %rdi
callq 0x6340
leaq 0x137(%rsp), %rdi
callq 0x6570
jmp 0x1627a
callq 0x60b0
movq %rax, 0xc8(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x108(%rsp)
movl 0x158(%rsp), %edi
movq 0x118(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x6730
callq 0x60b0
movq %rax, 0xc0(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x100(%rsp)
leaq 0x100(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x7a60
movq %rax, 0xa8(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x7a30
movq %rax, 0xb0(%rsp)
leaq 0xbc(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x7ab0
movq 0x40(%rsp), %rdi
addq $0x18c, %rdi            # imm = 0x18C
leaq 0xbc(%rsp), %rsi
callq 0x7af0
movl 0x168(%rsp), %eax
subl 0xd0(%rsp), %eax
movl %eax, 0x114(%rsp)
movq 0xd8(%rsp), %rdi
movl 0xd0(%rsp), %eax
addq %rax, %rdi
movq 0x118(%rsp), %rsi
movslq 0x114(%rsp), %rdx
callq 0x6440
jmp 0x160a9
movq 0x40(%rsp), %rax
movq 0xd8(%rsp), %rdi
movq 0x118(%rsp), %rsi
movslq 0x114(%rsp), %rcx
addq %rcx, %rsi
movslq 0xcc(%rax), %rdx
callq 0x6440
movq 0x40(%rsp), %rax
movl 0xcc(%rax), %eax
addl 0x114(%rsp), %eax
movl %eax, 0x114(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0x8(%rsp)
leaq 0x87(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x6860
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
leaq 0x88(%rsp), %rdi
callq 0x66f0
jmp 0x160e1
movq 0x160(%rsp), %rdi
movslq 0xd4(%rsp), %rsi
callq 0x8040
movq %rax, %rdi
leaq 0x88(%rsp), %rsi
callq 0x65f0
leaq 0x88(%rsp), %rdi
callq 0x6340
leaq 0x87(%rsp), %rdi
callq 0x6570
movq 0xd8(%rsp), %rdi
movslq 0x168(%rsp), %rdx
xorl %esi, %esi
callq 0x62e0
movl 0xd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
jmp 0x15e99
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x128(%rsp)
movl %eax, 0x124(%rsp)
leaq 0x87(%rsp), %rdi
callq 0x6570
jmp 0x1627a
movq 0xd8(%rsp), %rdi
callq 0x65c0
movq 0x118(%rsp), %rdi
callq 0x65c0
movl 0x158(%rsp), %edi
callq 0x6910
movq 0x40(%rsp), %rax
cmpl $0x0, 0x234(%rax)
jle 0x16260
callq 0x60b0
movq %rax, 0x78(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x108(%rsp)
leaq 0x68(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x62e0
movq 0x40(%rsp), %rax
movq $0x0, 0x68(%rsp)
movslq 0x234(%rax), %rax
movq %rax, 0x70(%rsp)
leaq 0x68(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x61b0
callq 0x60b0
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x100(%rsp)
leaq 0x100(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x7a60
movq %rax, 0x48(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x7a30
movq %rax, 0x50(%rsp)
leaq 0x5c(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x7ab0
movq 0x40(%rsp), %rdi
addq $0x18c, %rdi            # imm = 0x18C
leaq 0x5c(%rsp), %rsi
callq 0x7af0
movl $0x0, 0x184(%rsp)
movl 0x184(%rsp), %eax
addq $0x188, %rsp            # imm = 0x188
retq
movq 0x128(%rsp), %rdi
callq 0x6850
nopw (%rax,%rax)