// File: 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
movq %rsp, %rbp
subq $0x200, %rsp            # imm = 0x200
movb %dl, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
andb $0x1, %al
movb %al, -0x11(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x160(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x34b0
movq -0x10(%rbp), %rax
movq %rax, -0x158(%rbp)
leaq -0xa1(%rbp), %rdi
movq %rdi, -0x150(%rbp)
callq 0x35d0
movq -0x158(%rbp), %rsi
movq -0x150(%rbp), %rdx
leaq -0xa0(%rbp), %rdi
callq 0xa400
jmp 0x9b58
leaq 0x1d7e2(%rip), %rax     # 0x27341
movsbl (%rax), %edx
leaq -0x80(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0xa3b0
jmp 0x9b74
leaq 0x12a3b(%rip), %rdx     # 0x1c5b6
leaq -0x60(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0xa360
jmp 0x9b8a
leaq -0x40(%rbp), %rdi
movq %rdi, -0x170(%rbp)
leaq -0x60(%rbp), %rsi
movq %rsi, -0x178(%rbp)
callq 0x3450
movq -0x178(%rbp), %rdi
callq 0x31d0
leaq -0x80(%rbp), %rdi
callq 0x31d0
leaq -0xa0(%rbp), %rdi
callq 0x31d0
leaq -0xa1(%rbp), %rdi
callq 0x33c0
movq -0x170(%rbp), %rdi
callq 0x3120
movq %rax, %rdi
leaq 0x12f46(%rip), %rsi     # 0x1cb2e
callq 0x3420
movq %rax, -0x168(%rbp)
jmp 0x9bf6
movq -0x168(%rbp), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
jne 0x9c9a
leaq 0x129b6(%rip), %rdi     # 0x1c5c8
callq 0x32a0
jmp 0x9c19
movl $0x1, %edi
callq 0x3440
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0x9c72
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0x9c66
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x31d0
leaq -0xa0(%rbp), %rdi
callq 0x31d0
leaq -0xa1(%rbp), %rdi
callq 0x33c0
jmp 0xa1bb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0xa1bb
movq -0x160(%rbp), %rdi
addq $0x20, %rdi
callq 0xa620
movq -0x160(%rbp), %rdi
movq %rax, -0xc0(%rbp)
addq $0x38, %rdi
callq 0xa620
movq %rax, %rsi
leaq 0x12421(%rip), %rdi     # 0x1c0ec
xorl %eax, %eax
callq 0x3060
jmp 0x9cd4
movq -0xc0(%rbp), %rsi
leaq 0x12428(%rip), %rdi     # 0x1c10a
xorl %eax, %eax
callq 0x3060
jmp 0x9ceb
movq -0x160(%rbp), %rdi
movq 0x18(%rdi), %rax
movq %rax, -0x190(%rbp)
addq $0x38, %rdi
callq 0xa620
movq -0x190(%rbp), %rdi
movq %rax, %rdx
leaq 0x123d5(%rip), %rsi     # 0x1c0ec
xorl %eax, %eax
movb %al, -0x181(%rbp)
callq 0x34d0
movq -0x160(%rbp), %rcx
movb -0x181(%rbp), %al
movq 0x18(%rcx), %rdi
movq -0xc0(%rbp), %rdx
leaq 0x123dc(%rip), %rsi     # 0x1c11f
callq 0x34d0
movq -0x160(%rbp), %rdi
addq $0x20, %rdi
movq %rdi, -0x180(%rbp)
callq 0xa680
movq -0x180(%rbp), %rdi
movq %rax, -0xc8(%rbp)
callq 0xa6b0
movq %rax, -0xd0(%rbp)
movq -0xc8(%rbp), %rdi
movq -0xd0(%rbp), %rsi
callq 0xa640
jmp 0x9d8e
movq -0x160(%rbp), %rdi
addq $0x20, %rdi
movq %rdi, -0x1a0(%rbp)
callq 0xa680
movq -0x1a0(%rbp), %rdi
movq %rax, -0xe8(%rbp)
callq 0xa6b0
movq %rax, -0xf0(%rbp)
movq -0xe8(%rbp), %rdi
movq -0xf0(%rbp), %rsi
callq 0xa770
movq %rax, -0x198(%rbp)
jmp 0x9ddb
movq -0x198(%rbp), %rax
movq %rax, -0xe0(%rbp)
leaq -0xd8(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0xa7b0
movq -0x160(%rbp), %rdi
addq $0x20, %rdi
callq 0xa6b0
movq %rax, -0x100(%rbp)
leaq -0xf8(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0xa7b0
movq -0x1a0(%rbp), %rdi
movq -0xd8(%rbp), %rsi
movq -0xf8(%rbp), %rdx
callq 0xa6e0
movq %rax, -0x1a8(%rbp)
jmp 0x9e49
movq -0x160(%rbp), %rdi
movq -0x1a8(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0x1b0(%rbp)
addq $0x20, %rdi
callq 0xa620
movq %rax, %rcx
movq -0x1b0(%rbp), %rax
cmpq %rcx, %rax
je 0x9f5e
movq -0x160(%rbp), %rdi
movq -0xc0(%rbp), %rax
movq %rax, -0x1b8(%rbp)
addq $0x20, %rdi
callq 0xa620
movq -0x1b8(%rbp), %rsi
subq %rax, %rsi
leaq 0x12277(%rip), %rdi     # 0x1c12e
xorl %eax, %eax
callq 0x3060
jmp 0x9ec0
movq -0x160(%rbp), %rdi
addq $0x20, %rdi
callq 0xa620
movq %rax, %rsi
leaq 0x12273(%rip), %rdi     # 0x1c14d
xorl %eax, %eax
callq 0x3060
jmp 0x9ee3
movq -0x160(%rbp), %rdi
movq 0x18(%rdi), %rax
movq %rax, -0x1c8(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0x1d0(%rbp)
addq $0x20, %rdi
callq 0xa620
movq -0x1d0(%rbp), %rdx
movq -0x1c8(%rbp), %rdi
subq %rax, %rdx
leaq 0x12244(%rip), %rsi     # 0x1c168
movb $0x0, %al
callq 0x34d0
movq -0x160(%rbp), %rdi
movq 0x18(%rdi), %rax
movq %rax, -0x1c0(%rbp)
addq $0x20, %rdi
callq 0xa620
movq -0x1c0(%rbp), %rdi
movq %rax, %rdx
leaq 0x1222a(%rip), %rsi     # 0x1c181
movb $0x0, %al
callq 0x34d0
movq -0x160(%rbp), %rdi
movq -0x20(%rbp), %rsi
movzbl -0x11(%rbp), %edx
andl $0x1, %edx
callq 0x9920
jmp 0x9f77
leaq 0x12664(%rip), %rdi     # 0x1c5e2
xorl %eax, %eax
callq 0x3060
jmp 0x9f87
movq -0x160(%rbp), %rdi
addq $0x20, %rdi
callq 0xa620
movq %rax, -0x110(%rbp)
movq $0x1, -0x118(%rbp)
movq -0x110(%rbp), %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
mulq %rcx
shrq $0x3, %rdx
movq %rdx, -0x120(%rbp)
movq $0x0, -0x128(%rbp)
movl $0x80, %edi
callq 0x3340
movq %rax, -0x1d8(%rbp)
jmp 0x9fe6
movq -0x1d8(%rbp), %rdi
movq -0x160(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x16410
jmp 0x9fff
movq -0x160(%rbp), %rax
movq -0x1d8(%rbp), %rcx
movq %rcx, -0x128(%rbp)
addq $0x20, %rax
movq %rax, -0x130(%rbp)
movq -0x130(%rbp), %rdi
callq 0xa680
movq %rax, -0x138(%rbp)
movq -0x130(%rbp), %rdi
callq 0xa6b0
movq %rax, -0x140(%rbp)
leaq -0x138(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0xa7f0
testb $0x1, %al
jne 0xa061
jmp 0xa15b
leaq -0x138(%rbp), %rdi
callq 0xbf30
movq -0x160(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0x148(%rbp)
movq -0x128(%rbp), %rax
movq %rax, -0x1f0(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x1e8(%rbp)
addq $0x68, %rdi
leaq -0x148(%rbp), %rsi
callq 0xa540
movq %rax, -0x1e0(%rbp)
jmp 0xa0b0
movq -0x1e8(%rbp), %rsi
movq -0x1f0(%rbp), %rdi
movq -0x1e0(%rbp), %rax
movq (%rax), %rdx
callq 0x16be0
jmp 0xa0cf
movq -0x118(%rbp), %rax
cmpq -0x120(%rbp), %rax
jb 0xa138
leaq 0x126a1(%rip), %rdi     # 0x1c787
xorl %eax, %eax
callq 0x3060
jmp 0xa0ef
movq -0x110(%rbp), %rax
movl $0xa, %ecx
xorl %edx, %edx
divq %rcx
addq -0x120(%rbp), %rax
movq %rax, -0x120(%rbp)
jmp 0xa138
movq -0x1d8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
movl $0x80, %esi
callq 0x3350
jmp 0xa1bb
movq -0x118(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x118(%rbp)
leaq -0x138(%rbp), %rdi
callq 0xbf50
jmp 0xa045
leaq 0x122af(%rip), %rdi     # 0x1c411
xorl %eax, %eax
callq 0x3060
jmp 0xa16b
movq -0x128(%rbp), %rax
movq %rax, -0x1f8(%rbp)
cmpq $0x0, %rax
je 0xa19c
movq -0x1f8(%rbp), %rdi
callq 0xa4b0
movq -0x1f8(%rbp), %rdi
movl $0x80, %esi
callq 0x3350
movq -0x20(%rbp), %rdi
callq 0x3330
jmp 0xa1a7
leaq -0x40(%rbp), %rdi
callq 0x31d0
xorl %eax, %eax
addq $0x200, %rsp            # imm = 0x200
popq %rbp
retq
leaq -0x40(%rbp), %rdi
callq 0x31d0
movq -0xb0(%rbp), %rdi
callq 0x35c0