// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %rbx
movl %edx, %r13d
movq %rsi, %r15
movq (%rsi), %rdi
movl $0x101002, %esi         # imm = 0x101002
movl $0x1a4, %edx            # imm = 0x1A4
xorl %eax, %eax
callq 0x52f0
testl %eax, %eax
jle 0xddbc
movl %eax, %r14d
callq 0x56e0
movslq %eax, %rsi
movq %rsp, %r15
movl $0x1000, %edx           # imm = 0x1000
movq %r15, %rdi
callq 0x5200
movq %rbx, 0x10(%rsp)
movq (%r15), %r12
xorl %ebp, %ebp
movl $0x1000, %edx           # imm = 0x1000
movq %r12, %rdi
xorl %esi, %esi
callq 0x5240
movl $0x1000, %edx           # imm = 0x1000
movl %r14d, %edi
movq %r12, %rsi
callq 0x5590
movq (%r15), %r12
movslq 0x4(%r12), %r15
movl $0x1000, %edx           # imm = 0x1000
movq %r12, %rdi
xorl %esi, %esi
callq 0x5240
movl $0x1000, %edx           # imm = 0x1000
movl %r14d, 0xc(%rsp)
movl %r14d, %edi
movq %r12, %rsi
movq 0x10(%rsp), %r12
callq 0x5590
movl %r13d, 0x8(%rsp)
testl %r13d, %r13d
jle 0xdd7c
movl 0x8(%rsp), %ebx
xorl %r14d, %r14d
leal (%r15,%rbp), %r13d
cmpl $0x1000, %r13d          # imm = 0x1000
jl 0xdd51
xorl %r12d, %r12d
cmpl $0xfff, %ebp            # imm = 0xFFF
jg 0xdd24
movq 0x10(%rsp), %rax
movq (%rax,%r14,8), %rdi
movslq %ebp, %rsi
addq (%rsp), %rsi
movl $0x1000, %r12d          # imm = 0x1000
subl %ebp, %r12d
movq %r12, %rdx
callq 0x5330
movq (%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movl 0xc(%rsp), %edi
callq 0x5590
movl %r15d, %r13d
subl %r12d, %r13d
movl %r12d, %edi
movq 0x10(%rsp), %r12
addq (%r12,%r14,8), %rdi
movq (%rsp), %rsi
movslq %r13d, %rdx
jmp 0xdd5f
movq (%r12,%r14,8), %rdi
movslq %ebp, %rsi
addq (%rsp), %rsi
movq %r15, %rdx
callq 0x5330
movq (%r12,%r14,8), %rax
movb $0x0, (%rax,%r15)
incq %r14
movl %r13d, %ebp
cmpq %r14, %rbx
jne 0xdceb
movslq 0x8(%rsp), %r14
movq (%r12,%r14,8), %rdi
movl $0x7a, %esi
movq %r15, %rdx
callq 0x5240
movq (%r12,%r14,8), %rax
movb $0x0, (%rax,%r15)
movq (%rsp), %rdi
callq 0x5490
movl 0xc(%rsp), %edi
callq 0x5740
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2b20d(%rip), %rbx     # 0x38fd0
leaq 0x1b7b5(%rip), %rsi     # 0x2957f
movl $0x1a, %edx
movq %rbx, %rdi
callq 0x5470
movq (%r15), %rsi
movq 0x8(%r15), %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
callq 0x5060
movl (%rax), %edi
callq 0x5170
leaq 0x1b752(%rip), %rdi     # 0x29575
movq %rax, %rsi
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5070
nop