// File: /workspace/github_c_makefile_star2_O0/orangeduck[P]mpc/examples/tree_traversal.c
// Function: main
int main(int argc, char *argv[]) {

  mpc_parser_t *Input  = mpc_new("input");
  mpc_parser_t *Node  = mpc_new("node");
  mpc_parser_t *Leaf  = mpc_new("leaf");
  mpc_ast_t *ast, *tree, *child, *child_sub, *ast_next;
  mpc_ast_trav_t *trav;
  mpc_result_t r;
  int index, lb, i;

  mpca_lang(MPCA_LANG_PREDICTIVE,
        " node : '(' <node> ',' /foo/ ',' <node> ')' | <leaf>;"
        " leaf : /bar/;"
        " input : /^/ <node> /$/;",
        Node, Leaf, Input, NULL);

  if (argc > 1) {

    if (mpc_parse_contents(argv[1], Input, &r)) {
      ast = r.output;
    } else {
      mpc_err_print(r.error);
      mpc_err_delete(r.error);
      mpc_cleanup(3, Node, Leaf, Input);
      return EXIT_FAILURE;
    }

  } else {

    if (mpc_parse_pipe("<stdin>", stdin, Input, &r)) {
      ast = r.output;
    } else {
      mpc_err_print(r.error);
      mpc_err_delete(r.error);
      mpc_cleanup(3, Node, Leaf, Input);
      return EXIT_FAILURE;
    }

  }

  /* Get index or child of tree */
  tree = ast->children[1];

  index = mpc_ast_get_index(tree, "node|>");
  child = mpc_ast_get_child(tree, "node|>");

  if(child == NULL) {
    mpc_cleanup(3, Node, Leaf, Input);
    mpc_ast_delete(ast);
    return EXIT_FAILURE;
  }

  printf("Index: %d; Child: \"%s\"\n", index, child->tag);

  /* Get multiple indexes or children of trees */
  index     = mpc_ast_get_index_lb(child, "node|leaf|regex", 0);
  child_sub = mpc_ast_get_child_lb(child, "node|leaf|regex", 0);

  while(index != -1) {
    printf("-- Index: %d; Child: \"%s\"\n", index, child_sub->tag);

    lb = index + 1;
    index     = mpc_ast_get_index_lb(child, "node|leaf|regex", lb);
    child_sub = mpc_ast_get_child_lb(child, "node|leaf|regex", lb);
  }

  /* Traversal */
  printf("Pre order tree traversal.\n");
  trav = mpc_ast_traverse_start(ast, mpc_ast_trav_order_pre);

  ast_next = mpc_ast_traverse_next(&trav);

  while(ast_next != NULL) {
    printf("Tag: %s; Contents: %s\n",
      ast_next->tag,
      ast_next->contents);
    ast_next = mpc_ast_traverse_next(&trav);
  }

  mpc_ast_traverse_free(&trav);

  printf("Post order tree traversal.\n");

  trav = mpc_ast_traverse_start(ast, mpc_ast_trav_order_post);

  ast_next = mpc_ast_traverse_next(&trav);

  while(ast_next != NULL) {
    printf("Tag: %s; Contents: %s\n",
      ast_next->tag,
      ast_next->contents);
    ast_next = mpc_ast_traverse_next(&trav);
  }

  mpc_ast_traverse_free(&trav);

  printf("Partial traversal.\n");

  trav = mpc_ast_traverse_start(ast, mpc_ast_trav_order_post);

  ast_next = mpc_ast_traverse_next(&trav);

  for(i=0; i<2 && ast_next != NULL; i++) {
    printf("Tag: %s; Contents: %s\n",
      ast_next->tag,
      ast_next->contents);
    ast_next = mpc_ast_traverse_next(&trav);
  }

  mpc_ast_traverse_free(&trav);

  /* Clean up and return */
  mpc_cleanup(3, Node, Leaf, Input);
  mpc_ast_delete(ast);

  return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq 0xe3ac(%rip), %rdi      # 0xf699
callq 0x3b20
movq %rax, -0x18(%rbp)
leaq 0xdd07(%rip), %rdi      # 0xf004
callq 0x3b20
movq %rax, -0x20(%rbp)
leaq 0xdcfc(%rip), %rdi      # 0xf009
callq 0x3b20
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %r8
movl $0x1, %edi
leaq 0xdce0(%rip), %rsi      # 0xf00e
xorl %eax, %eax
movl %eax, %r9d
movb $0x0, %al
callq 0xa740
cmpl $0x1, -0x8(%rbp)
jle 0x139c
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rsi
leaq -0x60(%rbp), %rdx
callq 0x37c0
cmpl $0x0, %eax
je 0x1364
movq -0x60(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x139a
movq -0x60(%rbp), %rdi
callq 0x1720
movq -0x60(%rbp), %rdi
callq 0x16a0
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x18(%rbp), %rcx
movl $0x3, %edi
movb $0x0, %al
callq 0x4220
movl $0x1, -0x4(%rbp)
jmp 0x168d
jmp 0x1401
movq 0x12c3d(%rip), %rax     # 0x13fe0
movq (%rax), %rsi
movq -0x18(%rbp), %rdx
leaq 0xdcb9(%rip), %rdi      # 0xf06a
leaq -0x60(%rbp), %rcx
callq 0x3620
cmpl $0x0, %eax
je 0x13c9
movq -0x60(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x13ff
movq -0x60(%rbp), %rdi
callq 0x1720
movq -0x60(%rbp), %rdi
callq 0x16a0
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x18(%rbp), %rcx
movl $0x3, %edi
movb $0x0, %al
callq 0x4220
movl $0x1, -0x4(%rbp)
jmp 0x168d
jmp 0x1401
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
leaq 0xdc56(%rip), %rsi      # 0xf072
callq 0x8930
movl %eax, -0x64(%rbp)
movq -0x38(%rbp), %rdi
leaq 0xdc43(%rip), %rsi      # 0xf072
callq 0x89d0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x146c
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x18(%rbp), %rcx
movl $0x3, %edi
movb $0x0, %al
callq 0x4220
movq -0x30(%rbp), %rdi
callq 0x8040
movl $0x1, -0x4(%rbp)
jmp 0x168d
movl -0x64(%rbp), %esi
movq -0x40(%rbp), %rax
movq (%rax), %rdx
leaq 0xdc0f(%rip), %rdi      # 0xf08c
movb $0x0, %al
callq 0x1090
movq -0x40(%rbp), %rdi
leaq 0xdbea(%rip), %rsi      # 0xf079
xorl %edx, %edx
callq 0x8960
movl %eax, -0x64(%rbp)
movq -0x40(%rbp), %rdi
leaq 0xdbd5(%rip), %rsi      # 0xf079
xorl %edx, %edx
callq 0x8a00
movq %rax, -0x48(%rbp)
cmpl $-0x1, -0x64(%rbp)
je 0x1505
movl -0x64(%rbp), %esi
movq -0x48(%rbp), %rax
movq (%rax), %rdx
leaq 0xdbc3(%rip), %rdi      # 0xf089
movb $0x0, %al
callq 0x1090
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
movq -0x40(%rbp), %rdi
movl -0x68(%rbp), %edx
leaq 0xdb95(%rip), %rsi      # 0xf079
callq 0x8960
movl %eax, -0x64(%rbp)
movq -0x40(%rbp), %rdi
movl -0x68(%rbp), %edx
leaq 0xdb7f(%rip), %rsi      # 0xf079
callq 0x8a00
movq %rax, -0x48(%rbp)
jmp 0x14af
leaq 0xdb98(%rip), %rdi      # 0xf0a4
movb $0x0, %al
callq 0x1090
movq -0x30(%rbp), %rdi
xorl %esi, %esi
callq 0x8a80
movq %rax, -0x58(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x8b60
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x1562
movq -0x50(%rbp), %rax
movq (%rax), %rsi
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0xdb73(%rip), %rdi      # 0xf0bf
movb $0x0, %al
callq 0x1090
leaq -0x58(%rbp), %rdi
callq 0x8b60
movq %rax, -0x50(%rbp)
jmp 0x152f
leaq -0x58(%rbp), %rdi
callq 0x8dc0
leaq 0xdb64(%rip), %rdi      # 0xf0d6
movb $0x0, %al
callq 0x1090
movq -0x30(%rbp), %rdi
movl $0x1, %esi
callq 0x8a80
movq %rax, -0x58(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x8b60
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x15cb
movq -0x50(%rbp), %rax
movq (%rax), %rsi
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0xdb0a(%rip), %rdi      # 0xf0bf
movb $0x0, %al
callq 0x1090
leaq -0x58(%rbp), %rdi
callq 0x8b60
movq %rax, -0x50(%rbp)
jmp 0x1598
leaq -0x58(%rbp), %rdi
callq 0x8dc0
leaq 0xdb17(%rip), %rdi      # 0xf0f2
movb $0x0, %al
callq 0x1090
movq -0x30(%rbp), %rdi
movl $0x1, %esi
callq 0x8a80
movq %rax, -0x58(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x8b60
movq %rax, -0x50(%rbp)
movl $0x0, -0x6c(%rbp)
xorl %eax, %eax
cmpl $0x2, -0x6c(%rbp)
movb %al, -0x6d(%rbp)
jge 0x161e
cmpq $0x0, -0x50(%rbp)
setne %al
movb %al, -0x6d(%rbp)
movb -0x6d(%rbp), %al
testb $0x1, %al
jne 0x1627
jmp 0x165c
movq -0x50(%rbp), %rax
movq (%rax), %rsi
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0xda82(%rip), %rdi      # 0xf0bf
movb $0x0, %al
callq 0x1090
leaq -0x58(%rbp), %rdi
callq 0x8b60
movq %rax, -0x50(%rbp)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x1608
leaq -0x58(%rbp), %rdi
callq 0x8dc0
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x18(%rbp), %rcx
movl $0x3, %edi
movb $0x0, %al
callq 0x4220
movq -0x30(%rbp), %rdi
callq 0x8040
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)