// File: /workspace/github_cpp_makefile_star2_O2/tsfn[P]doc2txt/src/doc_text.cpp
// Function: doc_text(Storage const&)
uchar *doc_text(const Storage &storage) {
  const uchar *wds;
  uint wds_size;
  if (!storage.stream("WordDocument", &wds, &wds_size)) {
    return NULL;
  }

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

  uint buf_size = 0; // 文本块的大小

  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 NULL;
    uint fcCompressed = *(const uint *)(pcd + (n + 1) * 4 + i * 8 + 2);
    bool fCompressed = (fcCompressed >> 30) & 1;
    if (fCompressed) {
      uint cb = end - start;
      buf_size += cb * 2;
    } else {
      uint cb = (end - start) * 2;
      buf_size += cb;
    }
  }

  uchar *buf = (uchar *)malloc(buf_size + 4);
  uint ptr = 0;
  if (buf == NULL) return NULL;

  for (uint i = 0; i < n; ++i) {
    uint start = *(const uint *)(pcd + i * 4);
    uint end = *(const uint *)(pcd + i * 4 + 4);
    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) {
          buf[ptr++] = wds[fc + i];
          buf[ptr++] = 0;
        }
      }
    } else {
      uint fc = fcCompressed;
      uint cb = (end - start) * 2;
      for (uint i = 0; i < cb; ++i) {
        buf[ptr++] = wds[fc + i];
      }
    }
  }
  return buf;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rdi, %rbx
leaq 0x12db(%rip), %rsi      # 0x3044
leaq -0x30(%rbp), %rdx
leaq -0x24(%rbp), %rcx
callq 0x1998
testb %al, %al
je 0x1e67
movq %rbx, %rdi
callq 0x1e79
xorl %ecx, %ecx
testq %rax, %rax
je 0x1e69
movq %rax, %rsi
movl 0x1(%rax), %eax
addl $-0x4, %eax
pushq $0xc
popq %rdi
xorl %edx, %edx
divl %edi
movl %eax, %ebx
movq %rsi, %r14
addq $0x5, %r14
xorl %eax, %eax
xorl %edi, %edi
cmpq %rax, %rbx
je 0x1dc9
movl 0x9(%rsi,%rax,4), %edx
subl 0x5(%rsi,%rax,4), %edx
jbe 0x1e69
leal (%rdi,%rdx,2), %edi
incq %rax
jmp 0x1dae
addl $0x4, %edi
callq 0x1180
movq %rax, %rcx
xorl %eax, %eax
testq %rcx, %rcx
je 0x1e67
leaq (%r14,%rbx,4), %rdx
addq $0x6, %rdx
movl -0x24(%rbp), %esi
movq -0x30(%rbp), %rdi
xorl %r8d, %r8d
cmpq %rbx, %r8
je 0x1e69
movl (%r14,%r8,4), %r11d
movl 0x4(%r14,%r8,4), %r9d
movl (%rdx,%r8,8), %r10d
btl $0x1e, %r10d
jb 0x1e2b
subl %r11d, %r9d
addl %r9d, %r9d
subq $0x1, %r9
jb 0x1e62
movl %r10d, %r11d
movb (%rdi,%r11), %r11b
movl %eax, %r15d
incl %eax
movb %r11b, (%rcx,%r15)
incl %r10d
jmp 0x1e10
shrl %r10d
andl $0x5fffffff, %r10d      # imm = 0x5FFFFFFF
subl %r11d, %r9d
subq $0x1, %r9
jb 0x1e62
cmpl %esi, %r10d
jae 0x1e5d
movl %r10d, %r11d
movb (%rdi,%r11), %r11b
leal 0x1(%rax), %r15d
movl %eax, %r12d
movb %r11b, (%rcx,%r12)
addl $0x2, %eax
movb $0x0, (%rcx,%r15)
incl %r10d
jmp 0x1e38
incq %r8
jmp 0x1df1
xorl %ecx, %ecx
movq %rcx, %rax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq