// File: /workspace/github_c_makefile_star2_O1/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 0x6eb73
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 0x6eb62
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 0x6eb11
jmp 0x6ebaa
leal (%r8,%rdx,4), %eax
subl %edx, %r8d
xorl %ecx, %ecx
cltq
imulq $0x66666667, %rax, %rdx # imm = 0x66666667
movq %rdx, %r9
shrq $0x3f, %r9
shrq $0x21, %rdx
addl %r9d, %edx
movb %dl, -0x1e(%rsp,%rcx)
addl %r8d, %eax
incq %rcx
cmpq $0x4, %rcx
jne 0x6eb7c
movw $0xff00, -0x1a(%rsp)    # imm = 0xFF00
addq $0x2, %rsi
leaq -0x18(%rsp), %rdx
xorl %eax, %eax
movaps 0xec94(%rip), %xmm0     # 0x7d850
movdqa 0xec9c(%rip), %xmm1     # 0x7d860
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %rsi, %r9
movzbl (%r9), %r10d
shll %cl, %r10d
incq %r9
orl %r10d, %r8d
addl $0x8, %ecx
cmpl $0x18, %ecx
jne 0x6ebcc
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, (%rdx)
addq $0x8, %rdx
addq $0x3, %rsi
leal 0x1(%rax), %ecx
testl %eax, %eax
movl %ecx, %eax
je 0x6ebc4
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 0x6ec6f
retq
nop