#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 rs_pop(int *, int, int *);
int /*<<< orphan*/ rs_push(int, int *, int, int *);
int /*<<< orphan*/ rs_reset(int *, int, int *);
int s_pop(int *, int *);
int /*<<< orphan*/ s_push(int, int *, int *);
int /*<<< orphan*/ s_reset(int *, int *);
int /*<<< orphan*/ tree_parent(int, int *, int *, int *);
int *u_alloc(int, int **);
void tree_preorder(int n, int *child, int *next, int *perm, int *aux) {
  int *auxptr = aux;
  int *active = u_alloc(n, &auxptr);
  int *parent = u_alloc(n, &auxptr);
  tree_parent(n, child, next, parent);
  int size, rsize;
  s_reset(active, &size);
  rs_reset(active, n, &rsize);
  int p = 0;
  for (int i = 0; i < n; i++) {
    if (parent[i] < 0) {
      s_push(i, active, &size);
      while (size > 0) {
        int v = s_pop(active, &size);
        perm[p++] = v;
        for (int w = child[v]; w >= 0; w = next[w]) {
          rs_push(w, active, n, &rsize);
        }
        while (rsize > 0) {
          int w = rs_pop(active, n, &rsize);
          s_push(w, active, &size);
        }
      }
    }
  }
}


x86 assembly (clang, -O0):
<tree_preorder>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1070 <u_alloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1070 <u_alloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x40(%rbp),%rcx
call   10a0 <tree_parent@plt>
mov    -0x38(%rbp),%rdi
lea    -0x44(%rbp),%rsi
call   1050 <s_reset@plt>
mov    -0x38(%rbp),%rdi
mov    -0x4(%rbp),%esi
lea    -0x48(%rbp),%rdx
call   1030 <rs_reset@plt>
movl   $0x0,-0x4c(%rbp)
movl   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    12f4 <tree_preorder+0x174>
mov    -0x40(%rbp),%rax
movslq -0x50(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
jge    12e1 <tree_preorder+0x161>
mov    -0x50(%rbp),%edi
mov    -0x38(%rbp),%rsi
lea    -0x44(%rbp),%rdx
call   1090 <s_push@plt>
cmpl   $0x0,-0x44(%rbp)
jle    12dc <tree_preorder+0x15c>
mov    -0x38(%rbp),%rdi
lea    -0x44(%rbp),%rsi
call   1040 <s_pop@plt>
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%edx
mov    -0x20(%rbp),%rax
mov    -0x4c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x4c(%rbp)
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x10(%rbp),%rax
movslq -0x54(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x58(%rbp)
cmpl   $0x0,-0x58(%rbp)
jl     12a0 <tree_preorder+0x120>
mov    -0x58(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x4(%rbp),%edx
lea    -0x48(%rbp),%rcx
call   1080 <rs_push@plt>
mov    -0x18(%rbp),%rax
movslq -0x58(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x58(%rbp)
jmp    1270 <tree_preorder+0xf0>
jmp    12a5 <tree_preorder+0x125>
cmpl   $0x0,-0x48(%rbp)
jle    12d7 <tree_preorder+0x157>
mov    -0x38(%rbp),%rdi
mov    -0x4(%rbp),%esi
lea    -0x48(%rbp),%rdx
call   1060 <rs_pop@plt>
mov    %eax,-0x5c(%rbp)
mov    -0x5c(%rbp),%edi
mov    -0x38(%rbp),%rsi
lea    -0x44(%rbp),%rdx
call   1090 <s_push@plt>
jmp    12a5 <tree_preorder+0x125>
jmp    1230 <tree_preorder+0xb0>
jmp    12e1 <tree_preorder+0x161>
jmp    12e6 <tree_preorder+0x166>
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
jmp    1202 <tree_preorder+0x82>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tree_preorder>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rcx,0x38(%rsp)
mov    %rdx,%r12
mov    %rsi,%r15
mov    %edi,%r14d
mov    %r8,0x40(%rsp)
lea    0x40(%rsp),%rbp
mov    %rbp,%rsi
call   1070 <u_alloc@plt>
mov    %rax,%rbx
mov    %r14d,%edi
mov    %rbp,%rsi
call   1070 <u_alloc@plt>
mov    %rax,%r13
mov    %r14d,%edi
mov    %r15,0x30(%rsp)
mov    %r15,%rsi
mov    %r12,%rdx
mov    %rax,%rcx
call   10a0 <tree_parent@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <s_reset@plt>
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
call   1030 <rs_reset@plt>
test   %r14d,%r14d
jle    1317 <tree_preorder+0x197>
mov    %r14d,%eax
xor    %edi,%edi
lea    0xc(%rsp),%r15
xor    %ecx,%ecx
mov    %rcx,0x10(%rsp)
mov    %r13,0x28(%rsp)
mov    %rax,0x18(%rsp)
jmp    1241 <tree_preorder+0xc1>
nopl   0x0(%rax,%rax,1)
mov    %r13,0x10(%rsp)
mov    0x28(%rsp),%r13
mov    0x18(%rsp),%rax
mov    0x20(%rsp),%rdi
add    $0x1,%rdi
cmp    %rax,%rdi
je     1317 <tree_preorder+0x197>
cmpl   $0x0,0x0(%r13,%rdi,4)
jns    1234 <tree_preorder+0xb4>
mov    %rdi,0x20(%rsp)
mov    %rbx,%rsi
lea    0x8(%rsp),%rdx
call   1090 <s_push@plt>
cmpl   $0x0,0x8(%rsp)
jle    122a <tree_preorder+0xaa>
mov    0x10(%rsp),%r13
movslq %r13d,%r13
lea    0x8(%rsp),%rbp
jmp    1290 <tree_preorder+0x110>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x10(%rsp),%r13
add    $0x1,%r13
cmpl   $0x0,0x8(%rsp)
jle    1220 <tree_preorder+0xa0>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1040 <s_pop@plt>
mov    0x38(%rsp),%rcx
mov    %r13,0x10(%rsp)
mov    %eax,(%rcx,%r13,4)
cltq
mov    0x30(%rsp),%rcx
mov    (%rcx,%rax,4),%r13d
test   %r13d,%r13d
js     12dd <tree_preorder+0x15d>
nopl   0x0(%rax)
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
mov    %r15,%rcx
call   1080 <rs_push@plt>
mov    %r13d,%eax
mov    (%r12,%rax,4),%r13d
test   %r13d,%r13d
jns    12c0 <tree_preorder+0x140>
cmpl   $0x0,0xc(%rsp)
lea    0x8(%rsp),%rbp
jle    1280 <tree_preorder+0x100>
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %r15,%rdx
call   1060 <rs_pop@plt>
mov    %eax,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1090 <s_push@plt>
cmpl   $0x0,0xc(%rsp)
jg     12f0 <tree_preorder+0x170>
jmp    1280 <tree_preorder+0x100>
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @tree_preorder(i32 %0, i32* %1, i32* %2, i32* %3, i32* %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
store i32 %0, i32* %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
store i32* %4, i32** %10
%21 = load i32*, i32** %10
store i32* %21, i32** %11
%22 = load i32, i32* %6
%23 = call i32* @u_alloc(i32 %22, i32** %11)
store i32* %23, i32** %12
%24 = load i32, i32* %6
%25 = call i32* @u_alloc(i32 %24, i32** %11)
store i32* %25, i32** %13
%26 = load i32, i32* %6
%27 = load i32*, i32** %7
%28 = load i32*, i32** %8
%29 = load i32*, i32** %13
%30 = call i32 @tree_parent(i32 %26, i32* %27, i32* %28, i32* %29)
%31 = load i32*, i32** %12
%32 = call i32 @s_reset(i32* %31, i32* %14)
%33 = load i32*, i32** %12
%34 = load i32, i32* %6
%35 = call i32 @rs_reset(i32* %33, i32 %34, i32* %15)
store i32 0, i32* %16
store i32 0, i32* %17
br label %36
36: ; preds = %96, %5
%37 = load i32, i32* %17
%38 = load i32, i32* %6
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %99
40: ; preds = %36
%41 = load i32*, i32** %13
%42 = load i32, i32* %17
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %41, i64 %43
%45 = load i32, i32* %44
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %95
47: ; preds = %40
%48 = load i32, i32* %17
%49 = load i32*, i32** %12
%50 = call i32 @s_push(i32 %48, i32* %49, i32* %14)
br label %51
51: ; preds = %93, %47
%52 = load i32, i32* %14
%53 = icmp sgt i32 %52, 0
br i1 %53, label %54, label %94
54: ; preds = %51
%55 = load i32*, i32** %12
%56 = call i32 @s_pop(i32* %55, i32* %14)
store i32 %56, i32* %18
%57 = load i32, i32* %18
%58 = load i32*, i32** %9
%59 = load i32, i32* %16
%60 = add nsw i32 %59, 1
store i32 %60, i32* %16
%61 = sext i32 %59 to i64
%62 = getelementptr inbounds i32, i32* %58, i64 %61
store i32 %57, i32* %62
%63 = load i32*, i32** %7
%64 = load i32, i32* %18
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i32, i32* %63, i64 %65
%67 = load i32, i32* %66
store i32 %67, i32* %19
br label %68
68: ; preds = %76, %54
%69 = load i32, i32* %19
%70 = icmp sge i32 %69, 0
br i1 %70, label %71, label %82
71: ; preds = %68
%72 = load i32, i32* %19
%73 = load i32*, i32** %12
%74 = load i32, i32* %6
%75 = call i32 @rs_push(i32 %72, i32* %73, i32 %74, i32* %15)
br label %76
76: ; preds = %71
%77 = load i32*, i32** %8
%78 = load i32, i32* %19
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds i32, i32* %77, i64 %79
%81 = load i32, i32* %80
store i32 %81, i32* %19
br label %68
82: ; preds = %68
br label %83
83: ; preds = %86, %82
%84 = load i32, i32* %15
%85 = icmp sgt i32 %84, 0
br i1 %85, label %86, label %93
86: ; preds = %83
%87 = load i32*, i32** %12
%88 = load i32, i32* %6
%89 = call i32 @rs_pop(i32* %87, i32 %88, i32* %15)
store i32 %89, i32* %20
%90 = load i32, i32* %20
%91 = load i32*, i32** %12
%92 = call i32 @s_push(i32 %90, i32* %91, i32* %14)
br label %83
93: ; preds = %83
br label %51
94: ; preds = %51
br label %95
95: ; preds = %94, %40
br label %96
96: ; preds = %95
%97 = load i32, i32* %17
%98 = add nsw i32 %97, 1
store i32 %98, i32* %17
br label %36
99: ; preds = %36
ret void
}

LLVM IR (-O2):
define void @tree_preorder(i32 %0, i32* %1, i32* %2, i32* nocapture writeonly %3, i32* %4) {
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = bitcast i32** %6 to i8*
store i32* %4, i32** %6
%10 = call i32* @u_alloc(i32 %0, i32** %6)
%11 = call i32* @u_alloc(i32 %0, i32** %6)
%12 = call i32 @tree_parent(i32 %0, i32* %1, i32* %2, i32* %11)
%13 = bitcast i32* %7 to i8*
%14 = bitcast i32* %8 to i8*
%15 = call i32 @s_reset(i32* %10, i32* %7)
%16 = call i32 @rs_reset(i32* %10, i32 %0, i32* %8)
%17 = icmp sgt i32 %0, 0
br i1 %17, label %18, label %20
18: ; preds = %5
%19 = zext i32 %0 to i64
br label %21
20: ; preds = %63, %5
ret void
21: ; preds = %18, %63
%22 = phi i64 [ 0, %18 ], [ %65, %63 ]
%23 = phi i32 [ 0, %18 ], [ %64, %63 ]
%24 = getelementptr inbounds i32, i32* %11, i64 %22
%25 = load i32, i32* %24
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %63
27: ; preds = %21
%28 = trunc i64 %22 to i32
%29 = call i32 @s_push(i32 %28, i32* %10, i32* %7)
%30 = load i32, i32* %7
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %63
32: ; preds = %27
%33 = sext i32 %23 to i64
br label %37
34: ; preds = %56, %46
%35 = load i32, i32* %7
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %61
37: ; preds = %32, %34
%38 = phi i64 [ %33, %32 ], [ %40, %34 ]
%39 = call i32 @s_pop(i32* %10, i32* %7)
%40 = add i64 %38, 1
%41 = getelementptr inbounds i32, i32* %3, i64 %38
store i32 %39, i32* %41
%42 = sext i32 %39 to i64
%43 = getelementptr inbounds i32, i32* %1, i64 %42
%44 = load i32, i32* %43
%45 = icmp sgt i32 %44, -1
br i1 %45, label %49, label %46
46: ; preds = %49, %37
%47 = load i32, i32* %8
%48 = icmp sgt i32 %47, 0
br i1 %48, label %56, label %34
49: ; preds = %37, %49
%50 = phi i32 [ %54, %49 ], [ %44, %37 ]
%51 = call i32 @rs_push(i32 %50, i32* %10, i32 %0, i32* %8)
%52 = zext i32 %50 to i64
%53 = getelementptr inbounds i32, i32* %2, i64 %52
%54 = load i32, i32* %53
%55 = icmp sgt i32 %54, -1
br i1 %55, label %49, label %46
56: ; preds = %46, %56
%57 = call i32 @rs_pop(i32* %10, i32 %0, i32* %8)
%58 = call i32 @s_push(i32 %57, i32* %10, i32* %7)
%59 = load i32, i32* %8
%60 = icmp sgt i32 %59, 0
br i1 %60, label %56, label %34
61: ; preds = %34
%62 = trunc i64 %40 to i32
br label %63
63: ; preds = %61, %27, %21
%64 = phi i32 [ %23, %21 ], [ %23, %27 ], [ %62, %61 ]
%65 = add nuw nsw i64 %22, 1
%66 = icmp eq i64 %65, %19
br i1 %66, label %20, label %21
}