// File: /workspace/github_cpp_makefile_star2_O3/4n6ist[P]usn_analytics/lib/usnjrnl.cpp
// Function: UsnJrnl::WriteExecutedRecords(char*, bool)
int UsnJrnl::WriteExecutedRecords(char *odname, bool lt) {

  vector<UsnExecuted> usnexecuted_set;  

  // create usnexecuted_set
  for(int i=0; i < usnmain_set.size(); i++) { 
    if (usnmain_set[i].file_name.size() >= 16 
      && usnmain_set[i].file_name.find(".pf", usnmain_set[i].file_name.size()-3) != string::npos
      && usnmain_set[i].file_name.rfind("-", usnmain_set[i].file_name.size()-12) != string::npos) {
      if (usnmain_set[i].reasons_i & CREATE || usnmain_set[i].reasons_i & EXTEND) {
        // calculate run count based on prefetch file name
        uint16_t exe_count=1;
        for(int j=0; j < usnexecuted_set.size(); j++) {
          if(usnmain_set[i].file_name == usnexecuted_set[j].file_name)
            exe_count++;
        }
        UsnExecuted* ue = new UsnExecuted();
        ue->StoreRecord(&usnmain_set[i], exe_count);
        usnexecuted_set.push_back(*ue);
        delete ue;
      }
    }
  }

  map<string, uint16_t> exe_name_table; // exe_name, exe_cnt

  // create exe_name table
  for(int i=0; i < usnexecuted_set.size(); i++)
    if(exe_name_table.find(usnexecuted_set[i].exe_name) == exe_name_table.end())
      exe_name_table[usnexecuted_set[i].exe_name] = usnexecuted_set[i].exe_cnt;
    else if(usnexecuted_set[i].exe_cnt > exe_name_table[usnexecuted_set[i].exe_name])
      exe_name_table[usnexecuted_set[i].exe_name] = usnexecuted_set[i].exe_cnt;

  // write to report  
  fprintf(fp_ofreport, "\n[Prefetch Exe Name] %lu exe (name, count)\n", exe_name_table.size());
  for(auto x: exe_name_table)
    fprintf(fp_ofreport, "%s, %d\n", x.first.c_str(), x.second);

  // write to file
  FILE *fp_ofexecuted;
  string ofexecuted;
  ofexecuted = string(odname) + SEP + "usn_analytics_executed.csv";

  if((fp_ofexecuted = fopen(ofexecuted.c_str(), "w")) == NULL) {
    perror("Output Records File Error");
    exit(EXIT_FAILURE);
  } 

  WriteExecutedHeader(fp_ofexecuted, lt);

  for(int i=0; i < usnexecuted_set.size(); i++)
    usnexecuted_set[i].WriteExecutedRecord(fp_ofexecuted);
    
  printf("...Done\n");
  fclose(fp_ofexecuted);
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movl %edx, 0x34(%rsp)
movq %rsi, 0x68(%rsp)
movq %rdi, %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x50(%rdi), %rbp
cmpq %rbp, 0x58(%rdi)
movq %rdi, 0x38(%rsp)
je 0x6cca
xorl %r12d, %r12d
leaq 0x899c(%rip), %rbx      # 0xf3b9
leaq 0x8959(%rip), %r14      # 0xf37d
imulq $0xca, %r12, %r13
movq 0x42(%rbp,%r13), %rdx
cmpq $0x10, %rdx
jb 0x6bd9
leaq 0x3a(,%r13), %rdi
addq %rbp, %rdi
addq $-0x3, %rdx
movl $0x3, %ecx
movq %rbx, %rsi
callq 0x3450
movq 0x50(%r15), %rbp
cmpq $-0x1, %rax
je 0x6bd9
leaq 0x3a(,%r13), %rdi
addq %rbp, %rdi
movq 0x8(%rdi), %rdx
addq $-0xc, %rdx
movl $0x1, %ecx
movq %r14, %rsi
callq 0x3330
movq 0x50(%r15), %rbp
cmpq $-0x1, %rax
je 0x6bd9
movzwl 0x5a(%rbp,%r13), %eax
testl $0x102, %eax            # imm = 0x102
je 0x6bd9
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %rbx
subq %r15, %rbx
movq %r13, 0x70(%rsp)
je 0x6b16
addq %r13, %rbp
sarq $0x2, %rbx
movabsq $-0x7063e7063e7063e7, %rax # imm = 0x8F9C18F9C18F9C19
imulq %rax, %rbx
movq 0x42(%rbp), %r14
cmpq $0x1, %rbx
adcq $0x0, %rbx
addq $0x52, %r15
movw $0x1, %r13w
cmpq (%r15), %r14
jne 0x6b08
testq %r14, %r14
je 0x6b05
movq -0x8(%r15), %rsi
movq 0x3a(%rbp), %rdi
movq %r14, %rdx
callq 0x3240
cmpl $0x1, %eax
adcw $0x0, %r13w
jmp 0x6b08
incl %r13d
addq $0xa4, %r15
decq %rbx
jne 0x6ae1
jmp 0x6b1b
movw $0x1, %r13w
movl $0xa4, %edi
callq 0x3220
movq 0x38(%rsp), %r15
movq %rax, %r14
movq %rax, %rbp
movq %r14, %rdi
callq 0xc384
movq 0x70(%rsp), %rsi
addq 0x50(%r15), %rsi
movzwl %r13w, %edx
movq %rbp, %rdi
callq 0xc3c4
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x92fc
movq 0x6a(%rbp), %rdi
leaq 0x7a(%r14), %rax
cmpq %rax, %rdi
je 0x6b72
movq (%rax), %rsi
incq %rsi
callq 0x3230
movq 0x4a(%rbp), %rdi
leaq 0x5a(%r14), %rax
cmpq %rax, %rdi
je 0x6b8a
movq (%rax), %rsi
incq %rsi
callq 0x3230
movq 0x28(%rbp), %rdi
leaq 0x38(%r14), %rax
cmpq %rax, %rdi
je 0x6ba2
movq (%rax), %rsi
incq %rsi
callq 0x3230
movq 0x8(%rbp), %rdi
addq $0x18, %r14
cmpq %r14, %rdi
je 0x6bba
movq (%r14), %rsi
incq %rsi
callq 0x3230
movl $0xa4, %esi
movq %rbp, %rdi
callq 0x3230
movq 0x50(%r15), %rbp
leaq 0x87e7(%rip), %rbx      # 0xf3b9
leaq 0x87a4(%rip), %r14      # 0xf37d
incq %r12
movq 0x58(%r15), %rax
subq %rbp, %rax
sarq %rax
movabsq $0x3a4c0a237c32b16d, %rcx # imm = 0x3A4C0A237C32B16D
imulq %rcx, %rax
cmpq %r12, %rax
ja 0x6a24
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rax
leaq 0xe0(%rsp), %r13
movl $0x0, (%r13)
xorl %edx, %edx
movq %rdx, 0x8(%r13)
movq %r13, 0x10(%r13)
movq %r13, 0x18(%r13)
movq %rdx, 0x20(%r13)
cmpq %rsi, %rax
je 0x6cec
movl $0x28, %r14d
xorl %r12d, %r12d
leaq 0xd8(%rsp), %rbx
addq %r14, %rsi
movq %rbx, %rdi
callq 0xa8ac
movq 0x10(%rsp), %rcx
leaq (%rcx,%r14), %rsi
movzwl 0x20(%rcx,%r14), %ebp
cmpq %r13, %rax
je 0x6c87
movq %rbx, %rdi
callq 0x9084
cmpw (%rax), %bp
jbe 0x6c92
movq 0x10(%rsp), %rsi
movzwl 0x20(%rsi,%r14), %ebp
addq %r14, %rsi
movq %rbx, %rdi
callq 0x9084
jmp 0x6c8f
movq %rbx, %rdi
callq 0x9084
movw %bp, (%rax)
incq %r12
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rax
subq %rsi, %rax
sarq $0x2, %rax
movabsq $-0x7063e7063e7063e7, %rbp # imm = 0x8F9C18F9C18F9C19
imulq %rbp, %rax
addq $0xa4, %r14
cmpq %r12, %rax
ja 0x6c43
movq 0x100(%rsp), %rdx
jmp 0x6cf6
leaq 0xe0(%rsp), %r13
movl $0x0, (%r13)
xorl %edx, %edx
movq %rdx, 0x8(%r13)
movq %r13, 0x10(%r13)
movq %r13, 0x18(%r13)
movq %rdx, 0x20(%r13)
movabsq $-0x7063e7063e7063e7, %rbp # imm = 0x8F9C18F9C18F9C19
movq 0x18(%r15), %rdi
leaq 0x86bc(%rip), %rsi      # 0xf3bd
xorl %eax, %eax
callq 0x3350
movq 0xf0(%rsp), %rbx
cmpq %r13, %rbx
je 0x6d86
leaq 0x50(%rsp), %r15
leaq 0x40(%rsp), %r14
leaq 0x86c3(%rip), %r12      # 0xf3e9
movq %r15, 0x40(%rsp)
movq 0x20(%rbx), %rsi
movq 0x28(%rbx), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x95ce
movzwl 0x40(%rbx), %ecx
movw %cx, 0x60(%rsp)
movq 0x38(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x40(%rsp), %rdx
movq %r12, %rsi
xorl %eax, %eax
callq 0x3350
movq 0x40(%rsp), %rdi
cmpq %r15, %rdi
je 0x6d76
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3230
movq %rbx, %rdi
callq 0x3150
movq %rax, %rbx
cmpq %r13, %rax
jne 0x6d26
leaq 0x50(%rsp), %r14
movq %r14, -0x10(%r14)
movq $0x0, -0x8(%r14)
movb $0x0, (%r14)
leaq 0x98(%rsp), %rdi
leaq 0xf(%rsp), %rdx
movq 0x68(%rsp), %rsi
callq 0x8e9c
leaq 0xd4c0(%rip), %rax      # 0x14279
movsbl (%rax), %r8d
leaq 0x98(%rsp), %rdi
movq 0x8(%rdi), %rsi
movl $0x1, %ecx
xorl %edx, %edx
callq 0x3490
leaq 0x88(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x6e02
movq %rdx, 0x78(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x88(%rsp)
jmp 0x6e09
movups (%rcx), %xmm0
movups %xmm0, (%r15)
movq 0x8(%rax), %rdx
leaq 0x78(%rsp), %rdi
movq %rdx, 0x8(%rdi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x85c5(%rip), %rsi      # 0xf3f1
callq 0x34a0
leaq 0xc8(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x6e62
movq %rdx, 0xb8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xc8(%rsp)
jmp 0x6e6a
movups (%rcx), %xmm0
movups %xmm0, (%r12)
movq 0x8(%rax), %rdx
leaq 0xb8(%rsp), %rbx
movq %rdx, 0x8(%rbx)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
callq 0x3300
movq (%rbx), %rdi
cmpq %r12, %rdi
je 0x6eae
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x3230
movq 0x78(%rsp), %rdi
cmpq %r15, %rdi
je 0x6ec8
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x3230
leaq 0xa8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6ee9
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x3230
movq 0x40(%rsp), %rdi
leaq 0x8aad(%rip), %rsi      # 0xf9a2
callq 0x32d0
testq %rax, %rax
je 0x6faa
movq %rax, %rbx
movzbl 0x34(%rsp), %edx
movq %rax, %rsi
callq 0x684e
movq 0x10(%rsp), %rdi
cmpq %rdi, 0x18(%rsp)
je 0x6f54
xorl %r15d, %r15d
xorl %r12d, %r12d
addq %r15, %rdi
movq %rbx, %rsi
callq 0xc4ae
incq %r12
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rax
subq %rdi, %rax
sarq $0x2, %rax
imulq %rbp, %rax
addq $0xa4, %r15
cmpq %r12, %rax
ja 0x6f25
leaq 0x86d2(%rip), %rdi      # 0xf62d
callq 0x33d0
movq %rbx, %rdi
callq 0x3210
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x6f7f
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3230
leaq 0xd8(%rsp), %rdi
callq 0x9664
leaq 0x10(%rsp), %rdi
callq 0x932c
xorl %eax, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x350f
jmp 0x7013
movq %rax, %rbx
movq 0x78(%rsp), %rdi
cmpq %r15, %rdi
je 0x6fd3
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x3230
jmp 0x6fd3
movq %rax, %rbx
leaq 0xa8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7016
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x3230
jmp 0x7016
jmp 0x7013
movq %rax, %rbx
movl $0xa4, %esi
movq %rbp, %rdi
callq 0x3230
jmp 0x703f
movq %rax, %rbx
jmp 0x703f
jmp 0x702f
jmp 0x702f
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x7032
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3230
jmp 0x7032
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
callq 0x9664
leaq 0x10(%rsp), %rdi
callq 0x932c
movq %rbx, %rdi
callq 0x3460
nop