// File: /workspace/github_c_makefile_star2_O3/jonof[P]jftekwar/jfbuild/libsquish/alpha.cpp
// Function: squish::DecompressAlphaDxt5(unsigned char*, void const*)
void DecompressAlphaDxt5( u8* rgba, void const* block )
{
    // get the two alpha values
    u8 const* bytes = reinterpret_cast< u8 const* >( block );
    int alpha0 = bytes[0];
    int alpha1 = bytes[1];

    // compare the values to build the codebook
    u8 codes[8];
    codes[0] = ( u8 )alpha0;
    codes[1] = ( u8 )alpha1;
    if( alpha0 <= alpha1 )
    {
        // use 5-alpha codebook
        for( int i = 1; i < 5; ++i )
            codes[1 + i] = ( u8 )( ( ( 5 - i )*alpha0 + i*alpha1 )/5 );
        codes[6] = 0;
        codes[7] = 255;
    }
    else
    {
        // use 7-alpha codebook
        for( int i = 1; i < 7; ++i )
            codes[1 + i] = ( u8 )( ( ( 7 - i )*alpha0 + i*alpha1 )/7 );
    }

    // decode the indices
    u8 indices[16];
    u8 const* src = bytes + 2;
    u8* dest = indices;
    for( int i = 0; i < 2; ++i )
    {
        // grab 3 bytes
        int value = 0;
        for( int j = 0; j < 3; ++j )
        {
            int byte = *src++;
            value |= ( byte << 8*j );
        }

        // unpack 8 3-bit values from it
        for( int j = 0; j < 8; ++j )
        {
            int index = ( value >> 3*j ) & 0x7;
            *dest++ = ( u8 )index;
        }
    }

    // write out the indexed codebook values
    for( int i = 0; i < 16; ++i )
        rgba[4*i + 3] = codes[indices[i]];
}

// Assembly:
movzbl (%rsi), %edx
movzbl 0x1(%rsi), %r8d
movb %dl, -0x20(%rsp)
movb %r8b, -0x1f(%rsp)
cmpb %r8b, %dl
jbe 0x708cd
leal (%rdx,%rdx,4), %ecx
leal (%r8,%r8), %eax
leal (%rcx,%r8,2), %ecx
addl %edx, %edx
subl %edx, %eax
leal (%rdx,%rdx,2), %edx
addl %r8d, %edx
xorl %r8d, %r8d
movswl %ax, %r9d
cmpq $0x5, %r8
ja 0x708bc
movzwl %dx, %r10d
imull $0x2493, %r10d, %r10d   # imm = 0x2493
shrl $0x10, %r10d
movl %edx, %r11d
subl %r10d, %r11d
movzwl %r11w, %r11d
shrl %r11d
addl %r10d, %r11d
shrl $0x2, %r11d
movb %r11b, -0x1e(%rsp,%r8)
movl %ecx, %r10d
imulq $0x24924925, %r10, %r10 # imm = 0x24924925
shrq $0x20, %r10
movl %ecx, %r11d
subl %r10d, %r11d
shrl %r11d
addl %r10d, %r11d
shrl $0x2, %r11d
movb %r11b, -0x1d(%rsp,%r8)
addq $0x2, %r8
addl %eax, %ecx
addl %r9d, %edx
cmpq $0x6, %r8
jne 0x7086b
jmp 0x708fe
leal (%r8,%rdx,4), %eax
subl %edx, %r8d
xorl %ecx, %ecx
movl $0xcccccccd, %edx       # imm = 0xCCCCCCCD
movl %eax, %r9d
imulq %rdx, %r9
shrq $0x22, %r9
movb %r9b, -0x1e(%rsp,%rcx)
addl %r8d, %eax
incq %rcx
cmpq $0x4, %rcx
jne 0x708db
movw $0xff00, -0x1a(%rsp)    # imm = 0xFF00
addq $0x4, %rsi
leaq -0x18(%rsp), %rcx
movb $0x1, %al
movaps 0x6ee0(%rip), %xmm0     # 0x777f0
movdqa 0xe3e8(%rip), %xmm1     # 0x7ed00
movzbl (%rsi), %edx
shll $0x10, %edx
movzwl -0x2(%rsi), %r8d
orl %edx, %r8d
movd %r8d, %xmm2
pshufd $0x0, %xmm2, %xmm2      # xmm2 = xmm2[0,0,0,0]
movdqa %xmm2, %xmm3
psrld $0x9, %xmm3
movdqa %xmm2, %xmm4
psrld $0x6, %xmm4
punpckhqdq %xmm3, %xmm4    # xmm4 = xmm4[1],xmm3[1]
movdqa %xmm2, %xmm3
psrld $0x3, %xmm3
movdqa %xmm2, %xmm5
punpcklqdq %xmm3, %xmm5    # xmm5 = xmm5[0],xmm3[0]
shufps $0xcc, %xmm4, %xmm5     # xmm5 = xmm5[0,3],xmm4[0,3]
movdqa %xmm2, %xmm3
psrld $0x15, %xmm3
movdqa %xmm2, %xmm4
psrld $0x12, %xmm4
punpckhqdq %xmm3, %xmm4    # xmm4 = xmm4[1],xmm3[1]
movdqa %xmm2, %xmm3
psrld $0xf, %xmm3
psrld $0xc, %xmm2
punpcklqdq %xmm3, %xmm2    # xmm2 = xmm2[0],xmm3[0]
shufps $0xcc, %xmm4, %xmm2     # xmm2 = xmm2[0,3],xmm4[0,3]
andps %xmm0, %xmm2
andps %xmm0, %xmm5
packuswb %xmm2, %xmm5
packuswb %xmm5, %xmm5
pand %xmm1, %xmm5
movq %xmm5, (%rcx)
addq $0x8, %rcx
addq $0x3, %rsi
testb $0x1, %al
movl $0x0, %eax
jne 0x70918
xorl %eax, %eax
movzbl -0x18(%rsp,%rax), %ecx
movb -0x20(%rsp,%rcx), %cl
movb %cl, 0x3(%rdi,%rax,4)
incq %rax
cmpq $0x10, %rax
jne 0x709b4
retq
nop