// File: /workspace/github_cpp_makefile_star2_O1/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), %rax
cmpq %rax, 0x58(%rdi)
je 0x737b
xorl %ebx, %ebx
movl $0x80200, %r14d         # imm = 0x80200
movabsq $0x3a4c0a237c32b16d, %r12 # imm = 0x3A4C0A237C32B16D
xorl %r13d, %r13d
movq 0x42(%rax,%rbx), %rdx
cmpq $0x4, %rdx
jb 0x729e
leaq (%rax,%rbx), %rdi
addq $0x3a, %rdi
addq $-0x4, %rdx
movl $0x4, %ecx
leaq 0x81dd(%rip), %rsi      # 0xf41f
callq 0x3460
cmpq $-0x1, %rax
je 0x729e
movq 0x50(%r15), %rax
movl 0x5a(%rax,%rbx), %eax
cmpl $0x80000800, %eax       # imm = 0x80000800
sete %cl
shrl $0x9, %eax
andl $0x1, %eax
orb %cl, %al
jne 0x7356
movl $0xa6, %edi
callq 0x3220
movq %rax, %rbp
movq %rax, %rdi
callq 0xc58e
movq 0x50(%r15), %rsi
addq %rbx, %rsi
movq %rbp, %rdi
callq 0xc5ce
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x9302
jmp 0x72e5
movq 0x50(%r15), %rax
movl 0x5a(%rax,%rbx), %eax
andl %r14d, %eax
cmpl $0x80000, %eax          # imm = 0x80000
jne 0x7356
movl $0xa6, %edi
callq 0x3220
movq %rax, %rbp
movq %rax, %rdi
callq 0xc58e
movq 0x50(%r15), %rsi
addq %rbx, %rsi
movq %rbp, %rdi
callq 0xc5ce
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x9302
movq 0x68(%rbp), %rdi
leaq 0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x72fd
movq (%rax), %rsi
incq %rsi
callq 0x3230
movq 0x48(%rbp), %rdi
leaq 0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x7315
movq (%rax), %rsi
incq %rsi
callq 0x3230
movq 0x28(%rbp), %rdi
leaq 0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x732d
movq (%rax), %rsi
incq %rsi
callq 0x3230
movq 0x8(%rbp), %rdi
movq %rbp, %rax
addq $0x18, %rax
cmpq %rax, %rdi
je 0x7349
movq 0x18(%rbp), %rsi
incq %rsi
callq 0x3230
movl $0xa6, %esi
movq %rbp, %rdi
callq 0x3230
incq %r13
movq 0x50(%r15), %rax
movq 0x58(%r15), %rcx
subq %rax, %rcx
sarq %rcx
imulq %r12, %rcx
addq $0xca, %rbx
cmpq %r13, %rcx
ja 0x721f
leaq 0xd0(%rsp), %r14
movl $0x0, (%r14)
xorl %eax, %eax
movq %rax, 0x8(%r14)
movq %r14, 0x10(%r14)
movq %r14, 0x18(%r14)
movq %rax, 0x20(%r14)
movq 0x10(%rsp), %rsi
movabsq $-0x5c87b5f9d4d1bc25, %rbp # imm = 0xA3784A062B2E43DB
cmpq %rsi, 0x18(%rsp)
je 0x7417
movl $0x48, %ebx
xorl %r13d, %r13d
leaq 0xc8(%rsp), %r12
addq %rbx, %rsi
movq %r12, %rdi
callq 0xa8b6
movq 0x10(%rsp), %rsi
addq %rbx, %rsi
cmpq %r14, %rax
je 0x73e7
movq %r12, %rdi
callq 0x902a
incw (%rax)
jmp 0x73f4
movq %r12, %rdi
callq 0x902a
movw $0x1, (%rax)
incq %r13
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rax
subq %rsi, %rax
sarq %rax
imulq %rbp, %rax
addq $0xa6, %rbx
cmpq %r13, %rax
ja 0x73c2
movq 0x18(%r15), %rdi
movq 0xf0(%rsp), %rdx
leaq 0x7ffa(%rip), %rsi      # 0xf424
xorl %eax, %eax
callq 0x3360
movq 0xe0(%rsp), %r12
cmpq %r14, %r12
je 0x74ed
movl $0x401, %r13d           # imm = 0x401
leaq 0x48(%rsp), %rbx
leaq 0x7f95(%rip), %rbp      # 0xf3e9
movq %rbx, 0x38(%rsp)
movq 0x20(%r12), %rsi
movq 0x28(%r12), %rdx
addq %rsi, %rdx
leaq 0x38(%rsp), %rdi
callq 0x9574
movzwl 0x40(%r12), %ecx
movw %cx, 0x58(%rsp)
movq 0x18(%r15), %rdi
movq 0x38(%rsp), %rdx
movq %rbp, %rsi
xorl %eax, %eax
callq 0x3360
decl %r13d
je 0x74bc
movq 0x38(%rsp), %rdi
cmpq %rbx, %rdi
je 0x74aa
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3230
movq %r12, %rdi
callq 0x3150
movq %rax, %r12
cmpq %r14, %rax
jne 0x7454
jmp 0x74ed
movq 0x18(%r15), %rcx
leaq 0x7f83(%rip), %rdi      # 0xf44a
movl $0x23, %esi
movl $0x1, %edx
callq 0x34c0
movq 0x38(%rsp), %rdi
cmpq %rbx, %rdi
je 0x74ed
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 0x8e42
leaq 0xcd61(%rip), %rax      # 0x14281
movsbl (%rax), %r8d
leaq 0x88(%rsp), %rdi
movq 0x8(%rdi), %rsi
movl $0x1, %ecx
xorl %edx, %edx
callq 0x34a0
leaq 0x78(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x7564
movq %rdx, 0x68(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x78(%rsp)
jmp 0x756c
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 0x7ed5(%rip), %rsi      # 0xf46e
callq 0x34b0
leaq 0xb8(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x75ce
movq %rdx, 0xa8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xb8(%rsp)
jmp 0x75d4
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 0x7618
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x3230
movq 0x68(%rsp), %rdi
cmpq %r12, %rdi
je 0x762f
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3230
leaq 0x98(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7650
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x3230
movq 0x38(%rsp), %rdi
leaq 0x8347(%rip), %rsi      # 0xf9a3
callq 0x32d0
testq %rax, %rax
je 0x770f
movq %rax, %r14
movzbl 0x34(%rsp), %edx
movq %rax, %rsi
callq 0x704a
movq 0x10(%rsp), %rdi
cmpq %rdi, 0x18(%rsp)
je 0x76b9
xorl %ebx, %ebx
xorl %r12d, %r12d
addq %rbx, %rdi
movq %r14, %rsi
callq 0xc654
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 0x768b
leaq 0x7f6d(%rip), %rdi      # 0xf62d
callq 0x33e0
movq %r14, %rdi
callq 0x3210
movq 0x38(%rsp), %rdi
cmpq %r15, %rdi
je 0x76e4
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3230
leaq 0xc8(%rsp), %rdi
callq 0x9604
leaq 0x10(%rsp), %rdi
callq 0x9332
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3534
jmp 0x7772
movq %rax, %rbx
movq 0x68(%rsp), %rdi
cmpq %r12, %rdi
je 0x7735
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3230
jmp 0x7735
movq %rax, %rbx
leaq 0x98(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7775
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x3230
jmp 0x7775
jmp 0x7772
jmp 0x775c
movq %rax, %rbx
movl $0xa6, %esi
movq %rbp, %rdi
callq 0x3230
jmp 0x77a5
jmp 0x77a2
jmp 0x7790
movq %rax, %rbx
movq 0x38(%rsp), %rdi
cmpq %r15, %rdi
je 0x7793
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3230
jmp 0x7793
jmp 0x7790
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x9604
jmp 0x77a5
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x9332
movq %rbx, %rdi
callq 0x3470
nop