#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__ EOF;
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ puts(char *);
scalar_t__ scanf(char *, int *);
int main() {
  int num;
  char ternary[128];
  int count;
  while (scanf("%d\n", &num) != EOF && num >= 0) {
    if (num == 0) {
      puts("0");
      continue;
    }
    count = 0;
    while (num > 0) {
      ternary[count++] = num % 3 + '0';
      num /= 3;
    }
    while (count > 0) {
      putchar(ternary[--count]);
    }
    printf("\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xea7(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1060 <scanf@plt>
mov    %rax,%rcx
xor    %eax,%eax
mov    0x2e7a(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x95(%rbp)
je     118a <main+0x4a>
cmpl   $0x0,-0x8(%rbp)
setge  %al
mov    %al,-0x95(%rbp)
mov    -0x95(%rbp),%al
test   $0x1,%al
jne    119d <main+0x5d>
jmp    124f <main+0x10f>
cmpl   $0x0,-0x8(%rbp)
jne    11b8 <main+0x78>
lea    0xe56(%rip),%rdi
call   1040 <puts@plt>
jmp    1152 <main+0x12>
movl   $0x0,-0x94(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    1209 <main+0xc9>
mov    -0x8(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
add    $0x30,%edx
mov    %dl,%cl
mov    -0x94(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x94(%rbp)
cltq
mov    %cl,-0x90(%rbp,%rax,1)
mov    -0x8(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
jmp    11c2 <main+0x82>
jmp    120e <main+0xce>
cmpl   $0x0,-0x94(%rbp)
jle    123e <main+0xfe>
mov    -0x94(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x94(%rbp)
cltq
movsbl -0x90(%rbp,%rax,1),%edi
ff
call   1030 <putchar@plt>
jmp    120e <main+0xce>
lea    0xdbd(%rip),%rdi
call   1050 <printf@plt>
jmp    1152 <main+0x12>
xor    %eax,%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
lea    0xeb8(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1050 <scanf@plt>
mov    0x2e8f(%rip),%r12
cmp    (%r12),%rax
je     1235 <main+0x105>
mov    0xc(%rsp),%eax
test   %eax,%eax
js     1235 <main+0x105>
lea    0xe8e(%rip),%r14
lea    0xe83(%rip),%r15
lea    0xc(%rsp),%r13
mov    $0xaaaaaaab,%ebx
nopw   0x0(%rax,%rax,1)
test   %eax,%eax
je     1210 <main+0xe0>
mov    $0x1,%esi
xor    %ecx,%ecx
nopl   0x0(%rax,%rax,1)
mov    %eax,%ebp
mov    %esi,%edx
mov    %eax,%eax
imul   %rbx,%rax
shr    $0x21,%rax
lea    (%rax,%rax,2),%esi
mov    %ebp,%edi
sub    %esi,%edi
or     $0x30,%dil
mov    %dil,0x10(%rsp,%rcx,1)
add    $0x1,%rcx
lea    0x1(%rdx),%esi
cmp    $0x3,%ebp
jae    11a0 <main+0x70>
mov    %eax,0xc(%rsp)
test   %ecx,%ecx
jle    11f7 <main+0xc7>
mov    %edx,%ebp
add    $0x1,%rbp
nopl   0x0(%rax,%rax,1)
lea    -0x2(%rbp),%eax
movsbl 0x10(%rsp,%rax,1),%edi
call   1030 <putchar@plt>
add    $0xffffffffffffffff,%rbp
cmp    $0x1,%rbp
ja     11e0 <main+0xb0>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    1218 <main+0xe8>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
mov    %r13,%rsi
call   1050 <scanf@plt>
cmp    (%r12),%rax
je     1235 <main+0x105>
mov    0xc(%rsp),%eax
test   %eax,%eax
jns    1190 <main+0x60>
xor    %eax,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%d\0A\00"
@EOF = global i64 0
@.str.1 = [2 x i8] c"0\00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [128 x i8]
%4 = alloca i32
store i32 0, i32* %1
br label %5
5: ; preds = %45, %17, %0
%6 = call i64 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %2)
%7 = load i64, i64* @EOF
%8 = icmp ne i64 %6, %7
br i1 %8, label %9, label %12
9: ; preds = %5
%10 = load i32, i32* %2
%11 = icmp sge i32 %10, 0
br label %12
12: ; preds = %9, %5
%13 = phi i1 [ false, %5 ], [ %11, %9 ]
br i1 %13, label %14, label %47
14: ; preds = %12
%15 = load i32, i32* %2
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @puts(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %5
19: ; preds = %14
store i32 0, i32* %4
br label %20
20: ; preds = %23, %19
%21 = load i32, i32* %2
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %34
23: ; preds = %20
%24 = load i32, i32* %2
%25 = srem i32 %24, 3
%26 = add nsw i32 %25, 48
%27 = trunc i32 %26 to i8
%28 = load i32, i32* %4
%29 = add nsw i32 %28, 1
store i32 %29, i32* %4
%30 = sext i32 %28 to i64
%31 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 %30
store i8 %27, i8* %31
%32 = load i32, i32* %2
%33 = sdiv i32 %32, 3
store i32 %33, i32* %2
br label %20
34: ; preds = %20
br label %35
35: ; preds = %38, %34
%36 = load i32, i32* %4
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %45
38: ; preds = %35
%39 = load i32, i32* %4
%40 = add nsw i32 %39, -1
store i32 %40, i32* %4
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 %41
%43 = load i8, i8* %42
%44 = call i32 @putchar(i8 signext %43)
br label %35
45: ; preds = %35
%46 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %5
47: ; preds = %12
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"%d\0A\00"
@EOF = global i64 0
@.str.1 = [2 x i8] c"0\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [128 x i8]
%3 = bitcast i32* %1 to i8*
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%5 = call i64 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %1)
%6 = load i64, i64* @EOF
%7 = icmp ne i64 %5, %6
%8 = load i32, i32* %1
%9 = icmp sgt i32 %8, -1
%10 = select i1 %7, i1 %9, i1 false
br i1 %10, label %11, label %48
11: ; preds = %0, %16
%12 = phi i32 [ %20, %16 ], [ %8, %0 ]
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %28
14: ; preds = %11
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %46
%17 = call i64 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %1)
%18 = load i64, i64* @EOF
%19 = icmp ne i64 %17, %18
%20 = load i32, i32* %1
%21 = icmp sgt i32 %20, -1
%22 = select i1 %19, i1 %21, i1 false
br i1 %22, label %11, label %48
23: ; preds = %28
%24 = trunc i64 %34 to i32
store i32 %36, i32* %1
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %46
26: ; preds = %23
%27 = and i64 %34, 4294967295
br label %38
28: ; preds = %11, %28
%29 = phi i64 [ %34, %28 ], [ 0, %11 ]
%30 = phi i32 [ %36, %28 ], [ %12, %11 ]
%31 = urem i32 %30, 3
%32 = trunc i32 %31 to i8
%33 = or i8 %32, 48
%34 = add nuw i64 %29, 1
%35 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 %29
store i8 %33, i8* %35
%36 = udiv i32 %30, 3
%37 = icmp ult i32 %30, 3
br i1 %37, label %23, label %28
38: ; preds = %26, %38
%39 = phi i64 [ %27, %26 ], [ %40, %38 ]
%40 = add nsw i64 %39, -1
%41 = and i64 %40, 4294967295
%42 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 %41
%43 = load i8, i8* %42
%44 = call i32 @putchar(i8 signext %43)
%45 = icmp ugt i64 %39, 1
br i1 %45, label %38, label %46
46: ; preds = %38, %23
%47 = call i32 bitcast (i32 (i8)* @putchar to i32 (i32)*)(i32 10)
br label %16
48: ; preds = %16, %0
ret i32 0
}