// File: /workspace/github_cpp_makefile_star2_O1/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, %r13d
movl %esi, %ebp
movq %rdi, %r12
cmpl $0x4, %esi
jg 0x4b9dd
testl %ebp, %ebp
jle 0x4b9d3
movzbl (%r12), %eax
shrl $0x4, %eax
leaq 0x8d32(%rip), %rcx      # 0x54700
cmpl %ebp, (%rcx,%rax,4)
jle 0x4b9dd
xorl %ebx, %ebx
xorl %r15d, %r15d
jmp 0x4ba85
movzbl (%r12), %ebx
movl $0x1, %r15d
testb %bl, %bl
jns 0x4ba85
movzbl 0x1(%r12), %eax
cmpb $-0x41, %bl
ja 0x4ba0b
andl $0x3f, %ebx
shll $0x8, %ebx
orl %eax, %ebx
movl $0x2, %r15d
jmp 0x4ba85
cmpb $-0x21, %bl
ja 0x4ba2b
shll $0x8, %eax
movzbl 0x2(%r12), %ecx
andl $0x1f, %ebx
shll $0x10, %ebx
orl %eax, %ebx
orl %ecx, %ebx
movl $0x3, %r15d
jmp 0x4ba85
cmpb $-0x11, %bl
ja 0x4ba56
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 0x4ba85
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 0x4360
testq %rax, %rax
je 0x4bc24
movq %rax, %r14
movq %r12, (%rsp)
movl $0x3, (%rax)
leaq -0x43b(%rip), %rax      # 0x4b676
movq %rax, 0x10(%r14)
movl %ebx, 0x30(%r14)
movslq %ebx, %rdi
shlq $0x4, %rdi
callq 0x4700
movq %rax, 0x38(%r14)
testq %rax, %rax
je 0x4bc1c
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 0x4bc2c
movl $0x1, %edx
leaq 0x8bfd(%rip), %rsi      # 0x54700
movq %r12, %rdi
movq %rcx, %r8
subq %r15, %r8
cmpq $0x4, %r8
jg 0x4bb39
movb $0x1, %r10b
cmpq %rcx, %r15
jae 0x4bb2b
movzbl (%r15), %r9d
shrl $0x4, %r9d
movslq (%rsi,%r9,4), %r9
cmpq %r9, %r8
jge 0x4bb39
xorl %r9d, %r9d
xorl %r8d, %r8d
xorl %r11d, %r11d
jmp 0x4bbff
movzbl (%r15), %r9d
movb $0x1, %r8b
testb %r9b, %r9b
js 0x4bb50
movl $0x1, %r11d
jmp 0x4bbfc
movzbl 0x1(%r15), %r10d
cmpb $-0x41, %r9b
ja 0x4bb71
andl $0x3f, %r9d
shll $0x8, %r9d
orl %r10d, %r9d
movl $0x2, %r11d
jmp 0x4bbfc
cmpb $-0x21, %r9b
ja 0x4bb96
shll $0x8, %r10d
movzbl 0x2(%r15), %r11d
andl $0x1f, %r9d
shll $0x10, %r9d
orl %r10d, %r9d
orl %r11d, %r9d
movl $0x3, %r11d
jmp 0x4bbfc
cmpb $-0x11, %r9b
ja 0x4bbc7
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 0x4bbfc
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 0x4bc2f
addq $0x10, %rdi
incq %rdx
testb %r8b, %r8b
jne 0x4bb06
jmp 0x4bc2f
movq %r14, %rdi
callq 0x4610
xorl %r14d, %r14d
jmp 0x4bf61
xorl %r10d, %r10d
testb %r10b, %r10b
jne 0x4bcad
movq %rcx, %rdx
subq %r15, %rdx
cmpq $0x4, %rdx
jg 0x4bc65
cmpq %rcx, %r15
jae 0x4bc5c
movzbl (%r15), %esi
shrl $0x4, %esi
leaq 0x8aad(%rip), %rdi      # 0x54700
movslq (%rdi,%rsi,4), %rsi
cmpq %rsi, %rdx
jge 0x4bc65
xorl %edx, %edx
xorl %esi, %esi
jmp 0x4bd0e
movzbl (%r15), %edx
movl $0x1, %esi
testb %dl, %dl
jns 0x4bd0e
movzbl 0x1(%r15), %esi
cmpb $-0x41, %dl
ja 0x4bc8f
andl $0x3f, %edx
shll $0x8, %edx
orl %esi, %edx
movl $0x2, %esi
jmp 0x4bd0e
cmpb $-0x21, %dl
ja 0x4bcba
shll $0x8, %esi
movzbl 0x2(%r15), %edi
andl $0x1f, %edx
shll $0x10, %edx
orl %esi, %edx
orl %edi, %edx
movl $0x3, %esi
jmp 0x4bd0e
movq %r14, %rdi
callq 0x6245
jmp 0x4bc24
cmpb $-0x11, %dl
ja 0x4bce3
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 0x4bd0e
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 0x4bf73
testl %ebx, %ebx
je 0x4be5f
addq %rsi, %r15
testl %ebx, %ebx
jle 0x4be6b
movq %r13, %rsi
shlq $0x4, %rsi
xorl %edi, %edi
leaq 0x89c7(%rip), %r8       # 0x54700
xorl %edx, %edx
movq %rcx, %rbp
subq %r15, %rbp
cmpq $0x4, %rbp
jg 0x4bd6d
movb $0x1, %r11b
xorl %r9d, %r9d
cmpq %rcx, %r15
jae 0x4bd81
movzbl (%r15), %r10d
shrl $0x4, %r10d
movslq (%r8,%r10,4), %rax
movl $0x0, %r10d
cmpq %rax, %rbp
jl 0x4be36
movzbl (%r15), %r9d
testb %r9b, %r9b
js 0x4bd89
movl $0x1, %r10d
jmp 0x4be33
xorl %r10d, %r10d
jmp 0x4be36
movzbl 0x1(%r15), %r10d
cmpb $-0x41, %r9b
ja 0x4bdaa
andl $0x3f, %r9d
shll $0x8, %r9d
orl %r10d, %r9d
movl $0x2, %r10d
jmp 0x4be33
cmpb $-0x21, %r9b
ja 0x4bdcf
shll $0x8, %r10d
movzbl 0x2(%r15), %eax
andl $0x1f, %r9d
shll $0x10, %r9d
orl %r10d, %r9d
orl %eax, %r9d
movl $0x3, %r10d
jmp 0x4be33
cmpb $-0x11, %r9b
ja 0x4bdff
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 0x4be33
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 0x4be71
movl 0xc(%r12,%rdi), %eax
cmpl %eax, %edx
cmovlel %eax, %edx
movl %r10d, %eax
addq %rax, %r15
addq $0x10, %rdi
cmpq %rdi, %rsi
jne 0x4bd3b
jmp 0x4be74
leaq -0x7cf(%rip), %rax      # 0x4b697
jmp 0x4bf5d
movb $0x1, %cl
xorl %edx, %edx
jmp 0x4be7a
xorl %r10d, %r10d
testl %r10d, %r10d
setg %cl
testb %cl, %cl
je 0x4bf80
subq (%rsp), %r15
cmpq 0x10(%rsp), %r15
jne 0x4bf80
cmpl %ebx, %edx
jge 0x4bf80
leaq 0xf2(%rip), %rcx        # 0x4bf92
movl $0x10, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x4a10
testl %ebx, %ebx
jle 0x4bee9
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 0x4bedc
addl %eax, %eax
decl %r8d
jne 0x4bed3
movl %edi, %edx
movl %eax, 0x8(%r12,%rsi)
incq %rcx
cmpq %r13, %rcx
jne 0x4bebd
testl %ebx, %ebx
jle 0x4bf19
movq %r12, %rax
addq $0xc, %rax
negq %r13
xorl %ecx, %ecx
xorl %esi, %esi
xorl %edx, %edx
movl (%rax), %edi
cmpl %esi, %edi
jle 0x4bf0a
movl -0x4(%rax), %edx
addl %ecx, %edx
movl %edi, %esi
movl %edx, -0x8(%rax)
decq %rcx
addq $0x10, %rax
cmpq %rcx, %r13
jne 0x4befd
movq 0x8(%rsp), %rcx
leal -0x3(%rcx), %eax
cmpl $0x1, %eax
ja 0x4bf37
cmpl $0x0, 0xc(%r12)
je 0x4bf4d
leaq -0x873(%rip), %rax      # 0x4b6c2
jmp 0x4bf5d
cmpl $0x5, %ecx
je 0x4bf8d
cmpl $0x0, 0xc(%r12)
je 0x4bf56
leaq -0x70b(%rip), %rax      # 0x4b840
jmp 0x4bf5d
leaq -0x8b7(%rip), %rax      # 0x4b69d
jmp 0x4bf5d
leaq -0x73b(%rip), %rax      # 0x4b822
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 0x61f3
jmp 0x4bc24
movq %r14, %rdi
callq 0x621c
jmp 0x4bc24
callq 0x42e0