#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 */
scalar_t__ ceil(int /*<<< orphan*/);
int /*<<< orphan*/ constructSTUtil(int *, int /*<<< orphan*/, int, int *,
                                   int /*<<< orphan*/);
int /*<<< orphan*/ log2(int);
scalar_t__ malloc(int);
scalar_t__ pow(int, int);
int *constructST(int arr[], int n) {
  int x = (int)(ceil(log2(n)));
  int max_size = 2 * (int)pow(2, x) - 1;
  int *st = (int *)malloc(max_size * sizeof(int));
  if (st == NULL)
    return NULL;
  constructSTUtil(arr, 0, n - 1, st, 0);
  return st;
}


x86 assembly (clang, -O0):
<constructST>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <log2@plt>
mov    %eax,%edi
call   1040 <ceil@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
mov    $0x2,%edi
call   1070 <pow@plt>
shl    $0x1,%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b2 <constructST+0x62>
movq   $0x0,-0x8(%rbp)
jmp    11d3 <constructST+0x83>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
mov    -0x28(%rbp),%rcx
xor    %r8d,%r8d
mov    %r8d,%esi
call   1060 <constructSTUtil@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<constructST>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
mov    %esi,%edi
call   1030 <log2@plt>
mov    %eax,%edi
call   1040 <ceil@plt>
mov    $0x2,%edi
mov    %eax,%esi
call   1070 <pow@plt>
lea    -0x4(,%rax,8),%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     119e <constructST+0x4e>
mov    %rax,%rbx
add    $0xffffffff,%ebp
mov    %r14,%rdi
xor    %esi,%esi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %r8d,%r8d
call   1060 <constructSTUtil@plt>
jmp    11a0 <constructST+0x50>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32* @constructST(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
store i32* %0, i32** %4
store i32 %1, i32* %5
%9 = load i32, i32* %5
%10 = call i32 @log2(i32 %9)
%11 = call i64 @ceil(i32 %10)
%12 = trunc i64 %11 to i32
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = call i64 @pow(i32 2, i32 %13)
%15 = trunc i64 %14 to i32
%16 = mul nsw i32 2, %15
%17 = sub nsw i32 %16, 1
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = sext i32 %18 to i64
%20 = mul i64 %19, 4
%21 = trunc i64 %20 to i32
%22 = call i64 @malloc(i32 %21)
%23 = inttoptr i64 %22 to i32*
store i32* %23, i32** %8
%24 = load i32*, i32** %8
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %27
26: ; preds = %2
store i32* null, i32** %3
br label %34
27: ; preds = %2
%28 = load i32*, i32** %4
%29 = load i32, i32* %5
%30 = sub nsw i32 %29, 1
%31 = load i32*, i32** %8
%32 = call i32 @constructSTUtil(i32* %28, i32 0, i32 %30, i32* %31, i32 0)
%33 = load i32*, i32** %8
store i32* %33, i32** %3
br label %34
34: ; preds = %27, %26
%35 = load i32*, i32** %3
ret i32* %35
}

LLVM IR (-O2):
define i32* @constructST(i32* %0, i32 %1) {
%3 = tail call i32 @log2(i32 %1)
%4 = tail call i64 @ceil(i32 %3)
%5 = trunc i64 %4 to i32
%6 = tail call i64 @pow(i32 2, i32 %5)
%7 = trunc i64 %6 to i32
%8 = shl i32 %7, 3
%9 = add i32 %8, -4
%10 = tail call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i32*
%12 = icmp eq i64 %10, 0
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = add nsw i32 %1, -1
%15 = tail call i32 @constructSTUtil(i32* %0, i32 0, i32 %14, i32* %11, i32 0)
br label %16
16: ; preds = %2, %13
%17 = phi i32* [ %11, %13 ], [ null, %2 ]
ret i32* %17
}