// File: /workspace/github_cpp_makefile_star2_O1/karel-brinda[P]ococo/ext/htslib/cram/cram_codecs.c
// Function: cram_codec_decoder2encoder
int cram_codec_decoder2encoder(cram_fd *fd, cram_codec *c) {
    int j;

    switch (c->codec) {
    case E_EXTERNAL:
	// shares struct with decode
	c->free = cram_external_encode_free;
	c->store = cram_external_encode_store;
	if (c->decode == cram_external_decode_int)
	    c->encode = cram_external_encode_int;
	else if (c->decode == cram_external_decode_char)
	    c->encode = cram_external_encode_char;
	else
	    return -1;
	break;

    case E_HUFFMAN: {
	// New structure, so switch.
	// FIXME: we huffman and e_huffman structs amended, we could
	// unify this.
	cram_codec *t = malloc(sizeof(*t));
	t->codec = E_HUFFMAN;
	t->free = cram_huffman_encode_free;
	t->store = cram_huffman_encode_store;
	t->e_huffman.codes = c->huffman.codes;
	t->e_huffman.nvals = c->huffman.ncodes;
	for (j = 0; j < t->e_huffman.nvals; j++) {
	    int32_t sym = t->e_huffman.codes[j].symbol;
	    if (sym >= -1 && sym < MAX_HUFF)
		t->e_huffman.val2code[sym+1] = j;
	}

	if (c->decode == cram_huffman_decode_char0)
	    t->encode = cram_huffman_encode_char0;
	else if (c->decode == cram_huffman_decode_char)
	    t->encode = cram_huffman_encode_char;
	else if (c->decode == cram_huffman_decode_int0)
	    t->encode = cram_huffman_encode_int0;
	else if (c->decode == cram_huffman_decode_int)
	    t->encode = cram_huffman_encode_int;
	else {
	    free(t);
	    return -1;
	}
	*c = *t;
	free(t);
	break;
    }

    case E_BETA:
	// shares struct with decode
	c->free = cram_beta_encode_free;
	c->store = cram_beta_encode_store;
	if (c->decode == cram_beta_decode_int)
	    c->encode = cram_beta_encode_int;
	else if (c->decode == cram_beta_decode_char)
	    c->encode = cram_beta_encode_char;
	else
	    return -1;
	break;

    case E_BYTE_ARRAY_LEN: {
	cram_codec *t = malloc(sizeof(*t));
	t->codec = E_BYTE_ARRAY_LEN;
	t->free   = cram_byte_array_len_encode_free;
	t->store  = cram_byte_array_len_encode_store;
	t->encode = cram_byte_array_len_encode;
	t->e_byte_array_len.len_codec = c->byte_array_len.len_codec;
	t->e_byte_array_len.val_codec = c->byte_array_len.val_codec;
	if (cram_codec_decoder2encoder(fd, t->e_byte_array_len.len_codec) == -1 ||
	    cram_codec_decoder2encoder(fd, t->e_byte_array_len.val_codec) == -1) {
	    t->free(t);
	    return -1;
	}

	// {len,val}_{encoding,dat} are undefined, but unused.
	// Leaving them unset here means we can test that assertion.
	*c = *t;
	free(t);
	break;
    }

    case E_BYTE_ARRAY_STOP:
	// shares struct with decode
	c->free   = cram_byte_array_stop_encode_free;
	c->store  = cram_byte_array_stop_encode_store;
	c->encode = cram_byte_array_stop_encode;
	break;

    default:
	return -1;
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movl (%rsi), %eax
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
decl %eax
cmpl $0x5, %eax
ja 0x4e0b5
movq %rsi, %rbx
leaq 0x86b0(%rip), %rcx      # 0x56538
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq -0x3b23(%rip), %rax     # 0x4a375
movq %rax, 0x10(%rbx)
leaq -0x3b24(%rip), %rax     # 0x4a37f
movq %rax, 0x28(%rbx)
movq 0x18(%rbx), %rax
leaq -0x40da(%rip), %rcx     # 0x49dd8
cmpq %rcx, %rax
je 0x4e05f
leaq -0x3f57(%rip), %rcx     # 0x49f6b
cmpq %rcx, %rax
jne 0x4e0b5
leaq -0x3c23(%rip), %rax     # 0x4a2af
jmp 0x4e078
movq %rdi, %r15
movl $0x240, %edi            # imm = 0x240
callq 0x4700
movq %rax, %r14
movl $0x4, (%rax)
leaq -0xecd(%rip), %rax      # 0x4d027
movq %rax, 0x10(%r14)
leaq -0xead(%rip), %rax      # 0x4d052
movq %rax, 0x28(%r14)
leaq -0xf2d(%rip), %rax      # 0x4cfdd
movq %rax, 0x20(%r14)
movq 0x30(%rbx), %rsi
movq %rsi, 0x48(%r14)
movq 0x38(%rbx), %rax
movq %rax, 0x50(%r14)
movq %r15, %rdi
callq 0x4de65
cmpl $-0x1, %eax
je 0x4df40
movq 0x50(%r14), %rsi
movq %r15, %rdi
callq 0x4de65
cmpl $-0x1, %eax
jne 0x4e09b
movq %r14, %rdi
callq *0x10(%r14)
jmp 0x4e0b5
movl $0x240, %edi            # imm = 0x240
callq 0x4700
movq %rax, %r14
movl $0x3, (%rax)
leaq -0x1e4d(%rip), %rax     # 0x4c119
movq %rax, 0x10(%r14)
leaq -0x1e37(%rip), %rax     # 0x4c13a
movq %rax, 0x28(%r14)
movq 0x38(%rbx), %rax
movq %rax, 0x30(%r14)
movl 0x30(%rbx), %eax
movl %eax, 0x38(%r14)
testl %eax, %eax
jle 0x4dfb4
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x30(%r14), %rdx
movl (%rdx,%rax), %edx
incl %edx
cmpl $0x80, %edx
ja 0x4dfa4
movl %edx, %edx
movl %ecx, 0x3c(%r14,%rdx,4)
incq %rcx
movslq 0x38(%r14), %rdx
addq $0x10, %rax
cmpq %rdx, %rcx
jl 0x4df8c
movq 0x18(%rbx), %rax
leaq -0x2922(%rip), %rcx     # 0x4b69d
cmpq %rcx, %rax
je 0x4e068
leaq -0x290d(%rip), %rcx     # 0x4b6c2
cmpq %rcx, %rax
je 0x4e07e
leaq -0x27bd(%rip), %rcx     # 0x4b822
cmpq %rcx, %rax
je 0x4e087
leaq -0x27af(%rip), %rcx     # 0x4b840
cmpq %rcx, %rax
je 0x4e090
movq %r14, %rdi
callq 0x4610
jmp 0x4e0b5
leaq -0x5eb(%rip), %rax      # 0x4da21
movq %rax, 0x10(%rbx)
leaq -0x5ec(%rip), %rax      # 0x4da2b
movq %rax, 0x28(%rbx)
leaq -0x767(%rip), %rax      # 0x4d8bb
jmp 0x4e078
leaq -0x30cd(%rip), %rax     # 0x4af5e
movq %rax, 0x10(%rbx)
leaq -0x3445(%rip), %rax     # 0x4abf1
movq %rax, 0x28(%rbx)
movq 0x18(%rbx), %rax
leaq -0x3a0d(%rip), %rcx     # 0x4a638
cmpq %rcx, %rax
je 0x4e071
leaq -0x36fb(%rip), %rcx     # 0x4a956
cmpq %rcx, %rax
jne 0x4e0b5
leaq -0x3148(%rip), %rax     # 0x4af15
jmp 0x4e078
leaq -0x3dc7(%rip), %rax     # 0x4a29f
jmp 0x4e078
leaq -0x20d0(%rip), %rax     # 0x4bf9f
jmp 0x4e097
leaq -0x32fd(%rip), %rax     # 0x4ad7b
movq %rax, 0x20(%rbx)
jmp 0x4e0b3
leaq -0x20e3(%rip), %rax     # 0x4bfa2
jmp 0x4e097
leaq -0x2036(%rip), %rax     # 0x4c058
jmp 0x4e097
leaq -0x203c(%rip), %rax     # 0x4c05b
movq %rax, 0x20(%r14)
movl $0x240, %edx            # imm = 0x240
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4420
movq %r14, %rdi
callq 0x4610
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nop