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

/* Variables and functions */
struct Tnode **malloc(int);
void treeToArray(struct Tnode **root, struct Tnode ***array) {
  void treeCopyToArray(struct Tnode **, struct Tnode **);
  void _qsort(struct Tnode **, int, int);
  unsigned int countNode(struct Tnode *);
  unsigned int nodeNum = countNode(*root);
  struct Tnode **tmp = malloc(sizeof(struct Tnode *) * nodeNum);
  treeCopyToArray(root, tmp);
  _qsort(tmp, 0, nodeNum - 1);
  *array = tmp;
}


x86 assembly (clang, -O0):
<treeToArray>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <countNode@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <treeCopyToArray@plt>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1060 <_qsort@plt>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<treeToArray>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    (%rdi),%rdi
call   1030 <countNode@plt>
mov    %eax,%ebx
lea    0x0(,%rbx,8),%edi
call   1050 <malloc@plt>
mov    %rax,%r12
mov    %r15,%rdi
mov    %rax,%rsi
call   1040 <treeCopyToArray@plt>
add    $0xffffffff,%ebx
mov    %r12,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1060 <_qsort@plt>
mov    %r12,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.Tnode = type { i32 }
define void @treeToArray(%struct.Tnode** %0, %struct.Tnode*** %1) {
%3 = alloca %struct.Tnode**
%4 = alloca %struct.Tnode***
%5 = alloca i32
%6 = alloca %struct.Tnode**
store %struct.Tnode** %0, %struct.Tnode*** %3
store %struct.Tnode*** %1, %struct.Tnode**** %4
%7 = load %struct.Tnode**, %struct.Tnode*** %3
%8 = load %struct.Tnode*, %struct.Tnode** %7
%9 = call i32 @countNode(%struct.Tnode* %8)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = zext i32 %10 to i64
%12 = mul i64 8, %11
%13 = trunc i64 %12 to i32
%14 = call %struct.Tnode** @malloc(i32 %13)
store %struct.Tnode** %14, %struct.Tnode*** %6
%15 = load %struct.Tnode**, %struct.Tnode*** %3
%16 = load %struct.Tnode**, %struct.Tnode*** %6
%17 = load %struct.Tnode**, %struct.Tnode*** %6
%18 = load i32, i32* %5
%19 = sub i32 %18, 1
%20 = load %struct.Tnode**, %struct.Tnode*** %6
%21 = load %struct.Tnode***, %struct.Tnode**** %4
store %struct.Tnode** %20, %struct.Tnode*** %21
ret void
}

LLVM IR (-O2):
%struct.Tnode = type { i32 }
define void @treeToArray(%struct.Tnode** %0, %struct.Tnode*** nocapture writeonly %1) {
%3 = load %struct.Tnode*, %struct.Tnode** %0
%4 = tail call i32 @countNode(%struct.Tnode* %3)
%5 = shl i32 %4, 3
%6 = tail call %struct.Tnode** @malloc(i32 %5)
tail call void @treeCopyToArray(%struct.Tnode** %0, %struct.Tnode** %6)
%7 = add i32 %4, -1
tail call void @_qsort(%struct.Tnode** %6, i32 0, i32 %7)
store %struct.Tnode** %6, %struct.Tnode*** %1
ret void
}