// File: /workspace/github_cpp_makefile_star2_O3/4n6ist[P]usn_analytics/lib/usnjrnl.cpp
// Function: UsnJrnl::WriteOpenedRecords(char*, bool)
int UsnJrnl::WriteOpenedRecords(char *odname, bool lt) {
  
  vector<UsnOpened> usnopened_set;

  // create usnexecuted_set
  for(int i=0; i < usnmain_set.size(); i++) { 
    if (usnmain_set[i].file_name.size() >= 4 && usnmain_set[i].file_name.find(".lnk", usnmain_set[i].file_name.size()-4) != string::npos) {
      if(usnmain_set[i].reasons_i != (SECURITY|CLOSE) && !(usnmain_set[i].reasons_i & DELETE)) {
        UsnOpened* uo = new UsnOpened();
        uo->StoreRecord(&usnmain_set[i]);
        usnopened_set.push_back(*uo);
        delete uo;
      }
    } else if (usnmain_set[i].reasons_i & (OBJECTID) && !(usnmain_set[i].reasons_i & DELETE)) {
      UsnOpened* uo = new UsnOpened();
      uo->StoreRecord(&usnmain_set[i]);
      usnopened_set.push_back(*uo);
      delete uo;
    }
  }

  map<string, uint16_t> open_name_table; // open_name, open_cnt

  // create open_name table (open_name, open_cnt)
  for(int i=0; i < usnopened_set.size(); i++)
    if(open_name_table.find(usnopened_set[i].file_name) == open_name_table.end())
      open_name_table[usnopened_set[i].file_name] = 1;
    else
      open_name_table[usnopened_set[i].file_name]++;

  // write to report  
  int i=0;
  fprintf(fp_ofreport, "\n[File Open] %lu files (name, count)\n", open_name_table.size());
  for(auto x: open_name_table) {
    fprintf(fp_ofreport, "%s, %d\n", x.first.c_str(), x.second);
    i++;
    if(i > 1024) {
      fprintf(fp_ofreport, "reached 1024 files...skip the rest\n");
      break;
    }
  }
  
  FILE *fp_ofopened;
  string ofopened;
  ofopened = string(odname) + SEP + "usn_analytics_opened.csv";

  if((fp_ofopened = fopen(ofopened.c_str(), "w")) == NULL) {
    perror("Output Records File Error");
    exit(EXIT_FAILURE);
  } 
  
  WriteOpenedHeader(fp_ofopened, lt);
  // write to file
  for(int i=0; i < usnopened_set.size(); i++)
    usnopened_set[i].WriteOpenedRecord(fp_ofopened);

  printf("...Done\n");
  fclose(fp_ofopened);
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movl %edx, 0x34(%rsp)
movq %rsi, 0x60(%rsp)
movq %rdi, %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x50(%rdi), %rcx
cmpq %rcx, 0x58(%rdi)
je 0x742e
xorl %r14d, %r14d
movl $0x80200, %r12d         # imm = 0x80200
movabsq $0x3a4c0a237c32b16d, %r13 # imm = 0x3A4C0A237C32B16D
xorl %ebx, %ebx
movq 0x42(%rcx,%r14), %rdx
cmpq $0x4, %rdx
jb 0x72a7
leaq (%rcx,%r14), %rdi
addq $0x3a, %rdi
addq $-0x4, %rdx
movl $0x4, %ecx
leaq 0x81d5(%rip), %rsi      # 0xf41f
callq 0x3450
movq 0x50(%r15), %rcx
cmpq $-0x1, %rax
je 0x72a7
movl 0x5a(%rcx,%r14), %eax
cmpl $0x80000800, %eax       # imm = 0x80000800
sete %cl
shrl $0x9, %eax
andl $0x1, %eax
orb %cl, %al
jne 0x735c
movl $0xa6, %edi
callq 0x3220
movq %rax, %rbp
movq %rax, %rdi
callq 0xc592
movq 0x50(%r15), %rsi
addq %r14, %rsi
movq %rbp, %rdi
callq 0xc5d2
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x935c
jmp 0x72eb
movl 0x5a(%rcx,%r14), %eax
andl %r12d, %eax
cmpl $0x80000, %eax          # imm = 0x80000
jne 0x735c
movl $0xa6, %edi
callq 0x3220
movq %rax, %rbp
movq %rax, %rdi
callq 0xc592
movq 0x50(%r15), %rsi
addq %r14, %rsi
movq %rbp, %rdi
callq 0xc5d2
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x935c
movq 0x68(%rbp), %rdi
leaq 0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x7303
movq (%rax), %rsi
incq %rsi
callq 0x3230
movq 0x48(%rbp), %rdi
leaq 0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x731b
movq (%rax), %rsi
incq %rsi
callq 0x3230
movq 0x28(%rbp), %rdi
leaq 0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x7333
movq (%rax), %rsi
incq %rsi
callq 0x3230
movq 0x8(%rbp), %rdi
movq %rbp, %rax
addq $0x18, %rax
cmpq %rax, %rdi
je 0x734f
movq 0x18(%rbp), %rsi
incq %rsi
callq 0x3230
movl $0xa6, %esi
movq %rbp, %rdi
callq 0x3230
incq %rbx
movq 0x50(%r15), %rcx
movq 0x58(%r15), %rax
subq %rcx, %rax
sarq %rax
imulq %r13, %rax
addq $0xca, %r14
cmpq %rbx, %rax
ja 0x7227
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rax
leaq 0xd0(%rsp), %rbx
movl $0x0, (%rbx)
xorl %edx, %edx
movq %rdx, 0x8(%rbx)
movq %rbx, 0x10(%rbx)
movq %rbx, 0x18(%rbx)
movq %rdx, 0x20(%rbx)
cmpq %rsi, %rax
je 0x744e
movl $0x48, %r14d
xorl %r13d, %r13d
leaq 0xc8(%rsp), %r12
addq %r14, %rsi
movq %r12, %rdi
callq 0xa8ac
movq 0x10(%rsp), %rsi
addq %r14, %rsi
cmpq %rbx, %rax
je 0x73ea
movq %r12, %rdi
callq 0x9084
incw (%rax)
jmp 0x73f7
movq %r12, %rdi
callq 0x9084
movw $0x1, (%rax)
incq %r13
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rax
subq %rsi, %rax
sarq %rax
movabsq $-0x5c87b5f9d4d1bc25, %rcx # imm = 0xA3784A062B2E43DB
imulq %rcx, %rax
addq $0xa6, %r14
cmpq %r13, %rax
ja 0x73c5
movq 0xf0(%rsp), %rdx
jmp 0x744e
leaq 0xd0(%rsp), %rbx
movl $0x0, (%rbx)
xorl %edx, %edx
movq %rdx, 0x8(%rbx)
movq %rbx, 0x10(%rbx)
movq %rbx, 0x18(%rbx)
movq %rdx, 0x20(%rbx)
movq 0x18(%r15), %rdi
leaq 0x7fcb(%rip), %rsi      # 0xf424
xorl %eax, %eax
callq 0x3350
movq 0xe0(%rsp), %r12
cmpq %rbx, %r12
je 0x751c
movl $0x401, %r13d           # imm = 0x401
leaq 0x48(%rsp), %r14
leaq 0x7f66(%rip), %rbp      # 0xf3e9
movq %r14, 0x38(%rsp)
movq 0x20(%r12), %rsi
movq 0x28(%r12), %rdx
addq %rsi, %rdx
leaq 0x38(%rsp), %rdi
callq 0x95ce
movzwl 0x40(%r12), %ecx
movw %cx, 0x58(%rsp)
movq 0x18(%r15), %rdi
movq 0x38(%rsp), %rdx
movq %rbp, %rsi
xorl %eax, %eax
callq 0x3350
decl %r13d
je 0x74eb
movq 0x38(%rsp), %rdi
cmpq %r14, %rdi
je 0x74d9
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3230
movq %r12, %rdi
callq 0x3150
movq %rax, %r12
cmpq %rbx, %rax
jne 0x7483
jmp 0x751c
movq 0x18(%r15), %rcx
leaq 0x7f54(%rip), %rdi      # 0xf44a
movl $0x23, %esi
movl $0x1, %edx
callq 0x34b0
movq 0x38(%rsp), %rdi
cmpq %r14, %rdi
je 0x751c
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3230
leaq 0x48(%rsp), %r15
movq %r15, -0x10(%r15)
movq $0x0, -0x8(%r15)
movb $0x0, (%r15)
leaq 0x88(%rsp), %rdi
leaq 0xf(%rsp), %rdx
movq 0x60(%rsp), %rsi
callq 0x8e9c
leaq 0xcd2a(%rip), %rax      # 0x14279
movsbl (%rax), %r8d
leaq 0x88(%rsp), %rdi
movq 0x8(%rdi), %rsi
movl $0x1, %ecx
xorl %edx, %edx
callq 0x3490
leaq 0x78(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x7593
movq %rdx, 0x68(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x78(%rsp)
jmp 0x759b
movups (%rcx), %xmm0
movups %xmm0, (%r12)
movabsq $-0x5c87b5f9d4d1bc25, %r13 # imm = 0xA3784A062B2E43DB
movq 0x8(%rax), %rdx
leaq 0x68(%rsp), %rdi
movq %rdx, 0x8(%rdi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x7ea6(%rip), %rsi      # 0xf46e
callq 0x34a0
leaq 0xb8(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x75fd
movq %rdx, 0xa8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xb8(%rsp)
jmp 0x7603
movups (%rcx), %xmm0
movups %xmm0, (%rbx)
movq 0x8(%rax), %rdx
leaq 0xa8(%rsp), %r14
movq %rdx, 0x8(%r14)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x38(%rsp), %rdi
movq %r14, %rsi
callq 0x3300
movq (%r14), %rdi
cmpq %rbx, %rdi
je 0x7647
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x3230
movq 0x68(%rsp), %rdi
cmpq %r12, %rdi
je 0x765e
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3230
leaq 0x98(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x767f
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x3230
movq 0x38(%rsp), %rdi
leaq 0x8317(%rip), %rsi      # 0xf9a2
callq 0x32d0
testq %rax, %rax
je 0x773e
movq %rax, %r14
movzbl 0x34(%rsp), %edx
movq %rax, %rsi
callq 0x7052
movq 0x10(%rsp), %rdi
cmpq %rdi, 0x18(%rsp)
je 0x76e8
xorl %ebx, %ebx
xorl %r12d, %r12d
addq %rbx, %rdi
movq %r14, %rsi
callq 0xc64c
incq %r12
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rax
subq %rdi, %rax
sarq %rax
imulq %r13, %rax
addq $0xa6, %rbx
cmpq %r12, %rax
ja 0x76ba
leaq 0x7f3e(%rip), %rdi      # 0xf62d
callq 0x33d0
movq %r14, %rdi
callq 0x3210
movq 0x38(%rsp), %rdi
cmpq %r15, %rdi
je 0x7713
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3230
leaq 0xc8(%rsp), %rdi
callq 0x9664
leaq 0x10(%rsp), %rdi
callq 0x938c
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3524
jmp 0x77a8
movq %rax, %rbx
movq 0x68(%rsp), %rdi
cmpq %r12, %rdi
je 0x7764
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3230
jmp 0x7764
movq %rax, %rbx
leaq 0x98(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x77ab
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x3230
jmp 0x77ab
jmp 0x77a8
jmp 0x778b
movq %rax, %rbx
movl $0xa6, %esi
movq %rbp, %rdi
callq 0x3230
jmp 0x77d4
jmp 0x77d1
jmp 0x77a3
jmp 0x77a3
movq %rax, %rbx
jmp 0x77c2
movq %rax, %rbx
movq 0x38(%rsp), %rdi
cmpq %r15, %rdi
je 0x77c2
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3230
leaq 0xc8(%rsp), %rdi
callq 0x9664
jmp 0x77d4
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x938c
movq %rbx, %rdi
callq 0x3460