// File: /workspace/github_cpp_makefile_star2_O3/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 0x1893
movq %rsi, %rbx
movq %rdi, %r14
xorl %r15d, %r15d
movq %rsi, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x1110
movq %rbx, %rdi
callq 0x1030
cmpl $0x200, %eax            # imm = 0x200
jb 0x1896
decl %eax
andl $0xfffffe00, %eax       # imm = 0xFFFFFE00
movq %rax, -0x78(%rbp)
leal 0x200(%rax), %r12d
movq %r12, %rdi
callq 0x1160
movq %rax, -0x30(%rbp)
testq %rax, %rax
je 0x1893
movq %r14, -0x50(%rbp)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1110
movq %rbx, %rdi
callq 0x1190
cmpl $-0x1, %eax
je 0x1478
movq -0x30(%rbp), %r14
movb %al, (%r14)
movq %rbx, %rdi
callq 0x1190
incq %r14
cmpl $-0x1, %eax
jne 0x145c
movq -0x30(%rbp), %rax
movl 0x44(%rax), %eax
jmp 0x1478
leaq -0xc0(%rbp), %r15
movl $0x0, (%r15)
xorl %r14d, %r14d
movq %r14, 0x8(%r15)
movq %r15, 0x10(%r15)
movq %r15, 0x18(%r15)
movq %r14, 0x20(%r15)
shrl $0x9, %r12d
decl %r12d
movl %eax, -0x48(%rbp)
movl $0x1b4, %ebx            # imm = 0x1B4
cmpl %r12d, %eax
jae 0x1536
leaq -0xc8(%rbp), %rbx
leaq -0x48(%rbp), %r13
movq -0xb8(%rbp), %rdi
testq %rdi, %rdi
je 0x1501
movq %r15, %r8
movq %r8, %rcx
movq %rdi, %rdx
movl 0x20(%rdi), %esi
xorl %edi, %edi
cmpl %eax, %esi
setb %dil
movq %rdx, %r8
cmovbq %rcx, %r8
movq 0x10(%rdx,%rdi,8), %rdi
testq %rdi, %rdi
jne 0x14cb
cmpq %r15, %r8
je 0x1501
cmpl %eax, %esi
cmovbq %rcx, %rdx
cmpl 0x20(%rdx), %eax
jae 0x1887
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1ad6
incl %r14d
movl -0x48(%rbp), %eax
shll $0x9, %eax
movq -0x30(%rbp), %rcx
movl 0x3fc(%rcx,%rax), %eax
movl %eax, -0x48(%rbp)
cmpl %r12d, %eax
jb 0x14bc
shll $0x9, %r14d
orl $0x1b4, %r14d           # imm = 0x1B4
movl %r14d, %ebx
movl %ebx, %edi
callq 0x1160
testq %rax, %rax
je 0x1887
movq %rbx, -0x40(%rbp)
movq -0x30(%rbp), %rbx
leaq 0x4c(%rbx), %rsi
movl $0x1b4, %edx            # imm = 0x1B4
movq %rax, -0x38(%rbp)
movq %rax, %rdi
callq 0x10c0
movl 0x44(%rbx), %r14d
cmpl %r12d, %r14d
jae 0x15ae
movq -0x30(%rbp), %rax
leaq 0x200(%rax), %r15
movq -0x38(%rbp), %r13
addq $0x1b4, %r13            # imm = 0x1B4
xorl %ebx, %ebx
movl %ebx, %edi
addq %r13, %rdi
shll $0x9, %r14d
leaq (%r15,%r14), %rsi
movl $0x1fc, %edx            # imm = 0x1FC
callq 0x10c0
movl 0x1fc(%r15,%r14), %r14d
addl $0x1fc, %ebx            # imm = 0x1FC
cmpl %r12d, %r14d
jb 0x1584
leaq -0xc8(%rbp), %rdi
callq 0x1bf2
movq -0x40(%rbp), %r13
shrl $0x2, %r13d
xorl %edi, %edi
movq -0x38(%rbp), %r15
cmpl %r12d, (%r15,%rdi,4)
ja 0x15d9
incq %rdi
cmpq %rdi, %r13
jne 0x15c8
movl %r13d, %edi
shll $0x9, %edi
callq 0x1160
testq %rax, %rax
je 0x1893
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rbx
addq $0x200, %rbx            # imm = 0x200
shlq $0x9, %r13
xorl %r14d, %r14d
movl (%r15), %esi
cmpl %r12d, %esi
ja 0x1635
movl %r14d, %edi
andl $0xfffffe00, %edi       # imm = 0xFFFFFE00
addq -0x40(%rbp), %rdi
shll $0x9, %esi
addq %rbx, %rsi
movl $0x200, %edx            # imm = 0x200
callq 0x10c0
addq $0x200, %r14            # imm = 0x200
addq $0x4, %r15
cmpq %r14, %r13
jne 0x1600
movq -0x38(%rbp), %rdi
callq 0x1140
movq -0x30(%rbp), %r15
movl 0x30(%r15), %edi
movq %rbx, %r12
leaq -0x94(%rbp), %rbx
movq %rbx, (%rsp)
leaq -0x90(%rbp), %r9
movl $0x200, %esi            # imm = 0x200
movq -0x40(%rbp), %r13
movq %r13, %rdx
movq %r12, %rcx
movq -0x78(%rbp), %r14
movl %r14d, %r8d
callq 0x18c2
movl (%rbx), %ebx
cmpl $0x7f, %ebx
seta %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x1893
movq -0x90(%rbp), %rax
movq %rax, -0x38(%rbp)
movl 0x74(%rax), %edi
leaq -0x5c(%rbp), %rax
movq %rax, (%rsp)
leaq -0xc8(%rbp), %r9
movl $0x200, %esi            # imm = 0x200
movq %r13, %rdx
movq %r12, %rcx
movl %r14d, %r8d
callq 0x18c2
testb %al, %al
je 0x1893
movl 0x3c(%r15), %edi
leaq -0x98(%rbp), %rax
movq %rax, (%rsp)
leaq -0x48(%rbp), %r9
movl $0x200, %esi            # imm = 0x200
movq %r13, %rdx
movq %r12, -0x80(%rbp)
movq %r12, %rcx
movl %r14d, %r8d
callq 0x18c2
testb %al, %al
je 0x1893
movl 0x38(%r15), %eax
movl %eax, -0x58(%rbp)
shrl $0x7, %ebx
decl %ebx
movq -0x50(%rbp), %r14
movl %ebx, (%r14)
leaq (,%rbx,8), %r13
movl $0x1, %edi
movq %r13, %rsi
callq 0x10b0
movq %rax, 0x8(%r14)
movl $0x1, %edi
movq %r13, %rsi
callq 0x10b0
movq %rax, 0x10(%r14)
leaq (,%rbx,4), %rsi
movl $0x1, %edi
callq 0x10b0
movq %rax, 0x18(%r14)
movq -0xc8(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x70(%rbp)
testl %ebx, %ebx
je 0x1855
movq %r14, %rdx
movl -0x5c(%rbp), %eax
movl %eax, -0x54(%rbp)
xorl %r13d, %r13d
xorl %ebx, %ebx
xorl %r12d, %r12d
xorl %r14d, %r14d
movq %r12, -0x88(%rbp)
movl %r12d, %r15d
andl $-0x80, %r15d
movl $0x40, %edi
movq %r13, %r12
movq %rdx, %r13
callq 0x1160
movq %r13, %rdx
movq %r12, %r13
movq -0x88(%rbp), %r12
movq 0x8(%rdx), %rcx
movq %rax, (%rcx,%rbx)
movq 0x8(%rdx), %rax
movq (%rax,%rbx), %rax
movq -0x38(%rbp), %rcx
movups (%rcx,%r15), %xmm0
movups 0x10(%rcx,%r15), %xmm1
movups 0x20(%rcx,%r15), %xmm2
movups 0x30(%rcx,%r15), %xmm3
movups %xmm0, (%rax)
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
testq %r12, %r12
je 0x183b
addq -0x38(%rbp), %r15
movl 0x74(%r15), %edi
movl 0x78(%r15), %ecx
movq 0x18(%rdx), %rax
movl %ecx, (%rax,%r13)
addq %r13, %rax
movq 0x10(%rdx), %r9
addq %rbx, %r9
cmpl -0x58(%rbp), %ecx
jae 0x1819
movq %rax, (%rsp)
movl $0x40, %esi
movq -0x70(%rbp), %rdx
movq -0x68(%rbp), %rcx
movl -0x54(%rbp), %r8d
jmp 0x182e
movq %rax, (%rsp)
movl $0x200, %esi            # imm = 0x200
movq -0x40(%rbp), %rdx
movq -0x80(%rbp), %rcx
movq -0x78(%rbp), %r8
callq 0x18c2
testb %al, %al
movq -0x50(%rbp), %rdx
je 0x1893
incq %r14
movl (%rdx), %eax
subq $-0x80, %r12
addq $0x8, %rbx
addq $0x4, %r13
cmpq %rax, %r14
jb 0x1775
movq -0x30(%rbp), %rdi
callq 0x1140
movq -0x68(%rbp), %rdi
callq 0x1140
movq -0x40(%rbp), %rdi
callq 0x1140
movq -0x70(%rbp), %rdi
callq 0x1140
movq -0x38(%rbp), %rdi
callq 0x1140
movb $0x1, %r15b
jmp 0x1896
leaq -0xc8(%rbp), %rdi
callq 0x1bf2
xorl %r15d, %r15d
movl %r15d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0xc8(%rbp), %rdi
callq 0x1bf2
movq %rbx, %rdi
callq 0x11a0