// File: 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
movq %rsp, %rbp
subq $0x260, %rsp            # imm = 0x260
movb %dl, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
andb $0x1, %al
movb %al, -0x11(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x178(%rbp)
leaq -0x30(%rbp), %rdi
callq 0xb8a0
movl $0x0, -0x34(%rbp)
movq -0x178(%rbp), %rdi
movslq -0x34(%rbp), %rax
movq %rax, -0x180(%rbp)
addq $0x50, %rdi
callq 0xaf20
movq %rax, %rcx
movq -0x180(%rbp), %rax
cmpq %rcx, %rax
jae 0x7561
movq -0x178(%rbp), %rdi
addq $0x50, %rdi
movslq -0x34(%rbp), %rsi
callq 0xaf90
movq %rax, %rdi
addq $0x3a, %rdi
callq 0x3220
cmpq $0x10, %rax
jb 0x7551
movq -0x178(%rbp), %rdi
addq $0x50, %rdi
movslq -0x34(%rbp), %rsi
callq 0xaf90
movq -0x178(%rbp), %rdi
addq $0x3a, %rax
movq %rax, -0x188(%rbp)
addq $0x50, %rdi
movslq -0x34(%rbp), %rsi
callq 0xaf90
movq %rax, %rdi
addq $0x3a, %rdi
callq 0x3220
movq -0x188(%rbp), %rdi
movq %rax, %rdx
subq $0x3, %rdx
leaq 0x1509a(%rip), %rsi     # 0x1c3bb
callq 0x3630
cmpq $-0x1, %rax
je 0x7551
movq -0x178(%rbp), %rdi
addq $0x50, %rdi
movq %rdi, -0x1a0(%rbp)
movslq -0x34(%rbp), %rsi
callq 0xaf90
movq -0x1a0(%rbp), %rdi
addq $0x3a, %rax
movq %rax, -0x198(%rbp)
movslq -0x34(%rbp), %rsi
callq 0xaf90
movq %rax, %rdi
addq $0x3a, %rdi
callq 0x3220
movq -0x198(%rbp), %rdi
movq %rax, %rdx
addq $-0xc, %rdx
leaq 0x14ff8(%rip), %rsi     # 0x1c37f
callq 0x35a0
movq %rax, -0x190(%rbp)
jmp 0x7395
movq -0x190(%rbp), %rax
cmpq $-0x1, %rax
je 0x7551
movq -0x178(%rbp), %rdi
addq $0x50, %rdi
movslq -0x34(%rbp), %rsi
callq 0xaf90
movl 0x5a(%rax), %eax
andl $0x100, %eax            # imm = 0x100
cmpl $0x0, %eax
jne 0x73ea
movq -0x178(%rbp), %rdi
addq $0x50, %rdi
movslq -0x34(%rbp), %rsi
callq 0xaf90
movl 0x5a(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x754f
movw $0x1, -0x46(%rbp)
movl $0x0, -0x4c(%rbp)
movslq -0x4c(%rbp), %rax
movq %rax, -0x1a8(%rbp)
leaq -0x30(%rbp), %rdi
callq 0xb8c0
movq %rax, %rcx
movq -0x1a8(%rbp), %rax
cmpq %rcx, %rax
jae 0x748e
movq -0x178(%rbp), %rdi
addq $0x50, %rdi
movslq -0x34(%rbp), %rsi
callq 0xaf90
addq $0x3a, %rax
movq %rax, -0x1b0(%rbp)
movslq -0x4c(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0xb8f0
movq -0x1b0(%rbp), %rdi
movq %rax, %rsi
addq $0x4a, %rsi
callq 0xa9f0
testb $0x1, %al
jne 0x745f
jmp 0x747e
movw -0x46(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x46(%rbp)
jmp 0x747e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x7abd
jmp 0x7480
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x73f7
movl $0xa4, %edi
callq 0x3340
movq %rax, -0x1b8(%rbp)
jmp 0x74a1
movq -0x1b8(%rbp), %rdi
callq 0x17010
jmp 0x74af
movq -0x178(%rbp), %rdi
movq -0x1b8(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x1c0(%rbp)
addq $0x50, %rdi
movslq -0x34(%rbp), %rsi
callq 0xaf90
movq -0x1c0(%rbp), %rdi
movq %rax, %rsi
movzwl -0x46(%rbp), %edx
callq 0x17060
jmp 0x74ee
movq -0x58(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0xb910
jmp 0x74fd
movq -0x58(%rbp), %rax
movq %rax, -0x1c8(%rbp)
cmpq $0x0, %rax
je 0x752b
movq -0x1c8(%rbp), %rdi
callq 0xb980
movq -0x1c8(%rbp), %rdi
movl $0xa4, %esi
callq 0x3350
jmp 0x754f
movq -0x1b8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
movl $0xa4, %esi
callq 0x3350
jmp 0x7abd
jmp 0x7551
jmp 0x7553
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x7275
leaq -0x88(%rbp), %rdi
callq 0xad90
movl $0x0, -0x8c(%rbp)
movslq -0x8c(%rbp), %rax
movq %rax, -0x1d0(%rbp)
leaq -0x30(%rbp), %rdi
callq 0xb8c0
movq %rax, %rcx
movq -0x1d0(%rbp), %rax
cmpq %rcx, %rax
jae 0x775d
movslq -0x8c(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0xb8f0
movq %rax, %rsi
addq $0x28, %rsi
leaq -0x88(%rbp), %rdi
callq 0xaec0
movq %rax, -0x1d8(%rbp)
jmp 0x75cd
movq -0x1d8(%rbp), %rax
movq %rax, -0x98(%rbp)
leaq -0x88(%rbp), %rdi
callq 0xaef0
movq %rax, -0xa0(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0xae90
testb $0x1, %al
jne 0x7607
jmp 0x767f
movslq -0x8c(%rbp), %rsi
leaq -0x30(%rbp), %rdi
movq %rdi, -0x1f0(%rbp)
callq 0xb8f0
movq -0x1f0(%rbp), %rdi
movw 0x48(%rax), %ax
movw %ax, -0x1e2(%rbp)
movslq -0x8c(%rbp), %rsi
callq 0xb8f0
movq %rax, %rsi
addq $0x28, %rsi
leaq -0x88(%rbp), %rdi
callq 0xadb0
movq %rax, -0x1e0(%rbp)
jmp 0x7658
movq -0x1e0(%rbp), %rax
movw -0x1e2(%rbp), %cx
movw %cx, (%rax)
jmp 0x7747
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x7ab1
movslq -0x8c(%rbp), %rsi
leaq -0x30(%rbp), %rdi
movq %rdi, -0x208(%rbp)
callq 0xb8f0
movq -0x208(%rbp), %rdi
movzwl 0x48(%rax), %eax
movl %eax, -0x1fc(%rbp)
movslq -0x8c(%rbp), %rsi
callq 0xb8f0
movq %rax, %rsi
addq $0x28, %rsi
leaq -0x88(%rbp), %rdi
callq 0xadb0
movq %rax, -0x1f8(%rbp)
jmp 0x76cf
movl -0x1fc(%rbp), %eax
movq -0x1f8(%rbp), %rcx
movzwl (%rcx), %ecx
cmpl %ecx, %eax
jle 0x7745
movslq -0x8c(%rbp), %rsi
leaq -0x30(%rbp), %rdi
movq %rdi, -0x220(%rbp)
callq 0xb8f0
movq -0x220(%rbp), %rdi
movw 0x48(%rax), %ax
movw %ax, -0x212(%rbp)
movslq -0x8c(%rbp), %rsi
callq 0xb8f0
movq %rax, %rsi
addq $0x28, %rsi
leaq -0x88(%rbp), %rdi
callq 0xadb0
movq %rax, -0x210(%rbp)
jmp 0x7734
movq -0x210(%rbp), %rax
movw -0x212(%rbp), %cx
movw %cx, (%rax)
jmp 0x7747
jmp 0x7749
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x7577
movq -0x178(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x228(%rbp)
leaq -0x88(%rbp), %rdi
callq 0xb9d0
movq -0x228(%rbp), %rdi
movq %rax, %rdx
leaq 0x14c33(%rip), %rsi     # 0x1c3bf
movb $0x0, %al
callq 0x34d0
leaq -0x88(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rdi
callq 0xafb0
movq %rax, -0xb0(%rbp)
movq -0xa8(%rbp), %rdi
callq 0xaef0
movq %rax, -0xb8(%rbp)
leaq -0xb0(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0xafe0
testb $0x1, %al
jne 0x77e0
jmp 0x7857
leaq -0xb0(%rbp), %rdi
callq 0xb010
movq %rax, %rsi
leaq -0xe0(%rbp), %rdi
callq 0xb030
jmp 0x77fd
movq -0x178(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x230(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x3120
movq -0x230(%rbp), %rdi
movq %rax, %rdx
movzwl -0xc0(%rbp), %ecx
leaq 0x14bb8(%rip), %rsi     # 0x1c3eb
movb $0x0, %al
callq 0x34d0
leaq -0xe0(%rbp), %rdi
callq 0xb070
leaq -0xb0(%rbp), %rdi
callq 0xb090
jmp 0x77c7
leaq -0x108(%rbp), %rdi
callq 0x34b0
movq -0x10(%rbp), %rax
movq %rax, -0x240(%rbp)
leaq -0x169(%rbp), %rdi
movq %rdi, -0x238(%rbp)
callq 0x35d0
movq -0x240(%rbp), %rsi
movq -0x238(%rbp), %rdx
leaq -0x168(%rbp), %rdi
callq 0xa400
jmp 0x789d
leaq 0x1fa9d(%rip), %rax     # 0x27341
movsbl (%rax), %edx
leaq -0x148(%rbp), %rdi
leaq -0x168(%rbp), %rsi
callq 0xa3b0
jmp 0x78bc
leaq 0x14b30(%rip), %rdx     # 0x1c3f3
leaq -0x128(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0xa360
jmp 0x78d8
leaq -0x108(%rbp), %rdi
movq %rdi, -0x250(%rbp)
leaq -0x128(%rbp), %rsi
movq %rsi, -0x258(%rbp)
callq 0x3450
movq -0x258(%rbp), %rdi
callq 0x31d0
leaq -0x148(%rbp), %rdi
callq 0x31d0
leaq -0x168(%rbp), %rdi
callq 0x31d0
leaq -0x169(%rbp), %rdi
callq 0x33c0
movq -0x250(%rbp), %rdi
callq 0x3120
movq %rax, %rdi
leaq 0x151ef(%rip), %rsi     # 0x1cb2e
callq 0x3420
movq %rax, -0x248(%rbp)
jmp 0x794d
movq -0x248(%rbp), %rax
movq %rax, -0xe8(%rbp)
cmpq $0x0, %rax
jne 0x79db
leaq 0x14a19(%rip), %rdi     # 0x1c381
callq 0x32a0
jmp 0x796f
movl $0x1, %edi
callq 0x3440
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x79b9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x79ad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x31d0
leaq -0x168(%rbp), %rdi
callq 0x31d0
leaq -0x169(%rbp), %rdi
callq 0x33c0
jmp 0x7aa5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x7aa5
movq -0x178(%rbp), %rdi
movq -0xe8(%rbp), %rsi
movzbl -0x11(%rbp), %edx
andl $0x1, %edx
callq 0x7100
jmp 0x79f7
movl $0x0, -0x170(%rbp)
movslq -0x170(%rbp), %rax
movq %rax, -0x260(%rbp)
leaq -0x30(%rbp), %rdi
callq 0xb8c0
movq %rax, %rcx
movq -0x260(%rbp), %rax
cmpq %rcx, %rax
jae 0x7a5b
movslq -0x170(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0xb8f0
movq %rax, %rdi
movq -0xe8(%rbp), %rsi
callq 0x171b0
jmp 0x7a48
jmp 0x7a4a
movl -0x170(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x170(%rbp)
jmp 0x7a01
leaq 0x149ac(%rip), %rdi     # 0x1c40e
xorl %eax, %eax
callq 0x3060
jmp 0x7a6b
movq -0xe8(%rbp), %rdi
callq 0x3330
jmp 0x7a79
leaq -0x108(%rbp), %rdi
callq 0x31d0
leaq -0x88(%rbp), %rdi
callq 0xb0c0
leaq -0x30(%rbp), %rdi
callq 0xb9f0
xorl %eax, %eax
addq $0x260, %rsp            # imm = 0x260
popq %rbp
retq
leaq -0x108(%rbp), %rdi
callq 0x31d0
leaq -0x88(%rbp), %rdi
callq 0xb0c0
leaq -0x30(%rbp), %rdi
callq 0xb9f0
movq -0x40(%rbp), %rdi
callq 0x35c0
nop