// File: /workspace/github_c_makefile_star2_O3/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, %r12d
movl %esi, %ebp
movq %rdi, %r13
cmpl $0x5, %esi
jge 0x89405
testl %ebp, %ebp
jle 0x893fb
movzbl (%r13), %ecx
movl %ecx, %eax
shrl $0x4, %eax
leaq 0xf7aa(%rip), %rdx      # 0x98ba0
cmpl %ebp, (%rdx,%rax,4)
jle 0x89409
xorl %ebx, %ebx
xorl %r15d, %r15d
jmp 0x894a8
movb (%r13), %cl
movzbl %cl, %ebx
movl $0x1, %r15d
testb %bl, %bl
jns 0x894a8
movzbl 0x1(%r13), %eax
cmpb $-0x41, %cl
ja 0x89434
andl $0x3f, %ebx
shll $0x8, %ebx
orl %eax, %ebx
movl $0x2, %r15d
jmp 0x894a8
cmpb $-0x21, %cl
ja 0x89453
shll $0x8, %eax
movzbl 0x2(%r13), %ecx
andl $0x1f, %ebx
shll $0x10, %ebx
orl %eax, %ebx
orl %ecx, %ebx
movl $0x3, %r15d
jmp 0x894a8
cmpb $-0x11, %cl
ja 0x8947c
shll $0x10, %eax
movzbl 0x2(%r13), %ecx
shll $0x8, %ecx
movzbl 0x3(%r13), %edx
andl $0xf, %ebx
shll $0x18, %ebx
orl %eax, %ebx
orl %ecx, %ebx
orl %edx, %ebx
movl $0x4, %r15d
jmp 0x894a8
shll $0x1c, %ebx
shll $0x14, %eax
orl %ebx, %eax
movzbl 0x2(%r13), %ecx
shll $0xc, %ecx
orl %eax, %ecx
movzbl 0x3(%r13), %eax
shll $0x4, %eax
orl %ecx, %eax
movzbl 0x4(%r13), %ebx
andl $0xf, %ebx
orl %eax, %ebx
movl $0x5, %r15d
movl $0x1, %edi
movl $0x240, %esi            # imm = 0x240
callq 0x6470
testq %rax, %rax
je 0x89665
movq %rax, %r14
movq %r13, 0x8(%rsp)
movq %r12, 0x10(%rsp)
movl $0x3, (%rax)
leaq -0x4ca(%rip), %rax      # 0x89010
movq %rax, 0x10(%r14)
movl %ebx, 0x30(%r14)
movslq %ebx, %r13
movq %r13, %rdi
shlq $0x4, %rdi
callq 0x69d0
movq %rax, 0x38(%r14)
testq %rax, %rax
je 0x8965d
movq %rax, %r12
movslq %ebp, %rax
movq 0x8(%rsp), %rdx
leaq (%rdx,%rax), %rcx
addq %rdx, %r15
testl %ebx, %ebx
jle 0x8962a
xorl %edx, %edx
leaq 0xf67f(%rip), %rsi      # 0x98ba0
movq %r12, %rdi
movq %rcx, %r8
subq %r15, %r8
cmpq $0x5, %r8
jge 0x89552
cmpq %rcx, %r15
jae 0x896e7
movzbl (%r15), %r10d
movl %r10d, %r9d
shrl $0x4, %r9d
movslq (%rsi,%r9,4), %r9
cmpq %r9, %r8
jge 0x89555
jmp 0x896e7
movb (%r15), %r10b
movzbl %r10b, %r8d
movl $0x1, %r9d
testb %r8b, %r8b
jns 0x89614
movzbl 0x1(%r15), %r9d
cmpb $-0x41, %r10b
ja 0x89589
andl $0x3f, %r8d
shll $0x8, %r8d
orl %r9d, %r8d
movl $0x2, %r9d
jmp 0x89614
cmpb $-0x21, %r10b
ja 0x895ae
shll $0x8, %r9d
movzbl 0x2(%r15), %r10d
andl $0x1f, %r8d
shll $0x10, %r8d
orl %r9d, %r8d
orl %r10d, %r8d
movl $0x3, %r9d
jmp 0x89614
cmpb $-0x11, %r10b
ja 0x895df
shll $0x10, %r9d
movzbl 0x2(%r15), %r10d
shll $0x8, %r10d
movzbl 0x3(%r15), %r11d
andl $0xf, %r8d
shll $0x18, %r8d
orl %r9d, %r8d
orl %r10d, %r8d
orl %r11d, %r8d
movl $0x4, %r9d
jmp 0x89614
shll $0x1c, %r8d
shll $0x14, %r9d
orl %r8d, %r9d
movzbl 0x2(%r15), %r8d
shll $0xc, %r8d
orl %r9d, %r8d
movzbl 0x3(%r15), %r9d
shll $0x4, %r9d
orl %r8d, %r9d
movzbl 0x4(%r15), %r8d
andl $0xf, %r8d
orl %r9d, %r8d
movl $0x5, %r9d
movl %r8d, (%rdi)
incq %rdx
addq %r9, %r15
addq $0x10, %rdi
cmpq %r13, %rdx
jl 0x89524
movq %rcx, %rdx
subq %r15, %rdx
cmpq $0x5, %rdx
jge 0x8966d
cmpq %rcx, %r15
jae 0x89654
movzbl (%r15), %edi
movl %edi, %esi
shrl $0x4, %esi
leaq 0xf555(%rip), %r8       # 0x98ba0
movslq (%r8,%rsi,4), %rsi
cmpq %rsi, %rdx
jge 0x89670
xorl %edx, %edx
xorl %esi, %esi
jmp 0x8971f
movq %r14, %rdi
callq 0x68a0
xorl %r14d, %r14d
jmp 0x89949
movb (%r15), %dil
movzbl %dil, %edx
movl $0x1, %esi
testb %dl, %dl
jns 0x8971f
movzbl 0x1(%r15), %esi
cmpb $-0x41, %dil
ja 0x8969e
andl $0x3f, %edx
shll $0x8, %edx
orl %esi, %edx
movl $0x2, %esi
jmp 0x8971f
cmpb $-0x21, %dil
ja 0x896bd
shll $0x8, %esi
movzbl 0x2(%r15), %edi
andl $0x1f, %edx
shll $0x10, %edx
orl %esi, %edx
orl %edi, %edx
movl $0x3, %esi
jmp 0x8971f
cmpb $-0x11, %dil
ja 0x896f4
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 0x8971f
movq %r14, %rsi
callq 0x859a
jmp 0x89665
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 0x8995b
testl %ebx, %ebx
je 0x8985d
addq %rsi, %r15
movl %ebx, %r13d
testl %ebx, %ebx
jle 0x89869
movq %r13, %rsi
shlq $0x4, %rsi
xorl %edi, %edi
leaq 0xf453(%rip), %r8       # 0x98ba0
xorl %edx, %edx
movq %rcx, %r9
subq %r15, %r9
cmpq $0x5, %r9
jge 0x8977d
cmpq %rcx, %r15
jae 0x89968
movzbl (%r15), %r11d
movl %r11d, %r10d
shrl $0x4, %r10d
movslq (%r8,%r10,4), %r10
cmpq %r10, %r9
jge 0x89780
jmp 0x89968
movb (%r15), %r11b
movzbl %r11b, %r9d
movl $0x1, %r10d
testb %r9b, %r9b
jns 0x8983f
movzbl 0x1(%r15), %r10d
cmpb $-0x41, %r11b
ja 0x897b4
andl $0x3f, %r9d
shll $0x8, %r9d
orl %r10d, %r9d
movl $0x2, %r10d
jmp 0x8983f
cmpb $-0x21, %r11b
ja 0x897d9
shll $0x8, %r10d
movzbl 0x2(%r15), %r11d
andl $0x1f, %r9d
shll $0x10, %r9d
orl %r10d, %r9d
orl %r11d, %r9d
movl $0x3, %r10d
jmp 0x8983f
cmpb $-0x11, %r11b
ja 0x8980a
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, %r10d
jmp 0x8983f
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, %r10d
cmpl %r9d, %edx
cmovlel %r9d, %edx
movl %r9d, 0xc(%r12,%rdi)
addq %r10, %r15
addq $0x10, %rdi
cmpq %rdi, %rsi
jne 0x8974f
jmp 0x8986b
leaq -0x833(%rip), %rax      # 0x89031
jmp 0x89945
xorl %edx, %edx
subq 0x8(%rsp), %r15
cmpq %rax, %r15
jne 0x89971
cmpl %ebx, %edx
jge 0x89971
leaq 0xfb(%rip), %rcx        # 0x89983
movl $0x10, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x6e40
testl %ebx, %ebx
jle 0x89901
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 0x898c4
addl %eax, %eax
decl %r8d
jne 0x898bb
movl %edi, %edx
movl %eax, 0x8(%r12,%rsi)
incq %rcx
cmpq %r13, %rcx
jne 0x898a5
testl %ebx, %ebx
jle 0x89901
movq %r12, %rax
addq $0xc, %rax
negq %r13
xorl %ecx, %ecx
xorl %esi, %esi
xorl %edx, %edx
movl (%rax), %edi
cmpl %esi, %edi
jle 0x898f2
movl -0x4(%rax), %edx
addl %ecx, %edx
movl %edi, %esi
movl %edx, -0x8(%rax)
decq %rcx
addq $0x10, %rax
cmpq %rcx, %r13
jne 0x898e5
movq 0x10(%rsp), %rcx
leal -0x3(%rcx), %eax
cmpl $0x1, %eax
ja 0x8991f
cmpl $0x0, 0xc(%r12)
je 0x89935
leaq -0x8c6(%rip), %rax      # 0x89057
jmp 0x89945
cmpl $0x5, %ecx
je 0x8997e
cmpl $0x0, 0xc(%r12)
je 0x8993e
leaq -0x6a4(%rip), %rax      # 0x8928f
jmp 0x89945
leaq -0x905(%rip), %rax      # 0x89037
jmp 0x89945
leaq -0x7b7(%rip), %rax      # 0x8918e
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 0x8548
jmp 0x89665
movl $0x0, 0xc(%r12,%rdi)
movq %r14, %rdi
callq 0x8571
jmp 0x89665
callq 0x63c0