#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 */
unsigned char compute_sbox_coef(int, int, int);
unsigned char apply_bit_sbox(int chip, unsigned char state, int sel) {
  int i;
  unsigned char r = 0;
  for (i = 0; i < 8; i++)
    if (state & (1 << i))
      r ^= compute_sbox_coef(chip, sel, i);
  return r;
}


x86 assembly (clang, -O0):
<apply_bit_sbox>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %edi,-0x4(%rbp)
mov    %al,-0x5(%rbp)
mov    %edx,-0xc(%rbp)
movb   $0x0,-0x11(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x8,-0x10(%rbp)
jge    1181 <apply_bit_sbox+0x71>
movzbl -0x5(%rbp),%eax
mov    -0x10(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     116e <apply_bit_sbox+0x5e>
mov    -0x4(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1030 <compute_sbox_coef@plt>
movzbl %al,%ecx
movzbl -0x11(%rbp),%eax
xor    %ecx,%eax
mov    %al,-0x11(%rbp)
jmp    1173 <apply_bit_sbox+0x63>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    112f <apply_bit_sbox+0x1f>
movzbl -0x11(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<apply_bit_sbox>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%ebx
mov    %edi,%r15d
test   $0x1,%bl
jne    112d <apply_bit_sbox+0x1d>
xor    %ebp,%ebp
test   $0x2,%bl
jne    1141 <apply_bit_sbox+0x31>
jmp    1156 <apply_bit_sbox+0x46>
mov    %r15d,%edi
mov    %r14d,%esi
xor    %edx,%edx
call   1030 <compute_sbox_coef@plt>
mov    %eax,%ebp
test   $0x2,%bl
je     1156 <apply_bit_sbox+0x46>
mov    %r15d,%edi
mov    %r14d,%esi
mov    $0x1,%edx
call   1030 <compute_sbox_coef@plt>
xor    %bpl,%al
mov    %eax,%ebp
test   $0x4,%bl
jne    119a <apply_bit_sbox+0x8a>
test   $0x8,%bl
jne    11b4 <apply_bit_sbox+0xa4>
test   $0x10,%bl
jne    11ce <apply_bit_sbox+0xbe>
test   $0x20,%bl
jne    11e8 <apply_bit_sbox+0xd8>
test   $0x40,%bl
jne    1206 <apply_bit_sbox+0xf6>
test   $0x80,%bl
je     118d <apply_bit_sbox+0x7d>
mov    %r15d,%edi
mov    %r14d,%esi
mov    $0x7,%edx
call   1030 <compute_sbox_coef@plt>
xor    %bpl,%al
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15d,%edi
mov    %r14d,%esi
mov    $0x2,%edx
call   1030 <compute_sbox_coef@plt>
xor    %bpl,%al
mov    %eax,%ebp
test   $0x8,%bl
je     1160 <apply_bit_sbox+0x50>
mov    %r15d,%edi
mov    %r14d,%esi
mov    $0x3,%edx
call   1030 <compute_sbox_coef@plt>
xor    %bpl,%al
mov    %eax,%ebp
test   $0x10,%bl
je     1165 <apply_bit_sbox+0x55>
mov    %r15d,%edi
mov    %r14d,%esi
mov    $0x4,%edx
call   1030 <compute_sbox_coef@plt>
xor    %bpl,%al
mov    %eax,%ebp
test   $0x20,%bl
je     116a <apply_bit_sbox+0x5a>
mov    %r15d,%edi
mov    %r14d,%esi
mov    $0x5,%edx
call   1030 <compute_sbox_coef@plt>
xor    %bpl,%al
mov    %eax,%ebp
test   $0x40,%bl
je     1173 <apply_bit_sbox+0x63>
mov    %r15d,%edi
mov    %r14d,%esi
mov    $0x6,%edx
call   1030 <compute_sbox_coef@plt>
xor    %bpl,%al
mov    %eax,%ebp
test   $0x80,%bl
jne    1178 <apply_bit_sbox+0x68>
jmp    118d <apply_bit_sbox+0x7d>

LLVM IR (-O0):
define zeroext i8 @apply_bit_sbox(i32 %0, i8 zeroext %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
store i32 %0, i32* %4
store i8 %1, i8* %5
store i32 %2, i32* %6
store i8 0, i8* %8
store i32 0, i32* %7
br label %9
9: ; preds = %30, %3
%10 = load i32, i32* %7
%11 = icmp slt i32 %10, 8
br i1 %11, label %12, label %33
12: ; preds = %9
%13 = load i8, i8* %5
%14 = zext i8 %13 to i32
%15 = load i32, i32* %7
%16 = shl i32 1, %15
%17 = and i32 %14, %16
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %29
19: ; preds = %12
%20 = load i32, i32* %4
%21 = load i32, i32* %6
%22 = load i32, i32* %7
%23 = call zeroext i8 @compute_sbox_coef(i32 %20, i32 %21, i32 %22)
%24 = zext i8 %23 to i32
%25 = load i8, i8* %8
%26 = zext i8 %25 to i32
%27 = xor i32 %26, %24
%28 = trunc i32 %27 to i8
store i8 %28, i8* %8
br label %29
29: ; preds = %19, %12
br label %30
30: ; preds = %29
%31 = load i32, i32* %7
%32 = add nsw i32 %31, 1
store i32 %32, i32* %7
br label %9
33: ; preds = %9
%34 = load i8, i8* %8
ret i8 %34
}

LLVM IR (-O2):
define zeroext i8 @apply_bit_sbox(i32 %0, i8 zeroext %1, i32 %2) {
%4 = zext i8 %1 to i32
%5 = and i32 %4, 1
%6 = icmp eq i32 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %3
%8 = tail call zeroext i8 @compute_sbox_coef(i32 %0, i32 %2, i32 0)
br label %9
9: ; preds = %3, %7
%10 = phi i8 [ %8, %7 ], [ 0, %3 ]
%11 = and i32 %4, 2
%12 = icmp eq i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %9
%14 = tail call zeroext i8 @compute_sbox_coef(i32 %0, i32 %2, i32 1)
%15 = xor i8 %14, %10
br label %16
16: ; preds = %13, %9
%17 = phi i8 [ %15, %13 ], [ %10, %9 ]
%18 = and i32 %4, 4
%19 = icmp eq i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = tail call zeroext i8 @compute_sbox_coef(i32 %0, i32 %2, i32 2)
%22 = xor i8 %21, %17
br label %23
23: ; preds = %20, %16
%24 = phi i8 [ %22, %20 ], [ %17, %16 ]
%25 = and i32 %4, 8
%26 = icmp eq i32 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %23
%28 = tail call zeroext i8 @compute_sbox_coef(i32 %0, i32 %2, i32 3)
%29 = xor i8 %28, %24
br label %30
30: ; preds = %27, %23
%31 = phi i8 [ %29, %27 ], [ %24, %23 ]
%32 = and i32 %4, 16
%33 = icmp eq i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %30
%35 = tail call zeroext i8 @compute_sbox_coef(i32 %0, i32 %2, i32 4)
%36 = xor i8 %35, %31
br label %37
37: ; preds = %34, %30
%38 = phi i8 [ %36, %34 ], [ %31, %30 ]
%39 = and i32 %4, 32
%40 = icmp eq i32 %39, 0
br i1 %40, label %44, label %41
41: ; preds = %37
%42 = tail call zeroext i8 @compute_sbox_coef(i32 %0, i32 %2, i32 5)
%43 = xor i8 %42, %38
br label %44
44: ; preds = %41, %37
%45 = phi i8 [ %43, %41 ], [ %38, %37 ]
%46 = and i32 %4, 64
%47 = icmp eq i32 %46, 0
br i1 %47, label %51, label %48
48: ; preds = %44
%49 = tail call zeroext i8 @compute_sbox_coef(i32 %0, i32 %2, i32 6)
%50 = xor i8 %49, %45
br label %51
51: ; preds = %48, %44
%52 = phi i8 [ %50, %48 ], [ %45, %44 ]
%53 = and i32 %4, 128
%54 = icmp eq i32 %53, 0
br i1 %54, label %58, label %55
55: ; preds = %51
%56 = tail call zeroext i8 @compute_sbox_coef(i32 %0, i32 %2, i32 7)
%57 = xor i8 %56, %52
br label %58
58: ; preds = %55, %51
%59 = phi i8 [ %57, %55 ], [ %52, %51 ]
ret i8 %59
}