#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 */
struct Stack {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ moveDisksBetweenTwoPoles(struct Stack *, struct Stack *,
                                            char, char);
int pow(int, int);
int /*<<< orphan*/ push(struct Stack *, int);
void tohIterative(int num_of_disks, struct Stack *src, struct Stack *aux,
                  struct Stack *dest) {
  int i, total_num_of_moves;
  char s = 'S', d = 'D', a = 'A';
  if (num_of_disks % 2 == 0) {
    char temp = d;
    d = a;
    a = temp;
  }
  total_num_of_moves = pow(2, num_of_disks) - 1;
  for (i = num_of_disks; i >= 1; i--)
    push(src, i);
  for (i = 1; i <= total_num_of_moves; i++) {
    if (i % 3 == 1)
      moveDisksBetweenTwoPoles(src, dest, s, d);
    else if (i % 3 == 2)
      moveDisksBetweenTwoPoles(src, aux, s, a);
    else if (i % 3 == 0)
      moveDisksBetweenTwoPoles(aux, dest, a, d);
  }
}


x86 assembly (clang, -O0):
<tohIterative>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movb   $0x53,-0x29(%rbp)
movb   $0x44,-0x2a(%rbp)
movb   $0x41,-0x2b(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1179 <tohIterative+0x49>
mov    -0x2a(%rbp),%al
mov    %al,-0x2c(%rbp)
mov    -0x2b(%rbp),%al
mov    %al,-0x2a(%rbp)
mov    -0x2c(%rbp),%al
mov    %al,-0x2b(%rbp)
mov    -0x4(%rbp),%esi
mov    $0x2,%edi
call   1050 <pow@plt>
sub    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jl     11b6 <tohIterative+0x86>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1040 <push@plt>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    1192 <tohIterative+0x62>
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jg     1271 <tohIterative+0x141>
mov    -0x24(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
cmp    $0x1,%edx
jne    11f9 <tohIterative+0xc9>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x29(%rbp),%al
movsbl %al,%edx
movsbl -0x2a(%rbp),%ecx
call   1030 <moveDisksBetweenTwoPoles@plt>
jmp    125e <tohIterative+0x12e>
mov    -0x24(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
cmp    $0x2,%edx
jne    1229 <tohIterative+0xf9>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x29(%rbp),%al
movsbl %al,%edx
movsbl -0x2b(%rbp),%ecx
call   1030 <moveDisksBetweenTwoPoles@plt>
jmp    1259 <tohIterative+0x129>
mov    -0x24(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1254 <tohIterative+0x124>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x2b(%rbp),%al
movsbl %al,%edx
movsbl -0x2a(%rbp),%ecx
call   1030 <moveDisksBetweenTwoPoles@plt>
jmp    1259 <tohIterative+0x129>
jmp    125e <tohIterative+0x12e>
jmp    1263 <tohIterative+0x133>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11bd <tohIterative+0x8d>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tohIterative>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,0x8(%rsp)
mov    %rdx,0x10(%rsp)
mov    %rsi,%r14
mov    %edi,%ebx
xor    %r15d,%r15d
xor    %r12d,%r12d
test   $0x1,%bl
setne  %r13b
sete   0x7(%rsp)
mov    $0x2,%edi
mov    %ebx,%esi
call   1050 <pow@plt>
mov    %eax,%ebp
test   %ebx,%ebx
jle    1194 <tohIterative+0x64>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <push@plt>
lea    -0x1(%rbx),%eax
cmp    $0x1,%ebx
mov    %eax,%ebx
ja     1180 <tohIterative+0x50>
cmp    $0x2,%ebp
jl     1232 <tohIterative+0x102>
mov    %r13b,%r15b
mov    0x7(%rsp),%al
mov    %al,%r12b
lea    (%r15,%r15,2),%eax
add    $0x41,%eax
lea    (%r12,%r12,2),%ecx
add    $0x41,%ecx
add    $0xffffffff,%ebp
mov    $0x1,%ebx
mov    $0xaaaaaaab,%r15d
movsbl %cl,%r12d
movsbl %al,%r13d
jmp    11ed <tohIterative+0xbd>
nopl   (%rax)
mov    0x10(%rsp),%rdi
mov    0x8(%rsp),%rsi
mov    %r12d,%edx
mov    %r13d,%ecx
call   1030 <moveDisksBetweenTwoPoles@plt>
add    $0x1,%ebx
add    $0xffffffff,%ebp
je     1232 <tohIterative+0x102>
mov    %ebx,%eax
imul   %r15,%rax
shr    $0x21,%rax
lea    (%rax,%rax,2),%ecx
mov    %ebx,%eax
sub    %ecx,%eax
je     11d0 <tohIterative+0xa0>
cmp    $0x2,%eax
je     1220 <tohIterative+0xf0>
cmp    $0x1,%eax
jne    11e5 <tohIterative+0xb5>
mov    %r14,%rdi
mov    0x8(%rsp),%rsi
mov    $0x53,%edx
jmp    11dd <tohIterative+0xad>
nopl   0x0(%rax)
mov    %r14,%rdi
mov    0x10(%rsp),%rsi
mov    $0x53,%edx
mov    %r12d,%ecx
jmp    11e0 <tohIterative+0xb0>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.Stack = type { i32 }
define void @tohIterative(i32 %0, %struct.Stack* %1, %struct.Stack* %2, %struct.Stack* %3) {
%5 = and i32 %0, 1
%6 = icmp eq i32 %5, 0
%7 = select i1 %6, i8 65, i8 68
%8 = select i1 %6, i8 68, i8 65
%9 = tail call i32 @pow(i32 2, i32 %0)
%10 = icmp sgt i32 %0, 0
br i1 %10, label %13, label %11
11: ; preds = %13, %4
%12 = icmp sgt i32 %9, 1
br i1 %12, label %18, label %30
13: ; preds = %4, %13
%14 = phi i32 [ %16, %13 ], [ %0, %4 ]
%15 = tail call i32 @push(%struct.Stack* %1, i32 %14)
%16 = add nsw i32 %14, -1
%17 = icmp ugt i32 %14, 1
br i1 %17, label %13, label %11
18: ; preds = %11, %27
%19 = phi i32 [ %28, %27 ], [ 1, %11 ]
%20 = urem i32 %19, 3
switch i32 %20, label %27 [
i32 1, label %21
i32 2, label %23
i32 0, label %25
]
21: ; preds = %18
%22 = tail call i32 @moveDisksBetweenTwoPoles(%struct.Stack* %1, %struct.Stack* %3, i8 signext 83, i8 signext %7)
br label %27
23: ; preds = %18
%24 = tail call i32 @moveDisksBetweenTwoPoles(%struct.Stack* %1, %struct.Stack* %2, i8 signext 83, i8 signext %8)
br label %27
25: ; preds = %18
%26 = tail call i32 @moveDisksBetweenTwoPoles(%struct.Stack* %2, %struct.Stack* %3, i8 signext %8, i8 signext %7)
br label %27
27: ; preds = %18, %21, %25, %23
%28 = add nuw nsw i32 %19, 1
%29 = icmp eq i32 %28, %9
br i1 %29, label %30, label %18
30: ; preds = %27, %11
ret void
}