// File: /workspace/github_c_makefile_star2_O1/samtools[P]htslib/cram/cram_decode.c
// Function: cram_decode_TD
int cram_decode_TD(cram_fd *fd, char *cp, const char *endp,
                   cram_block_compression_hdr *h) {
    char *op = cp;
    unsigned char *dat;
    cram_block *b;
    int32_t blk_size = 0;
    int nTL, i, sz, err = 0;

    if (!(b = cram_new_block(0, 0)))
        return -1;

    if (h->TD_blk || h->TL) {
        hts_log_warning("More than one TD block found in compression header");
        cram_free_block(h->TD_blk);
        free(h->TL);
        h->TD_blk = NULL;
        h->TL = NULL;
    }

    /* Decode */
    blk_size = fd->vv.varint_get32(&cp, endp, &err);
    if (!blk_size) {
        h->nTL = 0;
        cram_free_block(b);
        return cp - op;
    }

    if (err || blk_size < 0 || endp - cp < blk_size) {
        cram_free_block(b);
        return -1;
    }

    BLOCK_APPEND(b, cp, blk_size);
    cp += blk_size;
    sz = cp - op;
    // Force nul termination if missing
    if (BLOCK_DATA(b)[BLOCK_SIZE(b)-1])
        BLOCK_APPEND_CHAR(b, '\0');

    /* Set up TL lookup table */
    dat = BLOCK_DATA(b);

    // Count
    for (nTL = i = 0; i < BLOCK_SIZE(b); i++) {
        nTL++;
        while (dat[i])
            i++;
    }

    // Copy
    if (!(h->TL = calloc(nTL, sizeof(*h->TL)))) {
        cram_free_block(b);
        return -1;
    }
    for (nTL = i = 0; i < BLOCK_SIZE(b); i++) {
        h->TL[nTL++] = &dat[i];
        while (dat[i])
            i++;
    }
    h->TD_blk = b;
    h->nTL = nTL;

    return sz;

 block_err:
    cram_free_block(b);
    return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %r14
movq %rdx, %r13
movq %rsi, %rbx
movq %rdi, %r12
movq %rsi, (%rsp)
movl $0x0, 0xc(%rsp)
xorl %edi, %edi
xorl %esi, %esi
callq 0x4ee42
movq %rax, %r15
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testq %r15, %r15
je 0x386d5
cmpq $0x0, 0x50(%r14)
jne 0x384da
cmpq $0x0, 0x60(%r14)
je 0x38510
leaq 0x63c06(%rip), %rsi     # 0x9c0e7
leaq 0x63c0e(%rip), %rdx     # 0x9c0f6
xorl %ebp, %ebp
movl $0x3, %edi
xorl %eax, %eax
callq 0x14378
movq 0x50(%r14), %rdi
callq 0x4f82d
movq 0x60(%r14), %rdi
callq 0x45c0
movq %rbp, 0x50(%r14)
movq %rbp, 0x60(%r14)
movq %rsp, %rdi
leaq 0xc(%rsp), %rdx
movq %r13, %rsi
callq *0x89a8(%r12)
movq %rax, %r12
testl %r12d, %r12d
je 0x386c0
js 0x386b1
cmpl $0x0, 0xc(%rsp)
jne 0x386b1
movq (%rsp), %rbp
subq %rbp, %r13
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
cmpq %r12, %r13
jl 0x386b1
movq 0x30(%r15), %r13
addq %r12, %r13
movq 0x28(%r15), %rax
cmpq %r13, %rax
ja 0x3859f
leaq 0x320(%rax), %rcx
shrq $0x2, %rcx
addq %rcx, %rax
addq $0x320, %rax            # imm = 0x320
cmpq %r13, %rax
cmovaq %rax, %r13
movq 0x20(%r15), %rdi
movq %r13, %rsi
callq 0x4840
testq %rax, %rax
je 0x386b1
movq %r13, 0x28(%r15)
movq %rax, 0x20(%r15)
testq %r12, %r12
je 0x385bb
movq 0x20(%r15), %rdi
addq 0x30(%r15), %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x43c0
addq %r12, 0x30(%r15)
addq (%rsp), %r12
movq %r12, (%rsp)
movq 0x20(%r15), %rdi
movq 0x30(%r15), %r13
cmpb $0x0, -0x1(%rdi,%r13)
je 0x38627
incq %r13
movq 0x28(%r15), %rax
cmpq %r13, %rax
ja 0x38613
leaq 0x320(%rax), %rcx
shrq $0x2, %rcx
addq %rcx, %rax
addq $0x320, %rax            # imm = 0x320
cmpq %r13, %rax
cmovaq %rax, %r13
movq %r13, %rsi
callq 0x4840
testq %rax, %rax
je 0x386b1
movq %r13, 0x28(%r15)
movq %rax, 0x20(%r15)
movq 0x20(%r15), %rax
movq 0x30(%r15), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x30(%r15)
movb $0x0, (%rax,%rcx)
movq %rbx, 0x10(%rsp)
movq 0x20(%r15), %r13
movq 0x30(%r15), %rbx
xorl %edi, %edi
testq %rbx, %rbx
je 0x3865a
xorl %eax, %eax
movslq %edi, %rcx
addq %r13, %rcx
incl %edi
cmpb $0x0, (%rcx)
leaq 0x1(%rcx), %rcx
jne 0x38643
incl %eax
movslq %edi, %rcx
cmpq %rcx, %rbx
ja 0x3863d
movl %eax, %edi
movl $0x8, %esi
callq 0x4300
movq %rax, 0x60(%r14)
testq %rax, %rax
je 0x386b1
xorl %eax, %eax
testq %rbx, %rbx
je 0x3869f
xorl %edx, %edx
xorl %ecx, %ecx
addq %r13, %rdx
movq 0x60(%r14), %rsi
movq %rdx, (%rsi,%rax,8)
movslq %ecx, %rdx
addq %r13, %rdx
incl %ecx
cmpb $0x0, (%rdx)
leaq 0x1(%rdx), %rdx
jne 0x38689
incq %rax
movslq %ecx, %rdx
cmpq %rdx, %rbx
ja 0x38678
subl 0x10(%rsp), %r12d
movq %r15, 0x50(%r14)
movl %eax, 0x58(%r14)
movl %r12d, %eax
jmp 0x386d5
movq %r15, %rdi
callq 0x4f82d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x386d5
movl $0x0, 0x58(%r14)
movq %r15, %rdi
callq 0x4f82d
movl (%rsp), %eax
subl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq