// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %esi, %ebp
movq %rdi, 0x8(%rsp)
movslq %edi, %rbx
leaq 0x2(%rbx), %rax
shrq $0x3d, %rax
leaq 0x10(,%rbx,8), %rcx
xorl %edi, %edi
negq %rax
sbbq %rdi, %rdi
orq %rcx, %rdi
callq 0x1030
movq %rax, %r15
movq %rbx, 0x28(%rsp)
cmpl $-0x1, %ebx
jl 0x1bcf
movq 0x8(%rsp), %rax
leal 0x2(%rax), %ebx
xorl %r14d, %r14d
movl $0x348, %edi            # imm = 0x348
callq 0x10d0
movq %rax, (%r15,%r14,8)
incq %r14
cmpq %r14, %rbx
jne 0x1bb9
movq (%r15), %r8
movl $0xffffffff, (%r8)      # imm = 0xFFFFFFFF
movl %ebp, 0x4(%r8)
movl %ebp, %r9d
testl %ebp, %ebp
jle 0x1c13
leaq 0x10(%r8), %rax
xorl %ecx, %ecx
movq %rax, %rdx
xorl %esi, %esi
movl %esi, -0x8(%rdx)
movq 0x8(%r15,%rsi,8), %rdi
incq %rsi
movq %rdi, (%rdx)
addq $0x10, %rdx
cmpq $0x4, %rsi
jne 0x1bef
incq %rcx
addq $0x40, %rax
cmpq %r9, %rcx
jne 0x1bea
movq %r9, 0x38(%rsp)
movq %r8, 0x30(%rsp)
movl %ebp, 0x10(%rsp)
cmpl $0x2, %ebp
jl 0x1cc0
movq 0x28(%rsp), %rax
leaq (%r15,%rax,8), %rbx
addq $0x8, %rbx
movq 0x30(%rsp), %rax
leaq 0x48(%rax), %r12
movl $0x1, %r13d
leal (,%r13,2), %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x20(%rsp)
movl $0x1, %ebp
xorl %r14d, %r14d
movsd 0x20(%rsp), %xmm0
callq 0x1090
xorps %xmm1, %xmm1
cvtsi2sd %ebp, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
cmpl 0x8(%rsp), %eax
jle 0x1c91
movl $0x7fffffff, (%r12,%r14) # imm = 0x7FFFFFFF
movq %rbx, %rax
jmp 0x1c9e
leal -0x1(%rax), %ecx
movl %ecx, (%r12,%r14)
cltq
leaq (%r15,%rax,8), %rax
movq (%rax), %rax
movq %rax, 0x8(%r12,%r14)
addq $0x10, %r14
incl %ebp
cmpq $0x40, %r14
jne 0x1c64
incq %r13
addq $0x40, %r12
cmpq 0x38(%rsp), %r13
jne 0x1c46
movq 0x8(%rsp), %rax
leal 0x1(%rax), %r13d
testl %eax, %eax
movl 0x10(%rsp), %ebp
movq %r15, %r14
jle 0x1e8a
movl %r13d, %esi
movl $0x1, %r12d
movl $0x2, %r9d
movq %rsi, 0x48(%rsp)
movl %r9d, 0x14(%rsp)
leal -0x1(%r12), %eax
movq (%r14,%r12,8), %rcx
movl %eax, (%rcx)
movq %rcx, 0x18(%rsp)
movl $0x1, 0x4(%rcx)
cmpl $0x2, %ebp
jl 0x1d3e
movl $0x1, %ebx
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
incl %ebx
addsd %xmm0, %xmm0
callq 0x1090
cvttsd2si %xmm0, %ecx
movl %r12d, %eax
cltd
idivl %ecx
testl %edx, %edx
jne 0x1d3a
movq 0x18(%rsp), %rax
movl %ebx, 0x4(%rax)
cmpl %ebx, %ebp
jne 0x1d12
movq 0x18(%rsp), %r10
movl (%r10), %eax
leaq 0x1(%r12), %r11
incl %eax
xorl %ecx, %ecx
movl 0x14(%rsp), %r9d
movl %r9d, %edx
movq 0x8(%rsp), %r8
cmpl %r8d, %eax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
cmovll %eax, %esi
movl %r13d, %edi
cmovll %edx, %edi
movq (%r14,%rdi,8), %rdi
movl %esi, 0x8(%r10,%rcx)
movq %rdi, 0x10(%r10,%rcx)
incl %edx
addq $0x10, %rcx
incl %eax
cmpq $0x40, %rcx
jne 0x1d5c
movl 0x4(%r10), %eax
cmpl $0x2, %eax
jl 0x1e47
movq %r11, 0x40(%rsp)
leaq 0x50(%r10), %rbp
movl $0x1, %ecx
movq %rcx, 0x50(%rsp)
leal (%rcx,%rcx), %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x20(%rsp)
movl $0x1, %ebx
xorl %r14d, %r14d
movsd 0x20(%rsp), %xmm0
callq 0x1090
xorps %xmm1, %xmm1
cvtsi2sd %ebx, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %ecx
leal (%r12,%rcx), %edx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %r13d, %esi
cmpl 0x8(%rsp), %edx
jg 0x1df6
leal (%r12,%rcx), %eax
decl %eax
movl %edx, %esi
movslq %esi, %rcx
movq (%r15,%rcx,8), %rcx
movl %eax, -0x8(%rbp,%r14)
movq %rcx, (%rbp,%r14)
addq $0x10, %r14
incl %ebx
cmpq $0x40, %r14
jne 0x1dc2
movq 0x50(%rsp), %rcx
incq %rcx
movq 0x18(%rsp), %r10
movslq 0x4(%r10), %rax
addq $0x40, %rbp
cmpq %rax, %rcx
jl 0x1da4
movl 0x10(%rsp), %ebp
movq %r15, %r14
movq 0x8(%rsp), %r8
movl 0x14(%rsp), %r9d
movq 0x40(%rsp), %r11
cltq
shlq $0x6, %rax
addq %r10, %rax
addq $-0x38, %rax
xorl %ecx, %ecx
movq 0x48(%rsp), %rsi
cmpl %r8d, (%rax,%rcx)
jl 0x1e71
movl $0x7fffffff, (%rax,%rcx) # imm = 0x7FFFFFFF
movq (%r14,%rsi,8), %rdx
movq %rdx, 0x8(%rax,%rcx)
addq $0x10, %rcx
cmpq $0x40, %rcx
jne 0x1e5b
incl %r9d
movq %r11, %r12
cmpq %rsi, %r11
jne 0x1cec
movq 0x28(%rsp), %rax
movq (%r14,%rax,8), %rax
movslq 0x4(%rax), %rcx
movslq %r13d, %rdx
testq %rcx, %rcx
movq 0x38(%rsp), %r9
jle 0x1ed4
movq (%r14,%rdx,8), %rsi
addq $0x10, %rax
xorl %edi, %edi
xorl %r8d, %r8d
movl $0x7fffffff, -0x8(%rax,%r8) # imm = 0x7FFFFFFF
movq %rsi, (%rax,%r8)
addq $0x10, %r8
cmpq $0x40, %r8
jne 0x1eb1
incq %rdi
addq $0x40, %rax
cmpq %rcx, %rdi
jne 0x1eae
movq (%r14,%rdx,8), %rbx
movl $0x7fffffff, (%rbx)     # imm = 0x7FFFFFFF
movl %ebp, 0x4(%rbx)
testl %ebp, %ebp
jle 0x1f0f
leaq 0x10(%rbx), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movl $0x7fffffff, -0x8(%rax,%rdx) # imm = 0x7FFFFFFF
movq %rbx, (%rax,%rdx)
addq $0x10, %rdx
cmpq $0x40, %rdx
jne 0x1eed
incq %rcx
addq $0x40, %rax
cmpq %r9, %rcx
jne 0x1eeb
movl $0x30, %edi
callq 0x10d0
movq $0x0, 0x28(%rax)
movq 0x30(%rsp), %rcx
movq %rcx, (%rax)
movq %rbx, 0x8(%rax)
movl %ebp, 0x10(%rax)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop