// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
movq (%rdi), %rdi
movq 0x8(%rdi), %rax
cmpq %rax, 0x10(%rdi)
jbe 0x2fb03
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%rdi)
movzbl (%rax), %ebp
jmp 0x2fb0f
callq 0x4703e
movl %eax, %ebp
cmpl $-0x1, %eax
je 0x2fb4f
movl %ebp, %eax
sarl $0x4, %eax
movslq %eax, %rcx
leaq 0x22cc2(%rip), %rax     # 0x527e0
movl (%rax,%rcx,4), %eax
cmpq $0x4, %rax
ja 0x2fbe9
leaq 0x22cee(%rip), %rdx     # 0x52820
andl (%rdx,%rcx,4), %ebp
leaq 0x22c14(%rip), %rcx     # 0x52750
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x1, %eax
jmp 0x2fd4f
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x2fd51
movq (%r14), %rdi
movq 0x8(%rdi), %rax
movq 0x10(%rdi), %rcx
cmpq %rax, %rcx
jbe 0x2fbf3
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%rdi)
movzbl (%rax), %eax
jmp 0x2fc03
movq (%r14), %rdi
movq 0x8(%rdi), %rax
movq 0x10(%rdi), %rdx
cmpq %rax, %rdx
jbe 0x2fc91
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%rdi)
movzbl (%rax), %eax
jmp 0x2fca1
movq (%r14), %rdi
movq 0x8(%rdi), %rax
movq 0x10(%rdi), %rcx
cmpq %rax, %rcx
jbe 0x2fcd2
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%rdi)
movzbl (%rax), %eax
jmp 0x2fce2
shll $0x8, %ebp
movq (%r14), %rdi
movq 0x8(%rdi), %rax
cmpq %rax, 0x10(%rdi)
jbe 0x2fd40
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%rdi)
movzbl (%rax), %eax
jmp 0x2fd45
movl $0x5, %eax
jmp 0x2fd51
callq 0x4703e
movq (%r14), %rdi
movq 0x8(%rdi), %rdx
movq 0x10(%rdi), %rcx
shll $0x10, %ebp
shll $0x8, %eax
movzwl %ax, %r15d
orl %ebp, %r15d
cmpq %rdx, %rcx
jbe 0x2fc22
leaq 0x1(%rdx), %rsi
movq %rsi, 0x8(%rdi)
movzbl (%rdx), %eax
jmp 0x2fc32
callq 0x4703e
movq (%r14), %rdi
movq 0x8(%rdi), %rsi
movq 0x10(%rdi), %rcx
movzbl %al, %ebp
orl %r15d, %ebp
cmpq %rsi, %rcx
jbe 0x2fc4a
leaq 0x1(%rsi), %rdx
movq %rdx, 0x8(%rdi)
movzbl (%rsi), %eax
jmp 0x2fc5a
callq 0x4703e
movq (%r14), %rdi
movq 0x8(%rdi), %rdx
movq 0x10(%rdi), %rcx
shll $0xc, %ebp
movzbl %al, %r14d
shll $0x4, %r14d
orl %ebp, %r14d
cmpq %rdx, %rcx
jbe 0x2fc7a
leaq 0x1(%rdx), %rax
movq %rax, 0x8(%rdi)
movzbl (%rdx), %ebp
jmp 0x2fc81
callq 0x4703e
movl %eax, %ebp
andl $0xf, %ebp
orl %r14d, %ebp
movl $0x5, %eax
jmp 0x2fd4f
callq 0x4703e
movq (%r14), %rdi
movq 0x8(%rdi), %rcx
movq 0x10(%rdi), %rdx
shll $0x10, %ebp
shll $0x8, %eax
movzwl %ax, %r14d
orl %ebp, %r14d
cmpq %rcx, %rdx
jbe 0x2fcc0
leaq 0x1(%rcx), %rax
movq %rax, 0x8(%rdi)
movzbl (%rcx), %eax
jmp 0x2fcc5
callq 0x4703e
movzbl %al, %ebp
orl %r14d, %ebp
movl $0x3, %eax
jmp 0x2fd4f
callq 0x4703e
movq (%r14), %rdi
movq 0x8(%rdi), %rdx
movq 0x10(%rdi), %rcx
shll $0x10, %ebp
shll $0x8, %eax
movzwl %ax, %r15d
orl %ebp, %r15d
cmpq %rdx, %rcx
jbe 0x2fd01
leaq 0x1(%rdx), %rsi
movq %rsi, 0x8(%rdi)
movzbl (%rdx), %eax
jmp 0x2fd11
callq 0x4703e
movq (%r14), %rdi
movq 0x8(%rdi), %rsi
movq 0x10(%rdi), %rcx
movzbl %al, %r14d
orl %r15d, %r14d
shll $0x8, %r14d
cmpq %rsi, %rcx
jbe 0x2fd2e
leaq 0x1(%rsi), %rax
movq %rax, 0x8(%rdi)
movzbl (%rsi), %eax
jmp 0x2fd33
callq 0x4703e
movzbl %al, %ebp
orl %r14d, %ebp
movl $0x4, %eax
jmp 0x2fd4f
callq 0x4703e
movzbl %al, %eax
orl %eax, %ebp
movl $0x2, %eax
movl %ebp, (%rbx)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq