// File: /workspace/github_c_makefile_star2_O1/PoulinV[P]AxiCLASS/build/../tools/quadrature.c
// Function: gk_adapt
int gk_adapt(
	     qss_node** node,
	     int (*test)(void * params_for_function, double q, double *psi),
	     int (*function)(void * params_for_function, double q, double *f0),
	     void * params_for_function,
	     double tol,
	     int treemode,
	     double a,
	     double b,
	     int isindefinite,
	     ErrorMsg errmsg){
  /* Do adaptive Gauss-Kronrod quadrature, while building the
     recurrence tree. If treemode!=0, store x-values and weights aswell.
     At first call, a and b should be 0 and 1 if isdefinite==_TRUE_. */
  double mid;
  /* Allocate current node: */
  class_alloc(*node,sizeof(qss_node),errmsg);
  if (treemode==0){
    (*node)->x = NULL;
    (*node)->w = NULL;
  }
  else{
    class_alloc((*node)->x,15*sizeof(double),errmsg);
    class_alloc((*node)->w,15*sizeof(double),errmsg);
  }
  (*node)->left = NULL; (*node)->right = NULL;

  gk_quad((*test), (*function), params_for_function, *node, a, b, isindefinite);
  if ((fabs((*node)->err/(*node)->I) < tol)||(tol>=1.0)){
    /* Stop recursion and return. tol>=1.0 in case of I=0 infinite recursion */
    return _SUCCESS_;
  }
  else{
    /* Call gk_adapt recursively on children:*/
    mid = 0.5*(a+b);
    //printf("<-%g,%g,%g,%g",mid,tol,(*node)->err,(*node)->I);
    gk_adapt(&((*node)->left),(*test),(*function), params_for_function, 1.5*tol,
	     treemode, a, mid, isindefinite, errmsg);
    //printf("%g->",mid);
    gk_adapt(&((*node)->right),(*test),(*function), params_for_function, 1.5*tol,
	     treemode, mid, b, isindefinite, errmsg);
    /* Update integral and error in this node and return: */
    /* Actually, it is more convenient just to keep the nodes own estimate of the
       integral for our purposes.
       (*node)->I = (*node)->left->I + (*node)->right->I;
       (*node)->err = sqrt(pow(node->left->err,2)+pow(node->right->err,2));
    */
    return _SUCCESS_;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x848, %rsp            # imm = 0x848
movl %r9d, 0x1c(%rsp)
movsd %xmm2, 0x30(%rsp)
movsd %xmm1, 0x20(%rsp)
movl %r8d, %r14d
movsd %xmm0, 0x28(%rsp)
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbx
movl $0x0, 0x14(%rsp)
movl $0x38, %edi
callq 0x8460
movq %rax, (%rbx)
testq %rax, %rax
je 0x214e8
movq %rax, %rbp
testl %r14d, %r14d
je 0x213f8
movl $0x78, %edi
callq 0x8460
movq %rax, 0x10(%rbp)
testq %rax, %rax
je 0x21527
movl $0x78, %edi
callq 0x8460
movq %rax, 0x18(%rbp)
testq %rax, %rax
jne 0x213ff
jmp 0x21566
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rbp)
movq (%rbx), %rax
xorl %ebp, %ebp
movq %rbp, 0x28(%rax)
movq (%rbx), %rax
movq %rbp, 0x30(%rax)
movq (%rbx), %rcx
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
movsd 0x20(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
movl 0x1c(%rsp), %r8d
callq 0x218eb
movsd 0x28(%rsp), %xmm0
ucomisd 0xcfe21(%rip), %xmm0    # 0xf1260
jae 0x215b2
movq (%rbx), %rdi
movsd 0x8(%rdi), %xmm1
divsd (%rdi), %xmm1
andpd 0xcfde7(%rip), %xmm1    # 0xf1240
ucomisd %xmm1, %xmm0
ja 0x215b2
movsd 0x20(%rsp), %xmm1
movapd %xmm1, %xmm2
addsd 0x30(%rsp), %xmm2
mulsd 0xd0855(%rip), %xmm2    # 0xf1cd0
movsd %xmm2, 0x38(%rsp)
addq $0x28, %rdi
mulsd 0xd2b2b(%rip), %xmm0    # 0xf3fb8
movq 0x880(%rsp), %rax
movq %rax, (%rsp)
movq %r13, %rsi
movq %r12, %rdx
movq %r15, %rcx
movsd %xmm0, 0x28(%rsp)
movl %r14d, %r8d
movl 0x1c(%rsp), %r9d
callq 0x2136c
movq (%rbx), %rbx
addq $0x30, %rbx
testb $0x1, 0x14(%rsp)
movl 0x18(%rsp), %eax
movl $0x0, %ecx
cmovel %ecx, %eax
movl %eax, 0x18(%rsp)
movb $0x1, %al
movl %eax, 0x14(%rsp)
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0x213ab
leaq 0xcfb15(%rip), %rsi     # 0xf1004
leaq 0xd2e66(%rip), %rdx     # 0xf435c
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movl $0x38, %ecx
xorl %eax, %eax
callq 0x30c2c
leaq 0xcfb23(%rip), %rsi     # 0xf1034
leaq 0xd2e4a(%rip), %rdx     # 0xf4362
movq 0x880(%rsp), %rdi
movl $0x1fd, %ecx            # imm = 0x1FD
jmp 0x215a3
leaq 0xcfad6(%rip), %rsi     # 0xf1004
leaq 0xd2e36(%rip), %rdx     # 0xf436b
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movl $0x78, %ecx
xorl %eax, %eax
callq 0x30c2c
leaq 0xcfae4(%rip), %rsi     # 0xf1034
leaq 0xd2e0b(%rip), %rdx     # 0xf4362
movq 0x880(%rsp), %rdi
movl $0x203, %ecx            # imm = 0x203
jmp 0x215a3
leaq 0xcfa97(%rip), %rsi     # 0xf1004
leaq 0xd2e02(%rip), %rdx     # 0xf4376
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movl $0x78, %ecx
xorl %eax, %eax
callq 0x30c2c
leaq 0xcfaa5(%rip), %rsi     # 0xf1034
leaq 0xd2dcc(%rip), %rdx     # 0xf4362
movq 0x880(%rsp), %rdi
movl $0x204, %ecx            # imm = 0x204
movq %rbx, %r8
xorl %eax, %eax
callq 0x30c2c
movl $0x1, %ebp
testb $0x1, 0x14(%rsp)
cmovnel 0x18(%rsp), %ebp
movl %ebp, %eax
addq $0x848, %rsp            # imm = 0x848
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq