int i_to_brc(int brc, int where) {
  switch (brc) {
  case 0:
    return (((i_to_brc(1, where) / 3) * 3) + (i_to_brc(2, where) / 3));
  case 1:
    return (where / 9);
  case 2:
    return (where % 9);
  }
  return (0);
}


x86 assembly (clang, -O0):
<i_to_brc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
test   %eax,%eax
je     1153 <i_to_brc+0x43>
jmp    1131 <i_to_brc+0x21>
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
je     1192 <i_to_brc+0x82>
jmp    1142 <i_to_brc+0x32>
mov    -0x10(%rbp),%eax
sub    $0x2,%eax
je     11a5 <i_to_brc+0x95>
jmp    11b8 <i_to_brc+0xa8>
mov    -0xc(%rbp),%esi
mov    $0x1,%edi
call   1030 <i_to_brc@plt>
mov    $0x3,%ecx
cltd
idiv   %ecx
imul   $0x3,%eax,%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%esi
mov    $0x2,%edi
call   1030 <i_to_brc@plt>
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11bf <i_to_brc+0xaf>
mov    -0xc(%rbp),%eax
mov    $0x9,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
jmp    11bf <i_to_brc+0xaf>
mov    -0xc(%rbp),%eax
mov    $0x9,%ecx
cltd
idiv   %ecx
mov    %edx,-0x4(%rbp)
jmp    11bf <i_to_brc+0xaf>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i_to_brc>:
cmp    $0x2,%edi
je     115f <i_to_brc+0x5f>
cmp    $0x1,%edi
je     117c <i_to_brc+0x7c>
xor    %eax,%eax
test   %edi,%edi
jne    117b <i_to_brc+0x7b>
movslq %esi,%rax
imul   $0x38e38e39,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x21,%rax
add    %ecx,%eax
cltq
imul   $0x55555556,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
shr    $0x20,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,2),%ecx
lea    (%rax,%rax,8),%eax
sub    %eax,%esi
movsbl %sil,%eax
imul   $0x56,%eax,%eax
movzwl %ax,%eax
mov    %eax,%edx
shr    $0xf,%edx
shr    $0x8,%eax
add    %dl,%al
movsbl %al,%eax
add    %ecx,%eax
ret
movslq %esi,%rax
imul   $0x38e38e39,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x21,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,8),%ecx
sub    %ecx,%eax
ret
movslq %esi,%rax
imul   $0x38e38e39,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x21,%rax
add    %ecx,%eax
ret

LLVM IR (-O0):
define i32 @i_to_brc(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%6 = load i32, i32* %4
switch i32 %6, label %22 [
i32 0, label %7
i32 1, label %16
i32 2, label %19
]
7: ; preds = %2
%8 = load i32, i32* %5
%9 = call i32 @i_to_brc(i32 1, i32 %8)
%10 = sdiv i32 %9, 3
%11 = mul nsw i32 %10, 3
%12 = load i32, i32* %5
%13 = call i32 @i_to_brc(i32 2, i32 %12)
%14 = sdiv i32 %13, 3
%15 = add nsw i32 %11, %14
store i32 %15, i32* %3
br label %23
16: ; preds = %2
%17 = load i32, i32* %5
%18 = sdiv i32 %17, 9
store i32 %18, i32* %3
br label %23
19: ; preds = %2
%20 = load i32, i32* %5
%21 = srem i32 %20, 9
store i32 %21, i32* %3
br label %23
22: ; preds = %2
store i32 0, i32* %3
br label %23
23: ; preds = %22, %19, %16, %7
%24 = load i32, i32* %3
ret i32 %24
}

LLVM IR (-O2):
define i32 @i_to_brc(i32 %0, i32 %1) {
switch i32 %0, label %16 [
i32 0, label %3
i32 1, label %12
i32 2, label %14
]
3: ; preds = %2
%4 = sdiv i32 %1, 9
%5 = srem i32 %4, 3
%6 = sub nsw i32 %4, %5
%7 = srem i32 %1, 9
%8 = trunc i32 %7 to i8
%9 = sdiv i8 %8, 3
%10 = sext i8 %9 to i32
%11 = add nsw i32 %6, %10
br label %16
12: ; preds = %2
%13 = sdiv i32 %1, 9
br label %16
14: ; preds = %2
%15 = srem i32 %1, 9
br label %16
16: ; preds = %2, %14, %12, %3
%17 = phi i32 [ %15, %14 ], [ %13, %12 ], [ %11, %3 ], [ 0, %2 ]
ret i32 %17
}