// File: /workspace/github_cpp_makefile_star2_O1/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, 0x3c(%rsp)
movq %rsi, 0x68(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x50(%rdi), %rax
cmpq %rax, 0x58(%rdi)
movq %rdi, 0x30(%rsp)
je 0x6c1c
movq %rdi, %r14
xorl %r12d, %r12d
leaq 0x8992(%rip), %r15      # 0xf3b9
leaq 0x894f(%rip), %r13      # 0xf37d
movabsq $0x3a4c0a237c32b16d, %rbp # imm = 0x3A4C0A237C32B16D
imulq $0xca, %r12, %rbx
movq 0x42(%rax,%rbx), %rdx
cmpq $0x10, %rdx
jb 0x6bfe
leaq (%rax,%rbx), %rdi
addq $0x3a, %rdi
addq $-0x3, %rdx
movl $0x3, %ecx
movq %r15, %rsi
callq 0x3460
cmpq $-0x1, %rax
je 0x6bfe
movq 0x50(%r14), %rax
leaq (%rax,%rbx), %rdi
addq $0x3a, %rdi
movq 0x8(%rdi), %rdx
addq $-0xc, %rdx
movl $0x1, %ecx
movq %r13, %rsi
callq 0x3330
cmpq $-0x1, %rax
je 0x6bfe
movq 0x50(%r14), %rax
movq %rax, %rcx
movzwl 0x5a(%rax,%rbx), %eax
testl $0x102, %eax            # imm = 0x102
je 0x6bfe
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %rbp
subq %r15, %rbp
je 0x6b32
addq %rbx, %rcx
sarq $0x2, %rbp
movabsq $-0x7063e7063e7063e7, %rax # imm = 0x8F9C18F9C18F9C19
imulq %rax, %rbp
movq %rcx, 0x70(%rsp)
movq 0x42(%rcx), %r14
cmpq $0x1, %rbp
adcq $0x0, %rbp
addq $0x52, %r15
movw $0x1, %r13w
testq %r14, %r14
sete %cl
cmpq (%r15), %r14
sete %al
setne %dl
orb %cl, %dl
jne 0x6b1e
movq -0x8(%r15), %rsi
movq 0x70(%rsp), %rax
movq 0x3a(%rax), %rdi
movq %r14, %rdx
callq 0x3240
testl %eax, %eax
sete %al
movzbl %al, %eax
addl %eax, %r13d
addq $0xa4, %r15
decq %rbp
jne 0x6af1
jmp 0x6b37
movw $0x1, %r13w
movl $0xa4, %edi
callq 0x3220
movq %rax, %r14
movq %rax, %rbp
movq %r14, %rdi
callq 0xc380
movq 0x30(%rsp), %rax
addq 0x50(%rax), %rbx
movzwl %r13w, %edx
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xc3c0
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x92a2
movq 0x6a(%rbp), %rdi
leaq 0x7a(%r14), %rax
cmpq %rax, %rdi
je 0x6b8c
movq (%rax), %rsi
incq %rsi
callq 0x3230
movq 0x4a(%rbp), %rdi
leaq 0x5a(%r14), %rax
cmpq %rax, %rdi
je 0x6ba4
movq (%rax), %rsi
incq %rsi
callq 0x3230
movq 0x28(%rbp), %rdi
leaq 0x38(%r14), %rax
cmpq %rax, %rdi
je 0x6bbc
movq (%rax), %rsi
incq %rsi
callq 0x3230
movq 0x8(%rbp), %rdi
addq $0x18, %r14
cmpq %r14, %rdi
je 0x6bd4
movq (%r14), %rsi
incq %rsi
callq 0x3230
movl $0xa4, %esi
movq %rbp, %rdi
callq 0x3230
movq 0x30(%rsp), %r14
leaq 0x87cc(%rip), %r15      # 0xf3b9
leaq 0x8789(%rip), %r13      # 0xf37d
movabsq $0x3a4c0a237c32b16d, %rbp # imm = 0x3A4C0A237C32B16D
incq %r12
movq 0x50(%r14), %rax
movq 0x58(%r14), %rcx
subq %rax, %rcx
sarq %rcx
imulq %rbp, %rcx
cmpq %r12, %rcx
ja 0x6a38
leaq 0xe0(%rsp), %r13
movl $0x0, (%r13)
xorl %eax, %eax
movq %rax, 0x8(%r13)
movq %r13, 0x10(%r13)
movq %r13, 0x18(%r13)
movq %rax, 0x20(%r13)
movq 0x10(%rsp), %rsi
cmpq %rsi, 0x18(%rsp)
je 0x6cdc
movl $0x28, %r14d
xorl %r12d, %r12d
leaq 0xd8(%rsp), %rbx
addq %r14, %rsi
movq %rbx, %rdi
callq 0xa8b6
movq 0x10(%rsp), %rcx
leaq (%rcx,%r14), %rsi
movzwl 0x20(%rcx,%r14), %ebp
cmpq %r13, %rax
je 0x6ca3
movq %rbx, %rdi
callq 0x902a
cmpw (%rax), %bp
jbe 0x6cae
movq 0x10(%rsp), %rsi
movzwl 0x20(%rsi,%r14), %ebp
addq %r14, %rsi
movq %rbx, %rdi
callq 0x902a
jmp 0x6cab
movq %rbx, %rdi
callq 0x902a
movw %bp, (%rax)
incq %r12
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rax
subq %rsi, %rax
sarq $0x2, %rax
movabsq $-0x7063e7063e7063e7, %rcx # imm = 0x8F9C18F9C18F9C19
imulq %rcx, %rax
addq $0xa4, %r14
cmpq %r12, %rax
ja 0x6c5f
movq 0x30(%rsp), %rbp
movq 0x18(%rbp), %rdi
movq 0x100(%rsp), %rdx
leaq 0x86c9(%rip), %rsi      # 0xf3bd
xorl %eax, %eax
callq 0x3360
movq 0xf0(%rsp), %rbx
cmpq %r13, %rbx
je 0x6d74
leaq 0x50(%rsp), %r15
leaq 0x40(%rsp), %r14
leaq 0x86d0(%rip), %r12      # 0xf3e9
movq %r15, 0x40(%rsp)
movq 0x20(%rbx), %rsi
movq 0x28(%rbx), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x9574
movzwl 0x40(%rbx), %ecx
movw %cx, 0x60(%rsp)
movq 0x18(%rbp), %rdi
movq 0x40(%rsp), %rdx
movq %r12, %rsi
xorl %eax, %eax
callq 0x3360
movq 0x40(%rsp), %rdi
cmpq %r15, %rdi
je 0x6d64
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3230
movq %rbx, %rdi
callq 0x3150
movq %rax, %rbx
cmpq %r13, %rax
jne 0x6d19
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 0x8e42
leaq 0xd4da(%rip), %rax      # 0x14281
movsbl (%rax), %r8d
leaq 0x98(%rsp), %rdi
movq 0x8(%rdi), %rsi
movl $0x1, %ecx
xorl %edx, %edx
callq 0x34a0
leaq 0x88(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x6df0
movq %rdx, 0x78(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x88(%rsp)
jmp 0x6df7
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 0x85d7(%rip), %rsi      # 0xf3f1
callq 0x34b0
leaq 0xc8(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x6e50
movq %rdx, 0xb8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xc8(%rsp)
jmp 0x6e58
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 0x6e9c
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x3230
movq 0x78(%rsp), %rdi
cmpq %r15, %rdi
je 0x6eb6
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x3230
leaq 0xa8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6ed7
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x3230
movq 0x40(%rsp), %rdi
leaq 0x8ac0(%rip), %rsi      # 0xf9a3
callq 0x32d0
testq %rax, %rax
je 0x6fa2
movq %rax, %rbx
movzbl 0x3c(%rsp), %edx
movq %rax, %rsi
callq 0x6858
movq 0x10(%rsp), %rdi
cmpq %rdi, 0x18(%rsp)
je 0x6f4c
xorl %r15d, %r15d
xorl %r12d, %r12d
addq %r15, %rdi
movq %rbx, %rsi
callq 0xc4aa
incq %r12
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rax
subq %rdi, %rax
sarq $0x2, %rax
movabsq $-0x7063e7063e7063e7, %rcx # imm = 0x8F9C18F9C18F9C19
imulq %rcx, %rax
addq $0xa4, %r15
cmpq %r12, %rax
ja 0x6f13
leaq 0x86da(%rip), %rdi      # 0xf62d
callq 0x33e0
movq %rbx, %rdi
callq 0x3210
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x6f77
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3230
leaq 0xd8(%rsp), %rdi
callq 0x9604
leaq 0x10(%rsp), %rdi
callq 0x92d2
xorl %eax, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x351f
jmp 0x7007
movq %rax, %rbx
movq 0x78(%rsp), %rdi
cmpq %r15, %rdi
je 0x6fcb
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x3230
jmp 0x6fcb
movq %rax, %rbx
leaq 0xa8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x700a
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x3230
jmp 0x700a
jmp 0x7007
movq %rax, %rbx
movl $0xa4, %esi
movq %rbp, %rdi
callq 0x3230
jmp 0x7037
movq %rax, %rbx
jmp 0x7037
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x702a
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3230
jmp 0x702a
jmp 0x7027
jmp 0x7027
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
callq 0x9604
leaq 0x10(%rsp), %rdi
callq 0x92d2
movq %rbx, %rdi
callq 0x3470
nop