// File: /workspace/github_cpp_makefile_star2_O2/narula[P]skiplists/skiplist-extra.cc
// Function: SkipList::fastFindTest(int)
int SkipList::fastFindTest(int key) {
  node* pred = head;
  int lFound = -1;
  node *preds[max_level][FANOUT], *succs[max_level][FANOUT];
  for (int i = 0; i < max_level; i++) {
	for (int k = 0; k < FANOUT; k++) {
	  preds[i][k] = NULL;
	}
  }	
  for (int level = pred->topLevel-1; level >= 0; level--) {
	node* curr = pred->nexts[level][FANOUT-1].nxt;
	next* prevFour = NULL;
	while (key > pred->nexts[level][FANOUT-1].prefix) {
	  prevFour = pred->nexts[level];
	  pred = curr;
	  curr = pred->nexts[level][FANOUT-1].nxt;
	  inc();
	}
	int extra = 0;
	while (key > pred->nexts[level][extra].prefix) extra++;
	if (lFound == -1 && key == pred->nexts[level][extra].prefix) {
	  lFound = level;
	}
	node* prev = pred;
	if (extra > 0) {
	  prev = pred->nexts[level][extra-1].nxt;
	}
	for (int i = 0; i < FANOUT; i++) {
	  succs[level][i] = prev->nexts[level][i].nxt;
	}
	// get the ones right before (extra)
	for (int i = 0; i < extra; i++) {
	  preds[level][i] = pred->nexts[level][extra-i-1].nxt;
	}
	if (prevFour == NULL) {
	  // did not make it past head, later in preds will be NULL.
	  preds[level][extra] = pred;
	} else {
	  // either get last n where n = FANOUT-extra)  or whole thing backwards.
	  for (int i = extra; i < FANOUT; i++) {
		preds[level][i] = prevFour[FANOUT-i-1+extra].nxt;
	  }
	  inc();
	  // reset pred to be before
	  pred = preds[level][FANOUT-1];
	}

	// ARGH FIX.
	if (level > 0) {
	  for (int i = 0; i < FANOUT; i++) {
	  	if (preds[level] && preds[level][i] && 
	  		preds[level][i]->nexts[level-1][FANOUT-1].prefix <= key) {
	  	  pred = preds[level][i];
	  	  break;
	  	}
	  }
	}
  }
  return lFound;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq (%rdi), %r11
movq %rdi, -0x38(%rbp)
movl 0x10(%rdi), %edx
movq %rsp, %rdi
movq %rdx, %rax
shlq $0x5, %rax
subq %rax, %rdi
movq %rdi, %rsp
xorl %eax, %eax
testl %edx, %edx
movl $0x0, %ecx
cmovgl %edx, %ecx
movq %rdi, -0x48(%rbp)
movq %rdi, %rdx
cmpq %rcx, %rax
je 0x14c0
xorl %edi, %edi
cmpq $0x4, %rdi
je 0x14b7
andq $0x0, (%rdx,%rdi,8)
incq %rdi
jmp 0x14a7
incq %rax
addq $0x20, %rdx
jmp 0x14a0
movl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
movl 0x4(%r11), %r15d
movq -0x38(%rbp), %rax
movl 0x28(%rax), %r14d
movl 0x2c(%rax), %r9d
leal -0x1(%r15), %ecx
pushq $-0x1
popq %r10
testl %r15d, %r15d
jle 0x164a
movl %r10d, -0x3c(%rbp)
movl %ecx, -0x2c(%rbp)
movl %ecx, %r13d
movq %r13, %rdi
shlq $0x5, %rdi
addq -0x48(%rbp), %rdi
shlq $0x6, %r13
leaq -0x1(%r15), %r12
movl %r12d, %r10d
movq %r10, -0x50(%rbp)
shlq $0x6, %r10
movq 0x40(%r11,%r10), %r8
xorl %ecx, %ecx
movq %r11, %rbx
cmpl %esi, 0x38(%rbx,%r10)
jge 0x1545
movq %r8, %r11
leaq (%rbx,%r10), %rcx
addq $0x8, %rcx
movq 0x40(%r8,%r10), %r8
movq %r11, %rbx
cmpl $0x1, %r9d
jne 0x1518
incl %r14d
movq -0x38(%rbp), %rax
movl %r14d, 0x28(%rax)
jmp 0x1515
movq %r12, -0x58(%rbp)
movl %r14d, -0x30(%rbp)
leaq (%rbx,%r13), %r8
addq $0x8, %r8
xorl %r12d, %r12d
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
pushq $-0x1
popq %r11
movq %r12, %r10
incq %r11
leaq 0x10(%r8), %r14
incq %r12
addq %rdx, %rax
addq $0x2, %rax
cmpl %esi, (%r8)
movq %r14, %r8
jl 0x1566
pushq $-0x1
popq %r8
movq -0x58(%rbp), %r12
cmovel %r12d, %r8d
sarq $0x1c, %rax
addq %rbx, %r13
addq %rax, %r13
movq %rdi, %rax
subq $0x1, %r10
jb 0x15b2
movq (%r13), %r14
movq %r14, (%rax)
addq $0x8, %rax
addq $-0x10, %r13
jmp 0x159b
movl -0x3c(%rbp), %r10d
cmpl $-0x1, %r10d
cmovel %r8d, %r10d
testq %rcx, %rcx
je 0x1608
addq $0x38, %rcx
movl -0x30(%rbp), %r14d
cmpq $0x4, %r11
jae 0x15e1
movq (%rcx), %rax
movq %rax, (%rdi,%r11,8)
incq %r11
addq $-0x10, %rcx
jmp 0x15cb
cmpl $0x1, %r9d
jne 0x15f2
incl %r14d
movq -0x38(%rbp), %rax
movl %r14d, 0x28(%rax)
movl -0x2c(%rbp), %ecx
movq -0x50(%rbp), %r8
shlq $0x5, %r8
movq -0x48(%rbp), %rax
movq 0x18(%rax,%r8), %rbx
jmp 0x1613
movq %rbx, (%rdi,%r11,8)
movl -0x30(%rbp), %r14d
movl -0x2c(%rbp), %ecx
cmpq $0x1, %r15
jne 0x1626
movq %rbx, %r11
decl %ecx
movq %r12, %r15
jmp 0x14dd
addl %edx, %r15d
shlq $0x6, %r15
xorl %eax, %eax
cmpq $0x4, %rax
je 0x1619
movq (%rdi,%rax,8), %r11
testq %r11, %r11
je 0x1645
cmpl %esi, 0x38(%r11,%r15)
jle 0x161c
incq %rax
jmp 0x162f
movl %r10d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq