// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %rsp
movq %rsi, -0x38(%rbp)
movq %rdi, %r14
leaq 0x2006(%rip), %rsi      # 0x4044
leaq -0x40(%rbp), %rdx
leaq -0x2c(%rbp), %rcx
callq 0x1a94
testb %al, %al
je 0x21b1
movq %r14, %rdi
callq 0x1f4d
testq %rax, %rax
je 0x21b1
movq %rax, %r14
movl $0xff, %edi
movq -0x38(%rbp), %rbx
movq %rbx, %rsi
callq 0x1150
movl $0xfe, %edi
movq %rbx, %rsi
callq 0x1150
movl 0x1(%r14), %ecx
addl $-0x4, %ecx
cmpl $0xc, %ecx
setb %al
jb 0x21b3
addq $0x5, %r14
movl %ecx, %eax
movl $0xaaaaaaab, %ecx       # imm = 0xAAAAAAAB
imulq %rax, %rcx
shrq $0x23, %rcx
leaq (%r14,%rcx,4), %rax
addq $0x6, %rax
movq %rax, -0x58(%rbp)
cmpl $0x2, %ecx
movl $0x1, %eax
movq %rcx, -0x48(%rbp)
cmovael %ecx, %eax
movq %rax, -0x50(%rbp)
xorl %esi, %esi
movq %r14, -0x60(%rbp)
movq -0x38(%rbp), %r15
movl (%r14,%rsi,4), %ecx
movl 0x4(%r14,%rsi,4), %r13d
movl %r13d, %eax
subl %ecx, %eax
jbe 0x21b1
movq -0x58(%rbp), %rdx
movq %rsi, -0x68(%rbp)
movl (%rdx,%rsi,8), %r12d
btl $0x1e, %r12d
jb 0x2155
addl %eax, %eax
je 0x2193
movl %eax, %r13d
xorl %r14d, %r14d
leal 0x1(%r12), %eax
cmpl -0x2c(%rbp), %eax
jae 0x2146
movq -0x40(%rbp), %rcx
movl %r12d, %edx
movzbl (%rcx,%rdx), %edi
movl %eax, %ebx
movzbl (%rcx,%rbx), %eax
shll $0x8, %eax
orl %edi, %eax
cmpl $0xa, %eax
jb 0x2146
movq %r15, %rsi
callq 0x1150
movq -0x40(%rbp), %rax
movzbl (%rax,%rbx), %edi
movq %r15, %rsi
callq 0x1150
addq $0x2, %r14
addl $0x2, %r12d
cmpq %r13, %r14
jb 0x2109
jmp 0x2193
subl %ecx, %r13d
je 0x2193
shrl %r12d
andl $0x5fffffff, %r12d      # imm = 0x5FFFFFFF
cmpl -0x2c(%rbp), %r12d
jae 0x218b
movq -0x40(%rbp), %rax
movl %r12d, %ecx
movzbl (%rax,%rcx), %edi
movq -0x38(%rbp), %rbx
movq %rbx, %rsi
callq 0x1150
xorl %edi, %edi
movq %rbx, %rsi
callq 0x1150
incl %r12d
decq %r13
jne 0x2164
movq -0x68(%rbp), %rsi
incq %rsi
cmpq -0x50(%rbp), %rsi
movq -0x60(%rbp), %r14
jne 0x20d4
cmpq -0x48(%rbp), %rsi
setae %al
jmp 0x21b3
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop