#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*/ printf(char *, int);
void threes(int n) {
  while (n != 1) {
    if (n % 3 == 0) {
      printf("%d 0\n", n);
      n = n / 3;
    } else if ((n + 1) % 3 == 0) {
      printf("%d 1\n", n);
      n = (n + 1) / 3;
    } else {
      printf("%d -1\n", n);
      n = (n - 1) / 3;
    }
  }
  printf("%d\n", n);
}


x86 assembly (clang, -O0):
<threes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x1,-0x4(%rbp)
je     11c1 <threes+0xb1>
mov    -0x4(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    115b <threes+0x4b>
mov    -0x4(%rbp),%esi
lea    0xebd(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
jmp    11bc <threes+0xac>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1197 <threes+0x87>
mov    -0x4(%rbp),%esi
lea    0xe8a(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
jmp    11b7 <threes+0xa7>
mov    -0x4(%rbp),%esi
lea    0xe6b(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
sub    $0x1,%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
jmp    11bc <threes+0xac>
jmp    111b <threes+0xb>
mov    -0x4(%rbp),%esi
lea    0xe48(%rip),%rdi
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<threes>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x1,%edi
jne    1136 <threes+0x26>
lea    0xeef(%rip),%rdi
mov    $0x1,%esi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <printf@plt>
mov    %edi,%ebx
lea    0xec1(%rip),%r14
lea    0xec0(%rip),%r15
lea    0xebf(%rip),%r12
jmp    1179 <threes+0x69>
nop
mov    %r14,%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
movslq %ebx,%rax
imul   $0x55555556,%rax,%rbx
mov    %rbx,%rcx
shr    $0x3f,%rcx
shr    $0x20,%rbx
add    %ecx,%ebx
add    $0xfffffffd,%eax
cmp    $0x2,%eax
jbe    111d <threes+0xd>
imul   $0xaaaaaaab,%ebx,%eax
add    $0x2aaaaaaa,%eax
cmp    $0x55555554,%eax
jbe    1150 <threes+0x40>
lea    0x1(%rbx),%ebp
imul   $0xaaaaaaab,%ebp,%eax
add    $0x2aaaaaaa,%eax
cmp    $0x55555554,%eax
jbe    11b0 <threes+0xa0>
mov    %r12,%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
add    $0xffffffff,%ebx
jmp    115a <threes+0x4a>
nop
mov    %r15,%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
mov    %ebp,%ebx
jmp    115a <threes+0x4a>

LLVM IR (-O0):
@.str = [6 x i8] c"%d 0\0A\00"
@.str.1 = [6 x i8] c"%d 1\0A\00"
@.str.2 = [7 x i8] c"%d -1\0A\00"
@.str.3 = [4 x i8] c"%d\0A\00"
define void @threes(i32 %0) {
%2 = alloca i32
store i32 %0, i32* %2
br label %3
3: ; preds = %33, %1
%4 = load i32, i32* %2
%5 = icmp ne i32 %4, 1
br i1 %5, label %6, label %34
6: ; preds = %3
%7 = load i32, i32* %2
%8 = srem i32 %7, 3
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %6
%11 = load i32, i32* %2
%12 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = load i32, i32* %2
%14 = sdiv i32 %13, 3
store i32 %14, i32* %2
br label %33
15: ; preds = %6
%16 = load i32, i32* %2
%17 = add nsw i32 %16, 1
%18 = srem i32 %17, 3
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %15
%21 = load i32, i32* %2
%22 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %2
%24 = add nsw i32 %23, 1
%25 = sdiv i32 %24, 3
store i32 %25, i32* %2
br label %32
26: ; preds = %15
%27 = load i32, i32* %2
%28 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%29 = load i32, i32* %2
%30 = sub nsw i32 %29, 1
%31 = sdiv i32 %30, 3
store i32 %31, i32* %2
br label %32
32: ; preds = %26, %20
br label %33
33: ; preds = %32, %10
br label %3
34: ; preds = %3
%35 = load i32, i32* %2
%36 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %35)
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"%d 0\0A\00"
@.str.1 = [6 x i8] c"%d 1\0A\00"
@.str.2 = [7 x i8] c"%d -1\0A\00"
@.str.3 = [4 x i8] c"%d\0A\00"
define void @threes(i32 %0) {
%2 = icmp eq i32 %0, 1
br i1 %2, label %23, label %3
3: ; preds = %1, %18
%4 = phi i32 [ %20, %18 ], [ %0, %1 ]
%5 = srem i32 %4, 3
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %4)
br label %18
9: ; preds = %3
%10 = add nsw i32 %4, 1
%11 = srem i32 %10, 3
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %4)
br label %18
15: ; preds = %9
%16 = tail call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %4)
%17 = add nsw i32 %4, -1
br label %18
18: ; preds = %13, %15, %7
%19 = phi i32 [ %4, %7 ], [ %10, %13 ], [ %17, %15 ]
%20 = sdiv i32 %19, 3
%21 = add i32 %19, -3
%22 = icmp ult i32 %21, 3
br i1 %22, label %23, label %3
23: ; preds = %18, %1
%24 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 1)
ret void
}