// File: /workspace/github_cpp_makefile_star2_O2/4n6ist[P]usn_analytics/lib/usnjrnl.cpp
// Function: UsnJrnl::WriteAllRecords(char*, bool)
int UsnJrnl::WriteAllRecords(char *odname, bool lt) {

  FILE *fp_ofraw;
  string ofraw;
  ofraw = string(odname) + SEP + "usn_parse_all.csv";

  if((fp_ofraw = fopen(ofraw.c_str(), "w")) == NULL) {
    perror("Output All Raw File Error");
    exit(EXIT_FAILURE);
  } 

  uint64_t usn_num = usn_set.size();
  printf("%8lu corrupt records skipped\n", corrupt_offset_set.size());
  printf("%8llu records found\n", usn_num);
  fprintf(fp_ofreport, "%8lu corrupt records skipped\n", corrupt_offset_set.size());
  fprintf(fp_ofreport, "%8llu records\n", usn_num);   

  // USN Sort & Deduplication
  sort(usn_set.begin(), usn_set.end());
  usn_set.erase(std::unique(usn_set.begin(), usn_set.end()), usn_set.end());
  if (usn_num != usn_set.size()) {
    printf("%8llu duplicate records found\n", usn_num - usn_set.size()); 
    printf("%8lu unique records found\n", usn_set.size()); 
    fprintf(fp_ofreport, "%8llu duplicate records\n", usn_num - usn_set.size()); 
    fprintf(fp_ofreport, "%8lu unique records\n", usn_set.size()); 
  }

  WriteAllHeader(fp_ofraw, lt);
  printf("Write all records");

  uint64_t usn_set_size = usn_set.size();
  uint64_t i=1;
  uint64_t progress = usn_set_size / 10;

  UsnRecord* ur = 0;
  ur = new UsnRecord(fp_in);

  for(uint64_t x: usn_set) {
    ur->ReadParseWriteRecord(fp_ofraw, usn_table[x]);
    if (i >= progress) {
  	  printf(".");
      progress += usn_set_size / 10;
    }
    ++i;
  }
  
  printf("Done\n");
  delete ur;
  fclose(fp_ofraw);
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %edx, %ebp
movq %rdi, %r13
leaq 0x88(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x18(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0x7072
leaq 0xb575(%rip), %rax      # 0x12299
movsbl (%rax), %edx
leaq 0x38(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x7050
leaq 0x586e(%rip), %rdx      # 0xc5ab
leaq 0x58(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x7031
leaq 0x78(%rsp), %r14
leaq 0x58(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x3320
movq %r15, %rdi
callq 0x3100
leaq 0x38(%rsp), %rdi
callq 0x3100
leaq 0x18(%rsp), %rdi
callq 0x3100
movq (%r14), %rdi
leaq 0x5c1b(%rip), %rsi      # 0xc9a2
callq 0x32f0
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x6fa1
movq 0x28(%r13), %r15
movq 0x40(%r13), %rsi
subq 0x20(%r13), %r15
sarq $0x3, %r15
subq 0x38(%r13), %rsi
sarq $0x3, %rsi
leaq 0x5333(%rip), %r12      # 0xc0ec
movq %r12, %rdi
xorl %eax, %eax
callq 0x3040
leaq 0x5340(%rip), %rdi      # 0xc10a
movq %r15, %rsi
xorl %eax, %eax
callq 0x3040
movq 0x18(%r13), %rdi
movq 0x40(%r13), %rdx
subq 0x38(%r13), %rdx
sarq $0x3, %rdx
movq %r12, %rsi
xorl %eax, %eax
callq 0x3380
movq 0x18(%r13), %rdi
leaq 0x5326(%rip), %rsi      # 0xc11f
movq %r15, %rdx
xorl %eax, %eax
callq 0x3380
movq 0x20(%r13), %rdi
movq 0x28(%r13), %rsi
callq 0x7e03
movq 0x20(%r13), %rdi
movq 0x28(%r13), %rsi
callq 0x8229
leaq 0x20(%r13), %rdi
movq 0x28(%r13), %rdx
movq %rax, %rsi
callq 0x711e
movq 0x28(%r13), %rax
subq 0x20(%r13), %rax
sarq $0x3, %rax
movq %r15, %rsi
subq %rax, %rsi
je 0x6eab
leaq 0x52e6(%rip), %rdi      # 0xc12e
xorl %eax, %eax
callq 0x3040
movq 0x28(%r13), %rsi
subq 0x20(%r13), %rsi
sarq $0x3, %rsi
leaq 0x52eb(%rip), %rdi      # 0xc14d
xorl %eax, %eax
callq 0x3040
movq 0x18(%r13), %rdi
movq 0x28(%r13), %rax
subq 0x20(%r13), %rax
sarq $0x3, %rax
subq %rax, %r15
leaq 0x52e5(%rip), %rsi      # 0xc168
movq %r15, %rdx
xorl %eax, %eax
callq 0x3380
movq 0x18(%r13), %rdi
movq 0x28(%r13), %rdx
subq 0x20(%r13), %rdx
sarq $0x3, %rdx
leaq 0x52dd(%rip), %rsi      # 0xc181
xorl %eax, %eax
callq 0x3380
movzbl %bpl, %edx
movq 0x8(%rsp), %rsi
callq 0x6ae4
leaq 0x5717(%rip), %rdi      # 0xc5d7
xorl %eax, %eax
callq 0x3040
movq 0x28(%r13), %rax
subq 0x20(%r13), %rax
sarq $0x3, %rax
pushq $0xa
popq %rcx
xorl %edx, %edx
divq %rcx
movq %rax, 0x10(%rsp)
movl $0x80, %edi
callq 0x3260
movq %rax, %r15
movq 0x10(%r13), %rsi
movq %rax, %rdi
callq 0x9ad4
movq 0x20(%r13), %rbp
movq 0x28(%r13), %r14
addq $0x68, %r13
pushq $0x1
popq %r12
movq 0x10(%rsp), %rbx
cmpq %r14, %rbp
je 0x6f54
movq (%rbp), %rax
movq %rax, 0x58(%rsp)
movq %r13, %rdi
leaq 0x58(%rsp), %rsi
callq 0x70ce
movq (%rax), %rdx
movq %r15, %rdi
movq 0x8(%rsp), %rsi
callq 0x9f6a
cmpq %rbx, %r12
jb 0x6f4b
pushq $0x2e
popq %rdi
callq 0x3370
addq 0x10(%rsp), %rbx
incq %r12
addq $0x8, %rbp
jmp 0x6f0e
leaq 0x56d5(%rip), %rdi      # 0xc630
callq 0x33f0
movq %r15, %rdi
addq $0x60, %rdi
callq 0x3100
movl $0x80, %esi
movq %r15, %rdi
callq 0x3270
movq 0x8(%rsp), %rdi
callq 0x3250
leaq 0x78(%rsp), %rdi
callq 0x3100
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5615(%rip), %rdi      # 0xc5bd
callq 0x31f0
pushq $0x1
popq %rdi
callq 0x3310
movq %rax, %rbx
movl $0x80, %esi
movq %r15, %rdi
callq 0x3270
jmp 0x6ff2
jmp 0x6fef
jmp 0x6fef
jmp 0x6fef
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x3100
jmp 0x6fdf
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x3100
jmp 0x6ff2
jmp 0x6fef
jmp 0x6fef
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x3100
movq %rbx, %rdi
callq 0x3480