#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_etc2_punchthrough_get_mode(const unsigned char *bitstring) {
  int opaque = bitstring[3] & 2;
  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_etc2_punchthrough_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
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
and    $0xf8,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movzbl (%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 0x1(%rax),%eax
and    $0xf8,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movzbl 0x1(%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    -0x10(%rbp),%rax
movzbl 0x2(%rax),%eax
and    $0xf8,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movzbl 0x2(%rax),%eax
and    $0x7,%eax
movzbl %al,%edi
call   1030 <complement3bitshifted@plt>
mov    %rax,%rcx
movslq -0x20(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
and    $0xff07,%eax
cmp    $0x0,%eax
je     11d5 <block4x4_etc2_punchthrough_get_mode+0xc5>
movl   $0x2,-0x4(%rbp)
jmp    1216 <block4x4_etc2_punchthrough_get_mode+0x106>
mov    -0x1c(%rbp),%eax
and    $0xff07,%eax
cmp    $0x0,%eax
je     11f2 <block4x4_etc2_punchthrough_get_mode+0xe2>
movl   $0x3,-0x4(%rbp)
jmp    1216 <block4x4_etc2_punchthrough_get_mode+0x106>
mov    -0x20(%rbp),%eax
and    $0xff07,%eax
cmp    $0x0,%eax
je     120f <block4x4_etc2_punchthrough_get_mode+0xff>
movl   $0x4,-0x4(%rbp)
jmp    1216 <block4x4_etc2_punchthrough_get_mode+0x106>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<block4x4_etc2_punchthrough_get_mode>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
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    118b <block4x4_etc2_punchthrough_get_mode+0x7b>
and    $0xfffffff8,%r12d
add    %r12d,%r14d
mov    $0x3,%ecx
test   $0xff07,%r14d
jne    118b <block4x4_etc2_punchthrough_get_mode+0x7b>
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_etc2_punchthrough_get_mode(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = getelementptr inbounds i8, i8* %8, i64 3
%10 = load i8, i8* %9
%11 = zext i8 %10 to i32
%12 = and i32 %11, 2
store i32 %12, i32* %4
%13 = load i8*, i8** %3
%14 = getelementptr inbounds i8, i8* %13, i64 0
%15 = load i8, i8* %14
%16 = zext i8 %15 to i32
%17 = and i32 %16, 248
store i32 %17, i32* %5
%18 = load i8*, i8** %3
%19 = getelementptr inbounds i8, i8* %18, i64 0
%20 = load i8, i8* %19
%21 = zext i8 %20 to i32
%22 = and i32 %21, 7
%23 = trunc i32 %22 to i8
%24 = call i64 @complement3bitshifted(i8 zeroext %23)
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = add nsw i64 %26, %24
%28 = trunc i64 %27 to i32
store i32 %28, i32* %5
%29 = load i8*, i8** %3
%30 = getelementptr inbounds i8, i8* %29, i64 1
%31 = load i8, i8* %30
%32 = zext i8 %31 to i32
%33 = and i32 %32, 248
store i32 %33, i32* %6
%34 = load i8*, i8** %3
%35 = getelementptr inbounds i8, i8* %34, i64 1
%36 = load i8, i8* %35
%37 = zext i8 %36 to i32
%38 = and i32 %37, 7
%39 = trunc i32 %38 to i8
%40 = call i64 @complement3bitshifted(i8 zeroext %39)
%41 = load i32, i32* %6
%42 = sext i32 %41 to i64
%43 = add nsw i64 %42, %40
%44 = trunc i64 %43 to i32
store i32 %44, i32* %6
%45 = load i8*, i8** %3
%46 = getelementptr inbounds i8, i8* %45, i64 2
%47 = load i8, i8* %46
%48 = zext i8 %47 to i32
%49 = and i32 %48, 248
store i32 %49, i32* %7
%50 = load i8*, i8** %3
%51 = getelementptr inbounds i8, i8* %50, i64 2
%52 = load i8, i8* %51
%53 = zext i8 %52 to i32
%54 = and i32 %53, 7
%55 = trunc i32 %54 to i8
%56 = call i64 @complement3bitshifted(i8 zeroext %55)
%57 = load i32, i32* %7
%58 = sext i32 %57 to i64
%59 = add nsw i64 %58, %56
%60 = trunc i64 %59 to i32
store i32 %60, i32* %7
%61 = load i32, i32* %5
%62 = and i32 %61, 65287
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %65
64: ; preds = %1
store i32 2, i32* %2
br label %76
65: ; preds = %1
%66 = load i32, i32* %6
%67 = and i32 %66, 65287
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %70
69: ; preds = %65
store i32 3, i32* %2
br label %76
70: ; preds = %65
%71 = load i32, i32* %7
%72 = and i32 %71, 65287
%73 = icmp ne i32 %72, 0
br i1 %73, label %74, label %75
74: ; preds = %70
store i32 4, i32* %2
br label %76
75: ; preds = %70
store i32 1, i32* %2
br label %76
76: ; preds = %75, %74, %69, %64
%77 = load i32, i32* %2
ret i32 %77
}

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