// File: /workspace/github_cpp_makefile_star2_O3/tsfn[P]doc2txt/src/doc_text.cpp
// Function: doc_text_file(Storage const&, _IO_FILE*)
bool doc_text_file(const Storage &storage, FILE *text_file) {
  const uchar *wds;
  uint wds_size;
  if (!storage.stream("WordDocument", &wds, &wds_size)) {
    return false;
  }

  const uchar *pt = get_piece_table(storage);
  if (pt == NULL) {
    return false;
  }

  fputc(0xFF, text_file);
  fputc(0xFE, text_file);
  uint lcb = *(const uint *)(pt + 1);
  uint n = (lcb - 4) / 12;
  const uchar *pcd = pt + 5;
  for (uint i = 0; i < n; ++i) {
    uint start = *(const uint *)(pcd + i * 4);
    uint end = *(const uint *)(pcd + i * 4 + 4);
    if (start >= end) return false;
    uint fcCompressed = *(const uint *)(pcd + (n + 1) * 4 + i * 8 + 2);
    bool fCompressed = (fcCompressed >> 30) & 1;
    if (fCompressed) {
      uint fc = (fcCompressed & ~(1U << 30)) / 2;
      uint cb = end - start;
      for (uint i = 0; i < cb; ++i) {
        if (fc + i < wds_size) {
          fputc(wds[fc + i], text_file);
          fputc(0, text_file);
        }
      }
    } else {
      uint fc = fcCompressed;
      uint cb = (end - start) * 2;
      for (uint i = 0; i < cb; i += 2) {
        if (fc + i + 1 < wds_size) {
          uchar low = wds[fc + i], high = wds[fc + i + 1];
          ushort ch = ((ushort)high << 8) + low;
          if (ch >= 0x000A) {
            fputc(wds[fc + i], text_file);
            fputc(wds[fc + i + 1], text_file);
          }
        }
      }
    }
  }
  return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x10e6(%rip), %rsi      # 0x3044
leaq -0x38(%rbp), %rdx
leaq -0x30(%rbp), %rcx
callq 0x1a16
testb %al, %al
je 0x20b1
movq %r14, %rdi
callq 0x1e77
testq %rax, %rax
je 0x20b1
movq %rax, %r15
movl $0xff, %edi
movq %rbx, %rsi
callq 0x1150
movl $0xfe, %edi
movq %rbx, %rsi
callq 0x1150
movl 0x1(%r15), %eax
addl $-0x4, %eax
movb $0x1, %cl
movl %ecx, -0x2c(%rbp)
cmpl $0xc, %eax
jb 0x20b8
addq $0x5, %r15
movl %eax, %eax
movl $0xaaaaaaab, %ecx       # imm = 0xAAAAAAAB
imulq %rax, %rcx
shrq $0x23, %rcx
movq %rcx, -0x48(%rbp)
leaq (%r15,%rcx,4), %rax
addq $0x6, %rax
movq %rax, -0x40(%rbp)
xorl %ecx, %ecx
movq %r15, -0x50(%rbp)
movl 0x4(%r15,%rcx,4), %r14d
subl (%r15,%rcx,4), %r14d
seta %al
jbe 0x20b1
movl %eax, -0x2c(%rbp)
movq -0x40(%rbp), %rax
movq %rcx, -0x58(%rbp)
movl (%rax,%rcx,8), %r12d
btl $0x1e, %r12d
jb 0x2065
addl %r14d, %r14d
je 0x209a
movl %r14d, %r14d
xorl %r15d, %r15d
leal 0x1(%r12), %eax
cmpl -0x30(%rbp), %eax
jae 0x2056
movq -0x38(%rbp), %rcx
movl %r12d, %edx
movzbl (%rcx,%rdx), %edi
movl %eax, %r13d
movzbl (%rcx,%r13), %eax
shll $0x8, %eax
orl %edi, %eax
cmpl $0xa, %eax
jb 0x2056
movq %rbx, %rsi
callq 0x1150
movq -0x38(%rbp), %rax
movzbl (%rax,%r13), %edi
movq %rbx, %rsi
callq 0x1150
addq $0x2, %r15
addl $0x2, %r12d
cmpq %r14, %r15
jb 0x2016
jmp 0x209a
shrl %r12d
andl $0x5fffffff, %r12d      # imm = 0x5FFFFFFF
cmpl -0x30(%rbp), %r12d
jae 0x2092
movq -0x38(%rbp), %rax
movl %r12d, %ecx
movzbl (%rax,%rcx), %edi
movq %rbx, %rsi
callq 0x1150
xorl %edi, %edi
movq %rbx, %rsi
callq 0x1150
incl %r12d
decq %r14
jne 0x206f
movq -0x58(%rbp), %rcx
incq %rcx
cmpq -0x48(%rbp), %rcx
movq -0x50(%rbp), %r15
jne 0x1fdf
jmp 0x20b8
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop