// File: /workspace/github_cpp_makefile_star2_O2/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 %r12
pushq %rbx
movl (%rsi), %eax
pushq $-0x1
popq %rbp
decl %eax
cmpl $0x5, %eax
ja 0x3f405
movq %rsi, %rbx
leaq 0x8364(%rip), %rcx      # 0x47548
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq -0x3159(%rip), %rax     # 0x3c09b
movq %rax, 0x10(%rbx)
leaq -0x315f(%rip), %rax     # 0x3c0a0
movq %rax, 0x28(%rbx)
movq 0x18(%rbx), %rax
leaq -0x362e(%rip), %rcx     # 0x3bbe0
cmpq %rcx, %rax
je 0x3f3af
leaq -0x34cf(%rip), %rcx     # 0x3bd4f
cmpq %rcx, %rax
jne 0x3f405
leaq -0x324a(%rip), %rax     # 0x3bfe4
jmp 0x3f3c8
movq %rdi, %r15
movl $0x240, %edi            # imm = 0x240
callq 0x46f0
movq %rax, %r14
movl $0x4, (%rax)
leaq -0xd80(%rip), %rax      # 0x3e4d0
movq %rax, 0x10(%r14)
leaq -0xd60(%rip), %rax      # 0x3e4fb
movq %rax, 0x28(%r14)
leaq -0xdde(%rip), %rax      # 0x3e488
movq %rax, 0x20(%r14)
movq 0x30(%rbx), %rsi
movq %rsi, 0x48(%r14)
movq 0x38(%rbx), %r12
movq %r12, 0x50(%r14)
movq %r15, %rdi
callq 0x3f1c2
cmpl $-0x1, %eax
je 0x3f29b
movq %r15, %rdi
movq %r12, %rsi
callq 0x3f1c2
cmpl $-0x1, %eax
jne 0x3f3eb
movq %r14, %rdi
callq 0x3e4d0
jmp 0x3f405
movl $0x240, %edi            # imm = 0x240
callq 0x46f0
movq %rax, %r14
movl $0x3, (%rax)
leaq -0x1a42(%rip), %rax     # 0x3d880
movq %rax, 0x10(%r14)
leaq -0x1a31(%rip), %rax     # 0x3d89c
movq %rax, 0x28(%r14)
movq 0x38(%rbx), %rax
movq %rax, 0x30(%r14)
movslq 0x30(%rbx), %rcx
movl %ecx, 0x38(%r14)
xorl %edx, %edx
cmpq %rcx, %rdx
jge 0x3f304
movl (%rax), %esi
incl %esi
cmpl $0x80, %esi
ja 0x3f2fb
movl %esi, %esi
movl %edx, 0x3c(%r14,%rsi,4)
incq %rdx
addq $0x10, %rax
jmp 0x3f2e3
movq 0x18(%rbx), %rax
leaq -0x2129(%rip), %rcx     # 0x3d1e6
cmpq %rcx, %rax
je 0x3f3b8
leaq -0x2112(%rip), %rcx     # 0x3d20d
cmpq %rcx, %rax
je 0x3f3ce
leaq -0x2010(%rip), %rcx     # 0x3d31f
cmpq %rcx, %rax
je 0x3f3d7
leaq -0x2000(%rip), %rcx     # 0x3d33f
cmpq %rcx, %rax
je 0x3f3e0
movq %r14, %rdi
callq 0x4600
jmp 0x3f405
leaq -0x5a1(%rip), %rax      # 0x3edbb
movq %rax, 0x10(%rbx)
leaq -0x5a7(%rip), %rax      # 0x3edc0
movq %rax, 0x28(%rbx)
leaq -0x6fe(%rip), %rax      # 0x3ec74
jmp 0x3f3c8
leaq -0x2744(%rip), %rax     # 0x3cc37
movq %rax, 0x10(%rbx)
leaq -0x2aa2(%rip), %rax     # 0x3c8e4
movq %rax, 0x28(%rbx)
movq 0x18(%rbx), %rax
leaq -0x3070(%rip), %rcx     # 0x3c325
cmpq %rcx, %rax
je 0x3f3c1
leaq -0x2d4c(%rip), %rcx     # 0x3c655
cmpq %rcx, %rax
jne 0x3f405
leaq -0x27be(%rip), %rax     # 0x3cbef
jmp 0x3f3c8
leaq -0x33e2(%rip), %rax     # 0x3bfd4
jmp 0x3f3c8
leaq -0x1cbc(%rip), %rax     # 0x3d703
jmp 0x3f3e7
leaq -0x2973(%rip), %rax     # 0x3ca55
movq %rax, 0x20(%rbx)
jmp 0x3f403
leaq -0x1ccf(%rip), %rax     # 0x3d706
jmp 0x3f3e7
leaq -0x1c21(%rip), %rax     # 0x3d7bd
jmp 0x3f3e7
leaq -0x1c27(%rip), %rax     # 0x3d7c0
movq %rax, 0x20(%r14)
movl $0x240, %edx            # imm = 0x240
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4420
movq %r14, %rdi
callq 0x4600
xorl %ebp, %ebp
movl %ebp, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq