// File: /workspace/github_cpp_makefile_star2_O3/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 $0x18, %rsp
movl %edx, %r12d
movl %esi, %ebp
movq %rdi, %r13
cmpl $0x5, %esi
jge 0x4a961
testl %ebp, %ebp
jle 0x4a957
movzbl (%r13), %ecx
movl %ecx, %eax
shrl $0x4, %eax
leaq 0x7f0e(%rip), %rdx      # 0x52860
cmpl %ebp, (%rdx,%rax,4)
jle 0x4a965
xorl %ebx, %ebx
xorl %r15d, %r15d
jmp 0x4aa04
movb (%r13), %cl
movzbl %cl, %ebx
movl $0x1, %r15d
testb %bl, %bl
jns 0x4aa04
movzbl 0x1(%r13), %eax
cmpb $-0x41, %cl
ja 0x4a990
andl $0x3f, %ebx
shll $0x8, %ebx
orl %eax, %ebx
movl $0x2, %r15d
jmp 0x4aa04
cmpb $-0x21, %cl
ja 0x4a9af
shll $0x8, %eax
movzbl 0x2(%r13), %ecx
andl $0x1f, %ebx
shll $0x10, %ebx
orl %eax, %ebx
orl %ecx, %ebx
movl $0x3, %r15d
jmp 0x4aa04
cmpb $-0x11, %cl
ja 0x4a9d8
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 0x4aa04
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 0x4360
testq %rax, %rax
je 0x4abc1
movq %rax, %r14
movq %r13, 0x8(%rsp)
movq %r12, 0x10(%rsp)
movl $0x3, (%rax)
leaq -0x4cf(%rip), %rax      # 0x4a567
movq %rax, 0x10(%r14)
movl %ebx, 0x30(%r14)
movslq %ebx, %r13
movq %r13, %rdi
shlq $0x4, %rdi
callq 0x4700
movq %rax, 0x38(%r14)
testq %rax, %rax
je 0x4abb9
movq %rax, %r12
movslq %ebp, %rax
movq 0x8(%rsp), %rdx
leaq (%rdx,%rax), %rcx
addq %rdx, %r15
testl %ebx, %ebx
jle 0x4ab86
xorl %edx, %edx
leaq 0x7de3(%rip), %rsi      # 0x52860
movq %r12, %rdi
movq %rcx, %r8
subq %r15, %r8
cmpq $0x5, %r8
jge 0x4aaae
cmpq %rcx, %r15
jae 0x4ac43
movzbl (%r15), %r10d
movl %r10d, %r9d
shrl $0x4, %r9d
movslq (%rsi,%r9,4), %r9
cmpq %r9, %r8
jge 0x4aab1
jmp 0x4ac43
movb (%r15), %r10b
movzbl %r10b, %r8d
movl $0x1, %r9d
testb %r8b, %r8b
jns 0x4ab70
movzbl 0x1(%r15), %r9d
cmpb $-0x41, %r10b
ja 0x4aae5
andl $0x3f, %r8d
shll $0x8, %r8d
orl %r9d, %r8d
movl $0x2, %r9d
jmp 0x4ab70
cmpb $-0x21, %r10b
ja 0x4ab0a
shll $0x8, %r9d
movzbl 0x2(%r15), %r10d
andl $0x1f, %r8d
shll $0x10, %r8d
orl %r9d, %r8d
orl %r10d, %r8d
movl $0x3, %r9d
jmp 0x4ab70
cmpb $-0x11, %r10b
ja 0x4ab3b
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 0x4ab70
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 0x4aa80
movq %rcx, %rdx
subq %r15, %rdx
cmpq $0x5, %rdx
jge 0x4abc9
cmpq %rcx, %r15
jae 0x4abb0
movzbl (%r15), %edi
movl %edi, %esi
shrl $0x4, %esi
leaq 0x7cb9(%rip), %r8       # 0x52860
movslq (%r8,%rsi,4), %rsi
cmpq %rsi, %rdx
jge 0x4abcc
xorl %edx, %edx
xorl %esi, %esi
jmp 0x4ac7b
movq %r14, %rdi
callq 0x4610
xorl %r14d, %r14d
jmp 0x4aea5
movb (%r15), %dil
movzbl %dil, %edx
movl $0x1, %esi
testb %dl, %dl
jns 0x4ac7b
movzbl 0x1(%r15), %esi
cmpb $-0x41, %dil
ja 0x4abfa
andl $0x3f, %edx
shll $0x8, %edx
orl %esi, %edx
movl $0x2, %esi
jmp 0x4ac7b
cmpb $-0x21, %dil
ja 0x4ac19
shll $0x8, %esi
movzbl 0x2(%r15), %edi
andl $0x1f, %edx
shll $0x10, %edx
orl %esi, %edx
orl %edi, %edx
movl $0x3, %esi
jmp 0x4ac7b
cmpb $-0x11, %dil
ja 0x4ac50
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 0x4ac7b
movq %r14, %rsi
callq 0x61a3
jmp 0x4abc1
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 0x4aeb7
testl %ebx, %ebx
je 0x4adb9
addq %rsi, %r15
movl %ebx, %r13d
testl %ebx, %ebx
jle 0x4adc5
movq %r13, %rsi
shlq $0x4, %rsi
xorl %edi, %edi
leaq 0x7bb7(%rip), %r8       # 0x52860
xorl %edx, %edx
movq %rcx, %r9
subq %r15, %r9
cmpq $0x5, %r9
jge 0x4acd9
cmpq %rcx, %r15
jae 0x4aec4
movzbl (%r15), %r11d
movl %r11d, %r10d
shrl $0x4, %r10d
movslq (%r8,%r10,4), %r10
cmpq %r10, %r9
jge 0x4acdc
jmp 0x4aec4
movb (%r15), %r11b
movzbl %r11b, %r9d
movl $0x1, %r10d
testb %r9b, %r9b
jns 0x4ad9b
movzbl 0x1(%r15), %r10d
cmpb $-0x41, %r11b
ja 0x4ad10
andl $0x3f, %r9d
shll $0x8, %r9d
orl %r10d, %r9d
movl $0x2, %r10d
jmp 0x4ad9b
cmpb $-0x21, %r11b
ja 0x4ad35
shll $0x8, %r10d
movzbl 0x2(%r15), %r11d
andl $0x1f, %r9d
shll $0x10, %r9d
orl %r10d, %r9d
orl %r11d, %r9d
movl $0x3, %r10d
jmp 0x4ad9b
cmpb $-0x11, %r11b
ja 0x4ad66
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 0x4ad9b
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 0x4acab
jmp 0x4adc7
leaq -0x838(%rip), %rax      # 0x4a588
jmp 0x4aea1
xorl %edx, %edx
subq 0x8(%rsp), %r15
cmpq %rax, %r15
jne 0x4aecd
cmpl %ebx, %edx
jge 0x4aecd
leaq 0xfb(%rip), %rcx        # 0x4aedf
movl $0x10, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x4a10
testl %ebx, %ebx
jle 0x4ae5d
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 0x4ae20
addl %eax, %eax
decl %r8d
jne 0x4ae17
movl %edi, %edx
movl %eax, 0x8(%r12,%rsi)
incq %rcx
cmpq %r13, %rcx
jne 0x4ae01
testl %ebx, %ebx
jle 0x4ae5d
movq %r12, %rax
addq $0xc, %rax
negq %r13
xorl %ecx, %ecx
xorl %esi, %esi
xorl %edx, %edx
movl (%rax), %edi
cmpl %esi, %edi
jle 0x4ae4e
movl -0x4(%rax), %edx
addl %ecx, %edx
movl %edi, %esi
movl %edx, -0x8(%rax)
decq %rcx
addq $0x10, %rax
cmpq %rcx, %r13
jne 0x4ae41
movq 0x10(%rsp), %rcx
leal -0x3(%rcx), %eax
cmpl $0x1, %eax
ja 0x4ae7b
cmpl $0x0, 0xc(%r12)
je 0x4ae91
leaq -0x8c6(%rip), %rax      # 0x4a5b3
jmp 0x4aea1
cmpl $0x5, %ecx
je 0x4aeda
cmpl $0x0, 0xc(%r12)
je 0x4ae9a
leaq -0x6a4(%rip), %rax      # 0x4a7eb
jmp 0x4aea1
leaq -0x90a(%rip), %rax      # 0x4a58e
jmp 0x4aea1
leaq -0x7b7(%rip), %rax      # 0x4a6ea
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 0x6151
jmp 0x4abc1
movl $0x0, 0xc(%r12,%rdi)
movq %r14, %rdi
callq 0x617a
jmp 0x4abc1
callq 0x42e0