#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 */
scalar_t__ complement3bitshifted(unsigned char const);
int block4x4_rgb8_etc2_get_mode(const unsigned char *bitstring) {
  if ((bitstring[3] & 2) == 0)
    return 0;
  int R = (bitstring[0] & 0xF8);
  R += complement3bitshifted(bitstring[0] & 7);
  int G = (bitstring[1] & 0xF8);
  G += complement3bitshifted(bitstring[1] & 7);
  int B = (bitstring[2] & 0xF8);
  B += complement3bitshifted(bitstring[2] & 7);
  if (R & 0xFF07)
    return 2;
  else if (G & 0xFF07)
    return 3;
  else if (B & 0xFF07)
    return 4;
  else
    return 1;
}


x86 assembly (clang, -O0):
<block4x4_rgb8_etc2_get_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movzbl 0x3(%rax),%eax
and    $0x2,%eax
cmp    $0x0,%eax
jne    113c <block4x4_rgb8_etc2_get_mode+0x2c>
movl   $0x0,-0x4(%rbp)
jmp    1228 <block4x4_rgb8_etc2_get_mode+0x118>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
and    $0xf8,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
and    $0x7,%eax
movzbl %al,%edi
call   1030 <complement3bitshifted@plt>
mov    %rax,%rcx
movslq -0x14(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%eax
and    $0xf8,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%eax
and    $0x7,%eax
movzbl %al,%edi
call   1030 <complement3bitshifted@plt>
mov    %rax,%rcx
movslq -0x18(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movzbl 0x2(%rax),%eax
and    $0xf8,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movzbl 0x2(%rax),%eax
and    $0x7,%eax
movzbl %al,%edi
call   1030 <complement3bitshifted@plt>
mov    %rax,%rcx
movslq -0x1c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
and    $0xff07,%eax
cmp    $0x0,%eax
je     11e7 <block4x4_rgb8_etc2_get_mode+0xd7>
movl   $0x2,-0x4(%rbp)
jmp    1228 <block4x4_rgb8_etc2_get_mode+0x118>
mov    -0x18(%rbp),%eax
and    $0xff07,%eax
cmp    $0x0,%eax
je     1204 <block4x4_rgb8_etc2_get_mode+0xf4>
movl   $0x3,-0x4(%rbp)
jmp    1228 <block4x4_rgb8_etc2_get_mode+0x118>
mov    -0x1c(%rbp),%eax
and    $0xff07,%eax
cmp    $0x0,%eax
je     1221 <block4x4_rgb8_etc2_get_mode+0x111>
movl   $0x4,-0x4(%rbp)
jmp    1228 <block4x4_rgb8_etc2_get_mode+0x118>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<block4x4_rgb8_etc2_get_mode>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %ecx,%ecx
testb  $0x2,0x3(%rdi)
je     1193 <block4x4_rgb8_etc2_get_mode+0x83>
mov    %rdi,%rbx
movzbl (%rdi),%edi
mov    %edi,%ebp
and    $0xfffffff8,%ebp
and    $0x7,%edi
call   1030 <complement3bitshifted@plt>
mov    %rax,%r15
add    %ebp,%r15d
movzbl 0x1(%rbx),%r12d
mov    %r12d,%edi
and    $0x7,%edi
call   1030 <complement3bitshifted@plt>
mov    %rax,%r14
movzbl 0x2(%rbx),%ebx
mov    %ebx,%edi
and    $0x7,%edi
call   1030 <complement3bitshifted@plt>
mov    $0x2,%ecx
test   $0xff07,%r15d
jne    1193 <block4x4_rgb8_etc2_get_mode+0x83>
and    $0xfffffff8,%r12d
add    %r12d,%r14d
mov    $0x3,%ecx
test   $0xff07,%r14d
jne    1193 <block4x4_rgb8_etc2_get_mode+0x83>
and    $0xfffffff8,%ebx
add    %rbx,%rax
xor    %ecx,%ecx
test   $0xff07,%eax
setne  %cl
lea    (%rcx,%rcx,2),%ecx
add    $0x1,%ecx
mov    %ecx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @block4x4_rgb8_etc2_get_mode(i8* nocapture %0) {
%2 = getelementptr inbounds i8, i8* %0, i64 3
%3 = load i8, i8* %2
%4 = and i8 %3, 2
%5 = icmp eq i8 %4, 0
br i1 %5, label %36, label %6
6: ; preds = %1
%7 = load i8, i8* %0
%8 = and i8 %7, -8
%9 = zext i8 %8 to i64
%10 = and i8 %7, 7
%11 = tail call i64 @complement3bitshifted(i8 zeroext %10)
%12 = add nsw i64 %11, %9
%13 = getelementptr inbounds i8, i8* %0, i64 1
%14 = load i8, i8* %13
%15 = and i8 %14, 7
%16 = tail call i64 @complement3bitshifted(i8 zeroext %15)
%17 = getelementptr inbounds i8, i8* %0, i64 2
%18 = load i8, i8* %17
%19 = and i8 %18, -8
%20 = zext i8 %19 to i64
%21 = and i8 %18, 7
%22 = tail call i64 @complement3bitshifted(i8 zeroext %21)
%23 = add nsw i64 %22, %20
%24 = and i64 %12, 65287
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %36
26: ; preds = %6
%27 = and i8 %14, -8
%28 = zext i8 %27 to i64
%29 = add nsw i64 %16, %28
%30 = and i64 %29, 65287
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %26
%33 = and i64 %23, 65287
%34 = icmp eq i64 %33, 0
%35 = select i1 %34, i32 1, i32 4
br label %36
36: ; preds = %6, %26, %32, %1
%37 = phi i32 [ 0, %1 ], [ 2, %6 ], [ 3, %26 ], [ %35, %32 ]
ret i32 %37
}