// File: narula[P]skiplists/skiplist-fanout.cc
// Function: SkipList::findNode(int, node* (*) [4], node* (*) [4])
int SkipList::findNode(int key, node* preds[][FANOUT], node* succs[][FANOUT]) {
  node* pred = head;
  int lFound = -1;
  for (int level = pred->topLevel-1; level >= 0; level--) {
	node* curr;
	curr = pred->nexts[level][FANOUT-1].nxt;
	inc();
	while (key > pred->nexts[level][FANOUT-1].prefix) {
	  pred = curr;
	  curr = pred->nexts[level][FANOUT-1].nxt;
	  inc();
	}
	preds[level][FANOUT-1] = pred;
	succs[level][FANOUT-1] = curr;
	if (lFound == -1 && key == pred->nexts[level][FANOUT-1].prefix) {
	  lFound = level;
	  continue;
	}

	// pred.key is less than key, curr.key is greater than key.
	int k = 0;
	while (key > pred->nexts[level][k].prefix) {
	  k++;
	}
	//assert(k < FANOUT);
	if (k != 0) {
	  if (lFound == -1 && key == pred->nexts[level][k-1].prefix) {
		lFound = level;
	  }
	  pred = pred->nexts[level][k-1].nxt;
	  inc();
	}
	// for (int k = 0; k < FANOUT; k++) {
	//   if (pred->nexts[level][k].nxt == NULL) {
	// 	continue;
	//   }
	//   if (key >= pred->nexts[level][k].prefix) {
	// 	//		preds[level][k] = pred->nexts[level][k].nxt;
	// 	continue;
	//   } else {
	// 	if (k != 0) {
	// 	  if (lFound == -1 && key == pred->nexts[level][k-1].prefix) {
	// 		lFound = level;
	// 	  }
	// 	  pred = pred->nexts[level][k-1].nxt;
	// 	  inc();
	// 	}
	//   }
	//   succs[level][k] = pred->nexts[level][k].nxt;
	// }
  }
  return lFound;
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
movl %esi, 0x3c(%rsp)
movq %rdx, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
movq 0x40(%rsp), %rax
movq %rax, (%rsp)
movq (%rax), %rax
movq %rax, 0x20(%rsp)
movl $0xffffffff, 0x1c(%rsp) # imm = 0xFFFFFFFF
movq 0x20(%rsp), %rax
movl 0x4(%rax), %eax
subl $0x1, %eax
movl %eax, 0x18(%rsp)
cmpl $0x0, 0x18(%rsp)
jl 0x1784
movq (%rsp), %rdi
movq 0x20(%rsp), %rax
addq $0x8, %rax
movslq 0x18(%rsp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq 0x38(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x2b10
movl 0x3c(%rsp), %eax
movq 0x20(%rsp), %rcx
addq $0x8, %rcx
movslq 0x18(%rsp), %rdx
shlq $0x6, %rdx
addq %rdx, %rcx
cmpl 0x30(%rcx), %eax
jle 0x1652
movq (%rsp), %rdi
movq 0x10(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
addq $0x8, %rax
movslq 0x18(%rsp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq 0x38(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x2b10
jmp 0x1601
movq 0x20(%rsp), %rcx
movq 0x30(%rsp), %rax
movslq 0x18(%rsp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movq %rcx, 0x18(%rax)
movq 0x10(%rsp), %rcx
movq 0x28(%rsp), %rax
movslq 0x18(%rsp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movq %rcx, 0x18(%rax)
cmpl $-0x1, 0x1c(%rsp)
jne 0x16b8
movl 0x3c(%rsp), %eax
movq 0x20(%rsp), %rcx
addq $0x8, %rcx
movslq 0x18(%rsp), %rdx
shlq $0x6, %rdx
addq %rdx, %rcx
cmpl 0x30(%rcx), %eax
jne 0x16b8
movl 0x18(%rsp), %eax
movl %eax, 0x1c(%rsp)
jmp 0x1774
movl $0x0, 0xc(%rsp)
movl 0x3c(%rsp), %eax
movq 0x20(%rsp), %rcx
addq $0x8, %rcx
movslq 0x18(%rsp), %rdx
shlq $0x6, %rdx
addq %rdx, %rcx
movslq 0xc(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
cmpl (%rcx), %eax
jle 0x16f6
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x16c0
cmpl $0x0, 0xc(%rsp)
je 0x1772
cmpl $-0x1, 0x1c(%rsp)
jne 0x173a
movl 0x3c(%rsp), %eax
movq 0x20(%rsp), %rcx
addq $0x8, %rcx
movslq 0x18(%rsp), %rdx
shlq $0x6, %rdx
addq %rdx, %rcx
movl 0xc(%rsp), %edx
subl $0x1, %edx
movslq %edx, %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
cmpl (%rcx), %eax
jne 0x173a
movl 0x18(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq (%rsp), %rdi
movq 0x20(%rsp), %rax
addq $0x8, %rax
movslq 0x18(%rsp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movl 0xc(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, 0x20(%rsp)
callq 0x2b10
jmp 0x1774
movl 0x18(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x15cf
movl 0x1c(%rsp), %eax
addq $0x48, %rsp
retq
nopl (%rax)