// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/cram/cram_io.c
// Function: itf8_decode_crc
int itf8_decode_crc(cram_fd *fd, int32_t *val_p, uint32_t *crc) {
    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
    };
    unsigned char c[5];

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

    c[0]=val;

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

    if (i > 0) {
        if (hread(fd->fp, &c[1], i) < i)
            return -1;
    }

    switch(i) {
    case 0:
        *val_p = val;
        *crc = crc32(*crc, c, 1);
        return 1;

    case 1:
        val = (val<<8) | c[1];
        *val_p = val;
        *crc = crc32(*crc, c, 2);
        return 2;

    case 2:
        val = (val<<8) | c[1];
        val = (val<<8) | c[2];
        *val_p = val;
        *crc = crc32(*crc, c, 3);
        return 3;

    case 3:
        val = (val<<8) | c[1];
        val = (val<<8) | c[2];
        val = (val<<8) | c[3];
        *val_p = val;
        *crc = crc32(*crc, c, 4);
        return 4;

    case 4: // really 3.5 more, why make it different?
        {
            uint32_t uv = val;
            uv = (uv<<8) |  c[1];
            uv = (uv<<8) |  c[2];
            uv = (uv<<8) |  c[3];
            uv = (uv<<4) | (c[4] & 0x0f);
            // Avoid implementation-defined behaviour on negative values
            *val_p = uv < 0x80000000UL ? (int32_t) uv : -((int32_t) (0xffffffffUL - uv)) - 1;
            *crc = crc32(*crc, c, 5);
        }
    }

    return 5;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r12
movq (%rdi), %rdi
movq 0x8(%rdi), %rax
cmpq %rax, 0x10(%rdi)
jbe 0x4afc5
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%rdi)
movzbl (%rax), %ebp
jmp 0x4afd5
callq 0xee9b
movl %eax, %ebp
cmpl $-0x1, %eax
je 0x4b09e
movb %bpl, 0x3(%rsp)
movl %ebp, %eax
sarl $0x4, %eax
cltq
leaq 0x4f468(%rip), %rcx     # 0x9a450
movl (%rcx,%rax,4), %r15d
leaq 0x4f49d(%rip), %rcx     # 0x9a490
andl (%rcx,%rax,4), %ebp
andq $-0x8, %rax
cmpq $0x8, %rax
jne 0x4b079
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq (%r12), %r12
leaq 0x4(%rsp), %rdi
movq 0x8(%r12), %r14
movq 0x10(%r12), %rbx
subq %r14, %rbx
cmpq %r15, %rbx
movq %r15, %r13
cmovbq %rbx, %r13
movq %r14, %rsi
movq %r13, %rdx
callq 0x43c0
addq %r13, %r14
movq %r14, 0x8(%r12)
cmpq %r15, %rbx
jae 0x4b060
testb $0x2, 0x30(%r12)
je 0x4b060
movq %r12, %rdi
leaq 0x4(%rsp), %rsi
movq %r15, %rdx
movq %r13, %rcx
callq 0xf129
movq %rax, %r13
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %r14
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpq %r15, %r13
jl 0x4b142
cmpl $0x4, %r15d
ja 0x4b154
leaq 0x4f20a(%rip), %rax     # 0x9a294
movslq (%rax,%r15,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movl $0x1, %r12d
jmp 0x4b12e
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x4b142
movzbl 0x4(%rsp), %eax
shll $0x10, %ebp
shll $0x8, %eax
orl %ebp, %eax
movzbl 0x5(%rsp), %ecx
orl %eax, %ecx
movzbl 0x6(%rsp), %eax
shll $0xc, %ecx
shll $0x4, %eax
movzbl 0x7(%rsp), %ebp
andl $0xf, %ebp
orl %eax, %ebp
orl %ecx, %ebp
movl $0x5, %r12d
jmp 0x4b12e
movzbl 0x4(%rsp), %eax
shll $0x10, %ebp
shll $0x8, %eax
orl %ebp, %eax
movzbl 0x5(%rsp), %ebp
orl %eax, %ebp
movl $0x3, %r12d
jmp 0x4b12e
movzbl 0x4(%rsp), %eax
shll $0x10, %ebp
shll $0x8, %eax
orl %ebp, %eax
movzbl 0x5(%rsp), %ecx
orl %eax, %ecx
shll $0x8, %ecx
movzbl 0x6(%rsp), %ebp
orl %ecx, %ebp
movl $0x4, %r12d
jmp 0x4b12e
shll $0x8, %ebp
movzbl 0x4(%rsp), %eax
orl %eax, %ebp
movl $0x2, %r12d
movl %ebp, (%r14)
movl (%rbx), %edi
leaq 0x3(%rsp), %rsi
movl %r12d, %edx
callq 0x4320
movl %eax, (%rbx)
movl %r12d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x5, %r12d
jmp 0x4b142