// File: karel-brinda[P]ococo/ext/htslib/cram/cram_decode.c
// Function: cram_decode_TD
int cram_decode_TD(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;

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

    /* Decode */
    cp += safe_itf8_get(cp, endp, &blk_size);
    if (!blk_size) {
	h->nTL = 0;
	h->TL = NULL;
	cram_free_block(b);
	return cp - op;
    }

    if (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
    h->nTL = nTL;
    if (!(h->TL = calloc(h->nTL, sizeof(unsigned char *)))) {
        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;
    
    return sz;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
movl $0x0, -0x3c(%rbp)
xorl %esi, %esi
movl %esi, %edi
callq 0x45b10
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x241b2
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2457f
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq -0x3c(%rbp), %rdx
callq 0x24590
movl %eax, %ecx
movq -0x10(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x10(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0x2420c
movq -0x20(%rbp), %rax
movl $0x0, 0x58(%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x60(%rax)
movq -0x38(%rbp), %rdi
callq 0x470c0
movq -0x10(%rbp), %rax
movq -0x28(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x4(%rbp)
jmp 0x2457f
cmpl $0x0, -0x3c(%rbp)
jl 0x24226
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
subq %rcx, %rax
movslq -0x3c(%rbp), %rcx
cmpq %rcx, %rax
jge 0x2423b
movq -0x38(%rbp), %rdi
callq 0x470c0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2457f
jmp 0x2423d
jmp 0x2423f
jmp 0x24241
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x38(%rbp), %rcx
movq 0x30(%rcx), %rcx
movslq -0x3c(%rbp), %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
ja 0x24311
movq -0x38(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x242aa
movq -0x38(%rbp), %rax
movq 0x28(%rax), %xmm0
movaps 0x56864(%rip), %xmm1    # 0x7aae0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x56868(%rip), %xmm1    # 0x7aaf0
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x57839(%rip), %xmm1    # 0x7bad8
mulsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
jmp 0x242b9
movsd 0x5781e(%rip), %xmm0    # 0x7bad0
movsd %xmm0, -0x50(%rbp)
jmp 0x242b9
movsd -0x50(%rbp), %xmm0
movsd 0x567fa(%rip), %xmm2    # 0x7aac0
movaps %xmm0, %xmm1
subsd %xmm2, %xmm1
cvttsd2si %xmm1, %rax
cvttsd2si %xmm0, %rcx
movq %rcx, %rdx
sarq $0x3f, %rdx
andq %rdx, %rax
orq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rsi
callq 0x4990
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x20(%rax)
jmp 0x24241
jmp 0x24313
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x38(%rbp), %rax
addq 0x30(%rax), %rdi
movq -0x10(%rbp), %rsi
movslq -0x3c(%rbp), %rdx
callq 0x4460
movslq -0x3c(%rbp), %rcx
movq -0x38(%rbp), %rax
addq 0x30(%rax), %rcx
movq %rcx, 0x30(%rax)
movl -0x3c(%rbp), %ecx
movq -0x10(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq -0x28(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x48(%rbp)
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x38(%rbp), %rcx
movq 0x30(%rcx), %rcx
subq $0x1, %rcx
cmpb $0x0, (%rax,%rcx)
je 0x24473
jmp 0x2437f
jmp 0x24381
jmp 0x24383
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x38(%rbp), %rcx
movq 0x30(%rcx), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
ja 0x24450
movq -0x38(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x243e9
movq -0x38(%rbp), %rax
movq 0x28(%rax), %xmm0
movaps 0x56725(%rip), %xmm1    # 0x7aae0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x56729(%rip), %xmm1    # 0x7aaf0
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x576fa(%rip), %xmm1    # 0x7bad8
mulsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
jmp 0x243f8
movsd 0x576df(%rip), %xmm0    # 0x7bad0
movsd %xmm0, -0x58(%rbp)
jmp 0x243f8
movsd -0x58(%rbp), %xmm0
movsd 0x566bb(%rip), %xmm2    # 0x7aac0
movaps %xmm0, %xmm1
subsd %xmm2, %xmm1
cvttsd2si %xmm1, %rax
cvttsd2si %xmm0, %rcx
movq %rcx, %rdx
sarq $0x3f, %rdx
andq %rdx, %rax
orq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rsi
callq 0x4990
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x20(%rax)
jmp 0x24383
jmp 0x24452
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x38(%rbp), %rdx
movq 0x30(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x30(%rdx)
movb $0x0, (%rax,%rcx)
jmp 0x24473
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x40(%rbp)
movslq -0x44(%rbp), %rax
movq -0x38(%rbp), %rcx
cmpq 0x30(%rcx), %rax
jae 0x244ca
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
movq -0x30(%rbp), %rax
movslq -0x44(%rbp), %rcx
cmpb $0x0, (%rax,%rcx)
je 0x244bd
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x244a4
jmp 0x244bf
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x2448d
movl -0x40(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x58(%rax)
movq -0x20(%rbp), %rax
movslq 0x58(%rax), %rdi
movl $0x8, %esi
callq 0x4370
movq -0x20(%rbp), %rcx
movq %rax, 0x60(%rcx)
cmpq $0x0, %rax
jne 0x24506
movq -0x38(%rbp), %rdi
callq 0x470c0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2457f
movl $0x0, -0x44(%rbp)
movl $0x0, -0x40(%rbp)
movslq -0x44(%rbp), %rax
movq -0x38(%rbp), %rcx
cmpq 0x30(%rcx), %rax
jae 0x2456d
movq -0x30(%rbp), %rdx
movslq -0x44(%rbp), %rax
addq %rax, %rdx
movq -0x20(%rbp), %rax
movq 0x60(%rax), %rax
movl -0x40(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x40(%rbp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x30(%rbp), %rax
movslq -0x44(%rbp), %rcx
cmpb $0x0, (%rax,%rcx)
je 0x24560
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x24547
jmp 0x24562
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x24514
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x50(%rax)
movl -0x48(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)