// File: /workspace/github_cpp_makefile_star2_O2/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, %r14d
movq %rsi, %r15
movq (%rsi), %rdi
xorl %r13d, %r13d
movl $0x101002, %esi         # imm = 0x101002
movl $0x1a4, %edx            # imm = 0x1A4
xorl %eax, %eax
callq 0x6300
testl %eax, %eax
jle 0xbcce
movl %eax, %ebp
callq 0x66e0
movslq %eax, %rsi
movq %rsp, %r15
movl $0x1000, %edx           # imm = 0x1000
movq %r15, %rdi
callq 0x6230
movl %r14d, 0x8(%rsp)
movq (%r15), %r12
movl $0x1000, %edx           # imm = 0x1000
movq %r12, %rdi
xorl %esi, %esi
callq 0x6260
movl $0x1000, %edx           # imm = 0x1000
movl %ebp, %edi
movq %r12, %rsi
callq 0x65a0
movq (%r15), %r12
movslq 0x4(%r12), %r15
movl $0x1000, %edx           # imm = 0x1000
movq %r12, %rdi
xorl %esi, %esi
callq 0x6260
movl $0x1000, %edx           # imm = 0x1000
movl %ebp, 0xc(%rsp)
movl %ebp, %edi
movq %r12, %rsi
movl 0x8(%rsp), %r12d
callq 0x65a0
testl %r12d, %r12d
movl $0x0, %eax
cmovgl %r12d, %eax
movq %rax, 0x10(%rsp)
xorl %ebp, %ebp
cmpq %rbp, 0x10(%rsp)
je 0xbc92
leal (%r15,%r13), %r14d
movslq %r13d, %rsi
cmpl $0x1000, %r14d          # imm = 0x1000
jl 0xbc6e
xorl %r12d, %r12d
cmpl $0xfff, %r13d           # imm = 0xFFF
jg 0xbc3c
movq (%rbx,%rbp,8), %rdi
addq (%rsp), %rsi
movl $0x1000, %r12d          # imm = 0x1000
subl %r13d, %r12d
movq %r12, %rdx
callq 0x6350
movq (%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movl 0xc(%rsp), %edi
callq 0x65a0
movl %r15d, %r14d
subl %r12d, %r14d
movl %r12d, %edi
addq (%rbx,%rbp,8), %rdi
movq (%rsp), %rsi
movslq %r14d, %rdx
callq 0x6350
movl 0x8(%rsp), %r12d
jmp 0xbc7e
movq (%rbx,%rbp,8), %rdi
addq (%rsp), %rsi
movq %r15, %rdx
callq 0x6350
movq (%rbx,%rbp,8), %rax
movb $0x0, (%rax,%r15)
incq %rbp
movl %r14d, %r13d
jmp 0xbbfc
movslq %r12d, %r14
movq (%rbx,%r14,8), %rdi
pushq $0x7a
popq %rsi
movq %r15, %rdx
callq 0x6260
movq (%rbx,%r14,8), %rax
movb $0x0, (%rax,%r15)
movq (%rsp), %rdi
callq 0x6490
movl 0xc(%rsp), %edi
callq 0x6750
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x292f3(%rip), %rdi     # 0x34fc8
leaq 0x16897(%rip), %rsi     # 0x22573
callq 0x6410
movq %rax, %rdi
movq %r15, %rsi
callq 0x63b0
movq %rax, %rdi
callq 0x6210
callq 0x6050
movl (%rax), %edi
callq 0x6170
leaq 0x16862(%rip), %rdi     # 0x22569
movq %rax, %rsi
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6060
nop