// File: BU-DiSC[P]BF-Shared-Hashing/db.cpp
// Function: db::read_index(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, char**)
inline void db::read_index( string filename, int size, char** index)
{
	//int flags = O_RDWR | O_DIRECT | O_SYNC;
	int flags = O_RDWR | O_SYNC;
	mode_t mode=S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;//644

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

	char* buf;
	posix_memalign((void**)&buf,getpagesize(),DB_PAGE_SIZE);

	memset(buf, 0, DB_PAGE_SIZE);
	read( fd, buf, DB_PAGE_SIZE );
	
	int key_size;
	memcpy(&key_size, buf+sizeof(unsigned int), sizeof(unsigned int));	

	int offset = 0;

	memset(buf, 0, DB_PAGE_SIZE);
	read( fd, buf, DB_PAGE_SIZE );

	for(int i = 0; i < size; i++){
		if(offset + key_size >= DB_PAGE_SIZE){
			unsigned int inner_key_offset = 0;
			if(offset < DB_PAGE_SIZE){
				memcpy(index[i], buf+offset,DB_PAGE_SIZE-offset);
				inner_key_offset = DB_PAGE_SIZE-offset;
			}
		
			read( fd, buf, DB_PAGE_SIZE );

			offset = key_size - inner_key_offset;	
			memcpy(index[i]+inner_key_offset, buf, offset);
		}else{
			memcpy(index[i], buf+offset, key_size);
			offset += key_size;
		}
		index[i][key_size] = '\0';
		
	}
	memset(index[size], 'z', key_size);
	index[size][key_size] = '\0';

	free( buf );
	close( fd );
	return;
}

// Assembly:
subq $0x58, %rsp
movq %rsi, (%rsp)
movq %rdi, %rax
movq (%rsp), %rdi
movq %rdi, 0x8(%rsp)
movq %rax, 0x50(%rsp)
movq %rdi, 0x48(%rsp)
movl %edx, 0x44(%rsp)
movq %rcx, 0x38(%rsp)
movl $0x101002, 0x34(%rsp)   # imm = 0x101002
movl $0x1a4, 0x30(%rsp)      # imm = 0x1A4
callq 0x6250
movq %rax, %rdi
movl 0x34(%rsp), %esi
movl 0x30(%rsp), %edx
movb $0x0, %al
callq 0x63e0
movl %eax, 0x2c(%rsp)
cmpl $0x0, 0x2c(%rsp)
jg 0x1286a
movq 0x6d7a0(%rip), %rdi     # 0x7ffc0
leaq 0x51d3d(%rip), %rsi     # 0x64564
callq 0x6500
movq 0x8(%rsp), %rsi
movq %rax, %rdi
callq 0x6490
movq %rax, %rdi
movq 0x6d765(%rip), %rsi     # 0x7ffa8
callq 0x6540
callq 0x6090
movl (%rax), %edi
callq 0x61e0
movq %rax, %rsi
leaq 0x51cfc(%rip), %rdi     # 0x6455a
movb $0x0, %al
callq 0x60a0
jmp 0x12a35
callq 0x6890
movslq %eax, %rsi
leaq 0x20(%rsp), %rdi
movl $0x1000, %edx           # imm = 0x1000
callq 0x62c0
movq 0x20(%rsp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x62e0
movl 0x2c(%rsp), %edi
movq 0x20(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x6730
movq 0x20(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, 0x1c(%rsp)
movl $0x0, 0x18(%rsp)
movq 0x20(%rsp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x62e0
movl 0x2c(%rsp), %edi
movq 0x20(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x6730
movl $0x0, 0x14(%rsp)
movl 0x14(%rsp), %eax
cmpl 0x44(%rsp), %eax
jge 0x129ee
movl 0x18(%rsp), %eax
addl 0x1c(%rsp), %eax
cmpl $0x1000, %eax           # imm = 0x1000
jl 0x12996
movl $0x0, 0x10(%rsp)
cmpl $0x1000, 0x18(%rsp)     # imm = 0x1000
jge 0x12951
movq 0x38(%rsp), %rax
movslq 0x14(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movq 0x20(%rsp), %rsi
movslq 0x18(%rsp), %rax
addq %rax, %rsi
movl $0x1000, %eax           # imm = 0x1000
subl 0x18(%rsp), %eax
movslq %eax, %rdx
callq 0x6440
movl $0x1000, %eax           # imm = 0x1000
subl 0x18(%rsp), %eax
movl %eax, 0x10(%rsp)
movl 0x2c(%rsp), %edi
movq 0x20(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x6730
movl 0x1c(%rsp), %eax
subl 0x10(%rsp), %eax
movl %eax, 0x18(%rsp)
movq 0x38(%rsp), %rax
movslq 0x14(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movl 0x10(%rsp), %eax
addq %rax, %rdi
movq 0x20(%rsp), %rsi
movslq 0x18(%rsp), %rdx
callq 0x6440
jmp 0x129c7
movq 0x38(%rsp), %rax
movslq 0x14(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movq 0x20(%rsp), %rsi
movslq 0x18(%rsp), %rax
addq %rax, %rsi
movslq 0x1c(%rsp), %rdx
callq 0x6440
movl 0x1c(%rsp), %eax
addl 0x18(%rsp), %eax
movl %eax, 0x18(%rsp)
movq 0x38(%rsp), %rax
movslq 0x14(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x1c(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x128e5
movq 0x38(%rsp), %rax
movslq 0x44(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movslq 0x1c(%rsp), %rdx
movl $0x7a, %esi
callq 0x62e0
movq 0x38(%rsp), %rax
movslq 0x44(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x1c(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movq 0x20(%rsp), %rdi
callq 0x65c0
movl 0x2c(%rsp), %edi
callq 0x6910
addq $0x58, %rsp
retq
nopw (%rax,%rax)