#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 *, size_t, size_t, size_t);
void merge_sort_bottom_up_internal(int array[], int temp[], size_t length) {
  for (size_t i = 1; i < length; i = i * 2) {
    size_t j = length - 1 - i;
    for (;;) {
      size_t low = 0;
      if (j >= i - 1) {
        low = j - i + 1;
      }
      size_t middle = j;
      size_t high = j + i;
      merge(array, temp, low, middle, high);
      if (j < i * 2) {
        break;
      }
      j = j - i * 2;
    }
  }
}


x86 assembly (clang, -O0):
<merge_sort_bottom_up_internal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11ec <merge_sort_bottom_up_internal+0xdc>
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
sub    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jb     1177 <merge_sort_bottom_up_internal+0x67>
mov    -0x28(%rbp),%rax
sub    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    -0x40(%rbp),%r8
call   1030 <merge@plt>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
shl    $0x1,%rcx
cmp    %rcx,%rax
jae    11be <merge_sort_bottom_up_internal+0xae>
jmp    11d6 <merge_sort_bottom_up_internal+0xc6>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
shl    $0x1,%rcx
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    114a <merge_sort_bottom_up_internal+0x3a>
jmp    11db <merge_sort_bottom_up_internal+0xcb>
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    112c <merge_sort_bottom_up_internal+0x1c>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<merge_sort_bottom_up_internal>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x2,%rdx
jb     11b0 <merge_sort_bottom_up_internal+0xa0>
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %rdi,%r12
lea    -0x1(%rdx),%rax
mov    %rax,0x8(%rsp)
mov    $0x1,%r13d
xor    %eax,%eax
mov    %rdx,0x10(%rsp)
nopw   0x0(%rax,%rax,1)
mov    %r13,%r14
lea    -0x1(%r13),%rcx
mov    %rcx,0x18(%rsp)
add    %r13,%r13
sub    %r13,%r15
mov    %r14,0x20(%rsp)
neg    %r14
mov    0x8(%rsp),%rbx
nop
lea    (%r14,%rbx,1),%rcx
cmp    0x18(%rsp),%rcx
mov    %r15,%rdx
cmovb  %rax,%rdx
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %rbx,%r8
call   1030 <merge@plt>
sub    %r13,%r15
sub    %r13,%rbx
mov    0x20(%rsp),%rax
add    %rbx,%rax
cmp    %r13,%rax
mov    $0x0,%eax
jae    1170 <merge_sort_bottom_up_internal+0x60>
mov    0x10(%rsp),%r15
cmp    %r15,%r13
jb     1150 <merge_sort_bottom_up_internal+0x40>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @merge_sort_bottom_up_internal(i32* %0, i32* %1, i64 %2) {
%4 = icmp ugt i64 %2, 1
br i1 %4, label %8, label %7
5: ; preds = %15
%6 = icmp ult i64 %14, %2
br i1 %6, label %8, label %7
7: ; preds = %5, %3
ret void
8: ; preds = %3, %5
%9 = phi i64 [ %14, %5 ], [ 1, %3 ]
%10 = xor i64 %9, -1
%11 = add i64 %10, %2
%12 = add i64 %9, -1
%13 = sub i64 1, %9
%14 = shl i64 %9, 1
br label %15
15: ; preds = %15, %8
%16 = phi i64 [ %11, %8 ], [ %23, %15 ]
%17 = icmp ult i64 %16, %12
%18 = add i64 %13, %16
%19 = select i1 %17, i64 0, i64 %18
%20 = add i64 %16, %9
%21 = tail call i32 @merge(i32* %0, i32* %1, i64 %19, i64 %16, i64 %20)
%22 = icmp ult i64 %16, %14
%23 = sub i64 %16, %14
br i1 %22, label %5, label %15
}