#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 */
int ternary_xor(int, int);
int ternary_xor_b8(int a, int b) {
  int i;
  int n, m;
  for (n = 0, i = 0; i < 5; ++i) {
    n = n * 3 + ternary_xor(a % 3, b % 3);
    a /= 3;
    b /= 3;
  }
  for (m = 0, i = 0; i < 5; ++i) {
    m = m * 3 + n % 3;
    n /= 3;
  }
  return m;
}


x86 assembly (clang, -O0):
<ternary_xor_b8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x5,-0xc(%rbp)
jge    1190 <ternary_xor_b8+0x80>
imul   $0x3,-0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
mov    -0x8(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %edx,%esi
call   1030 <ternary_xor@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    112c <ternary_xor_b8+0x1c>
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x5,-0xc(%rbp)
jge    11de <ternary_xor_b8+0xce>
imul   $0x3,-0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    -0x1c(%rbp),%eax
add    %edx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    119e <ternary_xor_b8+0x8e>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ternary_xor_b8>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
movslq %edi,%r14
imul   $0x55555556,%r14,%rbx
mov    %rbx,%rax
shr    $0x3f,%rax
shr    $0x20,%rbx
add    %eax,%ebx
lea    (%rbx,%rbx,2),%eax
mov    %r14d,%edi
sub    %eax,%edi
movslq %esi,%r15
imul   $0x55555556,%r15,%rbp
mov    %rbp,%rax
shr    $0x3f,%rax
shr    $0x20,%rbp
add    %eax,%ebp
lea    0x0(%rbp,%rbp,2),%eax
mov    %r15d,%esi
sub    %eax,%esi
call   1030 <ternary_xor@plt>
lea    (%rax,%rax,2),%r12d
movslq %ebx,%rdi
imul   $0x55555556,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
shr    $0x20,%rax
add    %ecx,%eax
lea    (%rax,%rax,2),%eax
sub    %eax,%edi
movslq %ebp,%rsi
imul   $0x55555556,%rsi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
shr    $0x20,%rax
add    %ecx,%eax
lea    (%rax,%rax,2),%eax
sub    %eax,%esi
call   1030 <ternary_xor@plt>
add    %r12d,%eax
imul   $0x38e38e39,%r14,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
shr    $0x20,%rcx
sar    %ecx
add    %edx,%ecx
imul   $0x38e38e39,%r15,%rdx
mov    %rdx,%rsi
shr    $0x3f,%rsi
shr    $0x20,%rdx
sar    %edx
add    %esi,%edx
lea    (%rax,%rax,2),%ebp
movslq %ecx,%rdi
imul   $0x55555556,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
shr    $0x20,%rax
add    %ecx,%eax
lea    (%rax,%rax,2),%eax
sub    %eax,%edi
movslq %edx,%rsi
imul   $0x55555556,%rsi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
shr    $0x20,%rax
add    %ecx,%eax
lea    (%rax,%rax,2),%eax
sub    %eax,%esi
call   1030 <ternary_xor@plt>
add    %ebp,%eax
imul   $0x4bda12f7,%r14,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
shr    $0x20,%rcx
sar    $0x3,%ecx
add    %edx,%ecx
imul   $0x4bda12f7,%r15,%rdx
mov    %rdx,%rsi
shr    $0x3f,%rsi
shr    $0x20,%rdx
sar    $0x3,%edx
add    %esi,%edx
lea    (%rax,%rax,2),%ebp
movslq %ecx,%rdi
imul   $0x55555556,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
shr    $0x20,%rax
add    %ecx,%eax
lea    (%rax,%rax,2),%eax
sub    %eax,%edi
movslq %edx,%rsi
imul   $0x55555556,%rsi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
shr    $0x20,%rax
add    %ecx,%eax
lea    (%rax,%rax,2),%eax
sub    %eax,%esi
call   1030 <ternary_xor@plt>
add    %ebp,%eax
imul   $0x1948b0fd,%r14,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
shr    $0x20,%rcx
sar    $0x3,%ecx
add    %edx,%ecx
imul   $0x1948b0fd,%r15,%rdx
mov    %rdx,%rsi
shr    $0x3f,%rsi
shr    $0x20,%rdx
sar    $0x3,%edx
add    %esi,%edx
lea    (%rax,%rax,2),%ebx
movslq %ecx,%rdi
imul   $0x55555556,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
shr    $0x20,%rax
add    %ecx,%eax
lea    (%rax,%rax,2),%eax
sub    %eax,%edi
movslq %edx,%rsi
imul   $0x55555556,%rsi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
shr    $0x20,%rax
add    %ecx,%eax
lea    (%rax,%rax,2),%eax
sub    %eax,%esi
call   1030 <ternary_xor@plt>
add    %ebx,%eax
cltq
imul   $0x55555556,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
shr    $0x20,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,2),%edx
imul   $0x38e38e39,%rax,%rsi
imul   $0x4bda12f7,%rax,%rdi
imul   $0x1948b0fd,%rax,%rbp
sub    %edx,%eax
movslq %ecx,%rcx
imul   $0x55555556,%rcx,%rdx
mov    %rdx,%rbx
shr    $0x3f,%rbx
shr    $0x20,%rdx
add    %ebx,%edx
lea    (%rax,%rax,2),%eax
lea    (%rdx,%rdx,2),%edx
sub    %edx,%ecx
add    %eax,%ecx
mov    %rsi,%rax
shr    $0x3f,%rax
shr    $0x20,%rsi
sar    %esi
add    %eax,%esi
movslq %esi,%rax
imul   $0x55555556,%rax,%rdx
mov    %rdx,%rsi
shr    $0x3f,%rsi
shr    $0x20,%rdx
add    %esi,%edx
lea    (%rcx,%rcx,2),%ecx
lea    (%rdx,%rdx,2),%edx
sub    %edx,%eax
add    %ecx,%eax
mov    %rdi,%rcx
shr    $0x3f,%rcx
shr    $0x20,%rdi
sar    $0x3,%edi
add    %ecx,%edi
lea    (%rax,%rax,2),%eax
movslq %edi,%rcx
imul   $0x55555556,%rcx,%rdx
mov    %rdx,%rsi
shr    $0x3f,%rsi
shr    $0x20,%rdx
add    %esi,%edx
lea    (%rdx,%rdx,2),%edx
sub    %edx,%ecx
add    %eax,%ecx
mov    %rbp,%rax
shr    $0x3f,%rax
shr    $0x20,%rbp
sar    $0x3,%ebp
add    %eax,%ebp
lea    (%rcx,%rcx,2),%ecx
movslq %ebp,%rax
imul   $0x55555556,%rax,%rdx
mov    %rdx,%rsi
shr    $0x3f,%rsi
shr    $0x20,%rdx
add    %esi,%edx
lea    (%rdx,%rdx,2),%edx
sub    %edx,%eax
add    %ecx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @ternary_xor_b8(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %6
store i32 0, i32* %5
br label %8
8: ; preds = %24, %2
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 5
br i1 %10, label %11, label %27
11: ; preds = %8
%12 = load i32, i32* %6
%13 = mul nsw i32 %12, 3
%14 = load i32, i32* %3
%15 = srem i32 %14, 3
%16 = load i32, i32* %4
%17 = srem i32 %16, 3
%18 = call i32 @ternary_xor(i32 %15, i32 %17)
%19 = add nsw i32 %13, %18
store i32 %19, i32* %6
%20 = load i32, i32* %3
%21 = sdiv i32 %20, 3
store i32 %21, i32* %3
%22 = load i32, i32* %4
%23 = sdiv i32 %22, 3
store i32 %23, i32* %4
br label %24
24: ; preds = %11
%25 = load i32, i32* %5
%26 = add nsw i32 %25, 1
store i32 %26, i32* %5
br label %8
27: ; preds = %8
store i32 0, i32* %7
store i32 0, i32* %5
br label %28
28: ; preds = %39, %27
%29 = load i32, i32* %5
%30 = icmp slt i32 %29, 5
br i1 %30, label %31, label %42
31: ; preds = %28
%32 = load i32, i32* %7
%33 = mul nsw i32 %32, 3
%34 = load i32, i32* %6
%35 = srem i32 %34, 3
%36 = add nsw i32 %33, %35
store i32 %36, i32* %7
%37 = load i32, i32* %6
%38 = sdiv i32 %37, 3
store i32 %38, i32* %6
br label %39
39: ; preds = %31
%40 = load i32, i32* %5
%41 = add nsw i32 %40, 1
store i32 %41, i32* %5
br label %28
42: ; preds = %28
%43 = load i32, i32* %7
ret i32 %43
}

LLVM IR (-O2):
define i32 @ternary_xor_b8(i32 %0, i32 %1) {
%3 = srem i32 %0, 3
%4 = srem i32 %1, 3
%5 = tail call i32 @ternary_xor(i32 %3, i32 %4)
%6 = sdiv i32 %0, 3
%7 = sdiv i32 %1, 3
%8 = mul nsw i32 %5, 3
%9 = srem i32 %6, 3
%10 = srem i32 %7, 3
%11 = tail call i32 @ternary_xor(i32 %9, i32 %10)
%12 = add nsw i32 %11, %8
%13 = sdiv i32 %0, 9
%14 = sdiv i32 %1, 9
%15 = mul nsw i32 %12, 3
%16 = srem i32 %13, 3
%17 = srem i32 %14, 3
%18 = tail call i32 @ternary_xor(i32 %16, i32 %17)
%19 = add nsw i32 %18, %15
%20 = sdiv i32 %0, 27
%21 = sdiv i32 %1, 27
%22 = mul nsw i32 %19, 3
%23 = srem i32 %20, 3
%24 = srem i32 %21, 3
%25 = tail call i32 @ternary_xor(i32 %23, i32 %24)
%26 = add nsw i32 %25, %22
%27 = sdiv i32 %0, 81
%28 = sdiv i32 %1, 81
%29 = mul nsw i32 %26, 3
%30 = srem i32 %27, 3
%31 = srem i32 %28, 3
%32 = tail call i32 @ternary_xor(i32 %30, i32 %31)
%33 = add nsw i32 %32, %29
%34 = srem i32 %33, 3
%35 = sdiv i32 %33, 3
%36 = mul nsw i32 %34, 3
%37 = srem i32 %35, 3
%38 = add nsw i32 %36, %37
%39 = sdiv i32 %33, 9
%40 = mul nsw i32 %38, 3
%41 = srem i32 %39, 3
%42 = add nsw i32 %40, %41
%43 = sdiv i32 %33, 27
%44 = mul nsw i32 %42, 3
%45 = srem i32 %43, 3
%46 = add nsw i32 %44, %45
%47 = sdiv i32 %33, 81
%48 = mul nsw i32 %46, 3
%49 = srem i32 %47, 3
%50 = add nsw i32 %48, %49
ret i32 %50
}