// File: /workspace/github_c_makefile_star2_O1/guszejnovdavid[P]MISFIT/RK4solver.c
// Function: rk4_step(double*, double, double, long)
void rk4_step ( double y[], double t, double dt, long dimension )


/******************************************************************************/
/*
  Purpose:

    Carries out the 4th order Runge-Kutta integration between t and tout
*/
{

  long j; //loop integer
  double *temp, *k1, *k2, *k3, *k4; //intermediate results
  //Allocating memory for arrays
  temp = ( double * ) malloc ( dimension * sizeof ( double ) );
  k1 = ( double * ) malloc (dimension * sizeof ( double ) );
  k2 = ( double * ) malloc ( dimension * sizeof ( double ) );
  k3 = ( double * ) malloc ( dimension * sizeof ( double ) );
  k4 = ( double * ) malloc ( dimension * sizeof ( double ) );
  
 
 Deriv_func ( t , y, k1, dt, dimension ); //calculating k1
 
 vector_const_multiply( k1, k1, dt, dimension );
 vector_const_multiply( temp, k1, 0.5, dimension ); // getting 0.5*k1
 vector_add( temp, y, temp,dimension ); // getting y+0.5*k1
 
 Deriv_func ( (t+0.5*dt) , temp, k2, dt, dimension ); //calculating k2
 
 vector_const_multiply( k2, k2, dt,dimension ); 
 vector_const_multiply( temp, k2, 0.5,dimension ); // getting 0.5*k2
 vector_add( temp, y, temp,dimension ); // getting y+0.5*k2
 Deriv_func ( (t+0.5*dt) , temp, k3, dt, dimension ); //calculating k3
 
 vector_const_multiply( k3, k3, dt,dimension ); 
 vector_add( temp, y, k3,dimension ); // getting y+k3
 Deriv_func ( (t+dt) , temp, k4, dt, dimension ); //calculating k4
 vector_const_multiply( k4, k4, dt,dimension );
 //Getting next y values
 for(j=0;j<dimension;j++){
 					 y[j]+=(k1[j]+2*k2[j]+2*k3[j]+k4[j])/6.0;
					 }		  

//Cleanup
free(temp);
free(k1);
free(k2);
free(k3);
free(k4);

return;           

      }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rbx
movsd %xmm1, 0x10(%rsp)
movsd %xmm0, 0x18(%rsp)
movq %rdi, %r14
leaq (,%rsi,8), %r15
movq %r15, %rdi
callq 0x2280
movq %rax, 0x8(%rsp)
movq %r15, %rdi
callq 0x2280
movq %rax, %r12
movq %r15, %rdi
callq 0x2280
movq %rax, %r13
movq %r15, %rdi
callq 0x2280
movq %rax, %rbp
movq %r15, %rdi
callq 0x2280
movq %rax, %r15
movsd 0x18(%rsp), %xmm0
movq %r14, %rdi
movq %r12, %rsi
movsd 0x10(%rsp), %xmm1
movq %rbx, %rdx
callq 0x8248
movq %r12, %rdi
movq %r12, %rsi
movsd 0x10(%rsp), %xmm0
movq %rbx, %rdx
callq 0xab9c
movsd 0x1f29(%rip), %xmm0     # 0xb428
movq 0x8(%rsp), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0xab9c
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movq %rdi, %rdx
movq %rbx, %rcx
callq 0xab7d
movsd 0x10(%rsp), %xmm1
movapd %xmm1, %xmm0
mulsd 0x1ef4(%rip), %xmm0     # 0xb428
addsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x8(%rsp), %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x8248
movq %r13, %rdi
movq %r13, %rsi
movsd 0x10(%rsp), %xmm0
movq %rbx, %rdx
callq 0xab9c
movq 0x8(%rsp), %rdi
movq %r13, %rsi
movsd 0x1eb4(%rip), %xmm0     # 0xb428
movq %rbx, %rdx
callq 0xab9c
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movq %rdi, %rdx
movq %rbx, %rcx
callq 0xab7d
movsd 0x20(%rsp), %xmm0
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
movsd 0x10(%rsp), %xmm1
movq %rbx, %rdx
callq 0x8248
movq %rbp, %rdi
movq %rbp, %rsi
movsd 0x10(%rsp), %xmm0
movq %rbx, %rdx
callq 0xab9c
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0xab7d
movsd 0x10(%rsp), %xmm1
movsd 0x18(%rsp), %xmm0
addsd %xmm1, %xmm0
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x8248
movq %r15, %rdi
movq %r15, %rsi
movsd 0x10(%rsp), %xmm0
movq %rbx, %rdx
callq 0xab9c
testq %rbx, %rbx
jle 0x9652
xorl %eax, %eax
movsd 0x22b3(%rip), %xmm0     # 0xb8c8
movsd (%r13,%rax,8), %xmm1
addsd %xmm1, %xmm1
addsd (%r12,%rax,8), %xmm1
movsd (%rbp,%rax,8), %xmm2
addsd %xmm2, %xmm2
addsd %xmm1, %xmm2
addsd (%r15,%rax,8), %xmm2
divsd %xmm0, %xmm2
addsd (%r14,%rax,8), %xmm2
movsd %xmm2, (%r14,%rax,8)
incq %rax
cmpq %rax, %rbx
jne 0x9615
movq 0x8(%rsp), %rdi
callq 0x2250
movq %r12, %rdi
callq 0x2250
movq %r13, %rdi
callq 0x2250
movq %rbp, %rdi
callq 0x2250
movq %r15, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2250
nop