// File: /workspace/github_cpp_makefile_star2_O3/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 0x1303(%rip), %rsi      # 0x3044
leaq -0x30(%rbp), %rdx
leaq -0x24(%rbp), %rcx
callq 0x1a16
testb %al, %al
je 0x1d79
movq %rbx, %rdi
callq 0x1e77
testq %rax, %rax
je 0x1d79
movq %rax, %rcx
movl 0x1(%rax), %edx
addl $-0x4, %edx
cmpl $0xc, %edx
jae 0x1d88
movl $0x4, %edi
callq 0x1160
jmp 0x1d7b
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl $0xaaaaaaab, %ebx       # imm = 0xAAAAAAAB
imulq %rdx, %rbx
shrq $0x23, %rbx
movq %rcx, %r14
addq $0x5, %r14
cmpl $0x1, %ebx
movl %ebx, %esi
adcl $0x0, %esi
xorl %eax, %eax
xorl %r8d, %r8d
xorl %edi, %edi
movl 0x9(%rcx,%r8,4), %r9d
subl 0x5(%rcx,%r8,4), %r9d
jbe 0x1d7b
leal (%rdi,%r9,2), %edi
incq %r8
cmpq %r8, %rsi
jne 0x1dab
cmpl $0xc, %edx
setb %r15b
addl $0x4, %edi
callq 0x1160
testq %rax, %rax
sete %cl
orb %r15b, %cl
jne 0x1d7b
movl %ebx, %ecx
leaq (%r14,%rcx,4), %rcx
addq $0x6, %rcx
movl -0x24(%rbp), %edx
movq -0x30(%rbp), %rsi
cmpl $0x1, %ebx
adcl $0x0, %ebx
xorl %edi, %edi
xorl %r8d, %r8d
movl (%r14,%rdi,4), %r11d
movl 0x4(%r14,%rdi,4), %r10d
movl (%rcx,%rdi,8), %r9d
btl $0x1e, %r9d
jb 0x1e33
subl %r11d, %r10d
addl %r10d, %r10d
je 0x1e6a
movl %r10d, %r10d
movl %r9d, %r11d
movb (%rsi,%r11), %r11b
movl %r8d, %r15d
incl %r8d
movb %r11b, (%rax,%r15)
incl %r9d
decq %r10
jne 0x1e18
jmp 0x1e6a
subl %r11d, %r10d
je 0x1e6a
shrl %r9d
andl $0x5fffffff, %r9d       # imm = 0x5FFFFFFF
cmpl %edx, %r9d
jae 0x1e62
movl %r9d, %r11d
movb (%rsi,%r11), %r11b
leal 0x1(%r8), %r15d
movl %r8d, %r12d
movb %r11b, (%rax,%r12)
addl $0x2, %r8d
movb $0x0, (%rax,%r15)
incl %r9d
decq %r10
jne 0x1e42
incq %rdi
cmpq %rbx, %rdi
jne 0x1df9
jmp 0x1d7b