// File: /workspace/github_cpp_makefile_star2_O1/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 0x2273(%rip), %rsi      # 0x4044
leaq -0x30(%rbp), %rdx
leaq -0x24(%rbp), %rcx
callq 0x1a94
testb %al, %al
je 0x1f3e
movq %rbx, %rdi
callq 0x1f4d
testq %rax, %rax
je 0x1f3e
movq %rax, %rbx
movl 0x1(%rax), %r15d
addl $-0x4, %r15d
movl $0xaaaaaaab, %r14d      # imm = 0xAAAAAAAB
imulq %r15, %r14
shrq $0x23, %r14
movl $0x4, %edi
cmpl $0xc, %r15d
setae %cl
jb 0x1e80
cmpl $0x1, %r14d
movl %r14d, %eax
adcl $0x0, %eax
movl 0x9(%rbx), %esi
movl 0x5(%rbx), %r8d
movl %esi, %edi
subl %r8d, %edi
addl %edi, %edi
xorl %edx, %edx
cmpl %r8d, %esi
cmovbel %edx, %edi
jbe 0x1e7d
movl %r14d, %ecx
movl $0x1, %r8d
movq %r8, %rsi
cmpq %r8, %rax
je 0x1e77
movl 0x9(%rbx,%rsi,4), %r9d
movl 0x5(%rbx,%rsi,4), %r10d
movl %r9d, %r8d
subl %r10d, %r8d
addl %r8d, %r8d
cmpl %r10d, %r9d
cmovbel %edx, %r8d
addl %r8d, %edi
leaq 0x1(%rsi), %r8
cmpl %r10d, %r9d
ja 0x1e49
cmpq %rcx, %rsi
setb %cl
addl $0x4, %edi
testb %cl, %cl
jne 0x1f3e
callq 0x1160
testq %rax, %rax
je 0x1f3e
cmpl $0xc, %r15d
jb 0x1f40
addq $0x5, %rbx
movl %r14d, %ecx
leaq (%rbx,%rcx,4), %rcx
addq $0x6, %rcx
movl -0x24(%rbp), %edx
movq -0x30(%rbp), %rsi
cmpl $0x1, %r14d
adcl $0x0, %r14d
xorl %edi, %edi
xorl %r8d, %r8d
movl (%rbx,%rdi,4), %r11d
movl 0x4(%rbx,%rdi,4), %r10d
movl (%rcx,%rdi,8), %r9d
btl $0x1e, %r9d
jb 0x1efd
subl %r11d, %r10d
addl %r10d, %r10d
je 0x1f34
movl %r10d, %r10d
movl %r9d, %r11d
movb (%rsi,%r11), %r11b
movl %r8d, %r15d
incl %r8d
movb %r11b, (%rax,%r15)
incl %r9d
decq %r10
jne 0x1ee2
jmp 0x1f34
subl %r11d, %r10d
je 0x1f34
shrl %r9d
andl $0x5fffffff, %r9d       # imm = 0x5FFFFFFF
cmpl %edx, %r9d
jae 0x1f2c
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 0x1f0c
incq %rdi
cmpq %r14, %rdi
jne 0x1ec3
jmp 0x1f40
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq