// File: narula[P]skiplists/skiplist-fanout.cc
// Function: SkipList::init_list(int, int)
SkipList* SkipList::init_list(int sz, int max_level) {
  node** sk;
  sk = new node* [(unsigned long long)sz+2];
  for (int i = 0; i < sz+2; i++) {
	sk[i] = new node;
  }

  // set up head
  sk[0]->key = -1;
  sk[0]->topLevel = max_level;
  for (int i = 0; i < max_level; i++) {
	for (int k = 0; k < FANOUT; k++) {
	  sk[0]->nexts[i][k].prefix = 0;
	  sk[0]->nexts[i][k].nxt = sk[1];
	}
  }
  for (int i = 1; i < max_level; i++) {
	for (int j = 0; j < FANOUT; j++) {
	  int ptr = int(pow(FANOUT, i-1)*(j+1)) - 1; // i=1 ptr=3, 7, 11, 15 
	  if (ptr < sz) {
		sk[0]->nexts[i][j].prefix = ptr;     // i=2 ptr=15, 31, 47, 63
		sk[0]->nexts[i][j].nxt = sk[ptr+1]; // i=3 ptr=63, ....
	  } else {
		sk[0]->nexts[i][j].prefix = INT_MAX;
		sk[0]->nexts[i][j].nxt = sk[sz+1];
	  }
	}
  }

  for (int i = 1; i < sz+1; i++) {
	sk[i]->key = i-1;
	sk[i]->topLevel = 1;
	for (int k = 2; k < max_level+1; k++) {
	  if (i % int(pow(FANOUT, k-1)) == 0) {
		sk[i]->topLevel = k;
	  }	
	}
	// fix up next when near end for +1 level
	int np = sk[i]->key+1;
	node* nptr = sk[i+1];
	if (np >= sz) {
	  np = INT_MAX;
	  nptr = sk[sz+1];
	}
	sk[i]->nexts[0][FANOUT-1].prefix = np;
	sk[i]->nexts[0][FANOUT-1].nxt = nptr;

	for (int j = 1; j < sk[i]->topLevel; j++) {
	  for (int k = 0; k < FANOUT; k++) {
		int nextp = int(pow(FANOUT, j-1)*(k+1)) + i - 1;
		node* nextptr;;
		if (nextp >= sz) {
		  nextp = INT_MAX;
		  nextptr = sk[sz+1];
		} else {
		  nextptr = sk[nextp+1];
		}
		sk[i]->nexts[j][k].prefix = nextp;
		sk[i]->nexts[j][k].nxt = nextptr;
	  }
	}
	int last = sk[i]->topLevel-1;
	for (int k = 0; k < FANOUT; k++) {
	  if (sk[i]->nexts[last][k].prefix >= sz) {
		sk[i]->nexts[last][k].prefix = INT_MAX;
		sk[i]->nexts[last][k].nxt = sk[sz+1];
	  }
	}
  }

  for (int j = 0; j < sk[sz]->topLevel; j++) {
	for (int k = 0; k < FANOUT; k++) {
	  sk[sz]->nexts[j][k].prefix = INT_MAX;
	  sk[sz]->nexts[j][k].nxt = sk[sz+1];
	}
  }
  // set up tail
  sk[sz+1]->key = INT_MAX;
  sk[sz+1]->topLevel = max_level;
  for (int j = 0; j < max_level; j++) {
	for (int k = 0; k < FANOUT; k++) {
	  sk[sz+1]->nexts[j][k].prefix = INT_MAX;
	  sk[sz+1]->nexts[j][k].nxt = sk[sz+1];
	}
  }


  SkipList* sk4 = new SkipList;
  sk4->head = sk[0];
  sk4->tail = sk[sz+1];
  sk4->max_level = max_level;
  return sk4;
}

// Assembly:
subq $0x98, %rsp
movl %edi, 0x94(%rsp)
movl %esi, 0x90(%rsp)
movslq 0x94(%rsp), %rax
addq $0x2, %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, 0x88(%rsp)
movl $0x0, 0x84(%rsp)
movl 0x84(%rsp), %eax
movl 0x94(%rsp), %ecx
addl $0x2, %ecx
cmpl %ecx, %eax
jge 0x1b3e
movl $0x348, %edi            # imm = 0x348
callq 0x10c0
movq %rax, %rdx
movq 0x88(%rsp), %rax
movslq 0x84(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0x1af5
movq 0x88(%rsp), %rax
movq (%rax), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl 0x90(%rsp), %ecx
movq 0x88(%rsp), %rax
movq (%rax), %rax
movl %ecx, 0x4(%rax)
movl $0x0, 0x80(%rsp)
movl 0x80(%rsp), %eax
cmpl 0x90(%rsp), %eax
jge 0x1c21
movl $0x0, 0x7c(%rsp)
cmpl $0x4, 0x7c(%rsp)
jge 0x1c09
movq 0x88(%rsp), %rax
movq (%rax), %rax
addq $0x8, %rax
movslq 0x80(%rsp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movslq 0x7c(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl $0x0, (%rax)
movq 0x88(%rsp), %rax
movq 0x8(%rax), %rcx
movq 0x88(%rsp), %rax
movq (%rax), %rax
addq $0x8, %rax
movslq 0x80(%rsp), %rdx
shlq $0x6, %rdx
addq %rdx, %rax
movslq 0x7c(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x1b8b
jmp 0x1c0b
movl 0x80(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x80(%rsp)
jmp 0x1b6f
movl $0x1, 0x78(%rsp)
movl 0x78(%rsp), %eax
cmpl 0x90(%rsp), %eax
jge 0x1d8a
movl $0x0, 0x74(%rsp)
cmpl $0x4, 0x74(%rsp)
jge 0x1d78
movl 0x78(%rsp), %esi
subl $0x1, %esi
movl $0x4, %edi
callq 0x2b40
movl 0x74(%rsp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
subl $0x1, %eax
movl %eax, 0x70(%rsp)
movl 0x70(%rsp), %eax
cmpl 0x94(%rsp), %eax
jge 0x1cf5
movl 0x70(%rsp), %ecx
movq 0x88(%rsp), %rax
movq (%rax), %rax
addq $0x8, %rax
movslq 0x78(%rsp), %rdx
shlq $0x6, %rdx
addq %rdx, %rax
movslq 0x74(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movq 0x88(%rsp), %rax
movl 0x70(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rcx
movq 0x88(%rsp), %rax
movq (%rax), %rax
addq $0x8, %rax
movslq 0x78(%rsp), %rdx
shlq $0x6, %rdx
addq %rdx, %rax
movslq 0x74(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
jmp 0x1d66
movq 0x88(%rsp), %rax
movq (%rax), %rax
addq $0x8, %rax
movslq 0x78(%rsp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movslq 0x74(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl $0x7fffffff, (%rax)     # imm = 0x7FFFFFFF
movq 0x88(%rsp), %rax
movl 0x94(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rcx
movq 0x88(%rsp), %rax
movq (%rax), %rax
addq $0x8, %rax
movslq 0x78(%rsp), %rdx
shlq $0x6, %rdx
addq %rdx, %rax
movslq 0x74(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
jmp 0x1d68
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0x1c42
jmp 0x1d7a
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x1c29
movl $0x1, 0x6c(%rsp)
movl 0x6c(%rsp), %eax
movl 0x94(%rsp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x2128
movl 0x6c(%rsp), %ecx
subl $0x1, %ecx
movq 0x88(%rsp), %rax
movslq 0x6c(%rsp), %rdx
movq (%rax,%rdx,8), %rax
movl %ecx, (%rax)
movq 0x88(%rsp), %rax
movslq 0x6c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movl $0x1, 0x4(%rax)
movl $0x2, 0x68(%rsp)
movl 0x68(%rsp), %eax
movl 0x90(%rsp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x1e44
movl 0x6c(%rsp), %eax
movl %eax, 0x10(%rsp)
movl 0x68(%rsp), %esi
subl $0x1, %esi
movl $0x4, %edi
callq 0x2b40
movl 0x10(%rsp), %eax
cvttsd2si %xmm0, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x1e35
movl 0x68(%rsp), %ecx
movq 0x88(%rsp), %rax
movslq 0x6c(%rsp), %rdx
movq (%rax,%rdx,8), %rax
movl %ecx, 0x4(%rax)
jmp 0x1e37
movl 0x68(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x68(%rsp)
jmp 0x1de2
movq 0x88(%rsp), %rax
movslq 0x6c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movl (%rax), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
movq 0x88(%rsp), %rax
movl 0x6c(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x58(%rsp)
movl 0x64(%rsp), %eax
cmpl 0x94(%rsp), %eax
jl 0x1eac
movl $0x7fffffff, 0x64(%rsp) # imm = 0x7FFFFFFF
movq 0x88(%rsp), %rax
movl 0x94(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x58(%rsp)
movl 0x64(%rsp), %ecx
movq 0x88(%rsp), %rax
movslq 0x6c(%rsp), %rdx
movq (%rax,%rdx,8), %rax
movl %ecx, 0x38(%rax)
movq 0x58(%rsp), %rcx
movq 0x88(%rsp), %rax
movslq 0x6c(%rsp), %rdx
movq (%rax,%rdx,8), %rax
movq %rcx, 0x40(%rax)
movl $0x1, 0x54(%rsp)
movl 0x54(%rsp), %eax
movq 0x88(%rsp), %rcx
movslq 0x6c(%rsp), %rdx
movq (%rcx,%rdx,8), %rcx
cmpl 0x4(%rcx), %eax
jge 0x2021
movl $0x0, 0x50(%rsp)
cmpl $0x4, 0x50(%rsp)
jge 0x200f
movl 0x54(%rsp), %esi
subl $0x1, %esi
movl $0x4, %edi
callq 0x2b40
movl 0x50(%rsp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
addl 0x6c(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
cmpl 0x94(%rsp), %eax
jl 0x1f7b
movl $0x7fffffff, 0x4c(%rsp) # imm = 0x7FFFFFFF
movq 0x88(%rsp), %rax
movl 0x94(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x40(%rsp)
jmp 0x1f96
movq 0x88(%rsp), %rax
movl 0x4c(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x40(%rsp)
movl 0x4c(%rsp), %ecx
movq 0x88(%rsp), %rax
movslq 0x6c(%rsp), %rdx
movq (%rax,%rdx,8), %rax
addq $0x8, %rax
movslq 0x54(%rsp), %rdx
shlq $0x6, %rdx
addq %rdx, %rax
movslq 0x50(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movq 0x40(%rsp), %rcx
movq 0x88(%rsp), %rax
movslq 0x6c(%rsp), %rdx
movq (%rax,%rdx,8), %rax
addq $0x8, %rax
movslq 0x54(%rsp), %rdx
shlq $0x6, %rdx
addq %rdx, %rax
movslq 0x50(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0x1f0c
jmp 0x2011
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x1ee6
movq 0x88(%rsp), %rax
movslq 0x6c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movl 0x4(%rax), %eax
subl $0x1, %eax
movl %eax, 0x3c(%rsp)
movl $0x0, 0x38(%rsp)
cmpl $0x4, 0x38(%rsp)
jge 0x2116
movq 0x88(%rsp), %rax
movslq 0x6c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
addq $0x8, %rax
movslq 0x3c(%rsp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movslq 0x38(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
cmpl 0x94(%rsp), %eax
jl 0x2104
movq 0x88(%rsp), %rax
movslq 0x6c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
addq $0x8, %rax
movslq 0x3c(%rsp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movslq 0x38(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl $0x7fffffff, (%rax)     # imm = 0x7FFFFFFF
movq 0x88(%rsp), %rax
movl 0x94(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rcx
movq 0x88(%rsp), %rax
movslq 0x6c(%rsp), %rdx
movq (%rax,%rdx,8), %rax
addq $0x8, %rax
movslq 0x3c(%rsp), %rdx
shlq $0x6, %rdx
addq %rdx, %rax
movslq 0x38(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
jmp 0x2106
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
jmp 0x2044
jmp 0x2118
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x1d92
movl $0x0, 0x34(%rsp)
movl 0x34(%rsp), %eax
movq 0x88(%rsp), %rcx
movslq 0x94(%rsp), %rdx
movq (%rcx,%rdx,8), %rcx
cmpl 0x4(%rcx), %eax
jge 0x2209
movl $0x0, 0x30(%rsp)
cmpl $0x4, 0x30(%rsp)
jge 0x21f7
movq 0x88(%rsp), %rax
movslq 0x94(%rsp), %rcx
movq (%rax,%rcx,8), %rax
addq $0x8, %rax
movslq 0x34(%rsp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movslq 0x30(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl $0x7fffffff, (%rax)     # imm = 0x7FFFFFFF
movq 0x88(%rsp), %rax
movl 0x94(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rcx
movq 0x88(%rsp), %rax
movslq 0x94(%rsp), %rdx
movq (%rax,%rdx,8), %rax
addq $0x8, %rax
movslq 0x34(%rsp), %rdx
shlq $0x6, %rdx
addq %rdx, %rax
movslq 0x30(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x2159
jmp 0x21f9
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x2130
movq 0x88(%rsp), %rax
movl 0x94(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movl $0x7fffffff, (%rax)     # imm = 0x7FFFFFFF
movl 0x90(%rsp), %ecx
movq 0x88(%rsp), %rax
movl 0x94(%rsp), %edx
addl $0x1, %edx
movslq %edx, %rdx
movq (%rax,%rdx,8), %rax
movl %ecx, 0x4(%rax)
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
cmpl 0x90(%rsp), %eax
jge 0x2326
movl $0x0, 0x28(%rsp)
cmpl $0x4, 0x28(%rsp)
jge 0x2314
movq 0x88(%rsp), %rax
movl 0x94(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
addq $0x8, %rax
movslq 0x2c(%rsp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movslq 0x28(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl $0x7fffffff, (%rax)     # imm = 0x7FFFFFFF
movq 0x88(%rsp), %rax
movl 0x94(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rcx
movq 0x88(%rsp), %rax
movl 0x94(%rsp), %edx
addl $0x1, %edx
movslq %edx, %rdx
movq (%rax,%rdx,8), %rax
addq $0x8, %rax
movslq 0x2c(%rsp), %rdx
shlq $0x6, %rdx
addq %rdx, %rax
movslq 0x28(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x226c
jmp 0x2316
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x2253
movl $0x20, %edi
callq 0x10c0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x8(%rsp)
callq 0x1220
jmp 0x2342
movq 0x8(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x88(%rsp), %rax
movq (%rax), %rcx
movq 0x20(%rsp), %rax
movq %rcx, (%rax)
movq 0x88(%rsp), %rax
movl 0x94(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x8(%rax)
movl 0x90(%rsp), %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x14(%rax)
movq 0x20(%rsp), %rax
addq $0x98, %rsp
retq
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x18(%rsp)
movl %eax, 0x14(%rsp)
movl $0x20, %esi
callq 0x10d0
movq 0x18(%rsp), %rdi
callq 0x1110
nopw %cs:(%rax,%rax)