#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*/ free(void *);
void *malloc(size_t);
int /*<<< orphan*/ merge(void *, size_t, size_t,
                         int (*)(void const *, void const *, void *), void *,
                         void *, size_t, size_t, size_t);
void bottom_up_merge_sort(void *base, size_t nmemb, size_t size,
                          int (*compare)(const void *a, const void *b,
                                         void *context),
                          void *context) {
  void *aux;
  size_t sz;
  size_t lo;
  aux = malloc(nmemb * size);
  if (aux == NULL) {
    return;
  }
  for (sz = 1; sz < nmemb; sz *= 2) {
    for (lo = 0; lo < nmemb - sz; lo += (sz * 2)) {
      merge(base, nmemb, size, compare, context, aux, lo, lo + sz - 1,
            (((lo + (sz * 2) - 1) < (nmemb - 1)) ? (lo + (sz * 2) - 1)
                                                 : (nmemb - 1)));
    }
  }
  free(aux);
}


x86 assembly (clang, -O0):
<bottom_up_merge_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
imul   -0x18(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1171 <bottom_up_merge_sort+0x41>
jmp    12ac <bottom_up_merge_sort+0x17c>
movq   $0x1,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    12a3 <bottom_up_merge_sort+0x173>
movq   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    -0x38(%rbp),%rcx
cmp    %rcx,%rax
jae    128d <bottom_up_merge_sort+0x15d>
mov    -0x8(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%rax
add    -0x38(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%rcx
shl    $0x1,%rcx
add    %rcx,%rax
sub    $0x1,%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jae    122f <bottom_up_merge_sort+0xff>
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%rcx
shl    $0x1,%rcx
add    %rcx,%rax
sub    $0x1,%rax
mov    %rax,-0x88(%rbp)
jmp    123e <bottom_up_merge_sort+0x10e>
mov    -0x10(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x88(%rbp)
mov    -0x48(%rbp),%r10
mov    -0x50(%rbp),%r11
mov    -0x58(%rbp),%r9
mov    -0x60(%rbp),%r8
mov    -0x68(%rbp),%rcx
mov    -0x70(%rbp),%rdx
mov    -0x78(%rbp),%rsi
mov    -0x80(%rbp),%rdi
mov    -0x88(%rbp),%rax
mov    %r11,(%rsp)
mov    %r10,0x8(%rsp)
mov    %rax,0x10(%rsp)
call   1040 <merge@plt>
mov    -0x38(%rbp),%rax
shl    $0x1,%rax
add    -0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    118f <bottom_up_merge_sort+0x5f>
jmp    1292 <bottom_up_merge_sort+0x162>
mov    -0x38(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1179 <bottom_up_merge_sort+0x49>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bottom_up_merge_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %rdi,%r12
mov    %rdx,%rdi
imul   %rsi,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     1266 <bottom_up_merge_sort+0x136>
mov    %r12,0x18(%rsp)
mov    %rbx,0x20(%rsp)
mov    %r15,0x28(%rsp)
mov    %r14,0x30(%rsp)
cmp    $0x2,%rbp
jb     1250 <bottom_up_merge_sort+0x120>
mov    %rbp,%r12
lea    -0x1(%rbp),%rsi
mov    $0x1,%ecx
mov    %rsi,0x10(%rsp)
jmp    11b3 <bottom_up_merge_sort+0x83>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    %rcx,%rcx
mov    %rcx,0x8(%rsp)
cmp    %r12,0x8(%rsp)
jae    1250 <bottom_up_merge_sort+0x120>
mov    %r12,%r14
sub    %rcx,%r14
je     11a0 <bottom_up_merge_sort+0x70>
lea    -0x1(%rcx),%rdi
mov    %rdi,0x40(%rsp)
lea    (%rcx,%rcx,1),%rdi
mov    %rdi,0x8(%rsp)
add    %rcx,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,0x38(%rsp)
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    0x40(%rsp),%rcx
lea    (%rcx,%rbx,1),%r10
mov    0x8(%rsp),%rcx
lea    (%rbx,%rcx,1),%r13
mov    0x38(%rsp),%rcx
lea    (%rcx,%rbx,1),%rbp
cmp    %rsi,%rbp
cmovae %rsi,%rbp
sub    $0x8,%rsp
mov    0x20(%rsp),%rdi
mov    %r12,%rsi
mov    0x28(%rsp),%rdx
mov    0x30(%rsp),%rcx
mov    0x38(%rsp),%r8
mov    %rax,%r15
mov    %rax,%r9
push   %rbp
push   %r10
push   %rbx
call   1040 <merge@plt>
mov    0x30(%rsp),%rsi
mov    %r15,%rax
add    $0x20,%rsp
mov    %r13,%rbx
cmp    %r14,%r13
jb     11e0 <bottom_up_merge_sort+0xb0>
mov    0x8(%rsp),%rcx
cmp    %r12,0x8(%rsp)
jb     11b3 <bottom_up_merge_sort+0x83>
mov    %rax,%rdi
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @bottom_up_merge_sort(i8* %0, i64 %1, i64 %2, i32 (i8*, i8*, i8*)* %3, i8* %4) {
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32 (i8*, i8*, i8*)*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i64
store i8* %0, i8** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i32 (i8*, i8*, i8*)* %3, i32 (i8*, i8*, i8*)** %9
store i8* %4, i8** %10
%14 = load i64, i64* %7
%15 = load i64, i64* %8
%16 = mul i64 %14, %15
%17 = call i8* @malloc(i64 %16)
store i8* %17, i8** %11
%18 = load i8*, i8** %11
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %5
br label %77
21: ; preds = %5
store i64 1, i64* %12
br label %22
22: ; preds = %71, %21
%23 = load i64, i64* %12
%24 = load i64, i64* %7
%25 = icmp ult i64 %23, %24
br i1 %25, label %26, label %74
26: ; preds = %22
store i64 0, i64* %13
br label %27
27: ; preds = %65, %26
%28 = load i64, i64* %13
%29 = load i64, i64* %7
%30 = load i64, i64* %12
%31 = sub i64 %29, %30
%32 = icmp ult i64 %28, %31
br i1 %32, label %33, label %70
33: ; preds = %27
%34 = load i8*, i8** %6
%35 = load i64, i64* %7
%36 = load i64, i64* %8
%37 = load i32 (i8*, i8*, i8*)*, i32 (i8*, i8*, i8*)** %9
%38 = load i8*, i8** %10
%39 = load i8*, i8** %11
%40 = load i64, i64* %13
%41 = load i64, i64* %13
%42 = load i64, i64* %12
%43 = add i64 %41, %42
%44 = sub i64 %43, 1
%45 = load i64, i64* %13
%46 = load i64, i64* %12
%47 = mul i64 %46, 2
%48 = add i64 %45, %47
%49 = sub i64 %48, 1
%50 = load i64, i64* %7
%51 = sub i64 %50, 1
%52 = icmp ult i64 %49, %51
br i1 %52, label %53, label %59
53: ; preds = %33
%54 = load i64, i64* %13
%55 = load i64, i64* %12
%56 = mul i64 %55, 2
%57 = add i64 %54, %56
%58 = sub i64 %57, 1
br label %62
59: ; preds = %33
%60 = load i64, i64* %7
%61 = sub i64 %60, 1
br label %62
62: ; preds = %59, %53
%63 = phi i64 [ %58, %53 ], [ %61, %59 ]
%64 = call i32 @merge(i8* %34, i64 %35, i64 %36, i32 (i8*, i8*, i8*)* %37, i8* %38, i8* %39, i64 %40, i64 %44, i64 %63)
br label %65
65: ; preds = %62
%66 = load i64, i64* %12
%67 = mul i64 %66, 2
%68 = load i64, i64* %13
%69 = add i64 %68, %67
store i64 %69, i64* %13
br label %27
70: ; preds = %27
br label %71
71: ; preds = %70
%72 = load i64, i64* %12
%73 = mul i64 %72, 2
store i64 %73, i64* %12
br label %22
74: ; preds = %22
%75 = load i8*, i8** %11
%76 = call i32 @free(i8* %75)
br label %77
77: ; preds = %74, %20
ret void
}

LLVM IR (-O2):
define void @bottom_up_merge_sort(i8* %0, i64 %1, i64 %2, i32 (i8*, i8*, i8*)* %3, i8* %4) {
%6 = mul i64 %2, %1
%7 = tail call i8* @malloc(i64 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %36, label %9
9: ; preds = %5
%10 = icmp ugt i64 %1, 1
br i1 %10, label %11, label %34
11: ; preds = %9
%12 = add i64 %1, -1
br label %13
13: ; preds = %11, %31
%14 = phi i64 [ 1, %11 ], [ %32, %31 ]
%15 = sub i64 %1, %14
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = shl i64 %14, 1
br label %31
19: ; preds = %13
%20 = add i64 %14, -1
%21 = shl i64 %14, 1
br label %22
22: ; preds = %19, %22
%23 = phi i64 [ 0, %19 ], [ %25, %22 ]
%24 = add i64 %20, %23
%25 = add i64 %23, %21
%26 = add i64 %25, -1
%27 = icmp ult i64 %26, %12
%28 = select i1 %27, i64 %26, i64 %12
%29 = tail call i32 @merge(i8* %0, i64 %1, i64 %2, i32 (i8*, i8*, i8*)* %3, i8* %4, i8* %7, i64 %23, i64 %24, i64 %28)
%30 = icmp ult i64 %25, %15
br i1 %30, label %22, label %31
31: ; preds = %22, %17
%32 = phi i64 [ %18, %17 ], [ %21, %22 ]
%33 = icmp ult i64 %32, %1
br i1 %33, label %13, label %34
34: ; preds = %31, %9
%35 = tail call i32 @free(i8* %7)
br label %36
36: ; preds = %5, %34
ret void
}