// File: /workspace/github_cpp_makefile_star2_O3/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 $0x78, %rsp
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, %r12
movq 0xc0(%rdi), %rcx
movq 0xc8(%rdi), %rsi
movq %rsi, %rax
shrq $0x3, %rax
movabsq $0x2040810204081021, %rdx # imm = 0x2040810204081021
mulq %rdx
shrq $0x4, %rdx
imulq $0x3f8, %rdx, %rax      # imm = 0x3F8
testq %rcx, %rcx
sete %r8b
cmpq $0x3f8, %rsi            # imm = 0x3F8
setb %dil
orb %r8b, %dil
cmpq %rsi, %rax
seta %al
xorl %esi, %esi
orb %dil, %al
cmoveq %rcx, %rsi
cmpb $0x1, %al
jne 0x35e4
leaq 0x1bf3(%rip), %rdi      # 0x51b0
callq 0x3a76
movq 0x8(%r12), %rdx
movq $0x3, (%rsp)
leaq 0x20(%rsp), %r8
movq %r12, %rdi
xorl %esi, %esi
movq %r13, %rcx
movq %rbx, %r9
jmp 0x39f4
movq %rbx, 0x18(%rsp)
movq 0x8(%r12), %rax
movq %rax, 0x38(%rsp)
xorl %ebx, %ebx
movq $0x0, 0x30(%rsp)
movq %r12, 0x40(%rsp)
movq %rdx, 0x60(%rsp)
movq %rsi, 0x48(%rsp)
movq %r13, 0x50(%rsp)
movq %rbx, 0x68(%rsp)
imulq $0x3f8, %rbx, %rax      # imm = 0x3F8
addq %rsi, %rax
movq %rax, 0x70(%rsp)
xorl %ebp, %ebp
xorl %r14d, %r14d
movzwl %bp, %ebp
movq 0x70(%rsp), %rax
movq (%rax,%rbp,8), %rax
cmpq $-0x1, %rax
je 0x37b0
movq 0xd0(%r12), %rcx
testq %rcx, %rcx
je 0x399f
movq 0xd8(%r12), %rdx
testq %rdx, %rdx
je 0x399f
bswapq %rax
leaq 0xa(%rax), %r14
cmpq %rdx, %r14
ja 0x399f
movzwl (%rcx,%rax), %esi
testw %si, %si
je 0x39c7
rolw $0x8, %si
movzwl %si, %r13d
leaq (%r14,%r13), %rsi
cmpq %rdx, %rsi
ja 0x39c7
movq 0x2(%rcx,%rax), %rax
bswapq %rax
movq %rax, 0x58(%rsp)
addq %rcx, %r14
movq %r13, 0x20(%rsp)
movq %r14, 0x28(%rsp)
leaq 0x1c0a(%rip), %rax      # 0x52c0
movzwl (%rax,%rbp,2), %ebx
movl $0x6, %edx
movq 0x48ea(%rip), %r15      # 0x7fb0
movq %r15, %rdi
leaq 0x1b58(%rip), %rsi      # 0x5228
callq 0x2190
movl $0x1, %edx
movq %r15, %rdi
leaq 0x1bda(%rip), %r12      # 0x52be
movq %r12, %rsi
callq 0x2190
movl %ebx, 0x14(%rsp)
movzwl %bx, %esi
movq %r15, %rdi
callq 0x20d0
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x2190
movl $0x7, %edx
movq %r15, %rdi
leaq 0x1b15(%rip), %rsi      # 0x522f
callq 0x2190
movl $0x1, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x2190
movq %r15, %rdi
movq %rbp, %rsi
callq 0x20d0
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x2190
movq %r13, %rdi
movq %r14, %rsi
callq 0x40a6
movq 0x50(%rsp), %r13
movq (%r13), %rdx
movq 0x8(%r13), %rsi
leaq 0x20(%rsp), %rdi
callq 0x41bc
testl %eax, %eax
js 0x37c5
movl 0x14(%rsp), %r14d
movq 0x58(%rsp), %r15
je 0x3a08
leal 0x1(,%rbp,2), %eax
movups 0x20(%rsp), %xmm0
movups %xmm0, (%rsp)
movzwl %ax, %ebp
leaq 0x1aab(%rip), %rdi      # 0x5245
movl %ebp, %esi
movq 0x30(%rsp), %rdx
movq %r15, %rcx
callq 0x3af0
movq %r15, 0x38(%rsp)
jmp 0x3803
leal 0x1(,%rbp,2), %ebp
leaq 0x1a17(%rip), %rdi      # 0x51d5
callq 0x3a76
jmp 0x3808
movl 0x14(%rsp), %r14d
incl %r14d
leal 0x2(,%rbp,2), %eax
movq 0x58(%rsp), %r12
incq %r12
movups 0x20(%rsp), %xmm0
movups %xmm0, (%rsp)
movzwl %ax, %ebp
leaq 0x1a48(%rip), %rdi      # 0x5237
movl %ebp, %esi
movq %r12, %rdx
movq 0x38(%rsp), %rcx
callq 0x3af0
movq %r12, 0x30(%rsp)
movq 0x40(%rsp), %r12
cmpw $0x7f, %bp
jb 0x362b
movq 0x68(%rsp), %rbx
shlq $0x7, %rbx
cmpw $0x7f, %r14w
jne 0x383a
subq $-0x80, %rbx
leaq 0x1a27(%rip), %rdi      # 0x5254
movq %rbx, %rsi
callq 0x3c45
jmp 0x38dc
movzwl %r14w, %r14d
addq %r14, %rbx
incq %rbx
movl $0x8, %edx
movq 0x4760(%rip), %r15      # 0x7fb0
movq %r15, %rdi
leaq 0x1a03(%rip), %rsi      # 0x525d
callq 0x2190
movl $0x1, %edx
movq %r15, %rdi
leaq 0x1a50(%rip), %rbp      # 0x52be
movq %rbp, %rsi
callq 0x2190
movq %r15, %rdi
movq %rbx, %rsi
callq 0x20d0
movl $0x1, %edx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x2190
movl $0x5, %edx
movq %r15, %rdi
leaq 0x19c6(%rip), %rsi      # 0x5266
callq 0x2190
movl $0x1, %edx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2190
movq %r15, %rdi
movq %r14, %rsi
callq 0x20d0
movl $0x1, %edx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x2190
leaq 0x1995(%rip), %rdi      # 0x526c
callq 0x3a76
movq 0x48(%rsp), %rsi
movq 0x60(%rsp), %rdx
cmpq %rdx, %rbx
jb 0x3612
leaq 0x1978(%rip), %rdi      # 0x526e
movq %rbx, %rsi
callq 0x3c45
movq 0x38(%rsp), %rbp
movq %rbp, %rbx
movq 0x30(%rsp), %r15
subq %r15, %rbx
movq 0x469b(%rip), %r14      # 0x7fb0
leaq 0x195e(%rip), %rsi      # 0x527a
movl $0x19, %edx
movq %r14, %rdi
callq 0x2190
leaq 0x198e(%rip), %r12      # 0x52be
movl $0x1, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x2190
movq %r14, %rdi
movq %r15, %rsi
callq 0x20d0
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x2190
movq %r14, %rdi
movq %rbp, %rsi
callq 0x20d0
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x2190
leaq 0x1917(%rip), %rdi      # 0x5294
movq %rbx, %rsi
callq 0x3c45
movq $0x3, (%rsp)
leaq 0x20(%rsp), %r8
movq 0x40(%rsp), %rdi
movq %r15, %rsi
movq %rbp, %rdx
jmp 0x39c2
leaq 0x1838(%rip), %rdi      # 0x51de
callq 0x3a76
movq 0x8(%r12), %rdx
movq $0x3, (%rsp)
leaq 0x20(%rsp), %r8
movq %r12, %rdi
xorl %esi, %esi
movq %r13, %rcx
jmp 0x39ef
leaq 0x1835(%rip), %rdi      # 0x5203
callq 0x3a76
movq 0x8(%r12), %rdx
movq $0x3, (%rsp)
leaq 0x20(%rsp), %r8
movq %r12, %rdi
xorl %esi, %esi
movq 0x50(%rsp), %rcx
movq 0x18(%rsp), %r9
callq 0x40ec
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x45a1(%rip), %rbx      # 0x7fb0
leaq 0x1834(%rip), %rsi      # 0x524a
movl $0x9, %edx
movq %rbx, %rdi
callq 0x2190
leaq 0x1894(%rip), %r14      # 0x52be
movl $0x1, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2190
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x20d0
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x2190
leaq 0x13(%rsp), %rsi
movb $0xa, (%rsi)
movl $0x1, %edx
movq %rbx, %rdi
callq 0x2190
movq 0x18(%rsp), %rax
movq %r15, (%rax)
movb $0x1, %al
jmp 0x39f9