// File: /workspace/github_c_makefile_star2_O1/samtools[P]htslib/htscodecs/htscodecs/pack.c
// Function: hts_unpack
uint8_t *hts_unpack(uint8_t *data, int64_t len, uint8_t *out, uint64_t out_len, int nsym, uint8_t *p) {
    //uint8_t *out;
    uint8_t c = 0;
    int64_t i, j = 0, olen;

    if (nsym == 1) {
        // raw data; FIXME: shortcut the need for malloc & memcpy here
        memcpy(out, data, len);
        return out;
    }

    switch(nsym) {
    case 8: {
        union {
            uint64_t w;
            uint8_t c[8];
        } map[256];
        int x;
        for (x = 0; x < 256; x++) {
            map[x].c[0] = p[x>>0&1];
            map[x].c[1] = p[x>>1&1];
            map[x].c[2] = p[x>>2&1];
            map[x].c[3] = p[x>>3&1];
            map[x].c[4] = p[x>>4&1];
            map[x].c[5] = p[x>>5&1];
            map[x].c[6] = p[x>>6&1];
            map[x].c[7] = p[x>>7&1];
        }
        if ((out_len+7)/8 > len)
            return NULL;
        olen = out_len & ~7;

        for (i = 0; i < olen; i+=8)
            memcpy(&out[i], &map[data[j++]].w, 8);

        if (out_len != olen) {
            c = data[j++];
            while (i < out_len) {
                out[i++] = p[c & 1];
                c >>= 1;
            }
        }
        break;
    }

    case 4: {
        union {
            uint32_t w;
            uint8_t c[4];
        } map[256];

        int x, y, z, _, P=0;
        for (x = 0; x < 4; x++)
            for (y = 0; y < 4; y++)
                for (z = 0; z < 4; z++)
                    for (_ = 0; _ < 4; _++, P++) {
                        map[P].c[0] = p[_];
                        map[P].c[1] = p[z];
                        map[P].c[2] = p[y];
                        map[P].c[3] = p[x];
                    }

        if ((out_len+3)/4 > len)
            return NULL;
        olen = out_len & ~3;

        for (i = 0; i < olen-12; i+=16) {
            uint32_t w[] = {
                map[data[j+0]].w,
                map[data[j+1]].w,
                map[data[j+2]].w,
                map[data[j+3]].w
            };
            j += 4;
            memcpy(&out[i], &w, 16);
        }

        for (; i < olen; i+=4)
            memcpy(&out[i], &map[data[j++]].w, 4);

        if (out_len != olen) {
            c = data[j++];
            while (i < out_len) {
                out[i++] = p[c & 3];
                c >>= 2;
            }
        }
        break;
    }

    case 2: {
        union {
            uint16_t w;
            uint8_t c[2];
        } map[256];

        int x, y;
        for (x = 0; x < 16; x++) {
            for (y = 0; y < 16; y++) {
                map[x*16+y].c[0] = p[y];
                map[x*16+y].c[1] = p[x];
            }
        }

        if ((out_len+1)/2 > len)
            return NULL;
        olen = out_len & ~1;

        for (i = j = 0; i+2 < olen; i+=4) {
            uint16_t w[] = {
                map[data[j+0]].w,
                map[data[j+1]].w
            };
            memcpy(&out[i], &w, 4);

            j += 2;
        }

        for (; i < olen; i+=2)
            memcpy(&out[i], &map[data[j++]].w, 2);

        if (out_len != olen) {
            c = data[j++];
            out[i+0] = p[c&15];
        }
        break;
    }

    case 0:
        memset(out, p[0], out_len);
        break;

    default:
        return NULL;
    }

    return out;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x798, %rsp            # imm = 0x798
xorl %eax, %eax
cmpl $0x8, %r8d
ja 0x64674
movq %rsi, %r11
movq %rdi, %rbx
movl %r8d, %edi
leaq 0x3a85d(%rip), %r8      # 0x9eb38
movslq (%r8,%rdi,4), %rdi
addq %r8, %rdi
jmpq *%rdi
movzbl (%r9), %esi
movq %rdx, %rdi
movq %rcx, %rdx
addq $0x798, %rsp            # imm = 0x798
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4260
leaq -0x6f(%rsp), %rax
xorl %edi, %edi
movb (%r9,%rdi), %r8b
xorl %r10d, %r10d
movb (%r9,%r10), %sil
movb %sil, -0x1(%rax,%r10,2)
movb %r8b, (%rax,%r10,2)
incq %r10
cmpq $0x10, %r10
jne 0x64312
incq %rdi
addq $0x20, %rax
cmpq $0x10, %rdi
jne 0x6430b
leaq 0x1(%rcx), %rax
shrq %rax
cmpq %r11, %rax
ja 0x6455b
movq %rcx, %rdi
andq $-0x2, %rdi
cmpq $0x3, %rdi
jl 0x6459e
movl $0x2, %eax
xorl %r8d, %r8d
movzbl (%rbx,%r8), %esi
movzwl -0x70(%rsp,%rsi,2), %esi
movzbl 0x1(%rbx,%r8), %r10d
movzwl -0x70(%rsp,%r10,2), %r10d
movw %si, -0x2(%rdx,%rax)
movw %r10w, (%rdx,%rax)
addq $0x2, %r8
addq $0x4, %rax
cmpq %rdi, %rax
jl 0x6435e
addq $-0x2, %rax
jmp 0x645ba
movq %rdx, %rdi
movq %rbx, %rsi
movq %r11, %rdx
addq $0x798, %rsp            # imm = 0x798
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x43c0
movq %r11, -0x78(%rsp)
movq %rbx, -0x80(%rsp)
leaq -0x6d(%rsp), %rax
xorl %edi, %edi
xorl %r8d, %r8d
movb (%r9,%rdi), %r11b
xorl %ebx, %ebx
movb (%r9,%rbx), %bpl
xorl %r12d, %r12d
movq %r8, %r14
movb (%r9,%r12), %r13b
movslq %r14d, %r8
leaq (%rax,%r8,4), %r10
xorl %r15d, %r15d
movb (%r9,%r15), %sil
movb %sil, -0x3(%r10,%r15,4)
movb %r13b, -0x2(%r10,%r15,4)
movb %bpl, -0x1(%r10,%r15,4)
movb %r11b, (%r10,%r15,4)
incq %r15
cmpq $0x4, %r15
jne 0x643e5
incq %r12
addq %r15, %r8
cmpq $0x4, %r12
jne 0x643d4
incq %rbx
movslq %r14d, %r8
addq %r15, %r8
cmpq $0x4, %rbx
jne 0x643cd
incq %rdi
movslq %r14d, %r8
addq %r15, %r8
cmpq $0x4, %rdi
jne 0x643c7
leaq 0x3(%rcx), %rax
shrq $0x2, %rax
cmpq -0x78(%rsp), %rax
ja 0x6455b
movq %rcx, %r15
movq %rcx, %rdi
andq $-0x4, %rdi
cmpq $0xd, %rdi
jl 0x645dc
leaq -0xc(%rdi), %r10
xorl %eax, %eax
xorl %r8d, %r8d
movq -0x80(%rsp), %rcx
movzbl (%rcx,%r8), %esi
movl -0x70(%rsp,%rsi,4), %esi
movzbl 0x1(%rcx,%r8), %r11d
movl -0x70(%rsp,%r11,4), %r11d
movzbl 0x2(%rcx,%r8), %ebx
movl -0x70(%rsp,%rbx,4), %ebx
movzbl 0x3(%rcx,%r8), %r14d
movl -0x70(%rsp,%r14,4), %ebp
addq $0x4, %r8
movl %esi, (%rdx,%rax)
movl %r11d, 0x4(%rdx,%rax)
movl %ebx, 0x8(%rdx,%rax)
movl %ebp, 0xc(%rdx,%rax)
addq $0x10, %rax
cmpq %r10, %rax
jl 0x64464
jmp 0x645fb
xorl %eax, %eax
movl %eax, %edi
andl $0x1, %edi
movb (%r9,%rdi), %dil
movb %dil, -0x70(%rsp,%rax,8)
btl $0x1, %eax
movq %r9, %rdi
adcq $0x0, %rdi
movb (%rdi), %dil
movb %dil, -0x6f(%rsp,%rax,8)
btl $0x2, %eax
movq %r9, %rdi
adcq $0x0, %rdi
movb (%rdi), %dil
movb %dil, -0x6e(%rsp,%rax,8)
btl $0x3, %eax
movq %r9, %rdi
adcq $0x0, %rdi
movb (%rdi), %dil
movb %dil, -0x6d(%rsp,%rax,8)
btl $0x4, %eax
movq %r9, %rdi
adcq $0x0, %rdi
movb (%rdi), %dil
movb %dil, -0x6c(%rsp,%rax,8)
btl $0x5, %eax
movq %r9, %rdi
adcq $0x0, %rdi
movb (%rdi), %dil
movb %dil, -0x6b(%rsp,%rax,8)
btl $0x6, %eax
movq %r9, %rdi
adcq $0x0, %rdi
movb (%rdi), %dil
movb %dil, -0x6a(%rsp,%rax,8)
movl %eax, %edi
shrl $0x7, %edi
movb (%r9,%rdi), %dil
movb %dil, -0x69(%rsp,%rax,8)
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x644b1
leaq 0x7(%rcx), %rax
shrq $0x3, %rax
cmpq %r11, %rax
jbe 0x64562
xorl %eax, %eax
jmp 0x64674
movq %rcx, %rdi
andq $-0x8, %rdi
jle 0x64635
leaq -0x1(%rdi), %r10
shrq $0x3, %r10
incq %r10
xorl %eax, %eax
xorl %r8d, %r8d
movzbl (%rbx,%r8), %r11d
incq %r8
movq -0x70(%rsp,%r11,8), %r11
movq %r11, (%rdx,%rax)
addq $0x8, %rax
cmpq %r8, %r10
jne 0x6457f
jmp 0x6463a
xorl %r8d, %r8d
xorl %eax, %eax
jmp 0x645ba
movzbl (%rbx,%r8), %esi
incq %r8
movzwl -0x70(%rsp,%rsi,2), %esi
movw %si, (%rdx,%rax)
addq $0x2, %rax
cmpq %rdi, %rax
jl 0x645a5
cmpq %rcx, %rdi
je 0x64671
movzbl (%rbx,%r8), %ecx
andl $0xf, %ecx
movb (%r9,%rcx), %cl
movb %cl, (%rdx,%rax)
jmp 0x64671
xorl %r8d, %r8d
xorl %eax, %eax
movq -0x80(%rsp), %rcx
jmp 0x645fb
movzbl (%rcx,%r8), %esi
incq %r8
movl -0x70(%rsp,%rsi,4), %esi
movl %esi, (%rdx,%rax)
addq $0x4, %rax
cmpq %rdi, %rax
jl 0x645e8
cmpq %r15, %rdi
sete %sil
cmpq %r15, %rax
setae %dil
orb %sil, %dil
jne 0x64671
movb (%rcx,%r8), %sil
movzbl %sil, %esi
movl %esi, %ecx
andl $0x3, %ecx
movb (%r9,%rcx), %cl
movb %cl, (%rdx,%rax)
incq %rax
shrb $0x2, %sil
cmpq %rax, %r15
jne 0x64617
jmp 0x64671
xorl %r8d, %r8d
xorl %eax, %eax
cmpq %rcx, %rdi
setne %dil
cmpq %rcx, %rax
setb %r10b
andb %dil, %r10b
cmpb $0x1, %r10b
jne 0x64671
movb (%rbx,%r8), %sil
movzbl %sil, %esi
movl %esi, %edi
andl $0x1, %edi
movb (%r9,%rdi), %dil
movb %dil, (%rdx,%rax)
incq %rax
shrb %sil
cmpq %rax, %rcx
jne 0x64655
movq %rdx, %rax
addq $0x798, %rsp            # imm = 0x798
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq