#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 _pow(int, int);
int _index(int i, int n) {
  if (n == 1)
    return i;
  else {
    int sq = (i / _pow(3, 2 * n - 1)) * 3 + (i % _pow(3, n) / _pow(3, n - 1));
    int next = i - (i / _pow(3, n) * _pow(3, n - 1) * 2 +
                    i % _pow(3, n) / _pow(3, n - 1) * _pow(3, n - 1) +
                    sq / 3 * _pow(9, n - 1));
    return sq * _pow(9, n - 1) + _index(next, n - 1);
  }
}


x86 assembly (clang, -O0):
<_index>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x1,-0xc(%rbp)
jne    1143 <_index+0x23>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b1 <_index+0x191>
mov    -0x8(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0xc(%rbp),%esi
shl    $0x1,%esi
sub    $0x1,%esi
mov    $0x3,%edi
call   1030 <_pow@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%eax
cltd
idiv   %ecx
imul   $0x3,%eax,%eax
mov    %eax,-0x44(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0xc(%rbp),%esi
mov    $0x3,%edi
call   1030 <_pow@plt>
mov    %eax,%ecx
mov    -0x4c(%rbp),%eax
cltd
idiv   %ecx
mov    %edx,-0x48(%rbp)
mov    -0xc(%rbp),%esi
sub    $0x1,%esi
mov    $0x3,%edi
call   1030 <_pow@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%eax
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0xc(%rbp),%esi
mov    $0x3,%edi
call   1030 <_pow@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%eax
cltd
idiv   %ecx
mov    %eax,-0x3c(%rbp)
mov    -0xc(%rbp),%esi
sub    $0x1,%esi
mov    $0x3,%edi
call   1030 <_pow@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
imul   %ecx,%eax
shl    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0xc(%rbp),%esi
mov    $0x3,%edi
call   1030 <_pow@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
cltd
idiv   %ecx
mov    %edx,-0x34(%rbp)
mov    -0xc(%rbp),%esi
sub    $0x1,%esi
mov    $0x3,%edi
call   1030 <_pow@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cltd
idiv   %ecx
mov    %eax,-0x30(%rbp)
mov    -0xc(%rbp),%esi
sub    $0x1,%esi
mov    $0x3,%edi
call   1030 <_pow@plt>
mov    -0x30(%rbp),%ecx
mov    %eax,%edx
mov    -0x2c(%rbp),%eax
imul   %edx,%ecx
add    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%esi
sub    $0x1,%esi
mov    $0x9,%edi
call   1030 <_pow@plt>
mov    -0x28(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    %eax,%esi
mov    -0x20(%rbp),%eax
imul   %esi,%edx
add    %edx,%ecx
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%esi
sub    $0x1,%esi
mov    $0x9,%edi
call   1030 <_pow@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
imul   %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    -0xc(%rbp),%esi
sub    $0x1,%esi
call   1040 <_index@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_index>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
xor    %eax,%eax
cmp    $0x1,%esi
je     126c <_index+0x15c>
mov    %esi,%r12d
lea    (%r12,%r12,1),%ecx
add    $0xffffffff,%ecx
xor    %eax,%eax
nopw   0x0(%rax,%rax,1)
mov    %ecx,0x8(%rsp)
mov    %eax,0xc(%rsp)
mov    $0x3,%edi
mov    0x8(%rsp),%esi
call   1030 <_pow@plt>
mov    %eax,%ecx
mov    %ebx,%eax
cltd
idiv   %ecx
lea    (%rax,%rax,2),%r15d
mov    $0x3,%edi
mov    %r12d,%esi
call   1030 <_pow@plt>
mov    %eax,%ecx
mov    %ebx,%eax
cltd
idiv   %ecx
mov    %edx,%ebp
lea    -0x1(%r12),%r13d
mov    $0x3,%edi
mov    %r13d,%esi
call   1030 <_pow@plt>
mov    %eax,%ecx
mov    %ebp,%eax
cltd
idiv   %ecx
mov    %eax,%r14d
add    %r15d,%r14d
mov    $0x3,%edi
mov    %r12d,%esi
call   1030 <_pow@plt>
mov    %eax,%ecx
mov    %ebx,%eax
cltd
idiv   %ecx
mov    %eax,0x14(%rsp)
mov    $0x3,%edi
mov    %r13d,%esi
call   1030 <_pow@plt>
mov    %eax,%ebp
mov    $0x3,%edi
mov    %r12d,%esi
call   1030 <_pow@plt>
mov    %eax,%ecx
mov    %ebx,%eax
cltd
idiv   %ecx
mov    %edx,%r12d
mov    $0x3,%edi
mov    %r13d,%esi
call   1030 <_pow@plt>
mov    %eax,%ecx
mov    %r12d,%eax
cltd
idiv   %ecx
mov    %eax,0x10(%rsp)
mov    $0x3,%edi
mov    %r13d,%esi
call   1030 <_pow@plt>
mov    %eax,%r12d
movslq %r14d,%r14
imul   $0x55555555,%r14,%r15
shr    $0x20,%r15
sub    %r14d,%r15d
mov    %r15d,%eax
shr    $0x1f,%eax
sar    %r15d
add    %eax,%r15d
mov    $0x9,%edi
mov    %r13d,%esi
call   1030 <_pow@plt>
imul   0x14(%rsp),%ebp
add    %ebp,%ebp
imul   0x10(%rsp),%r12d
imul   %r15d,%eax
sub    %r12d,%eax
sub    %ebp,%ebx
add    %eax,%ebx
mov    $0x9,%edi
mov    %r13d,%esi
call   1030 <_pow@plt>
mov    0x8(%rsp),%ecx
imul   %r14d,%eax
add    %eax,0xc(%rsp)
mov    0xc(%rsp),%eax
add    $0xfffffffe,%ecx
mov    %r13d,%r12d
cmp    $0x1,%r13d
jne    1140 <_index+0x30>
add    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @_index(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %5
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = load i32, i32* %4
store i32 %11, i32* %3
br label %70
12: ; preds = %2
%13 = load i32, i32* %4
%14 = load i32, i32* %5
%15 = mul nsw i32 2, %14
%16 = sub nsw i32 %15, 1
%17 = call i32 @_pow(i32 3, i32 %16)
%18 = sdiv i32 %13, %17
%19 = mul nsw i32 %18, 3
%20 = load i32, i32* %4
%21 = load i32, i32* %5
%22 = call i32 @_pow(i32 3, i32 %21)
%23 = srem i32 %20, %22
%24 = load i32, i32* %5
%25 = sub nsw i32 %24, 1
%26 = call i32 @_pow(i32 3, i32 %25)
%27 = sdiv i32 %23, %26
%28 = add nsw i32 %19, %27
store i32 %28, i32* %6
%29 = load i32, i32* %4
%30 = load i32, i32* %4
%31 = load i32, i32* %5
%32 = call i32 @_pow(i32 3, i32 %31)
%33 = sdiv i32 %30, %32
%34 = load i32, i32* %5
%35 = sub nsw i32 %34, 1
%36 = call i32 @_pow(i32 3, i32 %35)
%37 = mul nsw i32 %33, %36
%38 = mul nsw i32 %37, 2
%39 = load i32, i32* %4
%40 = load i32, i32* %5
%41 = call i32 @_pow(i32 3, i32 %40)
%42 = srem i32 %39, %41
%43 = load i32, i32* %5
%44 = sub nsw i32 %43, 1
%45 = call i32 @_pow(i32 3, i32 %44)
%46 = sdiv i32 %42, %45
%47 = load i32, i32* %5
%48 = sub nsw i32 %47, 1
%49 = call i32 @_pow(i32 3, i32 %48)
%50 = mul nsw i32 %46, %49
%51 = add nsw i32 %38, %50
%52 = load i32, i32* %6
%53 = sdiv i32 %52, 3
%54 = load i32, i32* %5
%55 = sub nsw i32 %54, 1
%56 = call i32 @_pow(i32 9, i32 %55)
%57 = mul nsw i32 %53, %56
%58 = add nsw i32 %51, %57
%59 = sub nsw i32 %29, %58
store i32 %59, i32* %7
%60 = load i32, i32* %6
%61 = load i32, i32* %5
%62 = sub nsw i32 %61, 1
%63 = call i32 @_pow(i32 9, i32 %62)
%64 = mul nsw i32 %60, %63
%65 = load i32, i32* %7
%66 = load i32, i32* %5
%67 = sub nsw i32 %66, 1
%68 = call i32 @_index(i32 %65, i32 %67)
%69 = add nsw i32 %64, %68
store i32 %69, i32* %3
br label %70
70: ; preds = %12, %10
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
define i32 @_index(i32 %0, i32 %1) {
%3 = icmp eq i32 %1, 1
br i1 %3, label %40, label %4
4: ; preds = %2, %4
%5 = phi i32 [ %15, %4 ], [ %1, %2 ]
%6 = phi i32 [ %35, %4 ], [ %0, %2 ]
%7 = phi i32 [ %38, %4 ], [ 0, %2 ]
%8 = shl nsw i32 %5, 1
%9 = add nsw i32 %8, -1
%10 = tail call i32 @_pow(i32 3, i32 %9)
%11 = sdiv i32 %6, %10
%12 = mul nsw i32 %11, 3
%13 = tail call i32 @_pow(i32 3, i32 %5)
%14 = srem i32 %6, %13
%15 = add nsw i32 %5, -1
%16 = tail call i32 @_pow(i32 3, i32 %15)
%17 = sdiv i32 %14, %16
%18 = add nsw i32 %17, %12
%19 = tail call i32 @_pow(i32 3, i32 %5)
%20 = sdiv i32 %6, %19
%21 = tail call i32 @_pow(i32 3, i32 %15)
%22 = tail call i32 @_pow(i32 3, i32 %5)
%23 = srem i32 %6, %22
%24 = tail call i32 @_pow(i32 3, i32 %15)
%25 = sdiv i32 %23, %24
%26 = tail call i32 @_pow(i32 3, i32 %15)
%27 = sdiv i32 %18, -3
%28 = tail call i32 @_pow(i32 9, i32 %15)
%29 = mul i32 %20, -2
%30 = mul i32 %29, %21
%31 = mul i32 %26, %25
%32 = mul i32 %27, %28
%33 = sub i32 %32, %31
%34 = add i32 %30, %6
%35 = add i32 %34, %33
%36 = tail call i32 @_pow(i32 9, i32 %15)
%37 = mul nsw i32 %36, %18
%38 = add nsw i32 %37, %7
%39 = icmp eq i32 %15, 1
br i1 %39, label %40, label %4
40: ; preds = %4, %2
%41 = phi i32 [ 0, %2 ], [ %38, %4 ]
%42 = phi i32 [ %0, %2 ], [ %35, %4 ]
%43 = add nsw i32 %42, %41
ret i32 %43
}