// File: /workspace/github_cpp_makefile_star2_O2/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 $0xe8, %rsp
movl %edx, 0xc(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdi, %r15
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
xorl %ebx, %ebx
movl $0xca, %r14d
movl $0x80200, %r13d         # imm = 0x80200
xorl %r12d, %r12d
movq 0x50(%r15), %rcx
movq 0x58(%r15), %rax
subq %rcx, %rax
cqto
idivq %r14
cmpq %r12, %rax
jbe 0x5d9c
movq 0x42(%rcx,%rbx), %rdx
cmpq $0x4, %rdx
jb 0x5d39
leaq (%rcx,%rbx), %rdi
addq $0x3a, %rdi
addq $-0x4, %rdx
leaq 0x6742(%rip), %rsi      # 0xc41f
callq 0x34f0
movq 0x50(%r15), %rcx
cmpq $-0x1, %rax
je 0x5d39
movl 0x5a(%rcx,%rbx), %eax
cmpl $0x80000800, %eax       # imm = 0x80000800
sete %cl
shrl $0x9, %eax
andl $0x1, %eax
orb %cl, %al
jne 0x5d8d
movl $0xa6, %edi
callq 0x3260
movq %rax, %rbp
movq %rax, %rdi
callq 0xa43c
movq 0x50(%r15), %rsi
addq %rbx, %rsi
movq %rbp, %rdi
callq 0xa480
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x75bc
jmp 0x5d78
movl 0x5a(%rcx,%rbx), %eax
andl %r13d, %eax
cmpl $0x80000, %eax          # imm = 0x80000
jne 0x5d8d
movl $0xa6, %edi
callq 0x3260
movq %rax, %rbp
movq %rax, %rdi
callq 0xa43c
movq 0x50(%r15), %rsi
addq %rbx, %rsi
movq %rbp, %rdi
callq 0xa480
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x75bc
movq %rbp, %rdi
callq 0x75ec
movl $0xa6, %esi
movq %rbp, %rdi
callq 0x3270
incq %r12
addq $0xca, %rbx
jmp 0x5ca6
leaq 0xa0(%rsp), %rbx
andl $0x0, (%rbx)
andq $0x0, 0x8(%rbx)
movq %rbx, 0x10(%rbx)
movq %rbx, 0x18(%rbx)
andq $0x0, 0x20(%rbx)
pushq $0x48
popq %r14
xorl %r13d, %r13d
movl $0xa6, %ebp
leaq 0x98(%rsp), %r12
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rax
subq %rsi, %rax
cqto
idivq %rbp
cmpq %r13, %rax
jbe 0x5e22
addq %r14, %rsi
movq %r12, %rdi
callq 0x8b14
movq 0x10(%rsp), %rsi
addq %r14, %rsi
cmpq %rbx, %rax
je 0x5e09
movq %r12, %rdi
callq 0x72f8
incw (%rax)
jmp 0x5e16
movq %r12, %rdi
callq 0x72f8
movw $0x1, (%rax)
incq %r13
addq $0xa6, %r14
jmp 0x5dcd
movq 0x18(%r15), %rdi
movq 0xc0(%rsp), %rdx
leaq 0x65ef(%rip), %rsi      # 0xc424
xorl %eax, %eax
callq 0x3380
movl $0x400, %r14d           # imm = 0x400
movq 0xb0(%rsp), %rbp
leaq 0x30(%rsp), %r12
leaq 0x6593(%rip), %r13      # 0xc3e9
cmpq %rbx, %rbp
je 0x5eba
leaq 0x20(%rbp), %rsi
movq %r12, %rdi
callq 0x735e
movq 0x18(%r15), %rdi
movq 0x30(%rsp), %rdx
movzwl 0x50(%rsp), %ecx
movq %r13, %rsi
xorl %eax, %eax
callq 0x3380
subl $0x1, %r14d
jb 0x5e9a
movq %r12, %rdi
callq 0x3100
movq %rbp, %rdi
callq 0x3180
movq %rax, %rbp
jmp 0x5e56
movq 0x18(%r15), %rcx
leaq 0x65a5(%rip), %rdi      # 0xc44a
pushq $0x23
popq %rsi
pushq $0x1
popq %rdx
callq 0x34d0
leaq 0x30(%rsp), %rdi
callq 0x3100
leaq 0x40(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x58(%rsp), %rdi
leaq 0xb(%rsp), %rdx
movq 0x28(%rsp), %rsi
callq 0x7072
leaq 0xc3b3(%rip), %rax      # 0x12299
movsbl (%rax), %edx
leaq 0x78(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x7050
leaq 0x656f(%rip), %rdx      # 0xc46e
leaq 0xc8(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x7031
leaq 0x30(%rsp), %r14
leaq 0xc8(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x3320
movq %r15, %rdi
callq 0x3100
leaq 0x78(%rsp), %rdi
callq 0x3100
leaq 0x58(%rsp), %rdi
callq 0x3100
movq (%r14), %rdi
leaq 0x6a53(%rip), %rsi      # 0xc9a2
callq 0x32f0
testq %rax, %rax
je 0x5fef
movq %rax, %r14
movzbl 0xc(%rsp), %edx
movq %rax, %rsi
callq 0x5b22
xorl %ebx, %ebx
movl $0xa6, %r15d
xorl %r12d, %r12d
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rax
subq %rdi, %rax
cqto
idivq %r15
cmpq %r12, %rax
jbe 0x5fa6
addq %rbx, %rdi
movq %r14, %rsi
callq 0xa4fa
incq %r12
addq $0xa6, %rbx
jmp 0x5f78
leaq 0x6680(%rip), %rdi      # 0xc62d
callq 0x33f0
movq %r14, %rdi
callq 0x3250
leaq 0x30(%rsp), %rdi
callq 0x3100
leaq 0x98(%rsp), %rdi
callq 0x7844
leaq 0x10(%rsp), %rdi
callq 0x7618
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6389(%rip), %rdi      # 0xc37f
callq 0x31f0
pushq $0x1
popq %rdi
callq 0x3310
jmp 0x603d
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x3100
jmp 0x6017
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x3100
jmp 0x6040
jmp 0x603d
jmp 0x6027
movq %rax, %rbx
movl $0xa6, %esi
movq %rbp, %rdi
callq 0x3270
jmp 0x6063
jmp 0x6060
jmp 0x604e
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x3100
jmp 0x6051
jmp 0x604e
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x7844
jmp 0x6063
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x7618
movq %rbx, %rdi
callq 0x3480
nop