// File: /workspace/github_cpp_makefile_star2_O2/nmmmnu[P]HM3/hm3/disktable.cc
// Function: hm3::DiskTable::btreeSearch_(StringRef const&, unsigned long&) const
bool DiskTable::btreeSearch_(const StringRef &key, size_type &result) const{
	using Node		= btreeindex::Node;
	using NodeData		= btreeindex::NodeData;

	using branch_type	= btreeindex::branch_type;

	constexpr auto VALUES   = btreeindex::VALUES;
	constexpr auto BRANCHES = btreeindex::BRANCHES;

	size_t const nodesCount = blobTree_.size() / sizeof(Node);

	const Node *nodes = blobTree_.as<const Node>(0, nodesCount);

	if (!nodes){
		// go try with binary search
		log__("Problem, switch to binary search (1)");
		return binarySearch_(key, result);
	}

	std::pair<size_type, size_type> bs{ 0, getCount() };

	size_type pos = 0;

	while(pos < nodesCount){
		const Node &node = nodes[pos];

		// MODIFIED LEVEL ORDERED MINI-BINARY SEARCH INSIDE BTREE NODE
		// OUTPUT PARAMETERS

		branch_type node_index = 0; // no need to have start value, but gcc gives a warning

		// MODIFIED LEVEL ORDERED MINI-BINARY SEARCH INSIDE BTREE NODE
		// CODE
		{
			const auto &ll = g_llholder.value;

			branch_type node_pos = 0;

			do{

				// ACCESS ELEMENT
				// ---
				const uint64_t offset = be64toh(node.values[ node_pos ]);

				if (offset == Node::NIL){
					// special case go left
					/*
					 *   1   0   2
					 * 1 2 n n n n n
					 *
					 *       n
					 *   2       n
					 * 1   n   n   n
					 *
					 */

					node_pos = branch_type(2 * node_pos + 1);

					log__("\t L: NIL");

					continue;
				}

				const NodeData *nd = blobKeys_.as<const NodeData>((size_t) offset);

				if (!nd){
					// go try with binary search
					log__("Problem, switch to binary search (2)");
					return binarySearch_(key, result);
				}

				size_t    const keysize = be16toh(nd->keysize);
				// if file is open, there will be guaranteed this to work on 32 bit system:
				size_type const dataid  = (size_type) be64toh(nd->dataid);

				// key is just after the NodeData
				const char *keyptr = blobKeys_.as<const char>((size_t) offset + sizeof(NodeData), keysize);

				if (!keyptr){
					// go try with binary search
					log__("Problem, switch to binary search (3)");
					return binarySearch_(key, result);
				}

				const StringRef keyx{ keyptr, keysize };
				// ---
				// EO ACCESS ELEMENT

				log__("node [", ll[node_pos], "]: POS:", node_pos, keyx);

				int const cmp = keyx.compare(key);

				if (cmp < 0){
					// this + 1 is because,
					// we want if possible to go LEFT instead of RIGHT
					// node_index will go out of bounds,
					// this is indicator for RIGHT
					node_index = branch_type( ll[node_pos] + 1 );

					// go right
					node_pos = branch_type( 2 * node_pos + 1 + 1 );

					bs.first = dataid + 1;

					log__("\t R:", node_pos, "BS:", bs.first, "-", bs.second, "KEYX", keyx);
				}else if (cmp > 0){
					node_index = ll[node_pos];

					// go left
					node_pos = branch_type( 2 * node_pos + 1 );

					bs.second = dataid;

					log__("\t L:", node_pos, "BS:", bs.first, "-", bs.second, "KEYX", keyx);
				}else{
					// found

					log__("\t F: POS:", node_pos);

					result = dataid;
					return true;
				}
			}while (node_pos < VALUES);
		}
		// EO MODIFIED LEVEL ORDERED MINI-BINARY SEARCH INSIDE BTREE NODE

		if ( node_index == VALUES ){
			// Go Right
			pos = pos * BRANCHES + VALUES + 1;

			log__("BTREE R:", pos);
		}else{
			// Go Left
			pos = pos * BRANCHES + node_index + 1;

			log__("BTREE L:", pos, "node[", node_index, "]");
		}
	}


	// leaf or similar
	// fallback to binary search :)

	log__("BTREE LEAF:", pos);
	log__("Fallback to binary search", bs.first, bs.second, "diff", bs.second - bs.first);

	return binarySearch(*this, bs.first, bs.second, key, BinarySearchCompList{}, result, BIN_SEARCH_MINIMUM_DISTANCE);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r12
addq $0xc0, %rdi
movq 0xc8(%r12), %rax
movl $0x3f8, %ecx            # imm = 0x3F8
xorl %edx, %edx
divq %rcx
movq %rax, %r14
xorl %esi, %esi
movq %rax, %rdx
callq 0x3e54
testq %rax, %rax
je 0x3529
movq %rax, %rcx
movq 0x8(%r12), %rax
movq %rax, 0x38(%rsp)
leaq 0xd0(%r12), %rax
movq %rax, 0x78(%rsp)
movq $0x0, 0x30(%rsp)
xorl %ebp, %ebp
movq %r15, 0x18(%rsp)
movq %r14, 0x50(%rsp)
movq %rcx, 0x48(%rsp)
movq %r12, 0x28(%rsp)
movq %rbx, 0x40(%rsp)
cmpq %r14, %rbp
jae 0x35e9
movq %rbp, 0x70(%rsp)
imulq $0x3f8, %rbp, %rax      # imm = 0x3F8
addq %rcx, %rax
movq %rax, 0x80(%rsp)
xorl %ebp, %ebp
xorl %r14d, %r14d
movzwl %bp, %r13d
movq 0x80(%rsp), %rax
movq (%rax,%r13,8), %rax
cmpq $-0x1, %rax
je 0x33ff
movq 0xd0(%r12), %rcx
testq %rcx, %rcx
je 0x3554
movq 0xd8(%r12), %rdx
testq %rdx, %rdx
je 0x3554
bswapq %rax
leaq 0xa(%rax), %rsi
cmpq %rdx, %rsi
ja 0x3554
movzwl (%rcx,%rax), %edx
rolw $0x8, %dx
movzwl %dx, %ebp
movq 0x2(%rcx,%rax), %r14
movq 0x78(%rsp), %rdi
movq %rbp, %rdx
callq 0x3d54
testq %rax, %rax
je 0x3568
movq %rax, %rbx
bswapq %r14
movq %r14, 0x58(%rsp)
movq %rbp, 0x60(%rsp)
movq %rax, 0x68(%rsp)
leaq 0x1f9c(%rip), %rax      # 0x52c0
movzwl (%rax,%r13,2), %r12d
movq 0x4c80(%rip), %r15      # 0x7fb0
movq %r15, %rdi
leaq 0x1eee(%rip), %rsi      # 0x5228
callq 0x2170
movq %rax, %rdi
leaq 0x1f75(%rip), %r14      # 0x52be
movq %r14, %rsi
callq 0x2170
movl %r12d, 0x24(%rsp)
movzwl %r12w, %esi
movq %r15, %rdi
callq 0x2240
movq %rax, %rdi
movq %r14, %rsi
callq 0x2170
movq %r15, %rdi
leaq 0x1eb8(%rip), %rsi      # 0x522f
callq 0x2170
movq %rax, %rdi
movq %r14, %rsi
callq 0x2170
movq %r15, %rdi
movl %r13d, %esi
callq 0x2240
movq %rax, %rdi
movq %r14, %rsi
callq 0x2170
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x3ac9
leaq 0x60(%rsp), %rdi
movq 0x40(%rsp), %rbx
movq %rbx, %rsi
callq 0x3c14
testl %eax, %eax
js 0x3415
movq 0x58(%rsp), %r15
movl 0x24(%rsp), %r14d
je 0x358a
leal 0x1(,%r13,2), %eax
movups 0x60(%rsp), %xmm0
movups %xmm0, (%rsp)
movzwl %ax, %ebp
leaq 0x1e5c(%rip), %rdi      # 0x5245
movl %ebp, %esi
movq 0x30(%rsp), %rdx
movq %r15, %rcx
callq 0x36ba
movq %r15, 0x38(%rsp)
jmp 0x3454
leal 0x1(,%r13,2), %ebp
leaq 0x1dc7(%rip), %rdi      # 0x51d5
callq 0x3695
jmp 0x3459
movl 0x24(%rsp), %r14d
incl %r14d
leal 0x2(,%r13,2), %eax
movq 0x58(%rsp), %r12
incq %r12
movups 0x60(%rsp), %xmm0
movups %xmm0, (%rsp)
movzwl %ax, %ebp
leaq 0x1df7(%rip), %rdi      # 0x5237
movl %ebp, %esi
movq %r12, %rdx
movq 0x38(%rsp), %rcx
callq 0x36ba
movq %r12, 0x30(%rsp)
movq 0x28(%rsp), %r12
cmpw $0x7f, %bp
jb 0x3296
movq 0x70(%rsp), %rbp
shlq $0x7, %rbp
movl %r14d, %eax
cmpw $0x7f, %ax
jne 0x348d
subq $-0x80, %rbp
leaq 0x1dd4(%rip), %rdi      # 0x5254
movq %rbp, %rsi
callq 0x37aa
jmp 0x3515
movq %rbx, %r14
movzwl %ax, %ebx
addq %rbx, %rbp
incq %rbp
movq 0x4b10(%rip), %r15      # 0x7fb0
movq %r15, %rdi
leaq 0x1db3(%rip), %rsi      # 0x525d
callq 0x2170
movq %rax, %rdi
leaq 0x1e05(%rip), %r13      # 0x52be
movq %r13, %rsi
callq 0x2170
movq %r15, %rdi
movq %rbp, %rsi
callq 0x20e0
movq %rax, %rdi
movq %r13, %rsi
callq 0x2170
movq %r15, %rdi
leaq 0x1d85(%rip), %rsi      # 0x5266
callq 0x2170
movq %rax, %rdi
movq %r13, %rsi
callq 0x2170
movq %r15, %rdi
movl %ebx, %esi
movq %r14, %rbx
callq 0x2240
movq %rax, %rdi
movq %r13, %rsi
callq 0x2170
leaq 0x1d5c(%rip), %rdi      # 0x526c
callq 0x3695
movq 0x18(%rsp), %r15
movq 0x50(%rsp), %r14
movq 0x48(%rsp), %rcx
jmp 0x3271
leaq 0x1c80(%rip), %rdi      # 0x51b0
callq 0x3695
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rdx
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3bf8
leaq 0x1c83(%rip), %rdi      # 0x51de
callq 0x3695
movq %r12, %rdi
movq %rbx, %rsi
jmp 0x357e
leaq 0x1c94(%rip), %rdi      # 0x5203
callq 0x3695
movq 0x28(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x3bf8
jmp 0x35d7
movq 0x4a1f(%rip), %rbx      # 0x7fb0
leaq 0x1cb2(%rip), %rsi      # 0x524a
movq %rbx, %rdi
callq 0x2170
leaq 0x1d17(%rip), %r14      # 0x52be
movq %rax, %rdi
movq %r14, %rsi
callq 0x2170
movq %rbx, %rdi
movl %r13d, %esi
callq 0x2240
movq %rax, %rdi
movq %r14, %rsi
callq 0x2170
callq 0x3aba
movq 0x18(%rsp), %rax
movq %r15, (%rax)
movb $0x1, %al
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c7e(%rip), %rdi      # 0x526e
movq %rbp, %rsi
callq 0x37aa
movq 0x38(%rsp), %r13
movq %r13, %rbx
movq 0x30(%rsp), %rbp
subq %rbp, %rbx
movq 0x49a1(%rip), %r14      # 0x7fb0
leaq 0x1c64(%rip), %rsi      # 0x527a
movq %r14, %rdi
callq 0x2170
leaq 0x1c99(%rip), %r12      # 0x52be
movq %rax, %rdi
movq %r12, %rsi
callq 0x2170
movq %r14, %rdi
movq %rbp, %rsi
callq 0x20e0
movq %rax, %rdi
movq %r12, %rsi
callq 0x2170
movq %r14, %rdi
movq %r13, %rsi
callq 0x20e0
movq %rax, %rdi
movq %r12, %rsi
callq 0x2170
leaq 0x1c31(%rip), %rdi      # 0x5294
movq %rbx, %rsi
callq 0x37aa
movq $0x3, (%rsp)
leaq 0x60(%rsp), %r8
movq 0x28(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rdx
movq 0x40(%rsp), %rcx
movq %r15, %r9
callq 0x3c20
jmp 0x35d7