// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movl %edx, %r14d
leaq 0xc(%rsp), %rdx
andl $0x0, (%rdx)
movslq %esi, %r12
leaq (%rdi,%r12), %r13
movq %rdi, 0x20(%rsp)
movq %r13, %rsi
callq 0x768c1
movl %eax, %ebx
pushq $0x1
popq %rdi
movl $0x240, %esi            # imm = 0x240
callq 0x6470
testq %rax, %rax
je 0x782b1
movq %rax, %rbp
movl $0x3, (%rax)
leaq -0x2db(%rip), %rax      # 0x77dd8
movq %rax, 0x10(%rbp)
movslq 0xc(%rsp), %rdi
movl %edi, 0x30(%rbp)
movq %rdi, (%rsp)
shlq $0x4, %rdi
callq 0x69d0
movq %rax, 0x38(%rbp)
testq %rax, %rax
je 0x782a9
movq %rax, %r15
movq %r12, 0x30(%rsp)
movq %r14, 0x28(%rsp)
pushq $0x1
popq %rax
xorl %r14d, %r14d
movq %r15, 0x18(%rsp)
movq 0x20(%rsp), %r12
movq %r12, %rcx
movl %ebx, %r12d
addq %rcx, %r12
cmpq (%rsp), %r14
jge 0x78122
testl %eax, %eax
je 0x78122
movq %r12, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x768c1
movl %eax, %ebx
incq %r14
addq $0x10, %r15
jmp 0x780f6
movl %r14d, 0x8(%rsp)
testl %eax, %eax
je 0x7828d
leaq 0x8(%rsp), %rbx
movq %r12, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x768c1
movq (%rsp), %rcx
cmpl %ecx, (%rbx)
jne 0x7828d
testl %ecx, %ecx
je 0x7826b
movq %rbp, 0x10(%rsp)
movl %eax, %eax
addq %rax, %r12
xorl %ebp, %ebp
testl %ecx, %ecx
movl $0x0, %r15d
cmovgl %ecx, %r15d
movq 0x18(%rsp), %rax
leaq 0xc(%rax), %rbx
xorl %r14d, %r14d
cmpq %rbp, %r15
je 0x781a8
movq %r12, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x768c1
testl %eax, %eax
je 0x781a8
movl (%rbx), %ecx
cmpl %ecx, %r14d
cmovlel %ecx, %r14d
incq %rbp
movl %eax, %eax
addq %rax, %r12
addq $0x10, %rbx
jmp 0x7817a
movq (%rsp), %rsi
cmpq %rsi, %rbp
jl 0x782db
subq 0x20(%rsp), %r12
cmpq 0x30(%rsp), %r12
jne 0x782db
cmpl %esi, %r14d
jge 0x782db
leaq 0x12e(%rip), %rcx       # 0x78303
pushq $0x10
popq %rdx
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x6e40
xorl %eax, %eax
pushq $-0x1
popq %rcx
xorl %edx, %edx
movq 0x10(%rsp), %rbp
movq 0x28(%rsp), %r9
cmpq %r15, %rax
je 0x78225
incl %ecx
movq %rax, %rsi
shlq $0x4, %rsi
movl 0xc(%rbx,%rsi), %edi
movl %edi, %r8d
subl %edx, %r8d
jle 0x7821c
subl $0x1, %r8d
jb 0x7821a
addl %ecx, %ecx
jmp 0x78210
movl %edi, %edx
movl %ecx, 0x8(%rbx,%rsi)
incq %rax
jmp 0x781f6
negq %r15
movq %rbx, %rax
addq $0xc, %rax
xorl %ecx, %ecx
xorl %edx, %edx
xorl %esi, %esi
cmpq %rcx, %r15
je 0x78253
movl (%rax), %edi
cmpl %esi, %edi
jle 0x78247
movl -0x4(%rax), %edx
addl %ecx, %edx
movl %edi, %esi
movl %edx, -0x8(%rax)
decq %rcx
addq $0x10, %rax
jmp 0x78235
leal -0x3(%r9), %eax
cmpl $0x1, %eax
ja 0x78274
cmpl $0x0, 0xc(%rbx)
je 0x782b5
leaq -0x44f(%rip), %rax      # 0x77e1a
jmp 0x782c5
leaq -0x47e(%rip), %rax      # 0x77df4
jmp 0x782c5
cmpl $0x5, %r9d
je 0x782fe
cmpl $0x0, 0xc(%rbx)
je 0x782be
leaq -0x33f(%rip), %rax      # 0x77f4c
jmp 0x782c5
movq 0x3ad44(%rip), %rax     # 0xb2fd8
movq (%rax), %rcx
leaq 0x12639(%rip), %rdi     # 0x8a8d7
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
callq 0x6e70
movq %rbp, %rdi
callq 0x68a0
xorl %ebp, %ebp
jmp 0x782c9
leaq -0x4c4(%rip), %rax      # 0x77df8
jmp 0x782c5
leaq -0x399(%rip), %rax      # 0x77f2c
movq %rax, 0x18(%rbp)
movq %rbp, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x3acf6(%rip), %rax     # 0xb2fd8
movq (%rax), %rcx
leaq 0x125eb(%rip), %rdi     # 0x8a8d7
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
callq 0x6e70
movq 0x10(%rsp), %rdi
jmp 0x782ac
callq 0x63c0