// File: /workspace/github_cpp_makefile_star2_O3/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, 0x20(%rsp)
movl %edx, 0x14(%rsp)
movl %esi, %r14d
movq %rdi, %r13
movl 0xa8(%rsp), %r12d
cvtsi2sd %esi, %xmm1
divsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
jb 0x181b
sqrtsd %xmm0, %xmm0
jmp 0x1820
callq 0x1080
movq 0xb8(%rsp), %rsi
movq 0xb0(%rsp), %r15
testl %r12d, %r12d
jle 0x1ace
movl %r12d, %eax
movl %r14d, %ebp
movl %r12d, %ecx
imull %r14d, %ecx
leaq -0x1(%rbp), %rdx
xorl %esi, %esi
movl %ecx, %edi
testl %r14d, %r14d
jle 0x18a6
leaq (,%rsi,8), %r8
addq %r13, %r8
movl %edi, %r9d
xorl %r10d, %r10d
movq %r10, %r11
imulq %rax, %r11
movq 0x20(%rsp), %rbx
movsd (%rbx,%r10,8), %xmm1
movsd %xmm1, (%r8,%r11,8)
cmpl $0x2, %r14d
jl 0x189b
movq %rdx, %r11
movl %r9d, %ebx
movslq %ebx, %rbx
movq $0x0, (%r13,%rbx,8)
addl %ecx, %ebx
decq %r11
jne 0x1888
incq %r10
addl %r12d, %r9d
cmpq %rbp, %r10
jne 0x1864
incq %rsi
incl %edi
cmpq %rax, %rsi
jne 0x184e
testl %r12d, %r12d
movq 0xb8(%rsp), %rsi
jle 0x1ace
movq %rbp, 0x40(%rsp)
movsd %xmm0, 0x48(%rsp)
movl 0x14(%rsp), %eax
movq %rax, 0x20(%rsp)
movl %r12d, %ebp
imull %r14d, %ebp
xorl %eax, %eax
movl %r12d, 0xc(%rsp)
movq %r14, 0x30(%rsp)
movl %eax, 0x28(%rsp)
cmpl $0x2, %r14d
jl 0x195c
movl $0x1, %eax
movl 0xc(%rsp), %ecx
movl %ecx, 0x10(%rsp)
movl %eax, 0x18(%rsp)
cmpl $0x0, 0x14(%rsp)
movq 0xa0(%rsp), %rbx
jle 0x193f
movq 0x20(%rsp), %r12
movl 0x10(%rsp), %eax
movl %eax, %r14d
movq %rbx, %rdi
callq 0x14ec
movq 0xb8(%rsp), %rsi
movslq %r14d, %r14
movsd %xmm0, (%rsi,%r14,8)
addl %ebp, %r14d
decq %r12
jne 0x191e
movl 0x18(%rsp), %eax
incl %eax
movl 0xa8(%rsp), %r12d
addl %r12d, 0x10(%rsp)
movq 0x30(%rsp), %r14
cmpl %r14d, %eax
jne 0x18ff
movl 0x28(%rsp), %eax
incl %eax
incl 0xc(%rsp)
cmpl %r12d, %eax
jne 0x18e8
movq %r15, %rdi
movl %r12d, %edx
movl %r14d, %ecx
movl 0x14(%rsp), %r8d
callq 0x1718
testl %r12d, %r12d
movsd 0x50(%rsp), %xmm2
movsd 0x48(%rsp), %xmm3
jle 0x1ae1
leal -0x1(%r14), %eax
movl %eax, 0x3c(%rsp)
movslq %eax, %rcx
movslq %r14d, %rax
movq %rax, 0x28(%rsp)
shlq $0x3, %rcx
xorl %esi, %esi
movl %r12d, 0xc(%rsp)
cmpl $0x2, %r14d
jl 0x1ab8
movl $0x1, %eax
movl 0xc(%rsp), %r9d
movl 0x3c(%rsp), %edx
movl %edx, 0x10(%rsp)
movq %rax, 0x18(%rsp)
cmpq 0x28(%rsp), %rax
jge 0x1a96
movq 0x18(%rsp), %rax
movq %rax, %r11
imulq 0x40(%rsp), %r11
movl 0x10(%rsp), %r14d
leal -0x1(%rax), %r12d
imull 0x30(%rsp), %r12d
movq 0x58(%rsp), %rdx
xorl %edi, %edi
cmpl $0x0, 0x14(%rsp)
jle 0x1a39
xorpd %xmm0, %xmm0
movq 0x20(%rsp), %rbx
movq %rdx, %rax
movl %r9d, %r10d
movsd (%rax), %xmm1
movslq %r10d, %r10
mulsd (%r15,%r10,8), %xmm1
addsd %xmm1, %xmm0
addl %ebp, %r10d
addq %rcx, %rax
decq %rbx
jne 0x1a1b
jmp 0x1a3d
xorpd %xmm0, %xmm0
mulsd %xmm3, %xmm0
leaq 0x1(%rdi), %rbx
leal (%r12,%rbx), %eax
movl 0xa8(%rsp), %r8d
imull %r8d, %eax
addl %esi, %eax
cltq
movq 0x60(%rsp), %r10
movsd (%r10,%rdi,8), %xmm1
mulsd %xmm2, %xmm1
addsd (%r13,%rax,8), %xmm1
addsd %xmm0, %xmm1
addl %r11d, %edi
imull %r8d, %edi
addl %esi, %edi
movslq %edi, %rax
movsd %xmm1, (%r13,%rax,8)
addq $0x8, %rdx
movq %rbx, %rdi
cmpq %r14, %rbx
jne 0x1a05
movq 0x18(%rsp), %rax
incq %rax
decl 0x10(%rsp)
movl 0xa8(%rsp), %r12d
addl %r12d, %r9d
cmpq 0x40(%rsp), %rax
jne 0x19d1
incl %esi
incl 0xc(%rsp)
cmpl %r12d, %esi
movq 0x30(%rsp), %r14
jne 0x19b5
jmp 0x1ae1
movq %r15, %rdi
movl %r12d, %edx
movl %r14d, %ecx
movl 0x14(%rsp), %r8d
callq 0x1718
movl $0x1, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)