// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movq %r9, 0x40(%rsp)
movq %r8, 0x50(%rsp)
movq %rcx, %r15
movl %edx, %ebp
movq %rdi, %r12
movq %rsi, 0x8(%rsp)
cvtsi2sd %esi, %xmm1
divsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
jb 0x1740
sqrtsd %xmm0, %xmm0
jmp 0x1745
callq 0x1080
movq 0xa0(%rsp), %r13
movq 0x8(%rsp), %r8
movslq %r8d, %rax
movq %rax, 0x48(%rsp)
movl 0x98(%rsp), %edi
movslq %edi, %rax
xorl %ecx, %ecx
testl %r8d, %r8d
movl $0x0, %edx
cmovgl %r8d, %edx
testl %eax, %eax
movl $0x0, %esi
cmovgl %edi, %esi
movq %rsi, 0x10(%rsp)
movl %r8d, %esi
imull %r8d, %edi
decq %rsi
cmpq 0x10(%rsp), %rcx
je 0x17de
leaq (%r12,%rcx,8), %r8
movl %edi, %r9d
xorl %r10d, %r10d
cmpq %rdx, %r10
je 0x17d9
movsd (%r15,%r10,8), %xmm1
movq %r10, %r11
imulq %rax, %r11
movsd %xmm1, (%r8,%r11,8)
movq %rsi, %r11
movl %r9d, %ebx
subq $0x1, %r11
jb 0x17cc
movslq %ebx, %rbx
andq $0x0, (%r8,%rbx,8)
addl %edi, %ebx
jmp 0x17ba
incq %r10
addl 0x98(%rsp), %r9d
jmp 0x179c
incq %rcx
jmp 0x178b
xorl %ecx, %ecx
testl %ebp, %ebp
movl $0x0, %eax
movl %ebp, 0x20(%rsp)
cmovgl %ebp, %eax
movq %rax, 0x28(%rsp)
movl 0x98(%rsp), %eax
movl %eax, %ebp
imull 0x8(%rsp), %ebp
movsd %xmm0, 0x30(%rsp)
cmpl 0x10(%rsp), %ecx
je 0x1875
movl %eax, 0x24(%rsp)
movl %eax, %edx
pushq $0x1
popq %r14
movl %ecx, 0x4(%rsp)
cmpl 0x8(%rsp), %r14d
jge 0x186b
movq 0x28(%rsp), %rbx
movl %edx, 0x18(%rsp)
movl %edx, %r15d
subq $0x1, %rbx
jb 0x1857
movq 0x90(%rsp), %rdi
callq 0x142c
movslq %r15d, %r15
movq 0xa8(%rsp), %rax
movsd %xmm0, (%rax,%r15,8)
addl %ebp, %r15d
jmp 0x182e
incl %r14d
movl 0x18(%rsp), %edx
addl 0x98(%rsp), %edx
movl 0x4(%rsp), %ecx
jmp 0x181b
incl %ecx
movl 0x24(%rsp), %eax
incl %eax
jmp 0x1807
movq %r13, %rdi
movq 0xa8(%rsp), %rsi
movl 0x98(%rsp), %r15d
movl %r15d, %edx
movq 0x8(%rsp), %rbx
movl %ebx, %ecx
movl 0x20(%rsp), %r8d
callq 0x1644
movsd 0x30(%rsp), %xmm3
leal -0x1(%rbx), %eax
cltq
shlq $0x3, %rax
xorl %ecx, %ecx
movl %r15d, %edi
movsd 0x38(%rsp), %xmm2
cmpl 0x10(%rsp), %ecx
je 0x19a3
movl %edi, 0x4(%rsp)
pushq $0x1
popq %rsi
movq 0x48(%rsp), %r9
subq %rsi, %r9
jle 0x1996
leal -0x1(%rsi), %r10d
movq 0x8(%rsp), %rdx
imull %edx, %r10d
movq %rsi, 0x18(%rsp)
movl %esi, %r11d
imull %edx, %r11d
movq 0x40(%rsp), %rdx
xorl %esi, %esi
cmpq %r9, %rsi
jge 0x1982
xorpd %xmm0, %xmm0
movq %rdx, %r8
movq 0x28(%rsp), %rbx
movl %edi, %r15d
subq $0x1, %rbx
jb 0x192e
movsd (%r8), %xmm1
movslq %r15d, %r15
mulsd (%r13,%r15,8), %xmm1
addsd %xmm1, %xmm0
addl %ebp, %r15d
addq %rax, %r8
jmp 0x190d
leaq 0x1(%rsi), %r8
leal (%r10,%r8), %ebx
movl 0x98(%rsp), %r14d
imull %r14d, %ebx
addl %ecx, %ebx
movslq %ebx, %rbx
movq 0x50(%rsp), %r15
movsd (%r15,%rsi,8), %xmm1
mulsd %xmm2, %xmm1
addsd (%r12,%rbx,8), %xmm1
mulsd %xmm3, %xmm0
addsd %xmm1, %xmm0
addl %r11d, %esi
imull %r14d, %esi
addl %ecx, %esi
movslq %esi, %rsi
movsd %xmm0, (%r12,%rsi,8)
addq $0x8, %rdx
movq %r8, %rsi
jmp 0x18f5
movq 0x18(%rsp), %rsi
incq %rsi
addl 0x98(%rsp), %edi
jmp 0x18c7
incl %ecx
movl 0x4(%rsp), %edi
incl %edi
jmp 0x18b6
pushq $0x1
popq %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)