// File: /workspace/github_cpp_makefile_star2_O2/karel-brinda[P]ococo/ext/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 0x3bc89
movl %eax, %ebx
pushq $0x1
popq %rdi
movl $0x240, %esi            # imm = 0x240
callq 0x4370
testq %rax, %rax
je 0x3d6a4
movq %rax, %rbp
movl $0x3, (%rax)
leaq -0x2e0(%rip), %rax      # 0x3d1c6
movq %rax, 0x10(%rbp)
movslq 0xc(%rsp), %rdi
movl %edi, 0x30(%rbp)
movq %rdi, (%rsp)
shlq $0x4, %rdi
callq 0x46f0
movq %rax, 0x38(%rbp)
testq %rax, %rax
je 0x3d69c
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 0x3d515
testl %eax, %eax
je 0x3d515
movq %r12, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x3bc89
movl %eax, %ebx
incq %r14
addq $0x10, %r15
jmp 0x3d4e9
movl %r14d, 0x8(%rsp)
testl %eax, %eax
je 0x3d680
leaq 0x8(%rsp), %rbx
movq %r12, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x3bc89
movq (%rsp), %rcx
cmpl %ecx, (%rbx)
jne 0x3d680
testl %ecx, %ecx
je 0x3d65e
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 0x3d59b
movq %r12, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x3bc89
testl %eax, %eax
je 0x3d59b
movl (%rbx), %ecx
cmpl %ecx, %r14d
cmovlel %ecx, %r14d
incq %rbp
movl %eax, %eax
addq %rax, %r12
addq $0x10, %rbx
jmp 0x3d56d
movq (%rsp), %rsi
cmpq %rsi, %rbp
jl 0x3d6ce
subq 0x20(%rsp), %r12
cmpq 0x30(%rsp), %r12
jne 0x3d6ce
cmpl %esi, %r14d
jge 0x3d6ce
leaq 0x12e(%rip), %rcx       # 0x3d6f6
pushq $0x10
popq %rdx
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x49f0
xorl %eax, %eax
pushq $-0x1
popq %rcx
xorl %edx, %edx
movq 0x10(%rsp), %rbp
movq 0x28(%rsp), %r9
cmpq %r15, %rax
je 0x3d618
incl %ecx
movq %rax, %rsi
shlq $0x4, %rsi
movl 0xc(%rbx,%rsi), %edi
movl %edi, %r8d
subl %edx, %r8d
jle 0x3d60f
subl $0x1, %r8d
jb 0x3d60d
addl %ecx, %ecx
jmp 0x3d603
movl %edi, %edx
movl %ecx, 0x8(%rbx,%rsi)
incq %rax
jmp 0x3d5e9
negq %r15
movq %rbx, %rax
addq $0xc, %rax
xorl %ecx, %ecx
xorl %edx, %edx
xorl %esi, %esi
cmpq %rcx, %r15
je 0x3d646
movl (%rax), %edi
cmpl %esi, %edi
jle 0x3d63a
movl -0x4(%rax), %edx
addl %ecx, %edx
movl %edi, %esi
movl %edx, -0x8(%rax)
decq %rcx
addq $0x10, %rax
jmp 0x3d628
leal -0x3(%r9), %eax
cmpl $0x1, %eax
ja 0x3d667
cmpl $0x0, 0xc(%rbx)
je 0x3d6a8
leaq -0x44f(%rip), %rax      # 0x3d20d
jmp 0x3d6b8
leaq -0x483(%rip), %rax      # 0x3d1e2
jmp 0x3d6b8
cmpl $0x5, %r9d
je 0x3d6f1
cmpl $0x0, 0xc(%rbx)
je 0x3d6b1
leaq -0x33f(%rip), %rax      # 0x3d33f
jmp 0x3d6b8
movq 0x19949(%rip), %rax     # 0x56fd0
movq (%rax), %rcx
leaq 0x9ff2(%rip), %rdi      # 0x47683
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
callq 0x4a20
movq %rbp, %rdi
callq 0x4600
xorl %ebp, %ebp
jmp 0x3d6bc
leaq -0x4c9(%rip), %rax      # 0x3d1e6
jmp 0x3d6b8
leaq -0x399(%rip), %rax      # 0x3d31f
movq %rax, 0x18(%rbp)
movq %rbp, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x198fb(%rip), %rax     # 0x56fd0
movq (%rax), %rcx
leaq 0x9fa4(%rip), %rdi      # 0x47683
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
callq 0x4a20
movq 0x10(%rsp), %rdi
jmp 0x3d69f
callq 0x42f0