// File: snesic[P]stochastic_thin_films/newton-raphson_tools.cpp
// Function: check_second_time_derivative(int const&, double*, double*, double*, double&, double, int&)
int check_second_time_derivative(const int& L, double *h, double *y, double *old_sol, double& dtt, double dt_old, int& no_good_solutions)
{ double maxq, t_err, dt=dtt;
    int i;

    maxq = (2*dt/dt_old)*(y[0]*dt_old+old_sol[0]*dt-(dt_old+dt)*h[0])/(h[0]*(dt+dt_old));

    for(i=0;i<L;i++)
    {   t_err = (2*dt/dt_old)*(y[i]*dt_old+old_sol[i]*dt-(dt_old+dt)*h[i])/(h[i]*(dt+dt_old));
        if(t_err>maxq) maxq=t_err;
    }
    
    if (maxq<0.01)
    {   no_good_solutions++;
        return 0;
    }
    else{ if(maxq>0.1)
            {   no_good_solutions=0;
                dtt=dt-pow(10,-fabs(round(log10(dt)))-2);
                //dtt=floorf(dt * pow(10,fabs(round(log10(dt)))+1) *100 ) / (100*pow(10,fabs(round(log10(dt)))+1));
                cout << "Time derivative error, repeat iteration for smaller dt " << dtt << endl;
                return 1;
            }
        }
    
return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movsd %xmm0, -0x38(%rbp)
movq %r9, -0x40(%rbp)
movq -0x30(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x58(%rbp)
movsd 0x1b36(%rip), %xmm0     # 0xa130
mulsd -0x58(%rbp), %xmm0
divsd -0x38(%rbp), %xmm0
movq -0x20(%rbp), %rax
movsd (%rax), %xmm2
movsd -0x38(%rbp), %xmm3
movq -0x28(%rbp), %rax
movsd (%rax), %xmm1
mulsd -0x58(%rbp), %xmm1
mulsd %xmm3, %xmm2
addsd %xmm1, %xmm2
movsd -0x38(%rbp), %xmm1
addsd -0x58(%rbp), %xmm1
movq -0x18(%rbp), %rax
movsd (%rax), %xmm3
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm3, %xmm1
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movq -0x18(%rbp), %rax
movsd (%rax), %xmm1
movsd -0x58(%rbp), %xmm2
addsd -0x38(%rbp), %xmm2
mulsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x8752
movsd 0x1a98(%rip), %xmm0     # 0xa130
mulsd -0x58(%rbp), %xmm0
divsd -0x38(%rbp), %xmm0
movq -0x20(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movsd -0x38(%rbp), %xmm3
movq -0x28(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd -0x58(%rbp), %xmm1
mulsd %xmm3, %xmm2
addsd %xmm1, %xmm2
movsd -0x38(%rbp), %xmm1
addsd -0x58(%rbp), %xmm1
movq -0x18(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm3
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm3, %xmm1
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movq -0x18(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movsd -0x58(%rbp), %xmm2
addsd -0x38(%rbp), %xmm2
mulsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0x50(%rbp), %xmm0
ucomisd -0x48(%rbp), %xmm0
jbe 0x8742
movsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
jmp 0x8744
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x8681
movsd 0x33de(%rip), %xmm0     # 0xbb38
ucomisd -0x48(%rbp), %xmm0
jbe 0x8778
movq -0x40(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x8842
movsd -0x48(%rbp), %xmm0
movsd 0x33bb(%rip), %xmm1     # 0xbb40
ucomisd %xmm1, %xmm0
jbe 0x8839
movq -0x40(%rbp), %rax
movl $0x0, (%rax)
movsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
movsd -0x58(%rbp), %xmm0
callq 0x3110
callq 0x30d0
movaps 0x3427(%rip), %xmm1     # 0xbbe0
pand %xmm1, %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd 0x1954(%rip), %xmm1     # 0xa130
subsd %xmm1, %xmm0
movl $0xa, %edi
callq 0x8aa0
movaps %xmm0, %xmm1
movsd -0x68(%rbp), %xmm0
subsd %xmm1, %xmm0
movq -0x30(%rbp), %rax
movsd %xmm0, (%rax)
movq 0x67d3(%rip), %rdi      # 0xefd8
leaq 0x345b(%rip), %rsi      # 0xbc67
callq 0x3270
movq %rax, %rdi
movq -0x30(%rbp), %rax
movsd (%rax), %xmm0
callq 0x34b0
movq %rax, %rdi
movq 0x679d(%rip), %rsi      # 0xefc8
callq 0x3290
movl $0x1, -0x4(%rbp)
jmp 0x8842
jmp 0x883b
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax,%rax)