// File: 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
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x2baa(%rip), %rsi      # 0x6045
leaq -0x20(%rbp), %rdx
leaq -0x24(%rbp), %rcx
callq 0x1e80
testb $0x1, %al
jne 0x34b5
movb $0x0, -0x1(%rbp)
jmp 0x36d3
movq -0x10(%rbp), %rdi
callq 0x3310
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x34d2
movb $0x0, -0x1(%rbp)
jmp 0x36d3
movq -0x18(%rbp), %rsi
movl $0xff, %edi
callq 0x1170
movq -0x18(%rbp), %rsi
movl $0xfe, %edi
callq 0x1170
movq -0x30(%rbp), %rax
movl 0x1(%rax), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
subl $0x4, %eax
movl $0xc, %ecx
xorl %edx, %edx
divl %ecx
movl %eax, -0x38(%rbp)
movq -0x30(%rbp), %rax
addq $0x5, %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x38(%rbp), %eax
jae 0x36cf
movq -0x40(%rbp), %rax
movl -0x44(%rbp), %ecx
shll $0x2, %ecx
movl %ecx, %ecx
movl (%rax,%rcx), %eax
movl %eax, -0x48(%rbp)
movq -0x40(%rbp), %rax
movl -0x44(%rbp), %ecx
shll $0x2, %ecx
movl %ecx, %ecx
movl 0x4(%rax,%rcx), %eax
movl %eax, -0x4c(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jb 0x355f
movb $0x0, -0x1(%rbp)
jmp 0x36d3
movq -0x40(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x1, %ecx
shll $0x2, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movl -0x44(%rbp), %ecx
shll $0x3, %ecx
movl %ecx, %ecx
movl 0x2(%rax,%rcx), %eax
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
shrl $0x1e, %eax
andl $0x1, %eax
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movb %al, -0x51(%rbp)
testb $0x1, -0x51(%rbp)
je 0x3600
movl -0x50(%rbp), %eax
andl $0xbfffffff, %eax       # imm = 0xBFFFFFFF
shrl %eax
movl %eax, -0x58(%rbp)
movl -0x4c(%rbp), %eax
subl -0x48(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jae 0x35fb
movl -0x58(%rbp), %eax
addl -0x60(%rbp), %eax
cmpl -0x24(%rbp), %eax
jae 0x35ee
movq -0x20(%rbp), %rax
movl -0x58(%rbp), %ecx
addl -0x60(%rbp), %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %edi
movq -0x18(%rbp), %rsi
callq 0x1170
movq -0x18(%rbp), %rsi
xorl %edi, %edi
callq 0x1170
jmp 0x35f0
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x35b7
jmp 0x36bf
movl -0x50(%rbp), %eax
movl %eax, -0x64(%rbp)
movl -0x4c(%rbp), %eax
subl -0x48(%rbp), %eax
shll %eax
movl %eax, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x68(%rbp), %eax
jae 0x36bd
movl -0x64(%rbp), %eax
addl -0x6c(%rbp), %eax
addl $0x1, %eax
cmpl -0x24(%rbp), %eax
jae 0x36ad
movq -0x20(%rbp), %rax
movl -0x64(%rbp), %ecx
addl -0x6c(%rbp), %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %al
movb %al, -0x6d(%rbp)
movq -0x20(%rbp), %rax
movl -0x64(%rbp), %ecx
addl -0x6c(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %al
movb %al, -0x6e(%rbp)
movzbl -0x6e(%rbp), %eax
movzwl %ax, %eax
shll $0x8, %eax
movzbl -0x6d(%rbp), %ecx
addl %ecx, %eax
movw %ax, -0x70(%rbp)
movzwl -0x70(%rbp), %eax
cmpl $0xa, %eax
jl 0x36ab
movq -0x20(%rbp), %rax
movl -0x64(%rbp), %ecx
addl -0x6c(%rbp), %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %edi
movq -0x18(%rbp), %rsi
callq 0x1170
movq -0x20(%rbp), %rax
movl -0x64(%rbp), %ecx
addl -0x6c(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %edi
movq -0x18(%rbp), %rsi
callq 0x1170
jmp 0x36ad
jmp 0x36af
movl -0x6c(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x6c(%rbp)
jmp 0x3618
jmp 0x36bf
jmp 0x36c1
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x351d
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x70, %rsp
popq %rbp
retq
nop