// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x6cbf0
movl %eax, -0x2c(%rbp)
cmpl $-0x1, -0x2c(%rbp)
jne 0x6cc75
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6cecb
movl -0x2c(%rbp), %eax
movb %al, -0x25(%rbp)
movl -0x2c(%rbp), %eax
sarl $0x4, %eax
movslq %eax, %rcx
leaq 0xa0d75(%rip), %rax     # 0x10da00
movl (%rax,%rcx,4), %eax
movl %eax, -0x30(%rbp)
movl -0x2c(%rbp), %eax
sarl $0x4, %eax
movslq %eax, %rcx
leaq 0xa0d9f(%rip), %rax     # 0x10da40
movl (%rax,%rcx,4), %eax
andl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x30(%rbp)
jle 0x6ccdf
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq -0x25(%rbp), %rsi
addq $0x1, %rsi
movslq -0x30(%rbp), %rdx
callq 0x6cee0
movslq -0x30(%rbp), %rcx
cmpq %rcx, %rax
jge 0x6ccdd
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6cecb
jmp 0x6ccdf
movl -0x30(%rbp), %eax
movq %rax, -0x40(%rbp)
subq $0x4, %rax
ja 0x6cec4
movq -0x40(%rbp), %rax
leaq 0x87e49(%rip), %rcx     # 0xf4b44
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl -0x2c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movl (%rax), %eax
movl %eax, %edi
leaq -0x25(%rbp), %rsi
movl $0x1, %edx
callq 0x4310
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x6cecb
movl -0x2c(%rbp), %eax
shll $0x8, %eax
movzbl -0x24(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movl (%rax), %eax
movl %eax, %edi
leaq -0x25(%rbp), %rsi
movl $0x2, %edx
callq 0x4310
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl $0x2, -0x4(%rbp)
jmp 0x6cecb
movl -0x2c(%rbp), %eax
shll $0x8, %eax
movzbl -0x24(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
shll $0x8, %eax
movzbl -0x23(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movl (%rax), %eax
movl %eax, %edi
leaq -0x25(%rbp), %rsi
movl $0x3, %edx
callq 0x4310
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl $0x3, -0x4(%rbp)
jmp 0x6cecb
movl -0x2c(%rbp), %eax
shll $0x8, %eax
movzbl -0x24(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
shll $0x8, %eax
movzbl -0x23(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
shll $0x8, %eax
movzbl -0x22(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movl (%rax), %eax
movl %eax, %edi
leaq -0x25(%rbp), %rsi
movl $0x4, %edx
callq 0x4310
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl $0x4, -0x4(%rbp)
jmp 0x6cecb
movl -0x2c(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
shll $0x8, %eax
movzbl -0x24(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
shll $0x8, %eax
movzbl -0x23(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
shll $0x8, %eax
movzbl -0x22(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
shll $0x4, %eax
movzbl -0x21(%rbp), %ecx
andl $0xf, %ecx
orl %ecx, %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl $0x80000000, %ecx       # imm = 0x80000000
cmpq %rcx, %rax
jae 0x6ce84
movl -0x34(%rbp), %eax
movl %eax, -0x44(%rbp)
jmp 0x6ce9d
movl -0x34(%rbp), %eax
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
subq %rcx, %rax
movl %eax, %ecx
xorl %eax, %eax
subl %ecx, %eax
subl $0x1, %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movl (%rax), %eax
movl %eax, %edi
leaq -0x25(%rbp), %rsi
movl $0x5, %edx
callq 0x4310
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl $0x5, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)