#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 /*<<< orphan*/ _merge(int *, int *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ min(int, int);
void bottom_up_merge_sort(int *array, int length) {
  int aux[length];
  for (int size = 1; size < length; size = 2 * size) {
    for (int low = 0; low < length - size; low += 2 * size) {
      _merge(array, aux, low, low + size - 1,
             min(low + size + size - 1, length - 1));
    }
  }
}


x86 assembly (clang, -O0):
<bottom_up_merge_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11ec <bottom_up_merge_sort+0xcc>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    -0x24(%rbp),%ecx
cmp    %ecx,%eax
jge    11d9 <bottom_up_merge_sort+0xb9>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%edi
add    -0x24(%rbp),%edi
add    -0x24(%rbp),%edi
sub    $0x1,%edi
mov    -0xc(%rbp),%esi
sub    $0x1,%esi
call   1040 <min@plt>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%edx
mov    -0x34(%rbp),%ecx
mov    %eax,%r8d
call   1030 <_merge@plt>
mov    -0x24(%rbp),%eax
shl    $0x1,%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    1170 <bottom_up_merge_sort+0x50>
jmp    11de <bottom_up_merge_sort+0xbe>
mov    -0x24(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    115d <bottom_up_merge_sort+0x3d>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bottom_up_merge_sort>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,-0x48(%rbp)
mov    %esi,%eax
mov    %rsp,%rcx
lea    0xf(,%rax,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
cmp    $0x2,%esi
jl     11d2 <bottom_up_merge_sort+0xb2>
lea    -0x1(%rsi),%r12d
mov    $0x1,%r15d
mov    %rsi,-0x38(%rbp)
jmp    117f <bottom_up_merge_sort+0x5f>
cs nopw 0x0(%rax,%rax,1)
nop
mov    -0x30(%rbp),%rcx
mov    %ecx,%r15d
mov    -0x38(%rbp),%rsi
cmp    %esi,%ecx
jge    11d2 <bottom_up_merge_sort+0xb2>
lea    (%r15,%r15,1),%ecx
mov    %rcx,-0x30(%rbp)
mov    %esi,%r13d
sub    %r15d,%r13d
jle    1170 <bottom_up_merge_sort+0x50>
mov    -0x30(%rbp),%rcx
add    $0xffffffff,%ecx
mov    %rcx,-0x50(%rbp)
add    $0xffffffff,%r15d
xor    %ebx,%ebx
lea    (%r15,%rbx,1),%r14d
mov    -0x50(%rbp),%rcx
lea    (%rcx,%rbx,1),%edi
mov    %r12d,%esi
call   1040 <min@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %ebx,%edx
mov    %r14d,%ecx
mov    %eax,%r8d
call   1030 <_merge@plt>
add    -0x30(%rbp),%ebx
cmp    %r13d,%ebx
jl     11a0 <bottom_up_merge_sort+0x80>
jmp    1170 <bottom_up_merge_sort+0x50>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @bottom_up_merge_sort(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%9 = load i32, i32* %4
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %5
%12 = alloca i32, i64 %10
store i64 %10, i64* %6
store i32 1, i32* %7
br label %13
13: ; preds = %47, %2
%14 = load i32, i32* %7
%15 = load i32, i32* %4
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %50
17: ; preds = %13
store i32 0, i32* %8
br label %18
18: ; preds = %41, %17
%19 = load i32, i32* %8
%20 = load i32, i32* %4
%21 = load i32, i32* %7
%22 = sub nsw i32 %20, %21
%23 = icmp slt i32 %19, %22
br i1 %23, label %24, label %46
24: ; preds = %18
%25 = load i32*, i32** %3
%26 = load i32, i32* %8
%27 = load i32, i32* %8
%28 = load i32, i32* %7
%29 = add nsw i32 %27, %28
%30 = sub nsw i32 %29, 1
%31 = load i32, i32* %8
%32 = load i32, i32* %7
%33 = add nsw i32 %31, %32
%34 = load i32, i32* %7
%35 = add nsw i32 %33, %34
%36 = sub nsw i32 %35, 1
%37 = load i32, i32* %4
%38 = sub nsw i32 %37, 1
%39 = call i32 @min(i32 %36, i32 %38)
%40 = call i32 @_merge(i32* %25, i32* %12, i32 %26, i32 %30, i32 %39)
br label %41
41: ; preds = %24
%42 = load i32, i32* %7
%43 = mul nsw i32 2, %42
%44 = load i32, i32* %8
%45 = add nsw i32 %44, %43
store i32 %45, i32* %8
br label %18
46: ; preds = %18
br label %47
47: ; preds = %46
%48 = load i32, i32* %7
%49 = mul nsw i32 2, %48
store i32 %49, i32* %7
br label %13
50: ; preds = %13
%51 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
define void @bottom_up_merge_sort(i32* %0, i32 %1) {
%3 = zext i32 %1 to i64
%4 = alloca i32, i64 %3
%5 = icmp sgt i32 %1, 1
br i1 %5, label %6, label %13
6: ; preds = %2
%7 = add nsw i32 %1, -1
br label %8
8: ; preds = %6, %14
%9 = phi i32 [ 1, %6 ], [ %12, %14 ]
%10 = sub nsw i32 %1, %9
%11 = icmp sgt i32 %10, 0
%12 = shl nsw i32 %9, 1
br i1 %11, label %16, label %14
13: ; preds = %14, %2
ret void
14: ; preds = %16, %8
%15 = icmp slt i32 %12, %1
br i1 %15, label %8, label %13
16: ; preds = %8, %16
%17 = phi i32 [ %23, %16 ], [ 0, %8 ]
%18 = add nsw i32 %17, %9
%19 = add i32 %18, -1
%20 = add i32 %19, %9
%21 = call i32 @min(i32 %20, i32 %7)
%22 = call i32 @_merge(i32* %0, i32* %4, i32 %17, i32 %19, i32 %21)
%23 = add nsw i32 %17, %12
%24 = icmp slt i32 %23, %10
br i1 %24, label %16, label %14
}