// File: /workspace/github_cpp_makefile_star2_O3/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, 0x10(%rsp)
movslq %edi, %r15
leaq 0x2(%r15), %rax
shrq $0x3d, %rax
leaq 0x10(,%r15,8), %rcx
xorl %ebx, %ebx
negq %rax
movl $0x0, %edi
sbbq %rdi, %rdi
orq %rcx, %rdi
callq 0x1030
movq %rax, %r12
movq %r15, 0x38(%rsp)
addl $0x2, %r15d
movl $0x348, %edi            # imm = 0x348
callq 0x10d0
movq %rax, (%r12,%rbx,8)
incq %rbx
cmpq %rbx, %r15
jne 0x168b
movq %r12, %r14
movq (%r12), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq %rax, 0x20(%rsp)
movl %ebp, 0x4(%rax)
movl %ebp, %eax
movq %rax, 0x18(%rsp)
movl %ebp, 0xc(%rsp)
testl %ebp, %ebp
jle 0x17a3
movq 0x8(%r14), %rax
movq 0x20(%rsp), %rcx
addq $0x10, %rcx
xorl %edx, %edx
movl 0xc(%rsp), %edi
movq 0x18(%rsp), %r8
xorl %esi, %esi
movl $0x0, -0x8(%rcx,%rsi)
movq %rax, (%rcx,%rsi)
addq $0x10, %rsi
cmpq $0x40, %rsi
jne 0x16e3
incq %rdx
addq $0x40, %rcx
cmpq %r8, %rdx
jne 0x16e1
cmpl $0x1, %edi
je 0x17a3
movq 0x38(%rsp), %rax
leaq (%r14,%rax,8), %rbx
addq $0x8, %rbx
movq 0x20(%rsp), %rax
leaq 0x48(%rax), %r12
movl $0x1, %r13d
leal -0x1(%r13), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
addsd %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
movl $0x1, %ebp
xorl %r15d, %r15d
movsd 0x30(%rsp), %xmm0
callq 0x1090
xorps %xmm1, %xmm1
cvtsi2sd %ebp, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
cmpl 0x10(%rsp), %eax
jle 0x1774
movl $0x7fffffff, (%r12,%r15) # imm = 0x7FFFFFFF
movq %rbx, %rax
jmp 0x1781
leal -0x1(%rax), %ecx
movl %ecx, (%r12,%r15)
cltq
leaq (%r14,%rax,8), %rax
movq (%rax), %rax
movq %rax, 0x8(%r12,%r15)
addq $0x10, %r15
incl %ebp
cmpq $0x40, %r15
jne 0x1747
incq %r13
addq $0x40, %r12
cmpq 0x18(%rsp), %r13
jne 0x172a
movq 0x10(%rsp), %rax
leal 0x1(%rax), %r13d
testl %eax, %eax
movl 0xc(%rsp), %ebp
movq %r14, %r15
jle 0x1949
movl %r13d, %esi
movl $0x1, %r12d
movq %rsi, 0x40(%rsp)
leal -0x1(%r12), %eax
movq (%r15,%r12,8), %rdi
movl %eax, (%rdi)
movl $0x1, 0x4(%rdi)
movl $0x1, %eax
movl %r12d, %ecx
cmpl $0x2, %ebp
movq %rdi, 0x28(%rsp)
jl 0x182f
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 0x181a
movq 0x28(%rsp), %rax
movl %ebx, 0x4(%rax)
cmpl %ebx, %ebp
jne 0x17f2
movq 0x28(%rsp), %rdi
movl (%rdi), %ecx
movl 0x4(%rdi), %eax
incl %ecx
movq 0x40(%rsp), %rsi
cmpl 0x10(%rsp), %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmovgel %edx, %ecx
leaq 0x1(%r12), %r8
movq %rsi, %rdx
cmovlq %r8, %rdx
movq (%r15,%rdx,8), %rdx
movl %ecx, 0x38(%rdi)
movq %rdx, 0x40(%rdi)
cmpl $0x2, %eax
jl 0x190a
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, 0x30(%rsp)
movl $0x1, %ebx
xorl %r15d, %r15d
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 0x10(%rsp), %edx
jg 0x18be
leal (%r12,%rcx), %eax
decl %eax
movl %edx, %esi
movslq %esi, %rcx
movq (%r14,%rcx,8), %rcx
movl %eax, -0x8(%rbp,%r15)
movq %rcx, (%rbp,%r15)
addq $0x10, %r15
incl %ebx
cmpq $0x40, %r15
jne 0x188a
movq 0x50(%rsp), %rcx
incq %rcx
movq 0x28(%rsp), %rdi
movslq 0x4(%rdi), %rax
addq $0x40, %rbp
cmpq %rax, %rcx
jl 0x1869
movl 0xc(%rsp), %ebp
movq %r14, %r15
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %r8
cltq
shlq $0x6, %rax
addq %rdi, %rax
addq $-0x38, %rax
xorl %ecx, %ecx
movq 0x10(%rsp), %rdx
cmpl %edx, (%rax,%rcx)
jl 0x1933
movl $0x7fffffff, (%rax,%rcx) # imm = 0x7FFFFFFF
movq (%r15,%rsi,8), %rdx
movq %rdx, 0x8(%rax,%rcx)
addq $0x10, %rcx
cmpq $0x40, %rcx
jne 0x1919
movq %r8, %r12
cmpq %rsi, %r8
jne 0x17c9
movq 0x38(%rsp), %rax
movq (%r15,%rax,8), %rax
movslq 0x4(%rax), %rcx
testq %rcx, %rcx
jle 0x1993
movslq %r13d, %rdx
movq (%r15,%rdx,8), %rbx
addq $0x10, %rax
xorl %edx, %edx
movq 0x18(%rsp), %rdi
xorl %esi, %esi
movl $0x7fffffff, -0x8(%rax,%rsi) # imm = 0x7FFFFFFF
movq %rbx, (%rax,%rsi)
addq $0x10, %rsi
cmpq $0x40, %rsi
jne 0x196f
incq %rdx
addq $0x40, %rax
cmpq %rcx, %rdx
jne 0x196d
jmp 0x199f
movslq %r13d, %rax
movq (%r15,%rax,8), %rbx
movq 0x18(%rsp), %rdi
movl $0x7fffffff, (%rbx)     # imm = 0x7FFFFFFF
movl %ebp, 0x4(%rbx)
testl %ebp, %ebp
jle 0x19d6
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 0x19b4
incq %rcx
addq $0x40, %rax
cmpq %rdi, %rcx
jne 0x19b2
movl $0x20, %edi
callq 0x10d0
movq $0x0, 0x18(%rax)
movq 0x20(%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