// File: narula[P]skiplists/skiplist-extra.cc
// Function: perfect_list(int, int)
SkipList* perfect_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 = k;
	  sk[0]->nexts[i][k].nxt = sk[1+k];
	}
  }
  for (int i = 1; i < max_level; i++) {
	for (int j = 0; j < FANOUT; j++) {
	  int ptr = int(pow(FANOUT, i)*(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
	for (int k = 0; k < FANOUT; k++) {
	  int np = sk[i]->key + 1 + k;
	  node* nptr = sk[i+1+k];
	  if (np >= sz) {
		np = INT_MAX;
		nptr = sk[sz+1];
	  }
	  sk[i]->nexts[0][k].prefix = np;
	  sk[i]->nexts[0][k].nxt = nptr;
	}

	for (int j = 1; j < sk[i]->topLevel; j++) {
	  for (int k = 0; k < FANOUT; k++) {
		int nextp = int(pow(FANOUT, j)*(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 0x243e
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 0x23f5
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 0x2532
movl $0x0, 0x7c(%rsp)
cmpl $0x4, 0x7c(%rsp)
jge 0x251a
movl 0x7c(%rsp), %ecx
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
movl %ecx, (%rax)
movq 0x88(%rsp), %rax
movl 0x7c(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %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 0x248b
jmp 0x251c
movl 0x80(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x80(%rsp)
jmp 0x246f
movl $0x1, 0x78(%rsp)
movl 0x78(%rsp), %eax
cmpl 0x90(%rsp), %eax
jge 0x2698
movl $0x0, 0x74(%rsp)
cmpl $0x4, 0x74(%rsp)
jge 0x2686
movl 0x78(%rsp), %esi
movl $0x4, %edi
callq 0x4180
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 0x2603
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 0x2674
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 0x2676
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0x2553
jmp 0x2688
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x253a
movl $0x1, 0x6c(%rsp)
movl 0x6c(%rsp), %eax
movl 0x94(%rsp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x2a7d
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 0x2752
movl 0x6c(%rsp), %eax
movl %eax, 0x10(%rsp)
movl 0x68(%rsp), %esi
subl $0x1, %esi
movl $0x4, %edi
callq 0x4180
movl 0x10(%rsp), %eax
cvttsd2si %xmm0, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x2743
movl 0x68(%rsp), %ecx
movq 0x88(%rsp), %rax
movslq 0x6c(%rsp), %rdx
movq (%rax,%rdx,8), %rax
movl %ecx, 0x4(%rax)
jmp 0x2745
movl 0x68(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x68(%rsp)
jmp 0x26f0
movl $0x0, 0x64(%rsp)
cmpl $0x4, 0x64(%rsp)
jge 0x2836
movq 0x88(%rsp), %rax
movslq 0x6c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movl (%rax), %eax
addl $0x1, %eax
addl 0x64(%rsp), %eax
movl %eax, 0x60(%rsp)
movq 0x88(%rsp), %rax
movl 0x6c(%rsp), %ecx
addl $0x1, %ecx
addl 0x64(%rsp), %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x58(%rsp)
movl 0x60(%rsp), %eax
cmpl 0x94(%rsp), %eax
jl 0x27d5
movl $0x7fffffff, 0x60(%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 0x60(%rsp), %ecx
movq 0x88(%rsp), %rax
movslq 0x6c(%rsp), %rdx
movq (%rax,%rdx,8), %rax
addq $0x8, %rax
movslq 0x64(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movq 0x58(%rsp), %rcx
movq 0x88(%rsp), %rax
movslq 0x6c(%rsp), %rdx
movq (%rax,%rdx,8), %rax
addq $0x8, %rax
movslq 0x64(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x275a
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 0x2976
movl $0x0, 0x50(%rsp)
cmpl $0x4, 0x50(%rsp)
jge 0x2964
movl 0x54(%rsp), %esi
movl $0x4, %edi
callq 0x4180
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 0x28d0
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 0x28eb
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 0x2864
jmp 0x2966
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x283e
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 0x2a6b
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 0x2a59
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 0x2a5b
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
jmp 0x2999
jmp 0x2a6d
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x26a0
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 0x2b5e
movl $0x0, 0x30(%rsp)
cmpl $0x4, 0x30(%rsp)
jge 0x2b4c
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 0x2aae
jmp 0x2b4e
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x2a85
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 0x2c7b
movl $0x0, 0x28(%rsp)
cmpl $0x4, 0x28(%rsp)
jge 0x2c69
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 0x2bc1
jmp 0x2c6b
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x2ba8
movl $0x30, %edi
callq 0x10c0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x8(%rsp)
callq 0x1230
jmp 0x2c97
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, 0x10(%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 $0x30, %esi
callq 0x10d0
movq 0x18(%rsp), %rdi
callq 0x1120
nopl (%rax)