#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 tree_node {
  int dummy;
};

/* Variables and functions */
struct tree_node *deserialize(size_t);
int /*<<< orphan*/ destroy_tree(struct tree_node *);
struct tree_node *flatten_bst(struct tree_node *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ right_traverse(struct tree_node *);
int scanf(char *, size_t *);
int main(void) {
  printf("Enter the number of nodes in the BST, followed by the preorder "
         "traversal, followed by the inorder traversal\n");
  printf("> ");
  size_t nodes;
  while (scanf("%zu", &nodes) == 1) {
    struct tree_node *root = deserialize(nodes);
    struct tree_node *flattenned = flatten_bst(root);
    printf("BST is now flattenned, traversing through right links only:\n");
    right_traverse(flattenned);
    printf("\n> ");
    destroy_tree(root);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
call   1040 <printf@plt>
lea    0xf2d(%rip),%rdi
call   1040 <printf@plt>
lea    0xedf(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1070 <scanf@plt>
cmp    $0x1,%eax
jne    11e9 <main+0x89>
mov    -0x10(%rbp),%rdi
call   1060 <deserialize@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <flatten_bst@plt>
mov    %rax,-0x20(%rbp)
lea    0xeb0(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1080 <right_traverse@plt>
lea    0xed8(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <destroy_tree@plt>
jmp    1187 <main+0x27>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe86(%rip),%rdi
call   1030 <puts@plt>
lea    0xe77(%rip),%rdi
call   1050 <printf@plt>
lea    0xe66(%rip),%rdi
mov    %rsp,%rsi
call   1080 <scanf@plt>
cmp    $0x1,%eax
jne    1207 <main+0x97>
lea    0xec6(%rip),%r14
lea    0xe4f(%rip),%r15
lea    0xe44(%rip),%r12
mov    %rsp,%r13
nop
mov    (%rsp),%rdi
call   1070 <deserialize@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <flatten_bst@plt>
mov    %rax,%rbp
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %rbp,%rdi
call   1090 <right_traverse@plt>
mov    %r15,%rdi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1060 <destroy_tree@plt>
mov    %r12,%rdi
mov    %r13,%rsi
call   1080 <scanf@plt>
cmp    $0x1,%eax
je     11c0 <main+0x50>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.tree_node = type { i32 }
@.str = [109 x i8] c"Enter the number of nodes in the BST, followed by the preorder traversal, followed by the inorder traversal\0A\00"
@.str.1 = [3 x i8] c"> \00"
@.str.2 = [4 x i8] c"%zu\00"
@.str.3 = [61 x i8] c"BST is now flattenned, traversing through right links only:\0A\00"
@.str.4 = [4 x i8] c"\0A> \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca %struct.tree_node*
%4 = alloca %struct.tree_node*
store i32 0, i32* %1
%5 = call i32 @printf(i8* getelementptr inbounds ([109 x i8], [109 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %7
7: ; preds = %10, %0
%8 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %2)
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %21
10: ; preds = %7
%11 = load i64, i64* %2
%12 = call %struct.tree_node* @deserialize(i64 %11)
store %struct.tree_node* %12, %struct.tree_node** %3
%13 = load %struct.tree_node*, %struct.tree_node** %3
%14 = call %struct.tree_node* @flatten_bst(%struct.tree_node* %13)
store %struct.tree_node* %14, %struct.tree_node** %4
%15 = call i32 @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.3, i64 0, i64 0))
%16 = load %struct.tree_node*, %struct.tree_node** %4
%17 = call i32 @right_traverse(%struct.tree_node* %16)
%18 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%19 = load %struct.tree_node*, %struct.tree_node** %3
%20 = call i32 @destroy_tree(%struct.tree_node* %19)
br label %7
21: ; preds = %7
ret i32 0
}

LLVM IR (-O2):
%struct.tree_node = type { i32 }
@.str.1 = [3 x i8] c"> \00"
@.str.2 = [4 x i8] c"%zu\00"
@.str.4 = [4 x i8] c"\0A> \00"
@str = [108 x i8] c"Enter the number of nodes in the BST, followed by the preorder traversal, followed by the inorder traversal\00"
@str.5 = [60 x i8] c"BST is now flattenned, traversing through right links only:\00"
define i32 @main() {
%1 = alloca i64
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([108 x i8], [108 x i8]* @str, i64 0, i64 0))
%3 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = bitcast i64* %1 to i8*
%5 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %1)
%6 = icmp eq i32 %5, 1
br i1 %6, label %7, label %17
7: ; preds = %0, %7
%8 = load i64, i64* %1
%9 = call %struct.tree_node* @deserialize(i64 %8)
%10 = call %struct.tree_node* @flatten_bst(%struct.tree_node* %9)
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @str.5, i64 0, i64 0))
%12 = call i32 @right_traverse(%struct.tree_node* %10)
%13 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%14 = call i32 @destroy_tree(%struct.tree_node* %9)
%15 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %1)
%16 = icmp eq i32 %15, 1
br i1 %16, label %7, label %17
17: ; preds = %7, %0
ret i32 0
}