// File: /workspace/github_c_makefile_star2_O2/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, 0x14(%rsp)
movsd %xmm2, 0x28(%rsp)
movsd %xmm1, 0x18(%rsp)
movl %r8d, %r14d
movsd %xmm0, 0x20(%rsp)
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbx
movl $0x0, 0x10(%rsp)
movq %rsi, 0x30(%rsp)
pushq $0x38
popq %rdi
callq 0x8490
movq %rax, (%rbx)
testq %rax, %rax
je 0x1f233
movq %rax, %rbp
testl %r14d, %r14d
je 0x1f156
movq %r12, %r13
movl %r14d, %r12d
movq %r15, %r14
pushq $0x78
popq %r15
movq %r15, %rdi
callq 0x8490
movq %rax, 0x10(%rbp)
testq %rax, %rax
je 0x1f270
movq %r15, %rdi
callq 0x8490
movq %rax, 0x18(%rbp)
testq %rax, %rax
movq %r14, %r15
movl %r12d, %r14d
movq %r13, %r12
movq 0x30(%rsp), %r13
jne 0x1f15d
jmp 0x1f2ad
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rbp)
movq (%rbx), %rax
andq $0x0, 0x28(%rax)
movq (%rbx), %rax
andq $0x0, 0x30(%rax)
movq (%rbx), %rcx
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
movsd 0x18(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movl 0x14(%rsp), %r8d
callq 0x1f586
xorl %ecx, %ecx
movsd 0x20(%rsp), %xmm0
ucomisd 0xc60c1(%rip), %xmm0    # 0xe5260
jae 0x1f2f5
movq (%rbx), %rdi
movsd 0x8(%rdi), %xmm1
divsd (%rdi), %xmm1
andpd 0xc6087(%rip), %xmm1    # 0xe5240
ucomisd %xmm1, %xmm0
ja 0x1f2f5
movsd 0x18(%rsp), %xmm1
movapd %xmm1, %xmm2
addsd 0x28(%rsp), %xmm2
mulsd 0xc6ad5(%rip), %xmm2    # 0xe5cb0
movsd %xmm2, 0x38(%rsp)
addq $0x28, %rdi
mulsd 0xc8d9b(%rip), %xmm0    # 0xe7f88
movq 0x880(%rsp), %rax
movq %rax, (%rsp)
movq %r13, %rsi
movq %r12, %rdx
movq %r15, %rcx
movsd %xmm0, 0x20(%rsp)
movl %r14d, %r8d
movl 0x14(%rsp), %r9d
callq 0x1f0b0
movq (%rbx), %rbx
addq $0x30, %rbx
movb $0x1, %al
movl %eax, 0x10(%rsp)
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
jmp 0x1f0f4
leaq 0xc5dca(%rip), %rsi     # 0xe5004
leaq 0xc90eb(%rip), %rdx     # 0xe832c
leaq 0x40(%rsp), %rbx
pushq $0x38
popq %rcx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2e014
leaq 0xc5dda(%rip), %rsi     # 0xe5034
leaq 0xc90d1(%rip), %rdx     # 0xe8332
movq 0x880(%rsp), %rdi
movl $0x1fd, %ecx            # imm = 0x1FD
jmp 0x1f2e8
leaq 0xc5d8d(%rip), %rsi     # 0xe5004
leaq 0xc90bd(%rip), %rdx     # 0xe833b
leaq 0x40(%rsp), %rbx
pushq $0x78
popq %rcx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2e014
leaq 0xc5d9d(%rip), %rsi     # 0xe5034
leaq 0xc9094(%rip), %rdx     # 0xe8332
movq 0x880(%rsp), %rdi
movl $0x203, %ecx            # imm = 0x203
jmp 0x1f2e8
leaq 0xc5d50(%rip), %rsi     # 0xe5004
leaq 0xc908b(%rip), %rdx     # 0xe8346
leaq 0x40(%rsp), %rbx
pushq $0x78
popq %rcx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2e014
leaq 0xc5d60(%rip), %rsi     # 0xe5034
leaq 0xc9057(%rip), %rdx     # 0xe8332
movq 0x880(%rsp), %rdi
movl $0x204, %ecx            # imm = 0x204
movq %rbx, %r8
xorl %eax, %eax
callq 0x2e014
pushq $0x1
popq %rcx
xorl %eax, %eax
testb $0x1, 0x10(%rsp)
cmovel %ecx, %eax
addq $0x848, %rsp            # imm = 0x848
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq