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


x86 assembly (clang, -O0):
<BottomUpSort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jae    124d <BottomUpSort+0x12d>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x4(%rbp),%eax
jae    11b3 <BottomUpSort+0x93>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%edi
add    -0x1c(%rbp),%edi
mov    -0x4(%rbp),%esi
call   1030 <min@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
add    %eax,%edi
mov    -0x4(%rbp),%esi
call   1030 <min@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    %eax,%ecx
mov    -0x18(%rbp),%r8
call   1040 <BottomUpMerge@plt>
mov    -0x20(%rbp),%eax
mov    -0x1c(%rbp),%ecx
shl    $0x1,%ecx
add    %ecx,%eax
mov    %eax,-0x20(%rbp)
jmp    114d <BottomUpSort+0x2d>
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jb     11cd <BottomUpSort+0xad>
jmp    124d <BottomUpSort+0x12d>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x4(%rbp),%eax
jae    123a <BottomUpSort+0x11a>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%edi
add    -0x1c(%rbp),%edi
mov    -0x4(%rbp),%esi
call   1030 <min@plt>
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
add    %eax,%edi
mov    -0x4(%rbp),%esi
call   1030 <min@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%esi
mov    -0x34(%rbp),%edx
mov    %eax,%ecx
mov    -0x10(%rbp),%r8
call   1040 <BottomUpMerge@plt>
mov    -0x20(%rbp),%eax
mov    -0x1c(%rbp),%ecx
shl    $0x1,%ecx
add    %ecx,%eax
mov    %eax,-0x20(%rbp)
jmp    11d4 <BottomUpSort+0xb4>
jmp    123f <BottomUpSort+0x11f>
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    113a <BottomUpSort+0x1a>
add    $0x40,%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,%edi
jb     11fb <BottomUpSort+0xdb>
mov    %rdx,%r14
mov    %edi,%r12d
mov    $0x1,%r15d
mov    %rdx,(%rsp)
mov    %rsi,0x10(%rsp)
nopl   0x0(%rax)
lea    (%r15,%r15,1),%eax
mov    %rax,0x8(%rsp)
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
lea    (%r15,%rbx,1),%edi
mov    %r12d,%esi
call   1030 <min@plt>
mov    %eax,%r13d
mov    0x8(%rsp),%rax
lea    (%rbx,%rax,1),%ebp
mov    %ebp,%edi
mov    %r12d,%esi
call   1030 <min@plt>
mov    0x10(%rsp),%rdi
mov    %ebx,%esi
mov    %r13d,%edx
mov    %eax,%ecx
mov    %r14,%r8
call   1040 <BottomUpMerge@plt>
mov    %ebp,%ebx
cmp    %r12d,%ebp
jb     1160 <BottomUpSort+0x40>
mov    0x8(%rsp),%rax
cmp    %r12d,%eax
jae    11fb <BottomUpSort+0xdb>
shl    $0x2,%r15d
xor    %ebx,%ebx
nopl   0x0(%rax)
lea    (%rax,%rbx,1),%edi
mov    %r12d,%esi
mov    %rax,%r14
call   1030 <min@plt>
mov    %eax,%r13d
lea    (%rbx,%r15,1),%ebp
mov    %ebp,%edi
mov    %r12d,%esi
call   1030 <min@plt>
mov    (%rsp),%rdi
mov    %ebx,%esi
mov    %r13d,%edx
mov    %eax,%ecx
mov    0x10(%rsp),%r8
call   1040 <BottomUpMerge@plt>
mov    %r14,%rax
mov    %ebp,%ebx
cmp    %r12d,%ebp
jb     11b0 <BottomUpSort+0x90>
cmp    %r12d,%r15d
mov    (%rsp),%r14
jb     1150 <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 = %73, %3
%10 = load i32, i32* %7
%11 = load i32, i32* %4
%12 = icmp ult i32 %10, %11
br i1 %12, label %13, label %76
13: ; preds = %9
store i32 0, i32* %8
br label %14
14: ; preds = %34, %13
%15 = load i32, i32* %8
%16 = load i32, i32* %4
%17 = icmp ult i32 %15, %16
br i1 %17, label %18, label %39
18: ; preds = %14
%19 = load i32*, i32** %5
%20 = load i32, i32* %8
%21 = load i32, i32* %8
%22 = load i32, i32* %7
%23 = add i32 %21, %22
%24 = load i32, i32* %4
%25 = call i32 @min(i32 %23, i32 %24)
%26 = load i32, i32* %8
%27 = load i32, i32* %7
%28 = mul i32 2, %27
%29 = add i32 %26, %28
%30 = load i32, i32* %4
%31 = call i32 @min(i32 %29, i32 %30)
%32 = load i32*, i32** %6
%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 i32 2, %36
%38 = add i32 %35, %37
store i32 %38, i32* %8
br label %14
39: ; preds = %14
%40 = load i32, i32* %7
%41 = mul i32 2, %40
store i32 %41, i32* %7
%42 = load i32, i32* %7
%43 = load i32, i32* %4
%44 = icmp uge i32 %42, %43
br i1 %44, label %45, label %46
45: ; preds = %39
br label %76
46: ; preds = %39
store i32 0, i32* %8
br label %47
47: ; preds = %67, %46
%48 = load i32, i32* %8
%49 = load i32, i32* %4
%50 = icmp ult i32 %48, %49
br i1 %50, label %51, label %72
51: ; preds = %47
%52 = load i32*, i32** %6
%53 = load i32, i32* %8
%54 = load i32, i32* %8
%55 = load i32, i32* %7
%56 = add i32 %54, %55
%57 = load i32, i32* %4
%58 = call i32 @min(i32 %56, i32 %57)
%59 = load i32, i32* %8
%60 = load i32, i32* %7
%61 = mul i32 2, %60
%62 = add i32 %59, %61
%63 = load i32, i32* %4
%64 = call i32 @min(i32 %62, i32 %63)
%65 = load i32*, i32** %5
%66 = call i32 @BottomUpMerge(i32* %52, i32 %53, i32 %58, i32 %64, i32* %65)
br label %67
67: ; preds = %51
%68 = load i32, i32* %8
%69 = load i32, i32* %7
%70 = mul i32 2, %69
%71 = add i32 %68, %70
store i32 %71, i32* %8
br label %47
72: ; preds = %47
br label %73
73: ; preds = %72
%74 = load i32, i32* %7
%75 = mul i32 2, %74
store i32 %75, i32* %7
br label %9
76: ; preds = %45, %9
ret void
}

LLVM IR (-O2):
define void @BottomUpSort(i32 %0, i32* %1, i32* %2) {
%4 = icmp ugt i32 %0, 1
br i1 %4, label %5, label %30
5: ; preds = %3, %28
%6 = phi i32 [ %19, %28 ], [ 1, %3 ]
%7 = shl i32 %6, 1
br label %8
8: ; preds = %5, %8
%9 = phi i32 [ 0, %5 ], [ %12, %8 ]
%10 = add i32 %9, %6
%11 = tail call i32 @min(i32 %10, i32 %0)
%12 = add i32 %9, %7
%13 = tail call i32 @min(i32 %12, i32 %0)
%14 = tail call i32 @BottomUpMerge(i32* %1, i32 %9, i32 %11, i32 %13, i32* %2)
%15 = icmp ult i32 %12, %0
br i1 %15, label %8, label %16
16: ; preds = %8
%17 = icmp ult i32 %7, %0
br i1 %17, label %18, label %30
18: ; preds = %16
%19 = shl i32 %6, 2
br label %20
20: ; preds = %18, %20
%21 = phi i32 [ 0, %18 ], [ %24, %20 ]
%22 = add i32 %21, %7
%23 = tail call i32 @min(i32 %22, i32 %0)
%24 = add i32 %21, %19
%25 = tail call i32 @min(i32 %24, i32 %0)
%26 = tail call i32 @BottomUpMerge(i32* %2, i32 %21, i32 %23, i32 %25, i32* %1)
%27 = icmp ult i32 %24, %0
br i1 %27, label %20, label %28
28: ; preds = %20
%29 = icmp ult i32 %19, %0
br i1 %29, label %5, label %30
30: ; preds = %28, %16, %3
ret void
}