// File: /workspace/github_cpp_makefile_star2_O3/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, 0x18(%rsp)
movslq %edi, %r14
leaq 0x2(%r14), %rax
shrq $0x3d, %rax
leaq 0x10(,%r14,8), %rcx
xorl %ebx, %ebx
negq %rax
movl $0x0, %edi
sbbq %rdi, %rdi
orq %rcx, %rdi
callq 0x1030
movq %rax, %r15
movq %r14, 0x38(%rsp)
addl $0x2, %r14d
movl $0x348, %edi            # imm = 0x348
callq 0x10d0
movq %rax, (%r15,%rbx,8)
incq %rbx
cmpq %rbx, %r14
jne 0x1bf8
movq (%r15), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq %rax, 0x20(%rsp)
movl %ebp, 0x4(%rax)
movl %ebp, %eax
movq %rax, 0x10(%rsp)
movl %ebp, 0x8(%rsp)
testl %ebp, %ebp
jle 0x1d10
movq 0x20(%rsp), %rax
addq $0x10, %rax
xorl %ecx, %ecx
movl 0x8(%rsp), %r8d
movq 0x10(%rsp), %r9
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 0x1c4c
incq %rcx
addq $0x40, %rax
cmpq %r9, %rcx
jne 0x1c47
cmpl $0x1, %r8d
je 0x1d10
movq 0x38(%rsp), %rax
leaq (%r15,%rax,8), %rbx
addq $0x8, %rbx
movq 0x20(%rsp), %rax
leaq 0x48(%rax), %r12
movl $0x1, %r13d
leal (,%r13,2), %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x30(%rsp)
movl $0x1, %ebp
xorl %r14d, %r14d
movsd 0x30(%rsp), %xmm0
callq 0x1090
xorps %xmm1, %xmm1
cvtsi2sd %ebp, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
cmpl 0x18(%rsp), %eax
jle 0x1ce1
movl $0x7fffffff, (%r12,%r14) # imm = 0x7FFFFFFF
movq %rbx, %rax
jmp 0x1cee
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 0x1cb4
incq %r13
addq $0x40, %r12
cmpq 0x10(%rsp), %r13
jne 0x1c96
movq 0x18(%rsp), %r14
leal 0x1(%r14), %r13d
testl %r14d, %r14d
movl 0x8(%rsp), %ebp
jle 0x1ed2
movl %r13d, %esi
movl $0x1, %r12d
movl $0x2, %r8d
movq %rsi, 0x48(%rsp)
leal -0x1(%r12), %eax
movq (%r15,%r12,8), %r9
movl %eax, (%r9)
movl $0x1, 0x4(%r9)
cmpl $0x2, %ebp
movq %r9, 0x28(%rsp)
movl %r8d, 0xc(%rsp)
jl 0x1d9b
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 0x1d8a
movq 0x28(%rsp), %rax
movl %ebx, 0x4(%rax)
cmpl %ebx, %ebp
jne 0x1d62
movq 0x28(%rsp), %r9
movl (%r9), %eax
movl 0xc(%rsp), %r8d
leaq 0x1(%r12), %r10
incl %eax
xorl %ecx, %ecx
movl %r8d, %edx
cmpl %r14d, %eax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
cmovll %eax, %esi
movl %r13d, %edi
cmovll %edx, %edi
movq (%r15,%rdi,8), %rdi
movl %esi, 0x8(%r9,%rcx)
movq %rdi, 0x10(%r9,%rcx)
incl %edx
addq $0x10, %rcx
incl %eax
cmpq $0x40, %rcx
jne 0x1da7
movl 0x4(%r9), %eax
cmpl $0x2, %eax
jl 0x1e8f
movq %r10, 0x40(%rsp)
leaq 0x50(%r9), %rbp
movl $0x1, %ecx
movq %rcx, 0x50(%rsp)
leal (%rcx,%rcx), %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x30(%rsp)
movl $0x1, %ebx
xorl %r14d, %r14d
movsd 0x30(%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 0x18(%rsp), %edx
jg 0x1e41
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 0x1e0d
movq 0x50(%rsp), %rcx
incq %rcx
movq 0x28(%rsp), %r9
movslq 0x4(%r9), %rax
addq $0x40, %rbp
cmpq %rax, %rcx
jl 0x1def
movl 0x8(%rsp), %ebp
movq 0x18(%rsp), %r14
movl 0xc(%rsp), %r8d
movq 0x40(%rsp), %r10
cltq
shlq $0x6, %rax
addq %r9, %rax
addq $-0x38, %rax
xorl %ecx, %ecx
movq 0x48(%rsp), %rsi
cmpl %r14d, (%rax,%rcx)
jl 0x1eb9
movl $0x7fffffff, (%rax,%rcx) # imm = 0x7FFFFFFF
movq (%r15,%rsi,8), %rdx
movq %rdx, 0x8(%rax,%rcx)
addq $0x10, %rcx
cmpq $0x40, %rcx
jne 0x1ea3
incl %r8d
movq %r10, %r12
cmpq %rsi, %r10
jne 0x1d3a
movq 0x38(%rsp), %rax
movq (%r15,%rax,8), %rax
movslq 0x4(%rax), %rcx
testq %rcx, %rcx
jle 0x1f1c
movslq %r13d, %rdx
movq (%r15,%rdx,8), %rbx
addq $0x10, %rax
xorl %edx, %edx
movq 0x10(%rsp), %rdi
xorl %esi, %esi
movl $0x7fffffff, -0x8(%rax,%rsi) # imm = 0x7FFFFFFF
movq %rbx, (%rax,%rsi)
addq $0x10, %rsi
cmpq $0x40, %rsi
jne 0x1ef8
incq %rdx
addq $0x40, %rax
cmpq %rcx, %rdx
jne 0x1ef6
jmp 0x1f28
movslq %r13d, %rax
movq (%r15,%rax,8), %rbx
movq 0x10(%rsp), %rdi
movl $0x7fffffff, (%rbx)     # imm = 0x7FFFFFFF
movl %ebp, 0x4(%rbx)
testl %ebp, %ebp
jle 0x1f5f
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 0x1f3d
incq %rcx
addq $0x40, %rax
cmpq %rdi, %rcx
jne 0x1f3b
movl $0x30, %edi
callq 0x10d0
movq $0x0, 0x28(%rax)
movq 0x20(%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