// File: narula[P]skiplists/skiplist-extra.cc
// Function: SkipList::insert(int)
int SkipList::insert(int key) {
  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;
	  succs[i][k] = NULL;
	}
  }
  int found = findNode(key, preds, succs);
  if (found != -1) return 0;
  node* add = new node();
  add->key = key;
  int topLevel = randomLevel(max_level);
  add->topLevel = topLevel;
  for (int level = 0; level< add->topLevel; level++) {
	for (int k = 0; k < FANOUT; k++) {
	  add->nexts[level][k].nxt = succs[level][k];
	  add->nexts[level][k].prefix = succs[level][k]->key;
	  node* pr = preds[level][k];
	  if (pr) {
		// scoot rest down
		for (int j = FANOUT-2; j >= k; j--) {
		  pr->nexts[level][j+1].nxt = pr->nexts[level][j].nxt;
		  pr->nexts[level][j+1].prefix = pr->nexts[level][j].prefix;
		}
		pr->nexts[level][k].nxt = add;
		pr->nexts[level][k].prefix  = key;
	  }
	}
  }
  return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x80(%rbp)
movl 0x10(%rax), %ecx
movq %rsp, %rdx
movq %rdx, -0x20(%rbp)
movq %rcx, %rsi
shlq $0x5, %rsi
movq %rsp, %rdx
subq %rsi, %rdx
movq %rdx, -0x78(%rbp)
movq %rdx, %rsp
movq %rcx, -0x28(%rbp)
movl 0x10(%rax), %eax
movq %rax, %rdx
shlq $0x5, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x70(%rbp)
movq %rcx, %rsp
movq %rax, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x80(%rbp), %rcx
movl -0x34(%rbp), %eax
cmpl 0x10(%rcx), %eax
jge 0x2065
movl $0x0, -0x38(%rbp)
cmpl $0x4, -0x38(%rbp)
jge 0x2058
movq -0x70(%rbp), %rax
movq -0x78(%rbp), %rcx
movslq -0x34(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movslq -0x38(%rbp), %rdx
movq $0x0, (%rcx,%rdx,8)
movslq -0x34(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq -0x38(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x2011
jmp 0x205a
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1ffe
movq -0x70(%rbp), %rcx
movq -0x78(%rbp), %rdx
movq -0x80(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x1bb0
movl %eax, -0x3c(%rbp)
cmpl $-0x1, -0x3c(%rbp)
je 0x2095
movl $0x0, -0x4(%rbp)
movl $0x1, -0x40(%rbp)
jmp 0x22c7
movl $0x348, %edi            # imm = 0x348
callq 0x10c0
movq %rax, %rdi
movq %rdi, -0x88(%rbp)
xorl %esi, %esi
movl $0x348, %edx            # imm = 0x348
callq 0x1060
movq -0x88(%rbp), %rcx
movq -0x80(%rbp), %rax
movq %rcx, -0x48(%rbp)
movl -0x14(%rbp), %edx
movq -0x48(%rbp), %rcx
movl %edx, (%rcx)
movl 0x10(%rax), %edi
callq 0x1710
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x4(%rax)
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jge 0x22b9
movl $0x0, -0x54(%rbp)
cmpl $0x4, -0x54(%rbp)
jge 0x22a9
movq -0x78(%rbp), %rax
movq -0x70(%rbp), %rcx
movslq -0x50(%rbp), %rsi
shlq $0x5, %rsi
movq %rcx, %rdx
addq %rsi, %rdx
movslq -0x54(%rbp), %rsi
movq (%rdx,%rsi,8), %rsi
movq -0x48(%rbp), %rdx
addq $0x8, %rdx
movslq -0x50(%rbp), %rdi
shlq $0x6, %rdi
addq %rdi, %rdx
movslq -0x54(%rbp), %rdi
shlq $0x4, %rdi
addq %rdi, %rdx
movq %rsi, 0x8(%rdx)
movslq -0x50(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movslq -0x54(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movl (%rcx), %edx
movq -0x48(%rbp), %rcx
addq $0x8, %rcx
movslq -0x50(%rbp), %rsi
shlq $0x6, %rsi
addq %rsi, %rcx
movslq -0x54(%rbp), %rsi
shlq $0x4, %rsi
addq %rsi, %rcx
movl %edx, (%rcx)
movslq -0x50(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq -0x54(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
je 0x2299
movl $0x2, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x54(%rbp), %eax
jl 0x2250
movq -0x60(%rbp), %rax
addq $0x8, %rax
movslq -0x50(%rbp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movslq -0x64(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
movq -0x60(%rbp), %rax
addq $0x8, %rax
movslq -0x50(%rbp), %rdx
shlq $0x6, %rdx
addq %rdx, %rax
movl -0x64(%rbp), %edx
addl $0x1, %edx
movslq %edx, %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x60(%rbp), %rax
addq $0x8, %rax
movslq -0x50(%rbp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movslq -0x64(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %ecx
movq -0x60(%rbp), %rax
addq $0x8, %rax
movslq -0x50(%rbp), %rdx
shlq $0x6, %rdx
addq %rdx, %rax
movl -0x64(%rbp), %edx
addl $0x1, %edx
movslq %edx, %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movl -0x64(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x21a8
movq -0x48(%rbp), %rcx
movq -0x60(%rbp), %rax
addq $0x8, %rax
movslq -0x50(%rbp), %rdx
shlq $0x6, %rdx
addq %rdx, %rax
movslq -0x54(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movl -0x14(%rbp), %ecx
movq -0x60(%rbp), %rax
addq $0x8, %rax
movslq -0x50(%rbp), %rdx
shlq $0x6, %rdx
addq %rdx, %rax
movslq -0x54(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
jmp 0x229b
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x2100
jmp 0x22ab
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x20e9
movl $0x1, -0x4(%rbp)
movl $0x1, -0x40(%rbp)
movq -0x20(%rbp), %rax
movq %rax, %rsp
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)