// File: karel-brinda[P]ococo/ext/htslib/cram/cram_io.c
// Function: itf8_decode
int itf8_decode(cram_fd *fd, int32_t *val_p) {
    static int nbytes[16] = {
	0,0,0,0, 0,0,0,0,                               // 0000xxxx - 0111xxxx
	1,1,1,1,                                        // 1000xxxx - 1011xxxx
	2,2,                                            // 1100xxxx - 1101xxxx
	3,                                              // 1110xxxx
	4,                                              // 1111xxxx
    };

    static int nbits[16] = {
	0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, // 0000xxxx - 0111xxxx
	0x3f, 0x3f, 0x3f, 0x3f,                         // 1000xxxx - 1011xxxx
	0x1f, 0x1f,                                     // 1100xxxx - 1101xxxx
	0x0f,                                           // 1110xxxx
	0x0f,                                           // 1111xxxx
    };

    int32_t val = hgetc(fd->fp);
    if (val == -1)
	return -1;

    int i = nbytes[val>>4];
    val &= nbits[val>>4];

    switch(i) {
    case 0:
	*val_p = val;
	return 1;

    case 1:
	val = (val<<8) | (unsigned char)hgetc(fd->fp);
	*val_p = val;
	return 2;

    case 2:
	val = (val<<8) | (unsigned char)hgetc(fd->fp);
	val = (val<<8) | (unsigned char)hgetc(fd->fp);
	*val_p = val;
	return 3;

    case 3:
	val = (val<<8) | (unsigned char)hgetc(fd->fp);
	val = (val<<8) | (unsigned char)hgetc(fd->fp);
	val = (val<<8) | (unsigned char)hgetc(fd->fp);
	*val_p = val;
	return 4;

    case 4: // really 3.5 more, why make it different?
	val = (val<<8) | (unsigned char)hgetc(fd->fp);
	val = (val<<8) | (unsigned char)hgetc(fd->fp);
	val = (val<<8) | (unsigned char)hgetc(fd->fp);
	val = (val<<4) | (((unsigned char)hgetc(fd->fp)) & 0x0f);
	*val_p = val;
    }

    return 5;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x43130
movl %eax, -0x1c(%rbp)
cmpl $-0x1, -0x1c(%rbp)
jne 0x42f11
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x43121
movl -0x1c(%rbp), %eax
sarl $0x4, %eax
movslq %eax, %rcx
leaq 0x4774f(%rip), %rax     # 0x8a670
movl (%rax,%rcx,4), %eax
movl %eax, -0x20(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, %ecx
sarl $0x4, %ecx
movslq %ecx, %rdx
leaq 0x47777(%rip), %rcx     # 0x8a6b0
movl (%rcx,%rdx,4), %ecx
andl %ecx, %eax
movl %eax, -0x1c(%rbp)
movl -0x20(%rbp), %eax
movq %rax, -0x28(%rbp)
subq $0x4, %rax
ja 0x4311a
movq -0x28(%rbp), %rax
leaq 0x39a13(%rip), %rcx     # 0x7c970
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x43121
movl -0x1c(%rbp), %eax
shll $0x8, %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x43130
movl %eax, %ecx
movl -0x2c(%rbp), %eax
movzbl %cl, %ecx
orl %ecx, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl $0x2, -0x4(%rbp)
jmp 0x43121
movl -0x1c(%rbp), %eax
shll $0x8, %eax
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x43130
movl %eax, %ecx
movl -0x34(%rbp), %eax
movzbl %cl, %ecx
orl %ecx, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
shll $0x8, %eax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x43130
movl %eax, %ecx
movl -0x30(%rbp), %eax
movzbl %cl, %ecx
orl %ecx, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl $0x3, -0x4(%rbp)
jmp 0x43121
movl -0x1c(%rbp), %eax
shll $0x8, %eax
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x43130
movl %eax, %ecx
movl -0x40(%rbp), %eax
movzbl %cl, %ecx
orl %ecx, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
shll $0x8, %eax
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x43130
movl %eax, %ecx
movl -0x3c(%rbp), %eax
movzbl %cl, %ecx
orl %ecx, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
shll $0x8, %eax
movl %eax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x43130
movl %eax, %ecx
movl -0x38(%rbp), %eax
movzbl %cl, %ecx
orl %ecx, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl $0x4, -0x4(%rbp)
jmp 0x43121
movl -0x1c(%rbp), %eax
shll $0x8, %eax
movl %eax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x43130
movl %eax, %ecx
movl -0x50(%rbp), %eax
movzbl %cl, %ecx
orl %ecx, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
shll $0x8, %eax
movl %eax, -0x4c(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x43130
movl %eax, %ecx
movl -0x4c(%rbp), %eax
movzbl %cl, %ecx
orl %ecx, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
shll $0x8, %eax
movl %eax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x43130
movl %eax, %ecx
movl -0x48(%rbp), %eax
movzbl %cl, %ecx
orl %ecx, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
shll $0x4, %eax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x43130
movl %eax, %ecx
movl -0x44(%rbp), %eax
movzbl %cl, %ecx
andl $0xf, %ecx
orl %ecx, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl $0x5, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)