#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 TreeNode {
  int dummy;
};
struct ListNode {
  int dummy;
};

/* Variables and functions */
struct TreeNode *build_tree(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ free(int *);
int *list_to_arr(struct ListNode *, int *);
struct TreeNode *sortedListToBST(struct ListNode *head) {
  if (!head)
    return (NULL);
  int *num, size;
  struct TreeNode *root;
  num = list_to_arr(head, &size);
  root = build_tree(num, 0, size - 1);
  free(num);
  return (root);
}


x86 assembly (clang, -O0):
<sortedListToBST>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1154 <sortedListToBST+0x24>
movq   $0x0,-0x8(%rbp)
jmp    118b <sortedListToBST+0x5b>
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1040 <list_to_arr@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1050 <build_tree@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sortedListToBST>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1164 <sortedListToBST+0x34>
lea    0x4(%rsp),%rsi
call   1040 <list_to_arr@plt>
mov    %rax,%r14
mov    0x4(%rsp),%edx
add    $0xffffffff,%edx
mov    %rax,%rdi
xor    %esi,%esi
call   1050 <build_tree@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <free@plt>
jmp    1166 <sortedListToBST+0x36>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.TreeNode = type { i32 }
%struct.ListNode = type { i32 }
define %struct.TreeNode* @sortedListToBST(%struct.ListNode* %0) {
%2 = alloca %struct.TreeNode*
%3 = alloca %struct.ListNode*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca %struct.TreeNode*
store %struct.ListNode* %0, %struct.ListNode** %3
%7 = load %struct.ListNode*, %struct.ListNode** %3
%8 = icmp ne %struct.ListNode* %7, null
br i1 %8, label %10, label %9
9: ; preds = %1
store %struct.TreeNode* null, %struct.TreeNode** %2
br label %20
10: ; preds = %1
%11 = load %struct.ListNode*, %struct.ListNode** %3
%12 = call i32* @list_to_arr(%struct.ListNode* %11, i32* %5)
store i32* %12, i32** %4
%13 = load i32*, i32** %4
%14 = load i32, i32* %5
%15 = sub nsw i32 %14, 1
%16 = call %struct.TreeNode* @build_tree(i32* %13, i32 0, i32 %15)
store %struct.TreeNode* %16, %struct.TreeNode** %6
%17 = load i32*, i32** %4
%18 = call i32 @free(i32* %17)
%19 = load %struct.TreeNode*, %struct.TreeNode** %6
store %struct.TreeNode* %19, %struct.TreeNode** %2
br label %20
20: ; preds = %10, %9
%21 = load %struct.TreeNode*, %struct.TreeNode** %2
ret %struct.TreeNode* %21
}

LLVM IR (-O2):
%struct.TreeNode = type { i32 }
%struct.ListNode = type { i32 }
define %struct.TreeNode* @sortedListToBST(%struct.ListNode* %0) {
%2 = alloca i32
%3 = icmp eq %struct.ListNode* %0, null
br i1 %3, label %11, label %4
4: ; preds = %1
%5 = bitcast i32* %2 to i8*
%6 = call i32* @list_to_arr(%struct.ListNode* %0, i32* %2)
%7 = load i32, i32* %2
%8 = add nsw i32 %7, -1
%9 = call %struct.TreeNode* @build_tree(i32* %6, i32 0, i32 %8)
%10 = call i32 @free(i32* %6)
br label %11
11: ; preds = %1, %4
%12 = phi %struct.TreeNode* [ %9, %4 ], [ null, %1 ]
ret %struct.TreeNode* %12
}