// File: /workspace/github_cpp_makefile_star2_O1/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 0x1903
movq %rsi, %rbx
movq %rdi, %r12
xorl %r14d, %r14d
movq %rsi, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x1110
movq %rbx, %rdi
callq 0x1030
movq %rax, %r13
cmpl $0x200, %r13d           # imm = 0x200
jae 0x1417
jmp 0x1471
decl %r13d
andl $0xfffffe00, %r13d      # imm = 0xFFFFFE00
addl $0x200, %r13d           # imm = 0x200
movq %r13, %rdi
callq 0x1160
movq %rax, -0x40(%rbp)
testq %rax, %rax
je 0x146e
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1110
movq %rbx, %rdi
callq 0x1190
movb $0x1, %r14b
cmpl $-0x1, %eax
je 0x1471
movq -0x40(%rbp), %r15
movb %al, (%r15)
movq %rbx, %rdi
callq 0x1190
incq %r15
cmpl $-0x1, %eax
jne 0x1459
jmp 0x1471
xorl %r14d, %r14d
testb %r14b, %r14b
je 0x1903
movq %r12, -0x70(%rbp)
movq %r13, %rax
leaq -0xc0(%rbp), %r13
movl $0x0, (%r13)
xorl %r15d, %r15d
movq %r15, 0x8(%r13)
movq %r13, 0x10(%r13)
movq %r13, 0x18(%r13)
movq %r15, 0x20(%r13)
movq %rax, -0x50(%rbp)
movl %eax, %r14d
shrl $0x9, %r14d
decl %r14d
movq -0x40(%rbp), %rax
movl 0x44(%rax), %eax
movl %eax, -0x48(%rbp)
movl $0x1b4, %r12d           # imm = 0x1B4
cmpl %r14d, %eax
jae 0x154e
leaq -0xc8(%rbp), %rbx
leaq -0x48(%rbp), %r12
movq -0xb8(%rbp), %rdx
movq %r13, %rcx
testq %rdx, %rdx
je 0x14fe
movq %r13, %rcx
xorl %esi, %esi
cmpl %eax, 0x20(%rdx)
setb %sil
cmovaeq %rdx, %rcx
movq 0x10(%rdx,%rsi,8), %rdx
testq %rdx, %rdx
jne 0x14e7
movq %r13, %rdx
cmpq %r13, %rcx
je 0x1510
cmpl 0x20(%rcx), %eax
cmovbq %r13, %rcx
movq %rcx, %rdx
cmpq %r13, %rdx
jne 0x15d2
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1b54
incl %r15d
movl -0x48(%rbp), %eax
shll $0x9, %eax
movq -0x40(%rbp), %rcx
movl 0x3fc(%rcx,%rax), %eax
movl %eax, -0x48(%rbp)
cmpl %r14d, %eax
jb 0x14d5
shll $0x9, %r15d
orl $0x1b4, %r15d           # imm = 0x1B4
movl %r15d, %r12d
movl %r12d, %edi
callq 0x1160
movq %rax, %r13
testq %rax, %rax
je 0x15ce
movl %r12d, -0x38(%rbp)
movq -0x40(%rbp), %rbx
leaq 0x4c(%rbx), %rsi
movl $0x1b4, %edx            # imm = 0x1B4
movq %r13, %rdi
callq 0x10c0
movl 0x44(%rbx), %r15d
cmpl %r14d, %r15d
movq %r13, -0x30(%rbp)
jae 0x15be
leaq 0x200(%rbx), %r12
addq $0x1b4, %r13            # imm = 0x1B4
xorl %ebx, %ebx
movl %ebx, %edi
addq %r13, %rdi
shll $0x9, %r15d
leaq (%r12,%r15), %rsi
movl $0x1fc, %edx            # imm = 0x1FC
callq 0x10c0
movl 0x1fc(%r12,%r15), %r15d
addl $0x1fc, %ebx            # imm = 0x1FC
cmpl %r14d, %r15d
jb 0x1594
movl %r15d, -0x48(%rbp)
movb $0x1, %bl
movq -0x30(%rbp), %r13
movl -0x38(%rbp), %r12d
jmp 0x15d4
xorl %ebx, %ebx
jmp 0x15d4
xorl %ebx, %ebx
leaq -0xc8(%rbp), %rdi
callq 0x1c7c
testb %bl, %bl
je 0x1903
movl %r12d, %r15d
shrl $0x2, %r15d
xorl %edi, %edi
movq %r13, -0x30(%rbp)
cmpl %r14d, (%r13,%rdi,4)
ja 0x1607
incq %rdi
cmpq %rdi, %r15
jne 0x15f5
movl %r15d, %edi
shll $0x9, %edi
callq 0x1160
movq %rax, %rbx
cmpl $0x4, %r12d
jb 0x166f
testq %rbx, %rbx
je 0x166f
movq -0x40(%rbp), %rax
addq $0x200, %rax            # imm = 0x200
movq %rax, -0x38(%rbp)
movl %r15d, %r15d
shlq $0x9, %r15
xorl %r12d, %r12d
movq -0x30(%rbp), %r13
movl (%r13), %esi
cmpl %r14d, %esi
ja 0x166f
movl %r12d, %edi
andl $0xfffffe00, %edi       # imm = 0xFFFFFE00
addq %rbx, %rdi
shll $0x9, %esi
addq -0x38(%rbp), %rsi
movl $0x200, %edx            # imm = 0x200
callq 0x10c0
addq $0x200, %r12            # imm = 0x200
addq $0x4, %r13
cmpq %r12, %r15
jne 0x1639
testq %rbx, %rbx
je 0x1903
movq -0x30(%rbp), %rdi
movq %rbx, %r12
callq 0x1140
movq -0x40(%rbp), %r15
movl 0x30(%r15), %edi
leaq 0x200(%r15), %r13
movq -0x50(%rbp), %r14
addl $0xfffffe00, %r14d      # imm = 0xFFFFFE00
leaq -0x94(%rbp), %rbx
movq %rbx, (%rsp)
leaq -0x90(%rbp), %r9
movl $0x200, %esi            # imm = 0x200
movq %r12, %rdx
movq %r13, %rcx
movl %r14d, %r8d
callq 0x1930
movl (%rbx), %ebx
cmpl $0x7f, %ebx
seta %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x1903
movq -0x90(%rbp), %rax
movq %rax, -0x30(%rbp)
movl 0x74(%rax), %edi
leaq -0x5c(%rbp), %rax
movq %rax, (%rsp)
leaq -0xc8(%rbp), %r9
movl $0x200, %esi            # imm = 0x200
movq %r12, %rdx
movq %r13, %rcx
movl %r14d, %r8d
callq 0x1930
testb %al, %al
je 0x1903
movl 0x3c(%r15), %edi
leaq -0x98(%rbp), %rax
movq %rax, (%rsp)
leaq -0x48(%rbp), %r9
movl $0x200, %esi            # imm = 0x200
movq %r12, -0x68(%rbp)
movq %r12, %rdx
movq %r13, %rcx
movl %r14d, %r8d
callq 0x1930
testb %al, %al
je 0x1903
movq %r13, -0x88(%rbp)
movq %r14, -0x50(%rbp)
movl 0x38(%r15), %eax
movl %eax, -0x58(%rbp)
shrl $0x7, %ebx
decl %ebx
movq -0x70(%rbp), %r14
movl %ebx, (%r14)
leaq (,%rbx,8), %r15
movq %r15, %rdi
callq 0x1160
movq %rax, %r12
movq %rax, 0x8(%r14)
movq %r15, %rdi
callq 0x1160
movq %rax, %r13
movq %rax, 0x10(%r14)
shlq $0x2, %rbx
movq %rbx, %rdi
callq 0x1160
movq %rax, -0x38(%rbp)
movq %rax, 0x18(%r14)
movq %r12, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x10a0
movq %r13, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x10a0
movq -0x38(%rbp), %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x10a0
cmpl $0x0, (%r14)
sete %bl
je 0x18d1
movq -0x48(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0x78(%rbp)
movl -0x5c(%rbp), %eax
movl %eax, -0x54(%rbp)
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %ecx, %ecx
movq %rcx, -0x38(%rbp)
movl %r12d, %ebx
andl $-0x80, %ebx
movl $0x40, %edi
callq 0x1160
movq 0x8(%r14), %rcx
movq %rax, (%rcx,%r13)
movq 0x8(%r14), %rax
movq (%rax,%r13), %rax
movq -0x30(%rbp), %rcx
movups (%rcx,%rbx), %xmm0
movups 0x10(%rcx,%rbx), %xmm1
movups 0x20(%rcx,%rbx), %xmm2
movups 0x30(%rcx,%rbx), %xmm3
movups %xmm0, (%rax)
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
testq %r12, %r12
je 0x1877
addq %rcx, %rbx
movl 0x74(%rbx), %edi
movl 0x78(%rbx), %ecx
movq 0x18(%r14), %rax
movl %ecx, (%rax,%r15)
addq %r15, %rax
movq 0x10(%r14), %r9
addq %r13, %r9
cmpl -0x58(%rbp), %ecx
jae 0x187e
movq %rax, (%rsp)
movl $0x40, %esi
movq -0x80(%rbp), %rdx
movq -0x78(%rbp), %rcx
movl -0x54(%rbp), %r8d
jmp 0x1896
movl $0x4, %ecx
jmp 0x18a6
movq %rax, (%rsp)
movl $0x200, %esi            # imm = 0x200
movq -0x68(%rbp), %rdx
movq -0x88(%rbp), %rcx
movq -0x50(%rbp), %r8
callq 0x1930
movl $0x1, %ecx
testb %al, %al
je 0x18a6
xorl %ecx, %ecx
testb $0x3, %cl
movq -0x38(%rbp), %rcx
jne 0x1903
incq %rcx
movq -0x70(%rbp), %r14
movl (%r14), %eax
subq $-0x80, %r12
addq $0x8, %r13
addq $0x4, %r15
cmpq %rax, %rcx
setae %bl
jb 0x17f1
movq -0x40(%rbp), %rdi
callq 0x1140
movq -0xc8(%rbp), %rdi
callq 0x1140
movq -0x68(%rbp), %rdi
callq 0x1140
movq -0x48(%rbp), %rdi
callq 0x1140
movq -0x30(%rbp), %rdi
callq 0x1140
jmp 0x1905
xorl %ebx, %ebx
movl %ebx, %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 0x1c7c
movq %rbx, %rdi
callq 0x11a0