// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movl %esi, 0x14(%rsp)
movl %edi, %r14d
movslq %edi, %rbx
leaq 0x2(%rbx), %rax
shrq $0x3d, %rax
leaq 0x10(,%rbx,8), %rcx
xorl %r12d, %r12d
negq %rax
movl $0x0, %edi
sbbq %rdi, %rdi
orq %rcx, %rdi
callq 0x1030
movq %rax, %r15
cmpl $-0x1, %ebx
pushq $-0x2
popq %r13
cmovgel %r14d, %r13d
addl $0x2, %r13d
cmpq %r12, %r13
je 0x1acb
movl $0x348, %edi            # imm = 0x348
callq 0x10d0
movq %rax, (%r15,%r12,8)
incq %r12
jmp 0x1ab3
movq (%r15), %r9
orl $-0x1, (%r9)
movl 0x14(%rsp), %r8d
movl %r8d, 0x4(%r9)
xorl %eax, %eax
testl %r8d, %r8d
movl $0x0, %r10d
cmovgl %r8d, %r10d
leaq 0x10(%r9), %rcx
cmpq %r10, %rax
je 0x1b1b
movq %rcx, %rdx
xorl %esi, %esi
cmpq $0x4, %rsi
je 0x1b12
movl %esi, -0x8(%rdx)
movq 0x8(%r15,%rsi,8), %rdi
incq %rsi
movq %rdi, (%rdx)
addq $0x10, %rdx
jmp 0x1af8
incq %rax
addq $0x40, %rcx
jmp 0x1aee
movq %r10, 0x38(%rsp)
leaq (%r15,%rbx,8), %rax
movq %rax, 0x30(%rsp)
leaq (%r15,%rbx,8), %rbx
addq $0x8, %rbx
movslq %r8d, %rax
movq %rax, 0x8(%rsp)
movq %r9, 0x40(%rsp)
leaq 0x48(%r9), %r13
pushq $0x1
popq %rax
cmpq 0x8(%rsp), %rax
jge 0x1bc2
movq %rax, 0x18(%rsp)
addl %eax, %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x20(%rsp)
pushq $0x1
popq %r12
xorl %ebp, %ebp
cmpq $0x40, %rbp
je 0x1bb4
movsd 0x20(%rsp), %xmm0
callq 0x1090
cvtsi2sd %r12d, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
cmpl %r14d, %eax
jle 0x1b95
movl $0x7fffffff, (%r13,%rbp) # imm = 0x7FFFFFFF
movq %rbx, %rax
jmp 0x1ba3
leal -0x1(%rax), %ecx
movl %ecx, (%r13,%rbp)
cltq
leaq (%r15,%rax,8), %rax
movq (%rax), %rax
movq %rax, 0x8(%r13,%rbp)
addq $0x10, %rbp
incl %r12d
jmp 0x1b64
movq 0x18(%rsp), %rax
incq %rax
addq $0x40, %r13
jmp 0x1b45
leal 0x1(%r14), %eax
movl %eax, 0x18(%rsp)
movslq %eax, %r13
xorl %eax, %eax
testl %r14d, %r14d
cmovgl %r14d, %eax
incl %eax
movq %rax, 0x50(%rsp)
pushq $0x1
popq %r12
pushq $0x2
popq %rax
movq %rax, 0x28(%rsp)
movl 0x14(%rsp), %ebp
movq %r13, 0x48(%rsp)
cmpq 0x50(%rsp), %r12
je 0x1d76
movq (%r15,%r12,8), %rcx
leal -0x1(%r12), %eax
movl %eax, (%rcx)
movq %rcx, 0x8(%rsp)
movl $0x1, 0x4(%rcx)
pushq $0x1
popq %rax
leal 0x1(%rax), %ebx
cmpl %ebp, %ebx
jg 0x1c45
cvtsi2sd %eax, %xmm0
addsd %xmm0, %xmm0
callq 0x1090
cvttsd2si %xmm0, %ecx
movl %r12d, %eax
cltd
idivl %ecx
testl %edx, %edx
jne 0x1c41
movq 0x8(%rsp), %rax
movl %ebx, 0x4(%rax)
movl %ebx, %eax
jmp 0x1c17
movq 0x8(%rsp), %rax
movl (%rax), %eax
leaq 0x1(%r12), %rcx
movq %rcx, 0x58(%rsp)
incl %eax
movq 0x28(%rsp), %rcx
xorl %edx, %edx
cmpq $0x40, %rdx
je 0x1c95
cmpl %r14d, %eax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
cmovll %eax, %esi
movq %r13, %rdi
cmovlq %rcx, %rdi
movq (%r15,%rdi,8), %rdi
movq 0x8(%rsp), %r8
movl %esi, 0x8(%r8,%rdx)
movq %rdi, 0x10(%r8,%rdx)
addq $0x10, %rdx
incq %rcx
incl %eax
jmp 0x1c5f
movq 0x8(%rsp), %rax
leaq 0x50(%rax), %r13
pushq $0x1
popq %rcx
movq 0x8(%rsp), %rax
movslq 0x4(%rax), %rax
cmpq %rax, %rcx
jge 0x1d26
movq %rcx, 0x60(%rsp)
leal (%rcx,%rcx), %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x20(%rsp)
pushq $0x1
popq %rbx
xorl %ebp, %ebp
cmpq $0x40, %rbp
je 0x1d15
movsd 0x20(%rsp), %xmm0
callq 0x1090
cvtsi2sd %ebx, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %ecx
leal (%r12,%rcx), %eax
cmpl %r14d, %eax
jg 0x1cf4
addl %r12d, %ecx
decl %ecx
jmp 0x1cfd
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl 0x18(%rsp), %eax
cltq
movq (%r15,%rax,8), %rax
movl %ecx, -0x8(%r13,%rbp)
movq %rax, (%r13,%rbp)
addq $0x10, %rbp
incl %ebx
jmp 0x1cc7
movq 0x60(%rsp), %rcx
incq %rcx
addq $0x40, %r13
jmp 0x1ca1
decl %eax
cltq
shlq $0x6, %rax
movq 0x8(%rsp), %rcx
addq %rcx, %rax
addq $0x8, %rax
xorl %ecx, %ecx
movl 0x14(%rsp), %ebp
movq 0x48(%rsp), %r13
cmpq $0x40, %rcx
je 0x1d67
cmpl %r14d, (%rax,%rcx)
jl 0x1d61
movl $0x7fffffff, (%rax,%rcx) # imm = 0x7FFFFFFF
movq (%r15,%r13,8), %rdx
movq %rdx, 0x8(%rax,%rcx)
addq $0x10, %rcx
jmp 0x1d45
incq 0x28(%rsp)
movq 0x58(%rsp), %r12
jmp 0x1bf2
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl 0x4(%rax), %ecx
xorl %edx, %edx
testl %ecx, %ecx
cmovlel %edx, %ecx
movq (%r15,%r13,8), %rbx
addq $0x10, %rax
movq 0x40(%rsp), %r14
movq 0x38(%rsp), %rdi
cmpq %rcx, %rdx
je 0x1dc2
xorl %esi, %esi
cmpq $0x40, %rsi
je 0x1db9
movl $0x7fffffff, -0x8(%rax,%rsi) # imm = 0x7FFFFFFF
movq %rbx, (%rax,%rsi)
addq $0x10, %rsi
jmp 0x1da1
incq %rdx
addq $0x40, %rax
jmp 0x1d9a
movl $0x7fffffff, (%rbx)     # imm = 0x7FFFFFFF
movl %ebp, 0x4(%rbx)
leaq 0x10(%rbx), %rax
xorl %ecx, %ecx
cmpq %rdi, %rcx
je 0x1df9
xorl %edx, %edx
cmpq $0x40, %rdx
je 0x1df0
movl $0x7fffffff, -0x8(%rax,%rdx) # imm = 0x7FFFFFFF
movq %rbx, (%rax,%rdx)
addq $0x10, %rdx
jmp 0x1dd8
incq %rcx
addq $0x40, %rax
jmp 0x1dd1
pushq $0x30
popq %rdi
callq 0x10d0
andq $0x0, 0x28(%rax)
movq %r14, (%rax)
movq %rbx, 0x8(%rax)
movl %ebp, 0x10(%rax)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop