// File: narula[P]skiplists/skiplist-extra.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 = 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];
	}
	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];
		  drop_down_pred[i]++;
	  	  break;
	  	}
	  }
	}
  }
  return lFound;
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x60(%rsp)
movl %esi, 0x5c(%rsp)
movq %rdx, 0x50(%rsp)
movq %rcx, 0x48(%rsp)
movq 0x60(%rsp), %rax
movq %rax, (%rsp)
movq (%rax), %rax
movq %rax, 0x40(%rsp)
movl $0xffffffff, 0x3c(%rsp) # imm = 0xFFFFFFFF
movq 0x40(%rsp), %rax
movl 0x4(%rax), %eax
subl $0x1, %eax
movl %eax, 0x38(%rsp)
cmpl $0x0, 0x38(%rsp)
jl 0x1f92
movq 0x40(%rsp), %rax
addq $0x8, %rax
movslq 0x38(%rsp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq 0x38(%rax), %rax
movq %rax, 0x30(%rsp)
movq $0x0, 0x28(%rsp)
movl 0x5c(%rsp), %eax
movq 0x40(%rsp), %rcx
addq $0x8, %rcx
movslq 0x38(%rsp), %rdx
shlq $0x6, %rdx
addq %rdx, %rcx
cmpl 0x30(%rcx), %eax
jle 0x1c8c
movq (%rsp), %rdi
movq 0x40(%rsp), %rax
addq $0x8, %rax
movslq 0x38(%rsp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, 0x28(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rax
addq $0x8, %rax
movslq 0x38(%rsp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq 0x38(%rax), %rax
movq %rax, 0x30(%rsp)
callq 0x4150
jmp 0x1c21
movl $0x0, 0x24(%rsp)
movl 0x5c(%rsp), %eax
movq 0x40(%rsp), %rcx
addq $0x8, %rcx
movslq 0x38(%rsp), %rdx
shlq $0x6, %rdx
addq %rdx, %rcx
movslq 0x24(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
cmpl (%rcx), %eax
jle 0x1cca
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x1c94
cmpl $-0x1, 0x3c(%rsp)
jne 0x1d02
movl 0x5c(%rsp), %eax
movq 0x40(%rsp), %rcx
addq $0x8, %rcx
movslq 0x38(%rsp), %rdx
shlq $0x6, %rdx
addq %rdx, %rcx
movslq 0x24(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
cmpl (%rcx), %eax
jne 0x1d02
movl 0x38(%rsp), %eax
movl %eax, 0x3c(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpl $0x0, 0x24(%rsp)
jle 0x1d42
movq 0x40(%rsp), %rax
addq $0x8, %rax
movslq 0x38(%rsp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movl 0x24(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
cmpl $0x4, 0x14(%rsp)
jge 0x1d9d
movq 0x18(%rsp), %rax
addq $0x8, %rax
movslq 0x38(%rsp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movslq 0x14(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdx
movq 0x48(%rsp), %rax
movslq 0x38(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x14(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x1d4a
movl $0x0, 0x10(%rsp)
movl 0x10(%rsp), %eax
cmpl 0x24(%rsp), %eax
jge 0x1e04
movq 0x40(%rsp), %rax
addq $0x8, %rax
movslq 0x38(%rsp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movl 0x24(%rsp), %ecx
subl 0x10(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdx
movq 0x50(%rsp), %rax
movslq 0x38(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x10(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0x1da5
cmpq $0x0, 0x28(%rsp)
jne 0x1e2d
movq 0x40(%rsp), %rdx
movq 0x50(%rsp), %rax
movslq 0x38(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x24(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x1ea9
movl 0x24(%rsp), %eax
movl %eax, 0xc(%rsp)
cmpl $0x4, 0xc(%rsp)
jge 0x1e86
movq 0x28(%rsp), %rax
movl $0x4, %ecx
subl 0xc(%rsp), %ecx
subl $0x1, %ecx
addl 0x24(%rsp), %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdx
movq 0x50(%rsp), %rax
movslq 0x38(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0xc(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x1e35
movq (%rsp), %rdi
callq 0x4150
movq 0x50(%rsp), %rax
movslq 0x38(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movq %rax, 0x40(%rsp)
cmpl $0x0, 0x38(%rsp)
jle 0x1f80
movl $0x0, 0x8(%rsp)
cmpl $0x4, 0x8(%rsp)
jge 0x1f7e
movq 0x50(%rsp), %rax
movslq 0x38(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpq $0x0, %rax
je 0x1f6c
movq 0x50(%rsp), %rax
movslq 0x38(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x8(%rsp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x1f6c
movq 0x50(%rsp), %rax
movslq 0x38(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x8(%rsp), %rcx
movq (%rax,%rcx,8), %rax
addq $0x8, %rax
movl 0x38(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movl 0x30(%rax), %eax
cmpl 0x5c(%rsp), %eax
jg 0x1f6c
movq (%rsp), %rax
movq 0x50(%rsp), %rcx
movslq 0x38(%rsp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movslq 0x8(%rsp), %rdx
movq (%rcx,%rdx,8), %rcx
movq %rcx, 0x40(%rsp)
movslq 0x8(%rsp), %rcx
movl 0x14(%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, 0x14(%rax,%rcx,4)
jmp 0x1f7e
jmp 0x1f6e
movl 0x8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8(%rsp)
jmp 0x1ebc
jmp 0x1f80
jmp 0x1f82
movl 0x38(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x38(%rsp)
jmp 0x1bef
movl 0x3c(%rsp), %eax
addq $0x68, %rsp
retq
nopl (%rax,%rax)