// File: /workspace/github_cpp_makefile_star2_O2/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 $0xf8, %rsp
movl %edx, 0x34(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdi, %r15
leaq 0x10(%rsp), %rax
andq $0x0, 0x10(%rax)
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
xorl %r12d, %r12d
movl $0xca, %r14d
leaq 0x6ca4(%rip), %r13      # 0xc3b9
movq 0x50(%r15), %rcx
movq 0x58(%r15), %rax
subq %rcx, %rax
cqto
idivq %r14
cmpq %r12, %rax
jbe 0x5849
imulq $0xca, %r12, %rbx
movq 0x42(%rcx,%rbx), %rdx
cmpq $0x10, %rdx
jb 0x5841
leaq (%rcx,%rbx), %rdi
addq $0x3a, %rdi
addq $-0x3, %rdx
movq %r13, %rsi
callq 0x34f0
cmpq $-0x1, %rax
je 0x5841
movq 0x50(%r15), %rax
leaq (%rax,%rbx), %rdi
addq $0x3a, %rdi
movq 0x8(%rdi), %rdx
addq $-0xc, %rdx
leaq 0x6c00(%rip), %rsi      # 0xc37d
callq 0x3470
cmpq $-0x1, %rax
je 0x5841
movq 0x50(%r15), %rax
movzwl 0x5a(%rax,%rbx), %eax
testw $0x102, %ax             # imm = 0x102
je 0x5841
movw $0x1, %bp
pushq $0x4a
popq %r14
xorl %r13d, %r13d
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rax
subq %rsi, %rax
cqto
movl $0xa4, %ecx
idivq %rcx
cmpq %r13, %rax
jbe 0x57eb
movq 0x50(%r15), %rax
leaq (%rax,%rbx), %rdi
addq $0x3a, %rdi
addq %r14, %rsi
callq 0x718f
movzbl %al, %eax
addl %eax, %ebp
incq %r13
addq $0xa4, %r14
jmp 0x57aa
movl $0xa4, %edi
callq 0x3260
leaq 0x6bbd(%rip), %r13      # 0xc3b9
movq %rax, %r14
movq %rax, %rdi
callq 0xa242
addq 0x50(%r15), %rbx
movzwl %bp, %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0xa286
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x753e
movq %r14, %rdi
callq 0x756e
movl $0xa4, %esi
movq %r14, %rdi
callq 0x3270
movl $0xca, %r14d
incq %r12
jmp 0x5715
leaq 0xb0(%rsp), %r13
andl $0x0, (%r13)
andq $0x0, 0x8(%r13)
movq %r13, 0x10(%r13)
movq %r13, 0x18(%r13)
andq $0x0, 0x20(%r13)
pushq $0x28
popq %r14
xorl %r12d, %r12d
leaq 0xa8(%rsp), %rbx
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rax
subq %rsi, %rax
cqto
movl $0xa4, %ecx
idivq %rcx
cmpq %r12, %rax
jbe 0x58ee
addq %r14, %rsi
movq %rbx, %rdi
callq 0x8b14
movq 0x10(%rsp), %rcx
leaq (%rcx,%r14), %rsi
movzwl 0x20(%rcx,%r14), %ebp
cmpq %r13, %rax
je 0x58d7
movq %rbx, %rdi
callq 0x72f8
cmpw (%rax), %bp
jbe 0x58e2
movq 0x10(%rsp), %rsi
movzwl 0x20(%rsi,%r14), %ebp
addq %r14, %rsi
movq %rbx, %rdi
callq 0x72f8
jmp 0x58df
movq %rbx, %rdi
callq 0x72f8
movw %bp, (%rax)
incq %r12
addq $0xa4, %r14
jmp 0x5877
movq 0x18(%r15), %rdi
movq 0xd0(%rsp), %rdx
leaq 0x6abc(%rip), %rsi      # 0xc3bd
xorl %eax, %eax
callq 0x3380
movq 0xc0(%rsp), %r12
leaq 0x40(%rsp), %rbx
leaq 0x6acd(%rip), %r14      # 0xc3e9
cmpq %r13, %r12
je 0x595b
leaq 0x20(%r12), %rsi
movq %rbx, %rdi
callq 0x735e
movq 0x18(%r15), %rdi
movq 0x40(%rsp), %rdx
movzwl 0x60(%rsp), %ecx
movq %r14, %rsi
xorl %eax, %eax
callq 0x3380
movq %rbx, %rdi
callq 0x3100
movq %r12, %rdi
callq 0x3180
movq %rax, %r12
jmp 0x591c
leaq 0x50(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x68(%rsp), %rdi
leaq 0xf(%rsp), %rdx
movq 0x38(%rsp), %rsi
callq 0x7072
leaq 0xc912(%rip), %rax      # 0x12299
movsbl (%rax), %edx
leaq 0x88(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x7050
leaq 0x6a4e(%rip), %rdx      # 0xc3f1
leaq 0xd8(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x7031
leaq 0x40(%rsp), %rbx
leaq 0xd8(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3320
movq %r14, %rdi
callq 0x3100
leaq 0x88(%rsp), %rdi
callq 0x3100
leaq 0x68(%rsp), %rdi
callq 0x3100
movq (%rbx), %rdi
leaq 0x6fa9(%rip), %rsi      # 0xc9a2
callq 0x32f0
testq %rax, %rax
je 0x5a9a
movq %rax, %rbx
movzbl 0x34(%rsp), %edx
movq %rax, %rsi
callq 0x5592
xorl %r14d, %r14d
movl $0xa4, %r15d
xorl %r12d, %r12d
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rax
subq %rdi, %rax
cqto
idivq %r15
cmpq %r12, %rax
jbe 0x5a51
addq %r14, %rdi
movq %rbx, %rsi
callq 0xa35a
incq %r12
addq $0xa4, %r14
jmp 0x5a23
leaq 0x6bd5(%rip), %rdi      # 0xc62d
callq 0x33f0
movq %rbx, %rdi
callq 0x3250
leaq 0x40(%rsp), %rdi
callq 0x3100
leaq 0xa8(%rsp), %rdi
callq 0x7844
leaq 0x10(%rsp), %rdi
callq 0x759a
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x68de(%rip), %rdi      # 0xc37f
callq 0x31f0
pushq $0x1
popq %rdi
callq 0x3310
jmp 0x5aec
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x3100
jmp 0x5ac5
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x3100
jmp 0x5aef
jmp 0x5aec
movq %rax, %rbx
movl $0xa4, %esi
movq %r14, %rdi
callq 0x3270
jmp 0x5b0f
jmp 0x5ae7
movq %rax, %rbx
jmp 0x5b0f
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x3100
jmp 0x5b02
jmp 0x5aff
jmp 0x5aff
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x7844
leaq 0x10(%rsp), %rdi
callq 0x759a
movq %rbx, %rdi
callq 0x3480
nop