// File: /workspace/github_cpp_makefile_star2_O1/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
movq 0xc0(%rdi), %rsi
movq 0xc8(%rdi), %rcx
movq %rcx, %rax
shrq $0x3, %rax
movabsq $0x2040810204081021, %rdx # imm = 0x2040810204081021
mulq %rdx
shrq $0x4, %rdx
movq %rdx, 0x78(%rsp)
imulq $0x3f8, %rdx, %rax      # imm = 0x3F8
decq %rax
cmpq %rcx, %rax
setae %al
movq %rsi, 0x70(%rsp)
testq %rsi, %rsi
sete %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x3613
leaq 0x1bc4(%rip), %rdi      # 0x51b0
callq 0x3b22
movq 0x8(%r12), %rdx
movq $0x3, (%rsp)
leaq 0x38(%rsp), %r8
movq %r12, %rdi
xorl %esi, %esi
movq %rbx, %rcx
movq %r15, %r9
jmp 0x3b01
movq %r15, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movq 0x8(%r12), %rax
movq %rax, 0x50(%rsp)
movq $0x0, 0x48(%rsp)
leaq 0x38(%rsp), %r13
xorl %ebx, %ebx
movq %r12, 0x58(%rsp)
cmpq 0x78(%rsp), %rbx
jae 0x3a48
movq %rbx, 0x60(%rsp)
imulq $0x3f8, %rbx, %rax      # imm = 0x3F8
addq 0x70(%rsp), %rax
movq %rax, 0x80(%rsp)
xorl %r14d, %r14d
xorl %eax, %eax
movq %rax, 0x30(%rsp)
movzwl %r14w, %ebx
movq 0x80(%rsp), %rax
movq (%rax,%rbx,8), %rax
cmpq $-0x1, %rax
je 0x36e8
movq 0xd0(%r12), %rcx
movq 0xd8(%r12), %rdx
testq %rcx, %rcx
sete %dil
testq %rdx, %rdx
sete %sil
orb %dil, %sil
jne 0x3703
bswapq %rax
leaq 0xa(%rax), %r12
cmpq %rdx, %r12
ja 0x3703
movzwl (%rcx,%rax), %edi
movl %edi, %r8d
rolw $0x8, %r8w
movzwl %r8w, %ebp
testw %di, %di
sete %dil
orb %dil, %sil
leaq (%r12,%rbp), %rdi
cmpq %rdx, %rdi
seta %dl
orb %sil, %dl
cmpb $0x1, %dl
jne 0x3763
leaq 0x1b1d(%rip), %rdi      # 0x5203
jmp 0x370a
leal 0x1(,%r14,2), %r14d
leaq 0x1ade(%rip), %rdi      # 0x51d5
callq 0x3b22
movl $0x5, %ebp
jmp 0x3741
leaq 0x1ad4(%rip), %rdi      # 0x51de
callq 0x3b22
movq 0x58(%rsp), %r12
movq 0x8(%r12), %rdx
movq $0x3, (%rsp)
movq %r12, %rdi
xorl %esi, %esi
movq 0x28(%rsp), %rcx
movq %r13, %r8
movq 0x20(%rsp), %r9
callq 0x419e
movb %al, 0x1a(%rsp)
movl $0x1, %ebp
movq 0x30(%rsp), %rax
cmpl $0x5, %ebp
je 0x3753
testl %ebp, %ebp
jne 0x395f
cmpw $0x7f, %r14w
jb 0x3665
jmp 0x395d
movq 0x2(%rcx,%rax), %rax
bswapq %rax
movq %rax, 0x68(%rsp)
addq %rcx, %r12
movq %rbp, 0x38(%rsp)
movq %r12, 0x40(%rsp)
leaq 0x1b3c(%rip), %rax      # 0x52c0
movzwl (%rax,%rbx,2), %r15d
movl $0x6, %edx
movq 0x481b(%rip), %r13      # 0x7fb0
movq %r13, %rdi
leaq 0x1a89(%rip), %rsi      # 0x5228
callq 0x2190
movl $0x1, %edx
movq %r13, %rdi
leaq 0x1b0b(%rip), %rsi      # 0x52be
callq 0x2190
movl %r15d, 0x1c(%rsp)
movzwl %r15w, %esi
movq %r13, %rdi
callq 0x20d0
movl $0x1, %edx
movq %rax, %rdi
leaq 0x1ae6(%rip), %r15      # 0x52be
movq %r15, %rsi
callq 0x2190
movl $0x7, %edx
movq %r13, %rdi
leaq 0x1a40(%rip), %rsi      # 0x522f
callq 0x2190
movl $0x1, %edx
movq %r13, %rdi
movq %r15, %rsi
callq 0x2190
movq %r13, %rdi
leaq 0x38(%rsp), %r13
movq %rbx, %rsi
callq 0x20d0
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x2190
movq %rbp, %rdi
movq %r12, %rsi
callq 0x4158
movq 0x28(%rsp), %rax
movq (%rax), %rdx
movq 0x8(%rax), %rsi
movq %r13, %rdi
callq 0x4290
testl %eax, %eax
js 0x3888
movl 0x1c(%rsp), %r15d
movq 0x68(%rsp), %r12
je 0x38cd
leal 0x1(,%r14,2), %r14d
movups 0x38(%rsp), %xmm0
movups %xmm0, (%rsp)
movzwl %r14w, %esi
leaq 0x19d6(%rip), %rdi      # 0x5245
movq 0x48(%rsp), %rdx
movq %r12, %rcx
callq 0x3b9c
xorl %ebp, %ebp
movq %r12, 0x50(%rsp)
jmp 0x3950
movl 0x1c(%rsp), %r15d
incl %r15d
leal 0x2(,%r14,2), %r14d
movq 0x68(%rsp), %rbx
incq %rbx
movups 0x38(%rsp), %xmm0
movups %xmm0, (%rsp)
movzwl %r14w, %esi
leaq 0x1983(%rip), %rdi      # 0x5237
movq %rbx, %rdx
movq 0x50(%rsp), %rcx
callq 0x3b9c
xorl %ebp, %ebp
movq %rbx, 0x48(%rsp)
jmp 0x3950
movl $0x9, %edx
movq 0x46d7(%rip), %r15      # 0x7fb0
movq %r15, %rdi
leaq 0x1967(%rip), %rsi      # 0x524a
callq 0x2190
movl $0x1, %edx
movq %r15, %rdi
leaq 0x19c7(%rip), %r13      # 0x52be
movq %r13, %rsi
callq 0x2190
movq %r15, %rdi
movq %rbx, %rsi
callq 0x20d0
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
leaq 0x38(%rsp), %r13
callq 0x2190
movb $0xa, 0x1b(%rsp)
movl $0x1, %edx
movq %r15, %rdi
leaq 0x1b(%rsp), %rsi
callq 0x2190
movq 0x20(%rsp), %rax
movq %r12, (%rax)
movb $0x1, 0x1a(%rsp)
movl $0x1, %ebp
movq 0x30(%rsp), %rax
movl %eax, %r15d
movl %r15d, %eax
movq 0x58(%rsp), %r12
jmp 0x3746
xorl %ebp, %ebp
testl %ebp, %ebp
je 0x396d
movq 0x60(%rsp), %rbx
jmp 0x3a3b
movq 0x60(%rsp), %rbx
shlq $0x7, %rbx
cmpw $0x7f, %ax
jne 0x3994
subq $-0x80, %rbx
leaq 0x18cd(%rip), %rdi      # 0x5254
movq %rbx, %rsi
callq 0x3cf1
jmp 0x3a3b
movzwl %ax, %r15d
addq %r15, %rbx
incq %rbx
movl $0x8, %edx
movq 0x4606(%rip), %r14      # 0x7fb0
movq %r14, %rdi
leaq 0x18a9(%rip), %rsi      # 0x525d
callq 0x2190
movl $0x1, %edx
movq %r14, %rdi
leaq 0x18f6(%rip), %r13      # 0x52be
movq %r13, %rsi
callq 0x2190
movq %r14, %rdi
movq %rbx, %rsi
callq 0x20d0
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x2190
movl $0x5, %edx
movq %r14, %rdi
leaq 0x186c(%rip), %rsi      # 0x5266
callq 0x2190
movl $0x1, %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0x2190
movq %r14, %rdi
movq %r15, %rsi
callq 0x20d0
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
leaq 0x38(%rsp), %r13
callq 0x2190
leaq 0x1836(%rip), %rdi      # 0x526c
callq 0x3b22
testl %ebp, %ebp
je 0x363c
jmp 0x3b0a
leaq 0x181f(%rip), %rdi      # 0x526e
movq %rbx, %rsi
callq 0x3cf1
movq 0x50(%rsp), %rbp
movq %rbp, %rbx
movq 0x48(%rsp), %r14
subq %r14, %rbx
movq %r12, %r15
movq 0x453f(%rip), %r12      # 0x7fb0
leaq 0x1802(%rip), %rsi      # 0x527a
movl $0x19, %edx
movq %r12, %rdi
callq 0x2190
leaq 0x1832(%rip), %r13      # 0x52be
movl $0x1, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x2190
movq %r12, %rdi
movq %r14, %rsi
callq 0x20d0
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x2190
movq %r12, %rdi
movq %rbp, %rsi
callq 0x20d0
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x2190
leaq 0x17bb(%rip), %rdi      # 0x5294
movq %rbx, %rsi
callq 0x3cf1
movq $0x3, (%rsp)
leaq 0x38(%rsp), %r8
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq 0x28(%rsp), %rcx
movq 0x20(%rsp), %r9
callq 0x419e
movb %al, 0x1a(%rsp)
movb 0x1a(%rsp), %al
andb $0x1, %al
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq