// File: /workspace/github_c_makefile_star2_O1/icebert[P]pblat/htslib/cram/cram_codecs.c
// Function: cram_huffman_decode_init
cram_codec *cram_huffman_decode_init(char *data, int size,
				     enum cram_external_type option,
				     int version) {
    int32_t ncodes = 0, i, j;
    char *cp = data, *data_end = &data[size];
    cram_codec *h;
    cram_huffman_code *codes;
    int32_t val, last_len, max_len = 0;
    int l;

    cp += safe_itf8_get(cp, data_end, &ncodes);
    h = calloc(1, sizeof(*h));
    if (!h)
	return NULL;

    h->codec  = E_HUFFMAN;
    h->free   = cram_huffman_decode_free;

    h->huffman.ncodes = ncodes;
    codes = h->huffman.codes = malloc(ncodes * sizeof(*codes));
    if (!codes) {
	free(h);
	return NULL;
    }

    /* Read symbols and bit-lengths */
    for (i = 0, l = 1; i < ncodes && l > 0; i++, cp += l) {
	l = safe_itf8_get(cp, data_end, &codes[i].symbol);
    }

    if (l < 1) {
	fprintf(stderr, "Malformed huffman header stream\n");
	free(h);
	return NULL;
    }
    cp += safe_itf8_get(cp, data_end, &i);
    if (i != ncodes) {
	fprintf(stderr, "Malformed huffman header stream\n");
	free(h);
	return NULL;
    }

    if (ncodes == 0) {
	/* NULL huffman stream.  Ensure it returns an error if
           anything tries to use it. */
        h->decode = cram_huffman_decode_null;
        return h;
    }

    for (i = 0, l = 1; i < ncodes; i++, cp += l) {
        l = safe_itf8_get(cp, data_end, &codes[i].len);
        if (l < 1)
            break;
	if (max_len < codes[i].len)
	    max_len = codes[i].len;
    }
    if (l < 1 || cp - data != size || max_len >= ncodes) {
	fprintf(stderr, "Malformed huffman header stream\n");
	free(h);
	return NULL;
    }

    /* Sort by bit length and then by symbol value */
    qsort(codes, ncodes, sizeof(*codes), code_sort);

    /* Assign canonical codes */
    val = -1, last_len = 0;
    for (i = 0; i < ncodes; i++) {
	val++;
	if (codes[i].len > last_len) {
	    while (codes[i].len > last_len) {
		val <<= 1;
		last_len++;
	    }
	}
	codes[i].code = val;
    }

    /*
     * Compute the next starting point, offset by the i'th value.
     * For example if codes 10, 11, 12, 13 are 30, 31, 32, 33 then
     * codes[10..13].p = 30 - 10.
     */
    last_len = 0;
    for (i = j = 0; i < ncodes; i++) {
	if (codes[i].len > last_len) {
	    j = codes[i].code - i;
	    last_len = codes[i].len;
	}
	codes[i].p = j;
    }

//    puts("==HUFF LEN==");
//    for (i = 0; i <= last_len+1; i++) {
//	printf("len %d=%d prefix %d\n", i, h->huffman.lengths[i], h->huffman.prefix[i]); 
//    }
//    puts("===HUFFMAN CODES===");
//    for (i = 0; i < ncodes; i++) {
//	int j;
//	printf("%d: %d %d %d ", i, codes[i].symbol, codes[i].len, codes[i].code);
//	j = codes[i].len;
//	while (j) {
//	    putchar(codes[i].code & (1 << --j) ? '1' : '0');
//	}
//	printf(" %d\n", codes[i].code);
//    }

    if (option == E_BYTE || option == E_BYTE_ARRAY) {
	if (h->huffman.codes[0].len == 0)
	    h->decode = cram_huffman_decode_char0;
	else
	    h->decode = cram_huffman_decode_char;
    } else if (option == E_BYTE_ARRAY_BLOCK) {
	abort();
    } else {
	if (h->huffman.codes[0].len == 0)
	    h->decode = cram_huffman_decode_int0;
	else
	    h->decode = cram_huffman_decode_int;
    }

    return (cram_codec *)h;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, %r13d
movl %esi, %ebp
movq %rdi, %r12
cmpl $0x4, %esi
jg 0x89865
testl %ebp, %ebp
jle 0x8985b
movzbl (%r12), %eax
shrl $0x4, %eax
leaq 0xf25a(%rip), %rcx      # 0x98ab0
cmpl %ebp, (%rcx,%rax,4)
jle 0x89865
xorl %ebx, %ebx
xorl %r15d, %r15d
jmp 0x8990d
movzbl (%r12), %ebx
movl $0x1, %r15d
testb %bl, %bl
jns 0x8990d
movzbl 0x1(%r12), %eax
cmpb $-0x41, %bl
ja 0x89893
andl $0x3f, %ebx
shll $0x8, %ebx
orl %eax, %ebx
movl $0x2, %r15d
jmp 0x8990d
cmpb $-0x21, %bl
ja 0x898b3
shll $0x8, %eax
movzbl 0x2(%r12), %ecx
andl $0x1f, %ebx
shll $0x10, %ebx
orl %eax, %ebx
orl %ecx, %ebx
movl $0x3, %r15d
jmp 0x8990d
cmpb $-0x11, %bl
ja 0x898de
shll $0x10, %eax
movzbl 0x2(%r12), %ecx
shll $0x8, %ecx
movzbl 0x3(%r12), %edx
andl $0xf, %ebx
shll $0x18, %ebx
orl %eax, %ebx
orl %ecx, %ebx
orl %edx, %ebx
movl $0x4, %r15d
jmp 0x8990d
shll $0x1c, %ebx
shll $0x14, %eax
orl %ebx, %eax
movzbl 0x2(%r12), %ecx
shll $0xc, %ecx
orl %eax, %ecx
movzbl 0x3(%r12), %eax
shll $0x4, %eax
orl %ecx, %eax
movzbl 0x4(%r12), %ebx
andl $0xf, %ebx
orl %eax, %ebx
movl $0x5, %r15d
movl $0x1, %edi
movl $0x240, %esi            # imm = 0x240
callq 0x6470
testq %rax, %rax
je 0x89aac
movq %rax, %r14
movq %r12, (%rsp)
movl $0x3, (%rax)
leaq -0x436(%rip), %rax      # 0x89503
movq %rax, 0x10(%r14)
movl %ebx, 0x30(%r14)
movslq %ebx, %rdi
shlq $0x4, %rdi
callq 0x69d0
movq %rax, 0x38(%r14)
testq %rax, %rax
je 0x89aa4
movq %rax, %r12
movq %r13, 0x8(%rsp)
movslq %ebp, %rcx
movq (%rsp), %rax
movq %rcx, 0x10(%rsp)
addq %rax, %rcx
addq %rax, %r15
movl %ebx, %r13d
testl %ebx, %ebx
jle 0x89ab4
movl $0x1, %edx
leaq 0xf125(%rip), %rsi      # 0x98ab0
movq %r12, %rdi
movq %rcx, %r8
subq %r15, %r8
cmpq $0x4, %r8
jg 0x899c1
movb $0x1, %r10b
cmpq %rcx, %r15
jae 0x899b3
movzbl (%r15), %r9d
shrl $0x4, %r9d
movslq (%rsi,%r9,4), %r9
cmpq %r9, %r8
jge 0x899c1
xorl %r9d, %r9d
xorl %r8d, %r8d
xorl %r11d, %r11d
jmp 0x89a87
movzbl (%r15), %r9d
movb $0x1, %r8b
testb %r9b, %r9b
js 0x899d8
movl $0x1, %r11d
jmp 0x89a84
movzbl 0x1(%r15), %r10d
cmpb $-0x41, %r9b
ja 0x899f9
andl $0x3f, %r9d
shll $0x8, %r9d
orl %r10d, %r9d
movl $0x2, %r11d
jmp 0x89a84
cmpb $-0x21, %r9b
ja 0x89a1e
shll $0x8, %r10d
movzbl 0x2(%r15), %r11d
andl $0x1f, %r9d
shll $0x10, %r9d
orl %r10d, %r9d
orl %r11d, %r9d
movl $0x3, %r11d
jmp 0x89a84
cmpb $-0x11, %r9b
ja 0x89a4f
shll $0x10, %r10d
movzbl 0x2(%r15), %r11d
shll $0x8, %r11d
movzbl 0x3(%r15), %ebp
andl $0xf, %r9d
shll $0x18, %r9d
orl %r10d, %r9d
orl %r11d, %r9d
orl %ebp, %r9d
movl $0x4, %r11d
jmp 0x89a84
shll $0x1c, %r9d
shll $0x14, %r10d
orl %r9d, %r10d
movzbl 0x2(%r15), %r9d
shll $0xc, %r9d
orl %r10d, %r9d
movzbl 0x3(%r15), %r10d
shll $0x4, %r10d
orl %r9d, %r10d
movzbl 0x4(%r15), %r9d
andl $0xf, %r9d
orl %r10d, %r9d
movl $0x5, %r11d
xorl %r10d, %r10d
movl %r9d, (%rdi)
addq %r11, %r15
cmpq %r13, %rdx
jae 0x89ab7
addq $0x10, %rdi
incq %rdx
testb %r8b, %r8b
jne 0x8998e
jmp 0x89ab7
movq %r14, %rdi
callq 0x68a0
xorl %r14d, %r14d
jmp 0x89de9
xorl %r10d, %r10d
testb %r10b, %r10b
jne 0x89b35
movq %rcx, %rdx
subq %r15, %rdx
cmpq $0x4, %rdx
jg 0x89aed
cmpq %rcx, %r15
jae 0x89ae4
movzbl (%r15), %esi
shrl $0x4, %esi
leaq 0xefd5(%rip), %rdi      # 0x98ab0
movslq (%rdi,%rsi,4), %rsi
cmpq %rsi, %rdx
jge 0x89aed
xorl %edx, %edx
xorl %esi, %esi
jmp 0x89b96
movzbl (%r15), %edx
movl $0x1, %esi
testb %dl, %dl
jns 0x89b96
movzbl 0x1(%r15), %esi
cmpb $-0x41, %dl
ja 0x89b17
andl $0x3f, %edx
shll $0x8, %edx
orl %esi, %edx
movl $0x2, %esi
jmp 0x89b96
cmpb $-0x21, %dl
ja 0x89b42
shll $0x8, %esi
movzbl 0x2(%r15), %edi
andl $0x1f, %edx
shll $0x10, %edx
orl %esi, %edx
orl %edi, %edx
movl $0x3, %esi
jmp 0x89b96
movq %r14, %rdi
callq 0x8676
jmp 0x89aac
cmpb $-0x11, %dl
ja 0x89b6b
shll $0x10, %esi
movzbl 0x2(%r15), %edi
shll $0x8, %edi
movzbl 0x3(%r15), %r8d
andl $0xf, %edx
shll $0x18, %edx
orl %esi, %edx
orl %edi, %edx
orl %r8d, %edx
movl $0x4, %esi
jmp 0x89b96
shll $0x1c, %edx
shll $0x14, %esi
orl %edx, %esi
movzbl 0x2(%r15), %edx
shll $0xc, %edx
orl %esi, %edx
movzbl 0x3(%r15), %esi
shll $0x4, %esi
orl %edx, %esi
movzbl 0x4(%r15), %edx
andl $0xf, %edx
orl %esi, %edx
movl $0x5, %esi
cmpl %ebx, %edx
jne 0x89dfb
testl %ebx, %ebx
je 0x89ce7
addq %rsi, %r15
testl %ebx, %ebx
jle 0x89cf3
movq %r13, %rsi
shlq $0x4, %rsi
xorl %edi, %edi
leaq 0xeeef(%rip), %r8       # 0x98ab0
xorl %edx, %edx
movq %rcx, %rbp
subq %r15, %rbp
cmpq $0x4, %rbp
jg 0x89bf5
movb $0x1, %r11b
xorl %r9d, %r9d
cmpq %rcx, %r15
jae 0x89c09
movzbl (%r15), %r10d
shrl $0x4, %r10d
movslq (%r8,%r10,4), %rax
movl $0x0, %r10d
cmpq %rax, %rbp
jl 0x89cbe
movzbl (%r15), %r9d
testb %r9b, %r9b
js 0x89c11
movl $0x1, %r10d
jmp 0x89cbb
xorl %r10d, %r10d
jmp 0x89cbe
movzbl 0x1(%r15), %r10d
cmpb $-0x41, %r9b
ja 0x89c32
andl $0x3f, %r9d
shll $0x8, %r9d
orl %r10d, %r9d
movl $0x2, %r10d
jmp 0x89cbb
cmpb $-0x21, %r9b
ja 0x89c57
shll $0x8, %r10d
movzbl 0x2(%r15), %eax
andl $0x1f, %r9d
shll $0x10, %r9d
orl %r10d, %r9d
orl %eax, %r9d
movl $0x3, %r10d
jmp 0x89cbb
cmpb $-0x11, %r9b
ja 0x89c87
shll $0x10, %r10d
movzbl 0x2(%r15), %eax
shll $0x8, %eax
movzbl 0x3(%r15), %r11d
andl $0xf, %r9d
shll $0x18, %r9d
orl %r10d, %r9d
orl %eax, %r9d
orl %r11d, %r9d
movl $0x4, %r10d
jmp 0x89cbb
shll $0x1c, %r9d
shll $0x14, %r10d
orl %r9d, %r10d
movzbl 0x2(%r15), %eax
shll $0xc, %eax
orl %r10d, %eax
movzbl 0x3(%r15), %r10d
shll $0x4, %r10d
orl %eax, %r10d
movzbl 0x4(%r15), %r9d
andl $0xf, %r9d
orl %r10d, %r9d
movl $0x5, %r10d
xorl %r11d, %r11d
movl %r9d, 0xc(%r12,%rdi)
testb %r11b, %r11b
jne 0x89cf9
movl 0xc(%r12,%rdi), %eax
cmpl %eax, %edx
cmovlel %eax, %edx
movl %r10d, %eax
addq %rax, %r15
addq $0x10, %rdi
cmpq %rdi, %rsi
jne 0x89bc3
jmp 0x89cfc
leaq -0x7ca(%rip), %rax      # 0x89524
jmp 0x89de5
movb $0x1, %cl
xorl %edx, %edx
jmp 0x89d02
xorl %r10d, %r10d
testl %r10d, %r10d
setg %cl
testb %cl, %cl
je 0x89e08
subq (%rsp), %r15
cmpq 0x10(%rsp), %r15
jne 0x89e08
cmpl %ebx, %edx
jge 0x89e08
leaq 0xf2(%rip), %rcx        # 0x89e1a
movl $0x10, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x6e40
testl %ebx, %ebx
jle 0x89d71
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
xorl %edx, %edx
incl %eax
movq %rcx, %rsi
shlq $0x4, %rsi
movl 0xc(%r12,%rsi), %edi
movl %edi, %r8d
subl %edx, %r8d
jle 0x89d64
addl %eax, %eax
decl %r8d
jne 0x89d5b
movl %edi, %edx
movl %eax, 0x8(%r12,%rsi)
incq %rcx
cmpq %r13, %rcx
jne 0x89d45
testl %ebx, %ebx
jle 0x89da1
movq %r12, %rax
addq $0xc, %rax
negq %r13
xorl %ecx, %ecx
xorl %esi, %esi
xorl %edx, %edx
movl (%rax), %edi
cmpl %esi, %edi
jle 0x89d92
movl -0x4(%rax), %edx
addl %ecx, %edx
movl %edi, %esi
movl %edx, -0x8(%rax)
decq %rcx
addq $0x10, %rax
cmpq %rcx, %r13
jne 0x89d85
movq 0x8(%rsp), %rcx
leal -0x3(%rcx), %eax
cmpl $0x1, %eax
ja 0x89dbf
cmpl $0x0, 0xc(%r12)
je 0x89dd5
leaq -0x873(%rip), %rax      # 0x8954a
jmp 0x89de5
cmpl $0x5, %ecx
je 0x89e15
cmpl $0x0, 0xc(%r12)
je 0x89dde
leaq -0x70b(%rip), %rax      # 0x896c8
jmp 0x89de5
leaq -0x8b2(%rip), %rax      # 0x8952a
jmp 0x89de5
leaq -0x73b(%rip), %rax      # 0x896aa
movq %rax, 0x18(%r14)
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x8624
jmp 0x89aac
movq %r14, %rdi
callq 0x864d
jmp 0x89aac
callq 0x63c0