// File: /workspace/github_cpp_makefile_star2_O1/jc-kim[P]swaptions/HJM_SimPath_Forward_Blocking.cpp
// Function: HJM_SimPath_Forward_Blocking(double*, int, int, double, double*, double*, double*, long*, int, double*, double*)
int HJM_SimPath_Forward_Blocking(FTYPE *ppdHJMPath,	//Matrix that stores generated HJM path (Output)
    int iN,					//Number of time-steps
    int iFactors,			//Number of factors in the HJM framework
    FTYPE dYears,			//Number of years
    FTYPE *pdForward,		//t=0 Forward curve
    FTYPE *pdTotalDrift,	//Vector containing total drift corrections for different maturities
    FTYPE *ppdFactors,	//Factor volatilities
    long *lRndSeed,			//Random number seed
    int BLOCKSIZE,
		FTYPE *pdZ, FTYPE *randZ)
{	
  //This function computes and stores an HJM Path for given inputs

  int iSuccess = 0;
  int i, j, l; //looping variables
  int b;
  FTYPE dTotalShock; //total shock by which the forward curve is hit at (t, T-t)
  FTYPE ddelt, sqrt_ddelt; //length of time steps	

  ddelt = (FTYPE)(dYears/iN);
  sqrt_ddelt = sqrt(ddelt);

  // t=0 forward curve stored iN first row of ppdHJMPath
  // At time step 0: insert expected drift 
  // rest reset to 0
  for(b = 0; b < BLOCKSIZE; b++){
    for(j = 0; j < iN; j++){
      ppdHJMPath[BLOCKSIZE*j + b] = pdForward[j]; 

      //initializing HJMPath to zero
      for(i = 1; i < iN; i++) ppdHJMPath[i * iN * BLOCKSIZE + BLOCKSIZE*j + b] = 0;
    }
  }

  // sequentially generating random numbers
  //compute random number in exact same sequence
  /* 10% of the total executition time */
  for(b = 0; b < BLOCKSIZE; b++){
    for(j = 1; j < iN; j++){
      for(l = 0; l < iFactors; l++){
        randZ[l * iN * BLOCKSIZE + BLOCKSIZE * j + b] = RanUnif(lRndSeed);
      }
    }
  }

  // shocks to hit various factors for forward curve at t
  /* 18% of the total executition time */
  serialB(pdZ, randZ, BLOCKSIZE, iN, iFactors);

  // Generation of HJM Path1
  for(b = 0; b < BLOCKSIZE; b++){ // b is the blocks
    for(j = 1; j < iN; j++) { // j is the timestep
      for(l = 0; l < iN - j; l++){ // l is the future steps
        dTotalShock = 0;

        // i steps through the stochastic factors
        for (i = 0; i < iFactors; i++) dTotalShock += ppdFactors[i * (iN - 1) + l] * pdZ[i * iN * BLOCKSIZE + BLOCKSIZE * j + b];

        ppdHJMPath[j * iN * BLOCKSIZE + BLOCKSIZE * l + b] = ppdHJMPath[(j - 1) * iN * BLOCKSIZE + BLOCKSIZE * (l + 1) + b] + pdTotalDrift[l] * ddelt + sqrt_ddelt * dTotalShock;
        //as per formula
      }
    }
  }

  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, 0x58(%rsp)
movq %r8, 0x60(%rsp)
movq %rcx, %rbx
movl %edx, 0x14(%rsp)
movq %rdi, %r15
movq %rsi, (%rsp)
cvtsi2sd %esi, %xmm1
divsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
jb 0x180e
sqrtsd %xmm0, %xmm0
jmp 0x1813
callq 0x1080
movl (%rsp), %eax
movq %rax, 0x20(%rsp)
cmpl $0x0, 0xa8(%rsp)
jle 0x189c
movl 0xa8(%rsp), %ecx
movl %ecx, %eax
imull (%rsp), %ecx
movq 0x20(%rsp), %rdx
decq %rdx
xorl %esi, %esi
movl %ecx, %edi
cmpl $0x0, (%rsp)
jle 0x1892
leaq (%r15,%rsi,8), %r8
movl %edi, %r9d
xorl %r10d, %r10d
movq %r10, %r11
imulq %rax, %r11
movsd (%rbx,%r10,8), %xmm1
movsd %xmm1, (%r8,%r11,8)
cmpl $0x2, (%rsp)
jl 0x1880
movq %rdx, %r11
movl %r9d, %r14d
movslq %r14d, %r14
movq $0x0, (%r15,%r14,8)
addl %ecx, %r14d
decq %r11
jne 0x186d
incq %r10
addl 0xa8(%rsp), %r9d
cmpq 0x20(%rsp), %r10
jne 0x184e
incq %rsi
incl %edi
cmpq %rax, %rsi
jne 0x183e
movq %r15, 0x40(%rsp)
movsd %xmm0, 0x48(%rsp)
movq 0xb8(%rsp), %r14
movq 0xb0(%rsp), %rbx
movl 0x14(%rsp), %eax
movq %rax, 0x38(%rsp)
cmpl $0x0, 0xa8(%rsp)
jle 0x195e
movq 0xa0(%rsp), %r12
movl 0xa8(%rsp), %eax
movl %eax, %r13d
imull (%rsp), %r13d
xorl %ecx, %ecx
movl %eax, 0xc(%rsp)
movl %ecx, 0x28(%rsp)
cmpl $0x2, (%rsp)
jl 0x194b
movl $0x1, %eax
movl 0xc(%rsp), %ecx
movl %ecx, 0x18(%rsp)
movl %eax, 0x10(%rsp)
cmpl $0x0, 0x14(%rsp)
jle 0x1931
movq 0x38(%rsp), %r15
movl 0x18(%rsp), %eax
movl %eax, %ebp
movq %r12, %rdi
callq 0x14ec
movslq %ebp, %rbp
movsd %xmm0, (%r14,%rbp,8)
addl %r13d, %ebp
decq %r15
jne 0x1918
movl 0x10(%rsp), %eax
incl %eax
movl 0x18(%rsp), %ecx
addl 0xa8(%rsp), %ecx
movl %ecx, 0x18(%rsp)
cmpl (%rsp), %eax
jne 0x1902
movl 0x28(%rsp), %ecx
incl %ecx
incl 0xc(%rsp)
cmpl 0xa8(%rsp), %ecx
jne 0x18eb
movq %rbx, %rdi
movq %r14, %rsi
movl 0xa8(%rsp), %ebp
movl %ebp, %edx
movq (%rsp), %rcx
movl 0x14(%rsp), %r8d
callq 0x1714
testl %ebp, %ebp
movsd 0x50(%rsp), %xmm2
movsd 0x48(%rsp), %xmm3
movq 0x40(%rsp), %r12
jle 0x1ad0
movq (%rsp), %rax
leal -0x1(%rax), %ecx
movl %ecx, 0x34(%rsp)
movslq %ecx, %rcx
movslq %eax, %rdx
movq %rdx, 0x28(%rsp)
movl 0xa8(%rsp), %edx
movl %edx, %esi
imull %eax, %esi
shlq $0x3, %rcx
xorl %edi, %edi
movl %edx, 0xc(%rsp)
cmpl $0x2, (%rsp)
jl 0x1abd
movl $0x1, %eax
movl 0xc(%rsp), %r10d
movl 0x34(%rsp), %edx
movl %edx, 0x10(%rsp)
movq %rax, 0x18(%rsp)
cmpq 0x28(%rsp), %rax
jge 0x1a9e
movq 0x18(%rsp), %rax
movq %rax, %r14
imulq 0x20(%rsp), %r14
movl 0x10(%rsp), %r15d
decl %eax
imull (%rsp), %eax
movq 0x58(%rsp), %r8
xorl %edx, %edx
cmpl $0x0, 0x14(%rsp)
jle 0x1a41
xorpd %xmm0, %xmm0
movq 0x38(%rsp), %r13
movq %r8, %rbp
movl %r10d, %r11d
movsd (%rbp), %xmm1
movslq %r11d, %r11
mulsd (%rbx,%r11,8), %xmm1
addsd %xmm1, %xmm0
addl %esi, %r11d
addq %rcx, %rbp
decq %r13
jne 0x1a22
jmp 0x1a45
xorpd %xmm0, %xmm0
mulsd %xmm3, %xmm0
leaq 0x1(%rdx), %r13
leal (%rax,%r13), %r11d
movl 0xa8(%rsp), %r9d
imull %r9d, %r11d
addl %edi, %r11d
movslq %r11d, %r11
movq 0x60(%rsp), %rbp
movsd (%rbp,%rdx,8), %xmm1
mulsd %xmm2, %xmm1
addsd (%r12,%r11,8), %xmm1
addsd %xmm0, %xmm1
addl %r14d, %edx
imull %r9d, %edx
addl %edi, %edx
movslq %edx, %rdx
movsd %xmm1, (%r12,%rdx,8)
addq $0x8, %r8
movq %r13, %rdx
cmpq %r15, %r13
jne 0x1a0c
movq 0x18(%rsp), %rax
incq %rax
decl 0x10(%rsp)
addl 0xa8(%rsp), %r10d
cmpq 0x20(%rsp), %rax
jne 0x19dc
incl %edi
incl 0xc(%rsp)
cmpl 0xa8(%rsp), %edi
jne 0x19c0
movl $0x1, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq