// File: /workspace/github_cpp_makefile_star2_O2/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, 0x4(%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 0x1648
movl $0x348, %edi            # imm = 0x348
callq 0x10d0
movq %rax, (%r15,%r12,8)
incq %r12
jmp 0x1630
movq (%r15), %rdi
orl $-0x1, (%rdi)
movl 0x4(%rsp), %ecx
movl %ecx, 0x4(%rdi)
xorl %eax, %eax
testl %ecx, %ecx
movl $0x0, %r8d
cmovgl %ecx, %r8d
leaq 0x10(%rdi), %rcx
cmpq %r8, %rax
je 0x1690
movq 0x8(%r15), %rdx
xorl %esi, %esi
cmpq $0x40, %rsi
je 0x1687
andl $0x0, -0x8(%rcx,%rsi)
movq %rdx, (%rcx,%rsi)
addq $0x10, %rsi
jmp 0x1672
incq %rax
addq $0x40, %rcx
jmp 0x1667
movq %r8, 0x38(%rsp)
leaq (%r15,%rbx,8), %rax
movq %rax, 0x30(%rsp)
leaq (%r15,%rbx,8), %rbx
addq $0x8, %rbx
movslq 0x4(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %rdi, 0x40(%rsp)
leaq 0x48(%rdi), %r13
pushq $0x1
popq %rax
cmpq 0x10(%rsp), %rax
jge 0x173c
movq %rax, 0x18(%rsp)
decl %eax
cvtsi2sd %eax, %xmm0
addsd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
pushq $0x1
popq %r12
xorl %ebp, %ebp
cmpq $0x40, %rbp
je 0x172e
movsd 0x20(%rsp), %xmm0
callq 0x1090
cvtsi2sd %r12d, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
cmpl %r14d, %eax
jle 0x170f
movl $0x7fffffff, (%r13,%rbp) # imm = 0x7FFFFFFF
movq %rbx, %rax
jmp 0x171d
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 0x16de
movq 0x18(%rsp), %rax
incq %rax
addq $0x40, %r13
jmp 0x16bc
leal 0x1(%r14), %eax
movl %eax, 0x18(%rsp)
movslq %eax, %rsi
xorl %eax, %eax
testl %r14d, %r14d
cmovgl %r14d, %eax
incl %eax
movq %rax, 0x48(%rsp)
pushq $0x1
popq %r12
movq %rsi, 0x28(%rsp)
cmpq 0x48(%rsp), %r12
je 0x18be
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 0x4(%rsp), %ebx
jg 0x17b5
cvtsi2sd %eax, %xmm0
addsd %xmm0, %xmm0
callq 0x1090
cvttsd2si %xmm0, %ecx
movl %r12d, %eax
cltd
idivl %ecx
testl %edx, %edx
jne 0x17b1
movq 0x8(%rsp), %rax
movl %ebx, 0x4(%rax)
movl %ebx, %eax
jmp 0x1785
movq 0x8(%rsp), %rdx
movl (%rdx), %eax
incl %eax
leaq 0x1(%r12), %rsi
cmpl %r14d, %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmovgel %ecx, %eax
movq 0x28(%rsp), %rcx
movq %rsi, 0x50(%rsp)
cmovlq %rsi, %rcx
movq (%r15,%rcx,8), %rcx
movl %eax, 0x38(%rdx)
movq %rcx, 0x40(%rdx)
leaq 0x50(%rdx), %rbx
pushq $0x1
popq %rcx
movq 0x8(%rsp), %rax
movslq 0x4(%rax), %rax
cmpq %rax, %rcx
jge 0x1877
movq %rcx, 0x10(%rsp)
leal -0x1(%rcx), %eax
cvtsi2sd %eax, %xmm0
addsd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
pushq $0x1
popq %r13
xorl %ebp, %ebp
cmpq $0x40, %rbp
je 0x1866
movsd 0x20(%rsp), %xmm0
callq 0x1090
cvtsi2sd %r13d, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %ecx
leal (%r12,%rcx), %eax
cmpl %r14d, %eax
jg 0x1846
addl %r12d, %ecx
decl %ecx
jmp 0x184f
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl 0x18(%rsp), %eax
cltq
movq (%r15,%rax,8), %rax
movl %ecx, -0x8(%rbx,%rbp)
movq %rax, (%rbx,%rbp)
addq $0x10, %rbp
incl %r13d
jmp 0x1818
movq 0x10(%rsp), %rcx
incq %rcx
addq $0x40, %rbx
jmp 0x17ee
decl %eax
cltq
shlq $0x6, %rax
movq 0x8(%rsp), %rcx
addq %rcx, %rax
addq $0x8, %rax
xorl %ecx, %ecx
movq 0x28(%rsp), %rsi
cmpq $0x40, %rcx
je 0x18b4
cmpl %r14d, (%rax,%rcx)
jl 0x18ae
movl $0x7fffffff, (%rax,%rcx) # imm = 0x7FFFFFFF
movq (%r15,%rsi,8), %rdx
movq %rdx, 0x8(%rax,%rcx)
addq $0x10, %rcx
jmp 0x1892
movq 0x50(%rsp), %r12
jmp 0x1760
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl 0x4(%rax), %ecx
xorl %edx, %edx
testl %ecx, %ecx
cmovlel %edx, %ecx
movq (%r15,%rsi,8), %rbx
addq $0x10, %rax
movq 0x40(%rsp), %r14
movq 0x38(%rsp), %rdi
cmpq %rcx, %rdx
je 0x190a
xorl %esi, %esi
cmpq $0x40, %rsi
je 0x1901
movl $0x7fffffff, -0x8(%rax,%rsi) # imm = 0x7FFFFFFF
movq %rbx, (%rax,%rsi)
addq $0x10, %rsi
jmp 0x18e9
incq %rdx
addq $0x40, %rax
jmp 0x18e2
movl $0x7fffffff, (%rbx)     # imm = 0x7FFFFFFF
movl 0x4(%rsp), %eax
movl %eax, 0x4(%rbx)
leaq 0x10(%rbx), %rax
xorl %ecx, %ecx
cmpq %rdi, %rcx
je 0x1945
xorl %edx, %edx
cmpq $0x40, %rdx
je 0x193c
movl $0x7fffffff, -0x8(%rax,%rdx) # imm = 0x7FFFFFFF
movq %rbx, (%rax,%rdx)
addq $0x10, %rdx
jmp 0x1924
incq %rcx
addq $0x40, %rax
jmp 0x191d
pushq $0x20
popq %rdi
callq 0x10d0
andq $0x0, 0x18(%rax)
movq %r14, (%rax)
movq %rbx, 0x8(%rax)
movl 0x4(%rsp), %ecx
movl %ecx, 0x14(%rax)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq