// File: /workspace/github_c_makefile_star2_O1/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, 0x10(%rsp)
movsd %xmm2, 0x30(%rsp)
movaps %xmm1, %xmm2
movl %ecx, %ebx
movq %rdx, %r12
movl %esi, %ebp
movq %rdi, %r13
movq 0x880(%rsp), %r15
leaq 0x4(%rsp), %rcx
movl $0x0, (%rcx)
movl (%rdx), %esi
leaq 0x8(%rsp), %rdx
movl %ebp, %edi
movsd %xmm0, 0x18(%rsp)
movsd %xmm3, 0x28(%rsp)
movapd %xmm3, %xmm1
movsd %xmm2, 0x20(%rsp)
callq *%r13
cmpl $0x1, %eax
jne 0xb4b4b
leaq 0x3c8e8(%rip), %rsi     # 0xf13f0
leaq 0x6f11f(%rip), %rdx     # 0x123c2e
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rcx
xorl %eax, %eax
callq 0x30c2c
leaq 0x3c50c(%rip), %rsi     # 0xf1034
leaq 0x6f149(%rip), %rdx     # 0x123c78
movq %r15, %rdi
movl $0x1261, %ecx           # imm = 0x1261
movq %rbx, %r8
xorl %eax, %eax
callq 0x30c2c
movl $0x1, %ebx
jmp 0xb4bd8
movsd 0x8(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jae 0xb4bc6
movslq %ebx, %rax
movl -0x4(%r12,%rax,4), %esi
leaq 0x8(%rsp), %rdx
leaq 0x4(%rsp), %rcx
movl %ebp, %edi
movsd 0x18(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
callq *%r13
cmpl $0x1, %eax
jne 0xb4bec
leaq 0x3c85e(%rip), %rsi     # 0xf13f0
leaq 0x6f0f1(%rip), %rdx     # 0x123c8a
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rcx
xorl %eax, %eax
callq 0x30c2c
leaq 0x3c482(%rip), %rsi     # 0xf1034
leaq 0x6f0bf(%rip), %rdx     # 0x123c78
movq %r15, %rdi
movl $0x1270, %ecx           # imm = 0x1270
jmp 0xb4b37
cmpl $0x3, %ebx
movl $0x2, %eax
cmovgel %ebx, %eax
decl %eax
movl %eax, (%r14)
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x848, %rsp            # imm = 0x848
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsd 0x30(%rsp), %xmm0
ucomisd 0x8(%rsp), %xmm0
jbe 0xb4c11
cmpl $0x3, %ebx
movl $0x2, %eax
cmovgel %ebx, %eax
addl $-0x2, %eax
movq 0x10(%rsp), %rcx
movl %eax, (%rcx)
jmp 0xb4bd6
movq 0x10(%rsp), %rax
movslq (%rax), %rax
movl (%r12,%rax,4), %esi
leaq 0x8(%rsp), %rdx
leaq 0x4(%rsp), %rcx
movl %ebp, %edi
movsd 0x18(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
callq *%r13
cmpl $0x1, %eax
jne 0xb4c86
leaq 0x3c7a6(%rip), %rsi     # 0xf13f0
leaq 0x6f089(%rip), %rdx     # 0x123cda
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq 0x880(%rsp), %r14
movq %r14, %rcx
xorl %eax, %eax
callq 0x30c2c
leaq 0x3c3c2(%rip), %rsi     # 0xf1034
leaq 0x6efff(%rip), %rdx     # 0x123c78
movq %r14, %rdi
movl $0x1282, %ecx           # imm = 0x1282
jmp 0xb4b37
movsd 0x8(%rsp), %xmm1
movsd 0x30(%rsp), %xmm0
movsd %xmm1, 0x38(%rsp)
ucomisd %xmm1, %xmm0
jb 0xb4d78
movslq (%r14), %rax
movl (%r12,%rax,4), %esi
leaq 0x8(%rsp), %rdx
leaq 0x4(%rsp), %rcx
movl %ebp, %edi
movsd 0x18(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
callq *%r13
cmpl $0x1, %eax
je 0xb4d35
decl %ebx
movl $0x1, %r15d
movsd 0x8(%rsp), %xmm0
ucomisd 0x30(%rsp), %xmm0
jae 0xb4de9
movl (%r14), %eax
movq 0x10(%rsp), %rcx
movl %eax, (%rcx)
movl (%r14), %eax
addl %r15d, %eax
movl %eax, (%r14)
cmpl %ebx, %eax
jge 0xb4de6
leal (%r15,%r15,4), %r15d
movslq (%r14), %rax
movl (%r12,%rax,4), %esi
movl %ebp, %edi
movsd 0x18(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
leaq 0x8(%rsp), %rdx
leaq 0x4(%rsp), %rcx
callq *%r13
cmpl $0x1, %eax
jne 0xb4cd7
leaq 0x3c6b4(%rip), %rsi     # 0xf13f0
leaq 0x6efed(%rip), %rdx     # 0x123d30
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq 0x880(%rsp), %r14
movq %r14, %rcx
xorl %eax, %eax
callq 0x30c2c
leaq 0x3c2d0(%rip), %rsi     # 0xf1034
leaq 0x6ef0d(%rip), %rdx     # 0x123c78
movq %r14, %rdi
movl $0x129e, %ecx           # imm = 0x129E
jmp 0xb4b37
movl $0x1, %r15d
movq 0x10(%rsp), %rcx
movl (%rcx), %eax
movl %eax, (%r14)
subl %r15d, (%rcx)
jle 0xb4dde
movslq (%rcx), %rax
movl (%r12,%rax,4), %esi
movl %ebp, %edi
movsd 0x18(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
leaq 0x8(%rsp), %rdx
leaq 0x4(%rsp), %rcx
callq *%r13
cmpl $0x1, %eax
je 0xb4c43
leal (%r15,%r15,4), %r15d
movsd 0x30(%rsp), %xmm0
ucomisd 0x8(%rsp), %xmm0
jb 0xb4d7e
ucomisd 0x38(%rsp), %xmm0
jae 0xb4ca2
jmp 0xb4de9
movl $0x0, (%rcx)
jmp 0xb4de9
movl %ebx, (%r14)
movl $0x0, 0x4(%rsp)
movl (%r14), %eax
movq 0x10(%rsp), %rcx
movl (%rcx), %ecx
movl %eax, %edx
subl %ecx, %edx
xorl %ebx, %ebx
cmpl $0x2, %edx
jl 0xb4bd8
addl %eax, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
mulsd 0x3ceb5(%rip), %xmm0    # 0xf1cd0
cvttsd2si %xmm0, %r15d
movslq %r15d, %rax
movl (%r12,%rax,4), %esi
movl %ebp, %edi
movsd 0x18(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
leaq 0x8(%rsp), %rdx
leaq 0x4(%rsp), %rcx
callq *%r13
cmpl $0x1, %eax
je 0xb4e7b
movsd 0x30(%rsp), %xmm0
ucomisd 0x8(%rsp), %xmm0
movq %r14, %rax
movq 0x10(%rsp), %rcx
cmovaq %rcx, %rax
movl %r15d, (%rax)
movl (%r14), %eax
movl (%rcx), %ecx
movl %eax, %edx
subl %ecx, %edx
cmpl $0x1, %edx
jg 0xb4e0a
jmp 0xb4bd8
leaq 0x3c56e(%rip), %rsi     # 0xf13f0
leaq 0x6eefe(%rip), %rdx     # 0x123d87
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq 0x880(%rsp), %r14
movq %r14, %rcx
xorl %eax, %eax
callq 0x30c2c
leaq 0x3c18a(%rip), %rsi     # 0xf1034
leaq 0x6edc7(%rip), %rdx     # 0x123c78
movq %r14, %rdi
movl $0x12bf, %ecx           # imm = 0x12BF
jmp 0xb4b37