#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 /*<<< orphan*/ memset(int *, int, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ scanf(char *, int *);
int main(void) {
  int numEmpty, numExchange, numFull, memoTable[100 + 1], cpyEmpty;
  memset(memoTable, -1, (100 + 1) * sizeof(int));
  while (1) {
    scanf("%d", &numEmpty);
    if (!numEmpty)
      break;
    if (memoTable[numEmpty] >= 0) {
      printf("%d\n", memoTable[numEmpty]);
      continue;
    }
    numFull = 0;
    cpyEmpty = numEmpty;
    while (numEmpty >= 3) {
      numExchange = numEmpty / 3;
      numEmpty = numEmpty % 3;
      numFull += numExchange;
      numEmpty += numExchange;
    }
    if (numEmpty == 3 - 1) {
      numFull++;
    }
    printf("%d\n", numFull);
    memoTable[cpyEmpty] = numFull;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1c0,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x1b0(%rbp),%rdi
mov    $0xffffffff,%esi
mov    $0x194,%edx
call   1040 <memset@plt>
lea    0xea1(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1050 <scanf@plt>
cmpl   $0x0,-0x8(%rbp)
jne    1177 <main+0x47>
jmp    122a <main+0xfa>
movslq -0x8(%rbp),%rax
cmpl   $0x0,-0x1b0(%rbp,%rax,4)
jl     11a5 <main+0x75>
movslq -0x8(%rbp),%rax
mov    -0x1b0(%rbp,%rax,4),%esi
lea    0xe68(%rip),%rdi
call   1030 <printf@plt>
jmp    1158 <main+0x28>
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x1b4(%rbp)
cmpl   $0x3,-0x8(%rbp)
jl     11f2 <main+0xc2>
mov    -0x8(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %edx,-0x8(%rbp)
mov    -0xc(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
jmp    11b5 <main+0x85>
cmpl   $0x2,-0x8(%rbp)
jne    1205 <main+0xd5>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%esi
lea    0xdf4(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%ecx
movslq -0x1b4(%rbp),%rax
mov    %ecx,-0x1b0(%rbp,%rax,4)
jmp    1158 <main+0x28>
xor    %eax,%eax
add    $0x1c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1a8,%rsp
lea    0x10(%rsp),%rdi
mov    $0xffffffff,%esi
mov    $0x194,%edx
call   1040 <memset@plt>
lea    0xea4(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1050 <scanf@plt>
mov    0xc(%rsp),%eax
test   %eax,%eax
je     11f8 <main+0xc8>
lea    0xe8a(%rip),%r14
lea    0xe80(%rip),%r15
lea    0xc(%rsp),%r12
mov    $0xaaaaaaab,%ebx
jmp    11ab <main+0x7b>
nopl   0x0(%rax)
mov    %r14,%rdi
call   1030 <printf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1050 <scanf@plt>
mov    0xc(%rsp),%eax
test   %eax,%eax
je     11f8 <main+0xc8>
movslq %eax,%r13
mov    0x10(%rsp,%r13,4),%esi
test   %esi,%esi
jns    1190 <main+0x60>
xor    %ecx,%ecx
cmp    $0x3,%eax
jl     11dc <main+0xac>
xor    %ecx,%ecx
mov    %eax,%edx
imul   %rbx,%rdx
shr    $0x21,%rdx
lea    (%rdx,%rdx,2),%esi
sub    %esi,%eax
add    %edx,%ecx
add    %edx,%eax
cmp    $0x2,%eax
ja     11c0 <main+0x90>
mov    %eax,0xc(%rsp)
xor    %ebp,%ebp
cmp    $0x2,%eax
sete   %bpl
add    %ecx,%ebp
mov    %r14,%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
mov    %ebp,0x10(%rsp,%r13,4)
jmp    1198 <main+0x68>
xor    %eax,%eax
add    $0x1a8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [101 x i32]
%3 = bitcast i32* %1 to i8*
%4 = bitcast [101 x i32]* %2 to i8*
%5 = getelementptr inbounds [101 x i32], [101 x i32]* %2, i64 0, i64 0
%6 = call i32 @memset(i32* %5, i32 -1, i32 404)
%7 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%8 = load i32, i32* %1
%9 = icmp eq i32 %8, 0
br i1 %9, label %40, label %10
10: ; preds = %0, %18
%11 = phi i32 [ %20, %18 ], [ %8, %0 ]
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [101 x i32], [101 x i32]* %2, i64 0, i64 %12
%14 = load i32, i32* %13
%15 = icmp sgt i32 %14, -1
br i1 %15, label %16, label %22
16: ; preds = %10
%17 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %14)
br label %18
18: ; preds = %16, %33
%19 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%20 = load i32, i32* %1
%21 = icmp eq i32 %20, 0
br i1 %21, label %40, label %10
22: ; preds = %10
%23 = icmp sgt i32 %11, 2
br i1 %23, label %24, label %33
24: ; preds = %22, %24
%25 = phi i32 [ %29, %24 ], [ 0, %22 ]
%26 = phi i32 [ %30, %24 ], [ %11, %22 ]
%27 = udiv i32 %26, 3
%28 = urem i32 %26, 3
%29 = add nuw nsw i32 %25, %27
%30 = add nuw nsw i32 %28, %27
%31 = icmp ugt i32 %30, 2
br i1 %31, label %24, label %32
32: ; preds = %24
store i32 %30, i32* %1
br label %33
33: ; preds = %32, %22
%34 = phi i32 [ %30, %32 ], [ %11, %22 ]
%35 = phi i32 [ %29, %32 ], [ 0, %22 ]
%36 = icmp eq i32 %34, 2
%37 = zext i1 %36 to i32
%38 = add nuw nsw i32 %35, %37
%39 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %38)
store i32 %38, i32* %13
br label %18
40: ; preds = %18, %0
ret i32 0
}