#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*/ BottomUpMerge(int *, int, int /*<<< orphan*/,
                                 int /*<<< orphan*/, int *);
int /*<<< orphan*/ CopyArray(int *, int *, int);
int /*<<< orphan*/ min(int, int);
void BottomUpSort(int A[], int B[], int n) {
  int width, i;
  for (width = 1; width < n; width = 2 * width) {
    for (i = 0; i < n; i = i + 2 * width) {
      BottomUpMerge(A, i, min(i + width, n), min(i + 2 * width, n), B);
    }
    CopyArray(B, A, n);
  }
}


x86 assembly (clang, -O0):
<BottomUpSort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11e1 <BottomUpSort+0xb1>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11c3 <BottomUpSort+0x93>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
add    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1030 <min@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%eax
shl    $0x1,%eax
add    %eax,%edi
mov    -0x14(%rbp),%esi
call   1030 <min@plt>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    %eax,%ecx
mov    -0x10(%rbp),%r8
call   1050 <BottomUpMerge@plt>
mov    -0x1c(%rbp),%eax
mov    -0x18(%rbp),%ecx
shl    $0x1,%ecx
add    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    115d <BottomUpSort+0x2d>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <CopyArray@plt>
mov    -0x18(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    114a <BottomUpSort+0x1a>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BottomUpSort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edx
jl     11cd <BottomUpSort+0x9d>
mov    %edx,%r12d
mov    %rsi,%r15
mov    %rdi,%r14
mov    $0x1,%eax
mov    %rax,0x8(%rsp)
nopw   0x0(%rax,%rax,1)
mov    0x8(%rsp),%rax
add    %eax,%eax
mov    %rax,0x10(%rsp)
xor    %ebx,%ebx
xchg   %ax,%ax
mov    0x8(%rsp),%rax
lea    (%rax,%rbx,1),%edi
mov    %r12d,%esi
call   1030 <min@plt>
mov    %eax,%r13d
mov    0x10(%rsp),%rax
lea    (%rbx,%rax,1),%ebp
mov    %ebp,%edi
mov    %r12d,%esi
call   1030 <min@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    %r13d,%edx
mov    %eax,%ecx
mov    %r15,%r8
call   1050 <BottomUpMerge@plt>
mov    %ebp,%ebx
cmp    %r12d,%ebp
jl     1170 <BottomUpSort+0x40>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1040 <CopyArray@plt>
mov    0x10(%rsp),%rax
mov    %eax,%ecx
mov    %rcx,0x8(%rsp)
cmp    %r12d,%eax
jl     1160 <BottomUpSort+0x30>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @BottomUpSort(i32* %0, i32* %1, i32 %2) {
%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
store i32 %2, i32* %6
store i32 1, i32* %7
br label %9
9: ; preds = %44, %3
%10 = load i32, i32* %7
%11 = load i32, i32* %6
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %47
13: ; preds = %9
store i32 0, i32* %8
br label %14
14: ; preds = %34, %13
%15 = load i32, i32* %8
%16 = load i32, i32* %6
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %39
18: ; preds = %14
%19 = load i32*, i32** %4
%20 = load i32, i32* %8
%21 = load i32, i32* %8
%22 = load i32, i32* %7
%23 = add nsw i32 %21, %22
%24 = load i32, i32* %6
%25 = call i32 @min(i32 %23, i32 %24)
%26 = load i32, i32* %8
%27 = load i32, i32* %7
%28 = mul nsw i32 2, %27
%29 = add nsw i32 %26, %28
%30 = load i32, i32* %6
%31 = call i32 @min(i32 %29, i32 %30)
%32 = load i32*, i32** %5
%33 = call i32 @BottomUpMerge(i32* %19, i32 %20, i32 %25, i32 %31, i32* %32)
br label %34
34: ; preds = %18
%35 = load i32, i32* %8
%36 = load i32, i32* %7
%37 = mul nsw i32 2, %36
%38 = add nsw i32 %35, %37
store i32 %38, i32* %8
br label %14
39: ; preds = %14
%40 = load i32*, i32** %5
%41 = load i32*, i32** %4
%42 = load i32, i32* %6
%43 = call i32 @CopyArray(i32* %40, i32* %41, i32 %42)
br label %44
44: ; preds = %39
%45 = load i32, i32* %7
%46 = mul nsw i32 2, %45
store i32 %46, i32* %7
br label %9
47: ; preds = %9
ret void
}

LLVM IR (-O2):
define void @BottomUpSort(i32* %0, i32* %1, i32 %2) {
%4 = icmp sgt i32 %2, 1
br i1 %4, label %5, label %19
5: ; preds = %3, %16
%6 = phi i32 [ %7, %16 ], [ 1, %3 ]
%7 = shl nsw i32 %6, 1
br label %8
8: ; preds = %5, %8
%9 = phi i32 [ 0, %5 ], [ %12, %8 ]
%10 = add nsw i32 %9, %6
%11 = tail call i32 @min(i32 %10, i32 %2)
%12 = add nsw i32 %9, %7
%13 = tail call i32 @min(i32 %12, i32 %2)
%14 = tail call i32 @BottomUpMerge(i32* %0, i32 %9, i32 %11, i32 %13, i32* %1)
%15 = icmp slt i32 %12, %2
br i1 %15, label %8, label %16
16: ; preds = %8
%17 = tail call i32 @CopyArray(i32* %1, i32* %0, i32 %2)
%18 = icmp slt i32 %7, %2
br i1 %18, label %5, label %19
19: ; preds = %16, %3
ret void
}