#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);
void bottom_up_merge_sort(int *array, int n) {
  int i, w;
  for (w = 2; w < n * 2; w *= 2) {
    for (i = 0; i + w < n; i += w)
      _merge(array, i, i + w / 2, i + w);
    if (i + w / 2 < n)
      _merge(array, i, i + w / 2, n);
  }
}


x86 assembly (clang, -O0):
<bottom_up_merge_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x2,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0xc(%rbp),%ecx
shl    $0x1,%ecx
cmp    %ecx,%eax
jge    11dc <bottom_up_merge_sort+0xcc>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1183 <bottom_up_merge_sort+0x73>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x18(%rbp),%edx
add    %eax,%edx
mov    -0x10(%rbp),%ecx
add    -0x14(%rbp),%ecx
call   1030 <_merge@plt>
mov    -0x14(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    113e <bottom_up_merge_sort+0x2e>
mov    -0x10(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
add    %ecx,%eax
cmp    -0xc(%rbp),%eax
jge    11c9 <bottom_up_merge_sort+0xb9>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0x10(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x20(%rbp),%edx
add    %eax,%edx
mov    -0xc(%rbp),%ecx
call   1030 <_merge@plt>
jmp    11ce <bottom_up_merge_sort+0xbe>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1126 <bottom_up_merge_sort+0x16>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bottom_up_merge_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%esi
jl     1195 <bottom_up_merge_sort+0x85>
mov    %esi,%r15d
mov    %rdi,%r14
lea    (%r15,%r15,1),%r12d
mov    $0x2,%ebp
jmp    1147 <bottom_up_merge_sort+0x37>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    %ebp,%ebp
cmp    %r12d,%ebp
jge    1195 <bottom_up_merge_sort+0x85>
xor    %ebx,%ebx
cmp    %r15d,%ebp
jge    117b <bottom_up_merge_sort+0x6b>
mov    %ebp,%r13d
shr    $0x1f,%r13d
add    %ebp,%r13d
sar    %r13d
xor    %esi,%esi
nopl   (%rax)
lea    (%rsi,%rbp,1),%ebx
lea    (%rsi,%r13,1),%edx
mov    %r14,%rdi
mov    %ebx,%ecx
call   1030 <_merge@plt>
lea    (%rbx,%rbp,1),%eax
mov    %ebx,%esi
cmp    %r15d,%eax
jl     1160 <bottom_up_merge_sort+0x50>
mov    %ebp,%edx
shr    %edx
add    %ebx,%edx
cmp    %r15d,%edx
jge    1140 <bottom_up_merge_sort+0x30>
mov    %r14,%rdi
mov    %ebx,%esi
mov    %r15d,%ecx
call   1030 <_merge@plt>
jmp    1140 <bottom_up_merge_sort+0x30>
add    $0x8,%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 i32
%6 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
store i32 2, i32* %6
br label %7
7: ; preds = %51, %2
%8 = load i32, i32* %6
%9 = load i32, i32* %4
%10 = mul nsw i32 %9, 2
%11 = icmp slt i32 %8, %10
br i1 %11, label %12, label %54
12: ; preds = %7
store i32 0, i32* %5
br label %13
13: ; preds = %30, %12
%14 = load i32, i32* %5
%15 = load i32, i32* %6
%16 = add nsw i32 %14, %15
%17 = load i32, i32* %4
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %34
19: ; preds = %13
%20 = load i32*, i32** %3
%21 = load i32, i32* %5
%22 = load i32, i32* %5
%23 = load i32, i32* %6
%24 = sdiv i32 %23, 2
%25 = add nsw i32 %22, %24
%26 = load i32, i32* %5
%27 = load i32, i32* %6
%28 = add nsw i32 %26, %27
%29 = call i32 @_merge(i32* %20, i32 %21, i32 %25, i32 %28)
br label %30
30: ; preds = %19
%31 = load i32, i32* %6
%32 = load i32, i32* %5
%33 = add nsw i32 %32, %31
store i32 %33, i32* %5
br label %13
34: ; preds = %13
%35 = load i32, i32* %5
%36 = load i32, i32* %6
%37 = sdiv i32 %36, 2
%38 = add nsw i32 %35, %37
%39 = load i32, i32* %4
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %50
41: ; preds = %34
%42 = load i32*, i32** %3
%43 = load i32, i32* %5
%44 = load i32, i32* %5
%45 = load i32, i32* %6
%46 = sdiv i32 %45, 2
%47 = add nsw i32 %44, %46
%48 = load i32, i32* %4
%49 = call i32 @_merge(i32* %42, i32 %43, i32 %47, i32 %48)
br label %50
50: ; preds = %41, %34
br label %51
51: ; preds = %50
%52 = load i32, i32* %6
%53 = mul nsw i32 %52, 2
store i32 %53, i32* %6
br label %7
54: ; preds = %7
ret void
}

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