// File: /workspace/github_cpp_makefile_star2_O1/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:
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 0x169e
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 0x1688
movq (%r15), %rdi
movl $0xffffffff, (%rdi)     # imm = 0xFFFFFFFF
movl %ebp, 0x4(%rdi)
movl %ebp, %r8d
testl %ebp, %ebp
jle 0x16df
movq 0x8(%r15), %rax
leaq 0x10(%rdi), %rcx
xorl %edx, %edx
xorl %esi, %esi
movl $0x0, -0x8(%rcx,%rsi)
movq %rax, (%rcx,%rsi)
addq $0x10, %rsi
cmpq $0x40, %rsi
jne 0x16bd
incq %rdx
addq $0x40, %rcx
cmpq %r8, %rdx
jne 0x16bb
movq %r8, 0x38(%rsp)
movq %rdi, 0x30(%rsp)
movl %ebp, 0x14(%rsp)
cmpl $0x2, %ebp
jl 0x178b
movq 0x28(%rsp), %rax
leaq (%r15,%rax,8), %rbx
addq $0x8, %rbx
movq 0x30(%rsp), %rax
leaq 0x48(%rax), %r12
movl $0x1, %r13d
leal -0x1(%r13), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
addsd %xmm0, %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 0x175c
movl $0x7fffffff, (%r12,%r14) # imm = 0x7FFFFFFF
movq %rbx, %rax
jmp 0x1769
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 0x172f
incq %r13
addq $0x40, %r12
cmpq 0x38(%rsp), %r13
jne 0x1712
movq 0x8(%rsp), %rax
leal 0x1(%rax), %r13d
testl %eax, %eax
movl 0x14(%rsp), %ebp
movq %r15, %r14
jle 0x1929
movl %r13d, %esi
movl $0x1, %r12d
movq %rsi, 0x40(%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 0x17fe
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 0x17fa
movq 0x18(%rsp), %rax
movl %ebx, 0x4(%rax)
cmpl %ebx, %ebp
jne 0x17d2
movq 0x18(%rsp), %rdi
movl (%rdi), %ecx
movl 0x4(%rdi), %eax
incl %ecx
cmpl 0x8(%rsp), %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmovgel %edx, %ecx
leaq 0x1(%r12), %r8
movq 0x40(%rsp), %rsi
movq %rsi, %rdx
cmovlq %r8, %rdx
movq (%r14,%rdx,8), %rdx
movl %ecx, 0x38(%rdi)
movq %rdx, 0x40(%rdi)
cmpl $0x2, %eax
jl 0x18ea
movq %r8, 0x48(%rsp)
leaq 0x50(%rdi), %rbp
movl $0x1, %ecx
movq %rcx, 0x50(%rsp)
leal -0x1(%rcx), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
addsd %xmm0, %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 0x189e
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 0x186a
movq 0x50(%rsp), %rcx
incq %rcx
movq 0x18(%rsp), %rdi
movslq 0x4(%rdi), %rax
addq $0x40, %rbp
cmpq %rax, %rcx
jl 0x1849
movl 0x14(%rsp), %ebp
movq %r15, %r14
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %r8
cltq
shlq $0x6, %rax
addq %rdi, %rax
addq $-0x38, %rax
xorl %ecx, %ecx
movq 0x8(%rsp), %rdx
cmpl %edx, (%rax,%rcx)
jl 0x1913
movl $0x7fffffff, (%rax,%rcx) # imm = 0x7FFFFFFF
movq (%r14,%rsi,8), %rdx
movq %rdx, 0x8(%rax,%rcx)
addq $0x10, %rcx
cmpq $0x40, %rcx
jne 0x18f9
movq %r8, %r12
cmpq %rsi, %r8
jne 0x17b1
movq 0x28(%rsp), %rax
movq (%r14,%rax,8), %rax
movslq 0x4(%rax), %rcx
movslq %r13d, %rdx
testq %rcx, %rcx
movq 0x38(%rsp), %r9
jle 0x1973
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 0x1950
incq %rdi
addq $0x40, %rax
cmpq %rcx, %rdi
jne 0x194d
movq (%r14,%rdx,8), %rbx
movl $0x7fffffff, (%rbx)     # imm = 0x7FFFFFFF
movl %ebp, 0x4(%rbx)
testl %ebp, %ebp
jle 0x19ae
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 0x198c
incq %rcx
addq $0x40, %rax
cmpq %r9, %rcx
jne 0x198a
movl $0x20, %edi
callq 0x10d0
movq $0x0, 0x18(%rax)
movq 0x30(%rsp), %rcx
movq %rcx, (%rax)
movq %rbx, 0x8(%rax)
movl %ebp, 0x14(%rax)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq