// File: /workspace/github_c_makefile_star2_O2/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
pushq %rax
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r12
movq (%rdi), %rdi
callq 0x3e519
pushq $-0x1
popq %rbp
cmpl $-0x1, %eax
je 0x3e65e
movb %al, 0x3(%rsp)
movl %eax, %ecx
sarl $0x4, %ecx
movslq %ecx, %rcx
leaq 0x3de52(%rip), %rdx     # 0x7c3c0
movl (%rdx,%rcx,4), %r15d
leaq 0x3de87(%rip), %rdx     # 0x7c400
movl (%rdx,%rcx,4), %r13d
andl %eax, %r13d
andq $-0x8, %rcx
cmpq $0x8, %rcx
jne 0x3e5a4
movq (%r12), %rdi
leaq 0x4(%rsp), %rsi
movq %r15, %rdx
callq 0x3e674
cmpq %r15, %rax
jl 0x3e65e
cmpl $0x4, %r15d
ja 0x3e66f
leaq 0x3dc4f(%rip), %rax     # 0x7c204
movslq (%rax,%r15,4), %rcx
addq %rax, %rcx
jmpq *%rcx
pushq $0x1
jmp 0x3e64a
movzbl 0x4(%rsp), %eax
shll $0x10, %r13d
shll $0x8, %eax
orl %r13d, %eax
movzbl 0x5(%rsp), %ecx
orl %eax, %ecx
movzbl 0x6(%rsp), %eax
shll $0xc, %ecx
shll $0x4, %eax
movzbl 0x7(%rsp), %r13d
andl $0xf, %r13d
orl %eax, %r13d
orl %ecx, %r13d
pushq $0x5
jmp 0x3e64a
movzbl 0x4(%rsp), %eax
shll $0x10, %r13d
shll $0x8, %eax
orl %r13d, %eax
movzbl 0x5(%rsp), %r13d
orl %eax, %r13d
pushq $0x3
jmp 0x3e64a
movzbl 0x4(%rsp), %eax
shll $0x10, %r13d
shll $0x8, %eax
orl %r13d, %eax
movzbl 0x5(%rsp), %ecx
orl %eax, %ecx
shll $0x8, %ecx
movzbl 0x6(%rsp), %r13d
orl %ecx, %r13d
pushq $0x4
jmp 0x3e64a
shll $0x8, %r13d
movzbl 0x4(%rsp), %eax
orl %eax, %r13d
pushq $0x2
popq %rbp
movl %r13d, (%r14)
movl (%rbx), %edi
leaq 0x3(%rsp), %rsi
movl %ebp, %edx
callq 0x4320
movl %eax, (%rbx)
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x5
popq %rbp
jmp 0x3e65e