// File: /workspace/github_c_makefile_star2_O2/PoulinV[P]AxiCLASS/build/../source/transfer.c
// Function: transfer_get_lmax
int transfer_get_lmax(int (*get_xmin_generic)(int sgnK,
                                              int l,
                                              double nu,
                                              double xtol,
                                              double phiminabs,
                                              double *x_nonzero,
                                              int *fevals),
                      int sgnK,
                      double nu,
                      int *lvec,
                      int lsize,
                      double phiminabs,
                      double xmax,
                      double xtol,
                      int *index_l_left,
                      int *index_l_right,
                      ErrorMsg error_message){
  double x_nonzero;
  int fevals=0, index_l_mid;
  int multiplier;
  int right_boundary_checked = _FALSE_;
  class_call(get_xmin_generic(sgnK,
                              lvec[0],
                              nu,
                              xtol,
                              phiminabs,
                              &x_nonzero,
                              &fevals),
             error_message,
             error_message);
  if (x_nonzero >= xmax){
    //printf("None relevant\n");
    //x at left boundary is already larger than xmax.
    *index_l_right = MAX(lsize-1,1);
    return _SUCCESS_;
  }
  class_call(get_xmin_generic(sgnK,
                              lvec[lsize-1],
                              nu,
                              xtol,
                              phiminabs,
                              &x_nonzero,
                              &fevals),
             error_message,
             error_message);

  if (x_nonzero < xmax){
    //All Bessels are relevant
    //printf("All relevant\n");
    *index_l_left = MAX(0,(lsize-2));
    return _SUCCESS_;
  }
  /* Hunt for left boundary: */
  for (multiplier=1; ;multiplier *= 5){
    class_call(get_xmin_generic(sgnK,
                                lvec[*index_l_left],
                                nu,
                                xtol,
                                phiminabs,
                                &x_nonzero,
                                &fevals),
               error_message,
               error_message);
    if (x_nonzero <= xmax){
      //Boundary found
      break;
    }
    else{
      //We can use current index_l_left as index_l_right:
      *index_l_right = *index_l_left;
      right_boundary_checked = _TRUE_;
    }
    //Update index_l_left:
    *index_l_left = (*index_l_left)-multiplier;
    if (*index_l_left<=0){
      *index_l_left = 0;
      break;
    }
  }
  /* If not found, hunt for right boundary: */
  if (right_boundary_checked == _FALSE_){
    for (multiplier=1; ;multiplier *= 5){
      class_call(get_xmin_generic(sgnK,
                                  lvec[*index_l_right],
                                  nu,
                                  xtol,
                                  phiminabs,
                                  &x_nonzero,
                                  &fevals),
                 error_message,
                 error_message);
      if (x_nonzero >= xmax){
        //Boundary found
        break;
      }
      else{
        //We can use current index_l_right as index_l_left:
        *index_l_left = *index_l_right;
      }
      //Update index_l_right:
      *index_l_right = (*index_l_right)+multiplier;
      if (*index_l_right>=(lsize-1)){
        *index_l_right = lsize-1;
        break;
      }
    }
  }
  //  int fevalshunt=fevals;
  fevals=0;
  //Do binary search
  //  printf("Do binary search in get_lmax. \n");
  //printf("Region: [%d, %d]\n",*index_l_left,*index_l_right);
  while (((*index_l_right) - (*index_l_left)) > 1) {
    index_l_mid= (int)(0.5*((*index_l_right)+(*index_l_left)));
    //printf("left:%d, mid=%d, right=%d\n",*index_l_left,index_l_mid,*index_l_right);
    class_call(get_xmin_generic(sgnK,
                                lvec[index_l_mid],
                                nu,
                                xtol,
                                phiminabs,
                                &x_nonzero,
                                &fevals),
               error_message,
               error_message);
    if (x_nonzero < xmax)
      *index_l_left=index_l_mid;
    else
      *index_l_right=index_l_mid;
  }
  //printf("Done\n");
  return _SUCCESS_;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x848, %rsp            # imm = 0x848
movq %r9, %r14
movq %r8, %r15
movsd %xmm2, 0x18(%rsp)
movaps %xmm1, %xmm2
movl %ecx, 0x8(%rsp)
movq %rdx, %r12
movl %esi, %ebp
movq %rdi, %r13
movq 0x880(%rsp), %rbx
leaq 0xc(%rsp), %rcx
andl $0x0, (%rcx)
movl (%rdx), %esi
leaq 0x10(%rsp), %rdx
movl %ebp, %edi
movsd %xmm0, 0x28(%rsp)
movsd %xmm3, 0x38(%rsp)
movapd %xmm3, %xmm1
movsd %xmm2, 0x30(%rsp)
callq *%r13
cmpl $0x1, %eax
jne 0xab95f
leaq 0x39ad5(%rip), %rsi     # 0xe53f0
leaq 0x6c1dc(%rip), %rdx     # 0x117afe
movq %rbx, %r14
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x2e014
leaq 0x396f6(%rip), %rsi     # 0xe5034
leaq 0x6c203(%rip), %rdx     # 0x117b48
movq %r14, %rdi
movl $0x1261, %ecx           # imm = 0x1261
movq %rbx, %r8
xorl %eax, %eax
callq 0x2e014
pushq $0x1
popq %rax
jmp 0xab9f3
movsd 0x10(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jae 0xab9df
movslq 0x8(%rsp), %rax
movl -0x4(%r12,%rax,4), %esi
leaq 0x10(%rsp), %rdx
leaq 0xc(%rsp), %rcx
movl %ebp, %edi
movsd 0x28(%rsp), %xmm0
movsd 0x38(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
callq *%r13
cmpl $0x1, %eax
jne 0xaba05
leaq 0x39a48(%rip), %rsi     # 0xe53f0
leaq 0x6c1ab(%rip), %rdx     # 0x117b5a
movq %rbx, %r14
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x2e014
leaq 0x39669(%rip), %rsi     # 0xe5034
leaq 0x6c176(%rip), %rdx     # 0x117b48
movq %r14, %rdi
movl $0x1270, %ecx           # imm = 0x1270
jmp 0xab94d
movl 0x8(%rsp), %ecx
cmpl $0x3, %ecx
pushq $0x2
popq %rax
cmovgel %ecx, %eax
decl %eax
movl %eax, (%r14)
xorl %eax, %eax
addq $0x848, %rsp            # imm = 0x848
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsd 0x18(%rsp), %xmm0
ucomisd 0x10(%rsp), %xmm0
jbe 0xaba28
movl 0x8(%rsp), %ecx
cmpl $0x3, %ecx
pushq $0x2
popq %rax
cmovgel %ecx, %eax
addl $-0x2, %eax
movl %eax, (%r15)
jmp 0xab9f1
decl 0x8(%rsp)
movl (%r15), %eax
movb $0x1, %cl
movl %ecx, 0x24(%rsp)
pushq $0x1
popq %rbx
cltq
movl (%r12,%rax,4), %esi
movl %ebp, %edi
movsd 0x28(%rsp), %xmm0
movsd 0x38(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
leaq 0x10(%rsp), %rdx
leaq 0xc(%rsp), %rcx
callq *%r13
cmpl $0x1, %eax
je 0xaba90
movsd 0x18(%rsp), %xmm0
ucomisd 0x10(%rsp), %xmm0
jae 0xabad3
movl (%r15), %eax
movl %eax, (%r14)
subl %ebx, %eax
movl %eax, (%r15)
jle 0xabb38
leal (%rbx,%rbx,4), %ebx
movl $0x0, 0x24(%rsp)
jmp 0xaba38
leaq 0x39959(%rip), %rsi     # 0xe53f0
leaq 0x6c10c(%rip), %rdx     # 0x117baa
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq 0x880(%rsp), %r14
movq %r14, %rcx
xorl %eax, %eax
callq 0x2e014
leaq 0x39575(%rip), %rsi     # 0xe5034
leaq 0x6c082(%rip), %rdx     # 0x117b48
movq %r14, %rdi
movl $0x1282, %ecx           # imm = 0x1282
jmp 0xab94d
testb $0x1, 0x24(%rsp)
je 0xabb3c
movl (%r14), %eax
pushq $0x1
popq %rbx
cltq
movl (%r12,%rax,4), %esi
movl %ebp, %edi
movsd 0x28(%rsp), %xmm0
movsd 0x38(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
leaq 0x10(%rsp), %rdx
leaq 0xc(%rsp), %rcx
callq *%r13
cmpl $0x1, %eax
je 0xabbf0
movsd 0x10(%rsp), %xmm0
ucomisd 0x18(%rsp), %xmm0
jae 0xabb3c
movl (%r14), %eax
movl %eax, (%r15)
addl %ebx, %eax
movl %eax, (%r14)
cmpl 0x8(%rsp), %eax
jge 0xabc33
leal (%rbx,%rbx,4), %ebx
jmp 0xabae0
andl $0x0, (%r15)
leaq 0xc(%rsp), %rax
andl $0x0, (%rax)
movl (%r14), %eax
movl (%r15), %ecx
movl %eax, %edx
subl %ecx, %edx
cmpl $0x2, %edx
jl 0xab9f1
addl %eax, %ecx
cvtsi2sd %ecx, %xmm0
mulsd 0x3a14b(%rip), %xmm0    # 0xe5cb0
cvttsd2si %xmm0, %ebx
movslq %ebx, %rax
movl (%r12,%rax,4), %esi
movl %ebp, %edi
movsd 0x28(%rsp), %xmm0
movsd 0x38(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
leaq 0x10(%rsp), %rdx
leaq 0xc(%rsp), %rcx
callq *%r13
cmpl $0x1, %eax
je 0xabbad
movsd 0x18(%rsp), %xmm0
ucomisd 0x10(%rsp), %xmm0
movq %r14, %rax
cmovaq %r15, %rax
movl %ebx, (%rax)
jmp 0xabb44
leaq 0x3983c(%rip), %rsi     # 0xe53f0
leaq 0x6c09c(%rip), %rdx     # 0x117c57
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq 0x880(%rsp), %r14
movq %r14, %rcx
xorl %eax, %eax
callq 0x2e014
leaq 0x39458(%rip), %rsi     # 0xe5034
leaq 0x6bf65(%rip), %rdx     # 0x117b48
movq %r14, %rdi
movl $0x12bf, %ecx           # imm = 0x12BF
jmp 0xab94d
leaq 0x397f9(%rip), %rsi     # 0xe53f0
leaq 0x6c002(%rip), %rdx     # 0x117c00
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq 0x880(%rsp), %r14
movq %r14, %rcx
xorl %eax, %eax
callq 0x2e014
leaq 0x39415(%rip), %rsi     # 0xe5034
leaq 0x6bf22(%rip), %rdx     # 0x117b48
movq %r14, %rdi
movl $0x129e, %ecx           # imm = 0x129E
jmp 0xab94d
movl 0x8(%rsp), %eax
movl %eax, (%r14)
jmp 0xabb3c