// File: narula[P]skiplists/skiplist-fanout.cc
// Function: SkipList::insert(int)
int SkipList::insert(int key) {
  node *preds[max_level][FANOUT], *succs[max_level][FANOUT];
  int found = findNode(key, preds, succs);
  if (found != -1) return 0;
  node* add = new node();
  add->key = key;
  int topLevel = randomLevel(max_level);
  for (int i = 0; i < topLevel; i++) {
	add->nexts[i][FANOUT-1].nxt = succs[i][FANOUT-1];
	add->nexts[i][FANOUT-1].prefix = succs[i][FANOUT-1]->key;
	preds[i][FANOUT-1]->nexts[i][FANOUT-1].nxt = add;
	preds[i][FANOUT-1]->nexts[i][FANOUT-1].prefix = key;
	for (int k = 0; k < FANOUT-1; k++) {
	  if (preds[i][FANOUT-1]->nexts[i][k].prefix > key) {
		add->nexts[i][k].nxt = preds[i][FANOUT-1]->nexts[i][k].nxt;
		add->nexts[i][k].prefix = preds[i][FANOUT-1]->nexts[i][k].prefix;
		preds[i][k]->nexts[i][k].nxt = NULL;
		preds[i][k]->nexts[i][k].prefix = 0;
	  }
	}
  }
  return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x68(%rbp)
movl 0x14(%rdi), %eax
movq %rsp, %rcx
movq %rcx, -0x20(%rbp)
movq %rax, %rcx
shlq $0x5, %rcx
movq %rsp, %rdx
subq %rcx, %rdx
movq %rdx, -0x60(%rbp)
movq %rdx, %rsp
movq %rax, -0x28(%rbp)
movl 0x14(%rdi), %eax
movq %rax, %rsi
shlq $0x5, %rsi
movq %rsp, %rcx
subq %rsi, %rcx
movq %rcx, -0x58(%rbp)
movq %rcx, %rsp
movq %rax, -0x30(%rbp)
movl -0x14(%rbp), %esi
callq 0x1590
movl %eax, -0x34(%rbp)
cmpl $-0x1, -0x34(%rbp)
je 0x1808
movl $0x0, -0x4(%rbp)
movl $0x1, -0x38(%rbp)
jmp 0x1a83
movl $0x348, %edi            # imm = 0x348
callq 0x10c0
movq %rax, %rdi
movq %rdi, -0x70(%rbp)
xorl %esi, %esi
movl $0x348, %edx            # imm = 0x348
callq 0x1060
movq -0x70(%rbp), %rcx
movq -0x68(%rbp), %rax
movq %rcx, -0x40(%rbp)
movl -0x14(%rbp), %edx
movq -0x40(%rbp), %rcx
movl %edx, (%rcx)
movl 0x14(%rax), %edi
callq 0x1520
movl %eax, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0x1a75
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %rcx
movslq -0x48(%rbp), %rsi
shlq $0x5, %rsi
movq %rcx, %rdx
addq %rsi, %rdx
movq 0x18(%rdx), %rsi
movq -0x40(%rbp), %rdx
addq $0x8, %rdx
movslq -0x48(%rbp), %rdi
shlq $0x6, %rdi
addq %rdi, %rdx
movq %rsi, 0x38(%rdx)
movslq -0x48(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movq 0x18(%rcx), %rcx
movl (%rcx), %edx
movq -0x40(%rbp), %rcx
addq $0x8, %rcx
movslq -0x48(%rbp), %rsi
shlq $0x6, %rsi
addq %rsi, %rcx
movl %edx, 0x30(%rcx)
movq -0x40(%rbp), %rdx
movslq -0x48(%rbp), %rsi
shlq $0x5, %rsi
movq %rax, %rcx
addq %rsi, %rcx
movq 0x18(%rcx), %rcx
addq $0x8, %rcx
movslq -0x48(%rbp), %rsi
shlq $0x6, %rsi
addq %rsi, %rcx
movq %rdx, 0x38(%rcx)
movl -0x14(%rbp), %ecx
movslq -0x48(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movq 0x18(%rax), %rax
addq $0x8, %rax
movslq -0x48(%rbp), %rdx
shlq $0x6, %rdx
addq %rdx, %rax
movl %ecx, 0x30(%rax)
movl $0x0, -0x4c(%rbp)
cmpl $0x3, -0x4c(%rbp)
jge 0x1a65
movq -0x60(%rbp), %rax
movslq -0x48(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
addq $0x8, %rax
movslq -0x48(%rbp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movslq -0x4c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
cmpl -0x14(%rbp), %eax
jle 0x1a55
movq -0x60(%rbp), %rax
movslq -0x48(%rbp), %rdx
shlq $0x5, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq 0x18(%rcx), %rcx
addq $0x8, %rcx
movslq -0x48(%rbp), %rdx
shlq $0x6, %rdx
addq %rdx, %rcx
movslq -0x4c(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq 0x8(%rcx), %rdx
movq -0x40(%rbp), %rcx
addq $0x8, %rcx
movslq -0x48(%rbp), %rsi
shlq $0x6, %rsi
addq %rsi, %rcx
movslq -0x4c(%rbp), %rsi
shlq $0x4, %rsi
addq %rsi, %rcx
movq %rdx, 0x8(%rcx)
movslq -0x48(%rbp), %rdx
shlq $0x5, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq 0x18(%rcx), %rcx
addq $0x8, %rcx
movslq -0x48(%rbp), %rdx
shlq $0x6, %rdx
addq %rdx, %rcx
movslq -0x4c(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl (%rcx), %edx
movq -0x40(%rbp), %rcx
addq $0x8, %rcx
movslq -0x48(%rbp), %rsi
shlq $0x6, %rsi
addq %rsi, %rcx
movslq -0x4c(%rbp), %rsi
shlq $0x4, %rsi
addq %rsi, %rcx
movl %edx, (%rcx)
movslq -0x48(%rbp), %rdx
shlq $0x5, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movslq -0x4c(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
addq $0x8, %rcx
movslq -0x48(%rbp), %rdx
shlq $0x6, %rdx
addq %rdx, %rcx
movslq -0x4c(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq $0x0, 0x8(%rcx)
movslq -0x48(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
addq $0x8, %rax
movslq -0x48(%rbp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movslq -0x4c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl $0x0, (%rax)
jmp 0x1a57
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x1904
jmp 0x1a67
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x184c
movl $0x1, -0x4(%rbp)
movl $0x1, -0x38(%rbp)
movq -0x20(%rbp), %rax
movq %rax, %rsp
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)