// File: 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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x54(%rbp)
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
leaq -0x20(%rbp), %rdx
callq 0x6ded0
movl %eax, %ecx
movq -0x30(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movl $0x1, %edi
movl $0x240, %esi            # imm = 0x240
callq 0x4370
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x70f62
movq $0x0, -0x8(%rbp)
jmp 0x71352
movq -0x40(%rbp), %rax
movl $0x3, (%rax)
movq -0x40(%rbp), %rax
leaq -0x567(%rip), %rcx      # 0x70a10
movq %rcx, 0x10(%rax)
movl -0x20(%rbp), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x30(%rax)
movslq -0x20(%rbp), %rdi
shlq $0x4, %rdi
callq 0x47a0
movq -0x40(%rbp), %rcx
movq %rax, 0x38(%rcx)
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x70fbb
movq -0x40(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
jmp 0x71352
movl $0x0, -0x24(%rbp)
movl $0x1, -0x58(%rbp)
movl -0x24(%rbp), %ecx
xorl %eax, %eax
cmpl -0x20(%rbp), %ecx
movb %al, -0x59(%rbp)
jge 0x70fe0
cmpl $0x0, -0x58(%rbp)
setg %al
movb %al, -0x59(%rbp)
movb -0x59(%rbp), %al
testb $0x1, %al
jne 0x70fe9
jmp 0x71024
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x48(%rbp), %rdx
movslq -0x24(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdx
callq 0x6ded0
movl %eax, -0x58(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movl -0x58(%rbp), %ecx
movq -0x30(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
jmp 0x70fc9
cmpl $0x1, -0x58(%rbp)
jge 0x71058
movq 0x18f9f(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
leaq 0xdaf8(%rip), %rsi      # 0x7eb33
movb $0x0, %al
callq 0x4830
movq -0x40(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
jmp 0x71352
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
leaq -0x24(%rbp), %rdx
callq 0x6ded0
movl %eax, %ecx
movq -0x30(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x20(%rbp), %eax
je 0x710af
movq 0x18f48(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
leaq 0xdaa1(%rip), %rsi      # 0x7eb33
movb $0x0, %al
callq 0x4830
movq -0x40(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
jmp 0x71352
cmpl $0x0, -0x20(%rbp)
jne 0x710d1
movq -0x40(%rbp), %rax
leaq -0x670(%rip), %rcx      # 0x70a50
movq %rcx, 0x18(%rax)
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x71352
movl $0x0, -0x24(%rbp)
movl $0x1, -0x58(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x7115c
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x48(%rbp), %rdx
movslq -0x24(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdx
addq $0xc, %rdx
callq 0x6ded0
movl %eax, -0x58(%rbp)
cmpl $0x1, -0x58(%rbp)
jge 0x71112
jmp 0x7115c
movl -0x54(%rbp), %eax
movq -0x48(%rbp), %rcx
movslq -0x24(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
cmpl 0xc(%rcx), %eax
jge 0x7113e
movq -0x48(%rbp), %rax
movslq -0x24(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, -0x54(%rbp)
jmp 0x71140
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movl -0x58(%rbp), %ecx
movq -0x30(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
jmp 0x710df
cmpl $0x1, -0x58(%rbp)
jl 0x7117e
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
subq %rcx, %rax
movslq -0x14(%rbp), %rcx
cmpq %rcx, %rax
jne 0x7117e
movl -0x54(%rbp), %eax
cmpl -0x20(%rbp), %eax
jl 0x711ac
movq 0x18e4b(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
leaq 0xd9a4(%rip), %rsi      # 0x7eb33
movb $0x0, %al
callq 0x4830
movq -0x40(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
jmp 0x71352
movq -0x48(%rbp), %rdi
movslq -0x20(%rbp), %rsi
movl $0x10, %edx
leaq 0x1a0(%rip), %rcx       # 0x71360
callq 0x4b00
movl $0xffffffff, -0x4c(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x50(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x71250
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
movq -0x48(%rbp), %rax
movslq -0x24(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
cmpl -0x50(%rbp), %eax
jle 0x71230
jmp 0x71204
movq -0x48(%rbp), %rax
movslq -0x24(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
cmpl -0x50(%rbp), %eax
jle 0x7122e
movl -0x4c(%rbp), %eax
shll %eax
movl %eax, -0x4c(%rbp)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x71204
jmp 0x71230
movl -0x4c(%rbp), %ecx
movq -0x48(%rbp), %rax
movslq -0x24(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x711da
movl $0x0, -0x50(%rbp)
movl $0x0, -0x28(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x712d1
movq -0x48(%rbp), %rax
movslq -0x24(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
cmpl -0x50(%rbp), %eax
jle 0x712b1
movq -0x48(%rbp), %rax
movslq -0x24(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
subl -0x24(%rbp), %eax
movl %eax, -0x28(%rbp)
movq -0x48(%rbp), %rax
movslq -0x24(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, -0x50(%rbp)
movl -0x28(%rbp), %ecx
movq -0x48(%rbp), %rax
movslq -0x24(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x71265
cmpl $0x3, -0x18(%rbp)
je 0x712dd
cmpl $0x4, -0x18(%rbp)
jne 0x7130d
movq -0x40(%rbp), %rax
movq 0x38(%rax), %rax
cmpl $0x0, 0xc(%rax)
jne 0x712fc
movq -0x40(%rbp), %rax
leaq -0x886(%rip), %rcx      # 0x70a70
movq %rcx, 0x18(%rax)
jmp 0x7130b
movq -0x40(%rbp), %rax
leaq -0x827(%rip), %rcx      # 0x70ae0
movq %rcx, 0x18(%rax)
jmp 0x7134a
cmpl $0x5, -0x18(%rbp)
jne 0x71318
callq 0x42e0
movq -0x40(%rbp), %rax
movq 0x38(%rax), %rax
cmpl $0x0, 0xc(%rax)
jne 0x71337
movq -0x40(%rbp), %rax
leaq -0x691(%rip), %rcx      # 0x70ca0
movq %rcx, 0x18(%rax)
jmp 0x71346
movq -0x40(%rbp), %rax
leaq -0x632(%rip), %rcx      # 0x70d10
movq %rcx, 0x18(%rax)
jmp 0x71348
jmp 0x7134a
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)