// File: /workspace/github_cpp_makefile_star2_O2/tsfn[P]doc2txt/src/Storage.cpp
// Function: Storage::init(_IO_FILE*)
bool Storage::init(FILE *doc_file) {
  if (doc_file == NULL) {
    return false;
  }

  uchar *buf, *mbuf, *msat, *sat, *ssat, *dir_stream;
  uint buf_size, mbuf_size, msat_size, sat_size, ssat_size, dir_stream_size;

  if (!read_buf(doc_file, &buf, &buf_size)) return false;
  if (!read_msat(buf, buf_size, &msat, &msat_size)) return false;
  if (!read_sat(buf, buf_size, msat, msat_size, &sat, &sat_size)) return false;
  free(msat);

  /* Directory Stream */
  if (!read_stream(*(uint *)(buf + 0x30), 512, sat, sat_size,
        buf + 512, buf_size - 512, &dir_stream, &dir_stream_size)) {
    return false;
  }
  if (dir_stream_size < 128) return false;

  /* short-sector Stream's mbuf and ssat */
  if (!read_stream(*(uint *)(dir_stream + 0x74), 512, sat, sat_size,
        buf + 512, buf_size - 512, &mbuf, &mbuf_size)) {
    return false;
  }
  if (!read_stream(*(uint *)(buf + 0x3C), 512, sat, sat_size,
        buf + 512, buf_size - 512, &ssat, &ssat_size)) {
    return false;
  }

  /* all streams */
  uint _ulMiniSectorCutoff = *(uint *)(buf + 0x38);

  stream_num = dir_stream_size / 128 - 1;
  stream_name = (uchar **)malloc(stream_num * sizeof(uchar *));
  stream_table = (uchar **)malloc(stream_num * sizeof(uchar *));
  stream_sizes = (uint *)malloc(stream_num * sizeof(uint));

  memset(stream_name, 0, stream_num * sizeof(uchar *));
  memset(stream_table, 0, stream_num * sizeof(uchar *));
  memset(stream_sizes, 0, stream_num * sizeof(uint));

  for (uint i = 0; i < stream_num; ++i) {
    uchar *dir_entry = dir_stream + i * 128;
    stream_name[i] = (uchar *)malloc(64);
    memcpy(stream_name[i], dir_entry, 64);
    if (i == 0) continue;
    uint id = *(uint *)(dir_entry + 0x74);
    stream_sizes[i] = *(uint *)(dir_entry + 0x78);
    /*
    for (uint j = 0; j < 32; ++j) {
      printf("%c", stream_name[i][j * 2]);
    }
    printf("-> sz=%u, id=%u\n", stream_sizes[i], id);
    */
    if (stream_sizes[i] >= _ulMiniSectorCutoff) {
      if (!read_stream(id, 512, sat, sat_size,
            buf + 512, buf_size - 512, &stream_table[i], &stream_sizes[i])) {
        return false;
      }
    } else {
      if (!read_stream(id, 64, ssat, ssat_size,
            mbuf, mbuf_size, &stream_table[i], &stream_sizes[i])) {
        return false;
      }
    }
  }

  free(buf);
  free(mbuf);
  free(sat);
  free(ssat);
  free(dir_stream);
  return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
testq %rsi, %rsi
je 0x182e
movq %rsi, %rbx
movq %rdi, %r14
xorl %r15d, %r15d
pushq $0x2
popq %rdx
movq %rsi, %rdi
xorl %esi, %esi
callq 0x1130
movq %rbx, %rdi
callq 0x1030
movq %rax, %r12
cmpl $0x200, %r12d           # imm = 0x200
jb 0x1831
decl %r12d
andl $0xfffffe00, %r12d      # imm = 0xFFFFFE00
leal 0x200(%r12), %edi
movq %rdi, %r13
callq 0x1180
testq %rax, %rax
je 0x182e
movq %rax, %r15
movq %r14, -0x58(%rbp)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1130
movq %r15, %r14
movq %rbx, %rdi
callq 0x11b0
cmpl $-0x1, %eax
je 0x1465
movb %al, (%r14)
incq %r14
jmp 0x1450
movq %r12, -0x88(%rbp)
leaq -0xc0(%rbp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x44(%r15), %r14
shrl $0x9, %r13d
decl %r13d
movq %r13, -0x30(%rbp)
movq %r15, -0x40(%rbp)
leaq 0x3fc(%r15), %r12
movl $0x1b4, %r13d           # imm = 0x1B4
leaq -0xc8(%rbp), %rbx
leaq -0x48(%rbp), %r15
movq %r14, %rax
movl (%rax), %eax
movl %eax, -0x48(%rbp)
cmpl -0x30(%rbp), %eax
jae 0x14f1
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1a02
testq %rax, %rax
jne 0x1565
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1a8c
movl -0x48(%rbp), %eax
shll $0x9, %eax
addq %r12, %rax
addl $0x200, %r13d           # imm = 0x200
jmp 0x14b6
movl %r13d, %edi
callq 0x1180
xorl %r15d, %r15d
testq %rax, %rax
je 0x1573
movq %rax, %r12
movq -0x40(%rbp), %rbx
leaq 0x4c(%rbx), %rsi
movl $0x1b4, %edx            # imm = 0x1B4
movq %rax, %rdi
callq 0x10e0
movq %r12, %rax
leaq 0x200(%rbx), %r12
movq %rax, -0x38(%rbp)
movq %rax, %rbx
addq $0x1b4, %rbx            # imm = 0x1B4
movl (%r14), %r14d
cmpl -0x30(%rbp), %r14d
jae 0x156a
movl %r15d, %edi
addq %rbx, %rdi
shll $0x9, %r14d
leaq (%r12,%r14), %rsi
movl $0x1fc, %edx            # imm = 0x1FC
callq 0x10e0
addq %r12, %r14
addq $0x1fc, %r14            # imm = 0x1FC
addl $0x1fc, %r15d           # imm = 0x1FC
jmp 0x1531
xorl %r15d, %r15d
jmp 0x1576
movb $0x1, %r15b
movq -0x38(%rbp), %r12
jmp 0x1576
xorl %r12d, %r12d
leaq -0xc8(%rbp), %rdi
callq 0x1c1c
testb %r15b, %r15b
je 0x182e
shrl $0x2, %r13d
xorl %edi, %edi
movq -0x30(%rbp), %rax
cmpq %rdi, %r13
je 0x15a5
cmpl %eax, (%r12,%rdi,4)
ja 0x15a8
incq %rdi
jmp 0x1595
movl %r13d, %edi
shll $0x9, %edi
callq 0x1180
testq %rax, %rax
je 0x182e
movq %rax, %rbx
movq -0x40(%rbp), %r15
addq $0x200, %r15            # imm = 0x200
shlq $0x9, %r13
xorl %r14d, %r14d
movq %r12, -0x38(%rbp)
cmpq %r14, %r13
je 0x1609
movl (%r12), %esi
cmpl -0x30(%rbp), %esi
ja 0x1609
movl %r14d, %edi
andl $0xfffffe00, %edi       # imm = 0xFFFFFE00
addq %rbx, %rdi
shll $0x9, %esi
addq %r15, %rsi
movl $0x200, %edx            # imm = 0x200
callq 0x10e0
addq $0x200, %r14            # imm = 0x200
addq $0x4, %r12
jmp 0x15d2
movq -0x38(%rbp), %rdi
callq 0x1160
movq -0x40(%rbp), %r12
movl 0x30(%r12), %edi
movq %rbx, %r13
leaq -0x94(%rbp), %rbx
movq %rbx, (%rsp)
leaq -0x90(%rbp), %r9
movl $0x200, %esi            # imm = 0x200
movq %r13, %rdx
movq %r15, %rcx
movq -0x88(%rbp), %r14
movl %r14d, %r8d
callq 0x188e
movl (%rbx), %edx
cmpl $0x7f, %edx
seta %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x182e
movq %rdx, -0x30(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0x50(%rbp)
movl 0x74(%rax), %edi
leaq -0x64(%rbp), %rax
movq %rax, (%rsp)
leaq -0xc8(%rbp), %r9
movl $0x200, %esi            # imm = 0x200
movq %r13, %rdx
movq %r15, %rcx
movl %r14d, %r8d
callq 0x188e
testb %al, %al
je 0x182e
movq %r13, %rbx
movl 0x3c(%r12), %edi
leaq -0x98(%rbp), %rax
movq %rax, (%rsp)
leaq -0x48(%rbp), %r9
movl $0x200, %esi            # imm = 0x200
movq %r13, %rdx
movq %r15, %rcx
movl %r14d, %r8d
callq 0x188e
testb %al, %al
je 0x182e
movl 0x38(%r12), %eax
movl %eax, -0x60(%rbp)
movq -0x30(%rbp), %r12
shrl $0x7, %r12d
decl %r12d
movq -0x58(%rbp), %r14
movl %r12d, (%r14)
leaq (,%r12,8), %r13
pushq $0x1
popq %rdi
movq %r13, %rsi
callq 0x10c0
movq %rax, 0x8(%r14)
pushq $0x1
popq %rdi
movq %r13, %rsi
callq 0x10c0
movq %rax, 0x10(%r14)
leaq (,%r12,4), %rsi
pushq $0x1
popq %rdi
callq 0x10c0
movq %r12, %rcx
movq %rax, 0x18(%r14)
movq -0x48(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0x70(%rbp)
xorl %r13d, %r13d
movl -0x64(%rbp), %eax
movl %eax, -0x5c(%rbp)
xorl %r12d, %r12d
xorl %edx, %edx
xorl %esi, %esi
movq %rbx, -0x80(%rbp)
movl %ecx, %eax
cmpq %rax, %rsi
jae 0x1846
movq %rsi, -0x38(%rbp)
movq %r14, %rbx
movq %rdx, -0x30(%rbp)
movl %edx, %r14d
andl $-0x80, %r14d
pushq $0x40
popq %rdi
callq 0x1180
movq 0x8(%rbx), %rcx
movq %rax, (%rcx,%r12)
movq 0x8(%rbx), %rax
movq (%rax,%r12), %rax
movq -0x50(%rbp), %rcx
movups (%rcx,%r14), %xmm0
movups 0x10(%rcx,%r14), %xmm1
movups 0x20(%rcx,%r14), %xmm2
movups 0x30(%rcx,%r14), %xmm3
movups %xmm0, (%rax)
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
testq %r13, %r13
je 0x1809
addq -0x50(%rbp), %r14
movl 0x74(%r14), %edi
movl 0x78(%r14), %ecx
movq 0x18(%rbx), %rax
movl %ecx, (%rax,%r13)
addq %r13, %rax
movq 0x10(%rbx), %r9
addq %r12, %r9
cmpl -0x60(%rbp), %ecx
jae 0x17e5
movq %rax, (%rsp)
pushq $0x40
popq %rsi
movq -0x78(%rbp), %rdx
movq -0x70(%rbp), %rcx
movl -0x5c(%rbp), %r8d
jmp 0x17fc
movq %rax, (%rsp)
movl $0x200, %esi            # imm = 0x200
movq -0x80(%rbp), %rdx
movq %r15, %rcx
movq -0x88(%rbp), %r8
callq 0x188e
testb %al, %al
movq -0x58(%rbp), %rbx
je 0x182e
movq -0x38(%rbp), %rsi
incq %rsi
movl (%rbx), %ecx
movq -0x30(%rbp), %rdx
subq $-0x80, %rdx
addq $0x8, %r12
addq $0x4, %r13
movq %rbx, %r14
movq -0x80(%rbp), %rbx
jmp 0x1749
xorl %r15d, %r15d
movl %r15d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x1160
movq -0x70(%rbp), %rdi
callq 0x1160
movq %rbx, %rdi
callq 0x1160
movq -0x78(%rbp), %rdi
callq 0x1160
movq -0x50(%rbp), %rdi
callq 0x1160
movb $0x1, %r15b
jmp 0x1831
movq %rax, %rbx
leaq -0xc8(%rbp), %rdi
callq 0x1c1c
movq %rbx, %rdi
callq 0x11c0