#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int color_distance(unsigned char const *, unsigned char *);
unsigned int match_colors_block_DXT1alpha(const unsigned char *block,
                                          unsigned char color[4][3]) {
  int i, d0, d1, d2, idx;
  unsigned int mask = 0;
  for (i = 15; i >= 0; --i) {
    mask <<= 2;
    d0 = color_distance(&block[4 * i], color[0]);
    d1 = color_distance(&block[4 * i], color[1]);
    d2 = color_distance(&block[4 * i], color[2]);
    if (block[4 * i + 3] < 128)
      idx = 3;
    else if (d0 < d1 && d0 < d2)
      idx = 0;
    else if (d1 < d2)
      idx = 1;
    else
      idx = 2;
    mask |= idx;
  }
  return (mask);
}


x86 assembly (clang, -O0):
<match_colors_block_DXT1alpha>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0xf,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1229 <match_colors_block_DXT1alpha+0x119>
mov    -0x28(%rbp),%eax
shl    $0x2,%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%eax
shl    $0x2,%eax
cltq
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <color_distance@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%eax
shl    $0x2,%eax
cltq
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
add    $0x3,%rsi
call   1030 <color_distance@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%eax
shl    $0x2,%eax
cltq
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
add    $0x6,%rsi
call   1030 <color_distance@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
shl    $0x2,%ecx
add    $0x3,%ecx
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0x80,%eax
jge    11c5 <match_colors_block_DXT1alpha+0xb5>
movl   $0x3,-0x24(%rbp)
jmp    1212 <match_colors_block_DXT1alpha+0x102>
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11e9 <match_colors_block_DXT1alpha+0xd9>
mov    -0x18(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    11e9 <match_colors_block_DXT1alpha+0xd9>
movl   $0x0,-0x24(%rbp)
jmp    120d <match_colors_block_DXT1alpha+0xfd>
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1201 <match_colors_block_DXT1alpha+0xf1>
movl   $0x1,-0x24(%rbp)
jmp    1208 <match_colors_block_DXT1alpha+0xf8>
movl   $0x2,-0x24(%rbp)
jmp    120d <match_colors_block_DXT1alpha+0xfd>
jmp    1212 <match_colors_block_DXT1alpha+0x102>
mov    -0x24(%rbp),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    112e <match_colors_block_DXT1alpha+0x1e>
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<match_colors_block_DXT1alpha>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x3(%rsi),%rax
mov    %rax,0x10(%rsp)
lea    0x6(%rsi),%rax
mov    %rax,0x8(%rsp)
add    $0x3c,%rbx
xor    %ebp,%ebp
mov    $0xfffffffffffffff0,%r12
jmp    116b <match_colors_block_DXT1alpha+0x5b>
xor    %edx,%edx
cmp    %eax,%r15d
setl   %dl
mov    $0x2,%ecx
sub    %edx,%ecx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
or     %ecx,%ebp
add    $0xfffffffffffffffc,%rbx
inc    %r12
je     11b2 <match_colors_block_DXT1alpha+0xa2>
shl    $0x2,%ebp
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <color_distance@plt>
mov    %eax,%r13d
mov    %rbx,%rdi
mov    0x10(%rsp),%rsi
call   1030 <color_distance@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
mov    0x8(%rsp),%rsi
call   1030 <color_distance@plt>
mov    $0x3,%ecx
cmpb   $0x0,0x3(%rbx)
jns    1160 <match_colors_block_DXT1alpha+0x50>
cmp    %r15d,%r13d
jge    1145 <match_colors_block_DXT1alpha+0x35>
xor    %ecx,%ecx
cmp    %eax,%r13d
jl     1160 <match_colors_block_DXT1alpha+0x50>
jmp    1145 <match_colors_block_DXT1alpha+0x35>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @match_colors_block_DXT1alpha(i8* %0, [3 x i8]* %1) {
%3 = alloca i8*
%4 = alloca [3 x i8]*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
store [3 x i8]* %1, [3 x i8]** %4
store i32 0, i32* %10
store i32 15, i32* %5
br label %11
11: ; preds = %75, %2
%12 = load i32, i32* %5
%13 = icmp sge i32 %12, 0
br i1 %13, label %14, label %78
14: ; preds = %11
%15 = load i32, i32* %10
%16 = shl i32 %15, 2
store i32 %16, i32* %10
%17 = load i8*, i8** %3
%18 = load i32, i32* %5
%19 = mul nsw i32 4, %18
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %17, i64 %20
%22 = load [3 x i8]*, [3 x i8]** %4
%23 = getelementptr inbounds [3 x i8], [3 x i8]* %22, i64 0
%24 = getelementptr inbounds [3 x i8], [3 x i8]* %23, i64 0, i64 0
%25 = call i32 @color_distance(i8* %21, i8* %24)
store i32 %25, i32* %6
%26 = load i8*, i8** %3
%27 = load i32, i32* %5
%28 = mul nsw i32 4, %27
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %26, i64 %29
%31 = load [3 x i8]*, [3 x i8]** %4
%32 = getelementptr inbounds [3 x i8], [3 x i8]* %31, i64 1
%33 = getelementptr inbounds [3 x i8], [3 x i8]* %32, i64 0, i64 0
%34 = call i32 @color_distance(i8* %30, i8* %33)
store i32 %34, i32* %7
%35 = load i8*, i8** %3
%36 = load i32, i32* %5
%37 = mul nsw i32 4, %36
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %35, i64 %38
%40 = load [3 x i8]*, [3 x i8]** %4
%41 = getelementptr inbounds [3 x i8], [3 x i8]* %40, i64 2
%42 = getelementptr inbounds [3 x i8], [3 x i8]* %41, i64 0, i64 0
%43 = call i32 @color_distance(i8* %39, i8* %42)
store i32 %43, i32* %8
%44 = load i8*, i8** %3
%45 = load i32, i32* %5
%46 = mul nsw i32 4, %45
%47 = add nsw i32 %46, 3
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %44, i64 %48
%50 = load i8, i8* %49
%51 = zext i8 %50 to i32
%52 = icmp slt i32 %51, 128
br i1 %52, label %53, label %54
53: ; preds = %14
store i32 3, i32* %9
br label %71
54: ; preds = %14
%55 = load i32, i32* %6
%56 = load i32, i32* %7
%57 = icmp slt i32 %55, %56
br i1 %57, label %58, label %63
58: ; preds = %54
%59 = load i32, i32* %6
%60 = load i32, i32* %8
%61 = icmp slt i32 %59, %60
br i1 %61, label %62, label %63
62: ; preds = %58
store i32 0, i32* %9
br label %70
63: ; preds = %58, %54
%64 = load i32, i32* %7
%65 = load i32, i32* %8
%66 = icmp slt i32 %64, %65
br i1 %66, label %67, label %68
67: ; preds = %63
store i32 1, i32* %9
br label %69
68: ; preds = %63
store i32 2, i32* %9
br label %69
69: ; preds = %68, %67
br label %70
70: ; preds = %69, %62
br label %71
71: ; preds = %70, %53
%72 = load i32, i32* %9
%73 = load i32, i32* %10
%74 = or i32 %73, %72
store i32 %74, i32* %10
br label %75
75: ; preds = %71
%76 = load i32, i32* %5
%77 = add nsw i32 %76, -1
store i32 %77, i32* %5
br label %11
78: ; preds = %11
%79 = load i32, i32* %10
ret i32 %79
}

LLVM IR (-O2):
define i32 @match_colors_block_DXT1alpha(i8* %0, [3 x i8]* %1) {
%3 = getelementptr inbounds [3 x i8], [3 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [3 x i8], [3 x i8]* %1, i64 1, i64 0
%5 = getelementptr inbounds [3 x i8], [3 x i8]* %1, i64 2, i64 0
br label %6
6: ; preds = %2, %26
%7 = phi i64 [ 15, %2 ], [ %29, %26 ]
%8 = phi i32 [ 0, %2 ], [ %28, %26 ]
%9 = shl i32 %8, 2
%10 = shl nsw i64 %7, 2
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = tail call i32 @color_distance(i8* %11, i8* %3)
%13 = tail call i32 @color_distance(i8* %11, i8* %4)
%14 = tail call i32 @color_distance(i8* %11, i8* %5)
%15 = or i64 %10, 3
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = load i8, i8* %16
%18 = icmp sgt i8 %17, -1
br i1 %18, label %26, label %19
19: ; preds = %6
%20 = icmp slt i32 %12, %13
%21 = icmp slt i32 %12, %14
%22 = select i1 %20, i1 %21, i1 false
br i1 %22, label %26, label %23
23: ; preds = %19
%24 = icmp slt i32 %13, %14
%25 = select i1 %24, i32 1, i32 2
br label %26
26: ; preds = %23, %19, %6
%27 = phi i32 [ 3, %6 ], [ 0, %19 ], [ %25, %23 ]
%28 = or i32 %27, %9
%29 = add nsw i64 %7, -1
%30 = icmp eq i64 %7, 0
br i1 %30, label %31, label %6
31: ; preds = %26
ret i32 %28
}