void ximage_convert_bgr233(const unsigned char *__restrict src, int srcstride,
                           unsigned char *__restrict dst, int dststride, int w,
                           int h) {
  unsigned char r, g, b;
  int x, y;
  for (y = 0; y < h; y++) {
    for (x = 0; x < w; x++) {
      r = src[4 * x + 0];
      g = src[4 * x + 1];
      b = src[4 * x + 2];
      dst[x] = (b & 0xC0) | ((g >> 2) & 0x38) | ((r >> 5) & 0x7);
    }
    src += srcstride;
    dst += dststride;
  }
}


x86 assembly (clang, -O0):
<ximage_convert_bgr233>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    %r9d,-0x24(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    11ee <ximage_convert_bgr233+0xee>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    11be <ximage_convert_bgr233+0xbe>
mov    -0x8(%rbp),%rax
mov    -0x2c(%rbp),%ecx
shl    $0x2,%ecx
add    $0x0,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x25(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x2c(%rbp),%ecx
shl    $0x2,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x26(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x2c(%rbp),%ecx
shl    $0x2,%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x27(%rbp)
movzbl -0x27(%rbp),%eax
and    $0xc0,%eax
movzbl -0x26(%rbp),%ecx
sar    $0x2,%ecx
and    $0x38,%ecx
or     %ecx,%eax
movzbl -0x25(%rbp),%ecx
sar    $0x5,%ecx
and    $0x7,%ecx
or     %ecx,%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1134 <ximage_convert_bgr233+0x34>
mov    -0xc(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1121 <ximage_convert_bgr233+0x21>
pop    %rbp
ret

x86 assembly (clang, -O2):
<ximage_convert_bgr233>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
test   %r9d,%r9d
jle    14d1 <ximage_convert_bgr233+0x3d1>
movslq %esi,%r11
movslq %ecx,%r14
mov    %r8d,%esi
mov    %esi,%eax
and    $0xf,%eax
test   %rax,%rax
mov    $0x10,%r10d
cmovne %rax,%r10
mov    %rsi,%r15
sub    %r10,%r15
mov    %esi,%eax
and    $0x7,%eax
test   %rax,%rax
mov    $0x8,%ebx
cmovne %rax,%rbx
mov    %rsi,%r13
sub    %rbx,%r13
xor    %r12d,%r12d
movdqa 0xea9(%rip),%xmm13
pxor   %xmm15,%xmm15
movdqa 0xeab(%rip),%xmm14
movdqa 0xee3(%rip),%xmm6
movdqa 0xeea(%rip),%xmm11
movdqa 0xec1(%rip),%xmm12
movaps 0xe99(%rip),%xmm9
movdqa 0xea0(%rip),%xmm10
jmp    11b3 <ximage_convert_bgr233+0xb3>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    %r11,%rdi
add    %r14,%rdx
add    $0x1,%r12d
cmp    %r9d,%r12d
je     14d1 <ximage_convert_bgr233+0x3d1>
test   %r8d,%r8d
jle    11a0 <ximage_convert_bgr233+0xa0>
cmp    $0x9,%r8d
jae    11d0 <ximage_convert_bgr233+0xd0>
xor    %ebx,%ebx
jmp    14a0 <ximage_convert_bgr233+0x3a0>
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
cmp    $0x11,%r8d
jb     13a0 <ximage_convert_bgr233+0x2a0>
nopl   0x0(%rax)
movdqu (%rdi,%rax,4),%xmm7
movdqu 0x10(%rdi,%rax,4),%xmm3
movdqu 0x20(%rdi,%rax,4),%xmm1
movdqu 0x30(%rdi,%rax,4),%xmm5
movdqa %xmm5,%xmm0
pand   %xmm13,%xmm0
movdqa %xmm1,%xmm4
pand   %xmm13,%xmm4
packuswb %xmm0,%xmm4
movdqa %xmm3,%xmm0
pand   %xmm13,%xmm0
movdqa %xmm7,%xmm6
pand   %xmm13,%xmm6
packuswb %xmm0,%xmm6
packuswb %xmm4,%xmm6
movdqa %xmm5,%xmm0
punpckhbw %xmm15,%xmm0
pshufd $0xe8,%xmm0,%xmm0
pshuflw $0xd4,%xmm0,%xmm0
movdqa %xmm5,%xmm4
punpcklbw %xmm15,%xmm4
pshufd $0xe8,%xmm4,%xmm4
pshuflw $0xd4,%xmm4,%xmm4
punpckldq %xmm0,%xmm4
movdqa %xmm1,%xmm0
punpckhbw %xmm15,%xmm0
pshufd $0xe8,%xmm0,%xmm0
pshuflw $0xed,%xmm0,%xmm0
movdqa %xmm1,%xmm2
punpcklbw %xmm15,%xmm2
pshufd $0xe8,%xmm2,%xmm2
pshuflw $0xed,%xmm2,%xmm2
punpckldq %xmm0,%xmm2
packuswb %xmm4,%xmm2
movdqa %xmm3,%xmm0
punpckhbw %xmm15,%xmm0
pshufd $0xe8,%xmm0,%xmm0
pshuflw $0xd4,%xmm0,%xmm0
movdqa %xmm3,%xmm4
punpcklbw %xmm15,%xmm4
pshufd $0xe8,%xmm4,%xmm4
pshuflw $0xd4,%xmm4,%xmm4
punpckldq %xmm0,%xmm4
movdqa %xmm7,%xmm0
punpckhbw %xmm15,%xmm0
pshufd $0xe8,%xmm0,%xmm0
pshuflw $0xed,%xmm0,%xmm8
movdqa %xmm7,%xmm0
punpcklbw %xmm15,%xmm0
pshufd $0xe8,%xmm0,%xmm0
pshuflw $0xed,%xmm0,%xmm0
punpckldq %xmm8,%xmm0
packuswb %xmm4,%xmm0
shufps $0xcc,%xmm2,%xmm0
pand   %xmm14,%xmm5
pshuflw $0xe7,%xmm5,%xmm2
pshufhw $0xe7,%xmm2,%xmm2
pshufd $0x24,%xmm2,%xmm2
pshufhw $0x1b,%xmm2,%xmm2
pand   %xmm14,%xmm1
pshuflw $0xe7,%xmm1,%xmm1
pshufhw $0xe7,%xmm1,%xmm1
pshufd $0xe8,%xmm1,%xmm1
pshuflw $0xb1,%xmm1,%xmm1
packuswb %xmm2,%xmm1
pand   %xmm14,%xmm3
pshuflw $0xe7,%xmm3,%xmm2
pshufhw $0xe7,%xmm2,%xmm2
pshufd $0x24,%xmm2,%xmm2
pshufhw $0x1b,%xmm2,%xmm2
pand   %xmm14,%xmm7
pshuflw $0xe7,%xmm7,%xmm3
pshufhw $0xe7,%xmm3,%xmm3
pshufd $0xe8,%xmm3,%xmm3
pshuflw $0xb1,%xmm3,%xmm3
packuswb %xmm2,%xmm3
shufps $0xcc,%xmm1,%xmm3
andps  %xmm9,%xmm3
psrlw  $0x2,%xmm0
pand   %xmm10,%xmm0
psrlw  $0x5,%xmm6
pand   %xmm12,%xmm6
por    %xmm0,%xmm6
por    %xmm3,%xmm6
movdqu %xmm6,(%rdx,%rax,1)
add    $0x10,%rax
cmp    %rax,%r15
jne    11e0 <ximage_convert_bgr233+0xe0>
mov    %r15,%rax
mov    %r15,%rbx
cmp    $0x9,%r10d
movdqa 0xcbf(%rip),%xmm6
jb     14a0 <ximage_convert_bgr233+0x3a0>
nopw   0x0(%rax,%rax,1)
movdqu (%rdi,%rax,4),%xmm0
movdqu 0x10(%rdi,%rax,4),%xmm4
movdqa %xmm4,%xmm1
pand   %xmm13,%xmm1
movdqa %xmm0,%xmm3
pand   %xmm13,%xmm3
packuswb %xmm1,%xmm3
packuswb %xmm3,%xmm3
movdqa %xmm4,%xmm1
punpckhbw %xmm15,%xmm1
pshufd $0xe8,%xmm1,%xmm1
pshuflw $0xd4,%xmm1,%xmm1
movdqa %xmm4,%xmm2
punpcklbw %xmm15,%xmm2
pshufd $0xe8,%xmm2,%xmm2
pshuflw $0xd4,%xmm2,%xmm2
punpckldq %xmm1,%xmm2
movdqa %xmm0,%xmm1
punpckhbw %xmm15,%xmm1
pshufd $0xe8,%xmm1,%xmm1
pshuflw $0xed,%xmm1,%xmm1
movdqa %xmm0,%xmm5
punpcklbw %xmm15,%xmm5
pshufd $0xe8,%xmm5,%xmm5
pshuflw $0xed,%xmm5,%xmm5
punpckldq %xmm1,%xmm5
packuswb %xmm2,%xmm5
pshufd $0xec,%xmm5,%xmm1
pand   %xmm14,%xmm4
pshuflw $0xe7,%xmm4,%xmm2
pshufhw $0xe7,%xmm2,%xmm2
pshufd $0x24,%xmm2,%xmm2
pshufhw $0x1b,%xmm2,%xmm2
pand   %xmm14,%xmm0
pshuflw $0xe7,%xmm0,%xmm0
pshufhw $0xe7,%xmm0,%xmm0
pshufd $0xe8,%xmm0,%xmm0
pshuflw $0xb1,%xmm0,%xmm0
packuswb %xmm2,%xmm0
pshufd $0xec,%xmm0,%xmm0
pand   %xmm6,%xmm0
psrlw  $0x2,%xmm1
pand   %xmm11,%xmm1
psrlw  $0x5,%xmm3
pand   %xmm12,%xmm3
por    %xmm1,%xmm3
por    %xmm0,%xmm3
movq   %xmm3,(%rdx,%rax,1)
add    $0x8,%rax
cmp    %rax,%r13
jne    13a0 <ximage_convert_bgr233+0x2a0>
mov    %r13,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl (%rdi,%rbx,4),%eax
movzbl 0x1(%rdi,%rbx,4),%ecx
movzbl 0x2(%rdi,%rbx,4),%ebp
and    $0xc0,%bpl
shr    $0x2,%cl
and    $0x38,%cl
shr    $0x5,%al
or     %cl,%al
or     %bpl,%al
mov    %al,(%rdx,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%rsi
jne    14a0 <ximage_convert_bgr233+0x3a0>
jmp    11a0 <ximage_convert_bgr233+0xa0>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @ximage_convert_bgr233(i8* noalias nocapture %0, i32 %1, i8* noalias nocapture writeonly %2, i32 %3, i32 %4, i32 %5) {
%7 = icmp sgt i32 %5, 0
br i1 %7, label %8, label %98
8: ; preds = %6
%9 = icmp sgt i32 %4, 0
%10 = sext i32 %1 to i64
%11 = sext i32 %3 to i64
%12 = zext i32 %4 to i64
%13 = icmp ult i32 %4, 9
%14 = icmp ult i32 %4, 17
%15 = and i64 %12, 15
%16 = icmp eq i64 %15, 0
%17 = select i1 %16, i64 16, i64 %15
%18 = sub nsw i64 %12, %17
%19 = icmp ult i64 %17, 9
%20 = and i64 %12, 7
%21 = icmp eq i64 %20, 0
%22 = select i1 %21, i64 8, i64 %20
%23 = sub nsw i64 %12, %22
br label %24
24: ; preds = %8, %93
%25 = phi i32 [ 0, %8 ], [ %96, %93 ]
%26 = phi i8* [ %0, %8 ], [ %94, %93 ]
%27 = phi i8* [ %2, %8 ], [ %95, %93 ]
br i1 %9, label %28, label %93
28: ; preds = %24
br i1 %13, label %71, label %29
29: ; preds = %28
br i1 %14, label %50, label %30
30: ; preds = %29, %30
%31 = phi i64 [ %47, %30 ], [ 0, %29 ]
%32 = shl nsw i64 %31, 2
%33 = getelementptr inbounds i8, i8* %26, i64 %32
%34 = bitcast i8* %33 to <64 x i8>*
%35 = load <64 x i8>, <64 x i8>* %34
%36 = shufflevector <64 x i8> %35, <64 x i8> poison, <16 x i32> <i32 0, i32 4, i32 8, i32 12, i32 16, i32 20, i32 24, i32 28, i32 32, i32 36, i32 40, i32 44, i32 48, i32 52, i32 56, i32 60>
%37 = shufflevector <64 x i8> %35, <64 x i8> poison, <16 x i32> <i32 1, i32 5, i32 9, i32 13, i32 17, i32 21, i32 25, i32 29, i32 33, i32 37, i32 41, i32 45, i32 49, i32 53, i32 57, i32 61>
%38 = shufflevector <64 x i8> %35, <64 x i8> poison, <16 x i32> <i32 2, i32 6, i32 10, i32 14, i32 18, i32 22, i32 26, i32 30, i32 34, i32 38, i32 42, i32 46, i32 50, i32 54, i32 58, i32 62>
%39 = and <16 x i8> %38, <i8 -64, i8 -64, i8 -64, i8 -64, i8 -64, i8 -64, i8 -64, i8 -64, i8 -64, i8 -64, i8 -64, i8 -64, i8 -64, i8 -64, i8 -64, i8 -64>
%40 = lshr <16 x i8> %37, <i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2>
%41 = and <16 x i8> %40, <i8 56, i8 56, i8 56, i8 56, i8 56, i8 56, i8 56, i8 56, i8 56, i8 56, i8 56, i8 56, i8 56, i8 56, i8 56, i8 56>
%42 = lshr <16 x i8> %36, <i8 5, i8 5, i8 5, i8 5, i8 5, i8 5, i8 5, i8 5, i8 5, i8 5, i8 5, i8 5, i8 5, i8 5, i8 5, i8 5>
%43 = or <16 x i8> %41, %42
%44 = or <16 x i8> %43, %39
%45 = getelementptr inbounds i8, i8* %27, i64 %31
%46 = bitcast i8* %45 to <16 x i8>*
store <16 x i8> %44, <16 x i8>* %46
%47 = add nuw i64 %31, 16
%48 = icmp eq i64 %47, %18
br i1 %48, label %49, label %30
49: ; preds = %30
br i1 %19, label %71, label %50
50: ; preds = %29, %49
%51 = phi i64 [ %18, %49 ], [ 0, %29 ]
br label %52
52: ; preds = %52, %50
%53 = phi i64 [ %51, %50 ], [ %69, %52 ]
%54 = shl nsw i64 %53, 2
%55 = getelementptr inbounds i8, i8* %26, i64 %54
%56 = bitcast i8* %55 to <32 x i8>*
%57 = load <32 x i8>, <32 x i8>* %56
%58 = shufflevector <32 x i8> %57, <32 x i8> poison, <8 x i32> <i32 0, i32 4, i32 8, i32 12, i32 16, i32 20, i32 24, i32 28>
%59 = shufflevector <32 x i8> %57, <32 x i8> poison, <8 x i32> <i32 1, i32 5, i32 9, i32 13, i32 17, i32 21, i32 25, i32 29>
%60 = shufflevector <32 x i8> %57, <32 x i8> poison, <8 x i32> <i32 2, i32 6, i32 10, i32 14, i32 18, i32 22, i32 26, i32 30>
%61 = and <8 x i8> %60, <i8 -64, i8 -64, i8 -64, i8 -64, i8 -64, i8 -64, i8 -64, i8 -64>
%62 = lshr <8 x i8> %59, <i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2>
%63 = and <8 x i8> %62, <i8 56, i8 56, i8 56, i8 56, i8 56, i8 56, i8 56, i8 56>
%64 = lshr <8 x i8> %58, <i8 5, i8 5, i8 5, i8 5, i8 5, i8 5, i8 5, i8 5>
%65 = or <8 x i8> %63, %64
%66 = or <8 x i8> %65, %61
%67 = getelementptr inbounds i8, i8* %27, i64 %53
%68 = bitcast i8* %67 to <8 x i8>*
store <8 x i8> %66, <8 x i8>* %68
%69 = add nuw i64 %53, 8
%70 = icmp eq i64 %69, %23
br i1 %70, label %71, label %52
71: ; preds = %52, %28, %49
%72 = phi i64 [ 0, %28 ], [ %18, %49 ], [ %23, %52 ]
br label %73
73: ; preds = %71, %73
%74 = phi i64 [ %91, %73 ], [ %72, %71 ]
%75 = shl nsw i64 %74, 2
%76 = getelementptr inbounds i8, i8* %26, i64 %75
%77 = load i8, i8* %76
%78 = or i64 %75, 1
%79 = getelementptr inbounds i8, i8* %26, i64 %78
%80 = load i8, i8* %79
%81 = or i64 %75, 2
%82 = getelementptr inbounds i8, i8* %26, i64 %81
%83 = load i8, i8* %82
%84 = and i8 %83, -64
%85 = lshr i8 %80, 2
%86 = and i8 %85, 56
%87 = lshr i8 %77, 5
%88 = or i8 %86, %87
%89 = or i8 %88, %84
%90 = getelementptr inbounds i8, i8* %27, i64 %74
store i8 %89, i8* %90
%91 = add nuw nsw i64 %74, 1
%92 = icmp eq i64 %91, %12
br i1 %92, label %93, label %73
93: ; preds = %73, %24
%94 = getelementptr inbounds i8, i8* %26, i64 %10
%95 = getelementptr inbounds i8, i8* %27, i64 %11
%96 = add nuw nsw i32 %25, 1
%97 = icmp eq i32 %96, %5
br i1 %97, label %98, label %24
98: ; preds = %93, %6
ret void
}