#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 */
struct ptrlist {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ assert(struct ptrlist *const);
int /*<<< orphan*/ merge_chunk(struct ptrlist *const, void *, size_t,
                               int (*)(void const *, void const *, void *));
size_t ptrlist_len(struct ptrlist *const);
void ptrlist_sort(struct ptrlist *const l, void *data,
                  int (*cmp)(const void *, const void *, void *)) {
  assert(l);
  const size_t len = ptrlist_len(l);
  if (len < 2)
    return;
  for (size_t chunk = 1; chunk < len; chunk *= 2)
    merge_chunk(l, data, chunk, cmp);
}


x86 assembly (clang, -O0):
<ptrlist_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1050 <ptrlist_len@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x2,-0x20(%rbp)
jae    116a <ptrlist_sort+0x3a>
jmp    11a6 <ptrlist_sort+0x76>
movq   $0x1,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11a6 <ptrlist_sort+0x76>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1030 <merge_chunk@plt>
mov    -0x28(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1172 <ptrlist_sort+0x42>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptrlist_sort>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
call   1040 <assert@plt>
mov    %r12,%rdi
call   1050 <ptrlist_len@plt>
cmp    $0x2,%rax
jb     1179 <ptrlist_sort+0x49>
mov    %rax,%r13
mov    $0x1,%ebx
nopl   (%rax)
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1030 <merge_chunk@plt>
add    %rbx,%rbx
cmp    %r13,%rbx
jb     1160 <ptrlist_sort+0x30>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.ptrlist = type { i32 }
define void @ptrlist_sort(%struct.ptrlist* %0, i8* %1, i32 (i8*, i8*, i8*)* %2) {
%4 = alloca %struct.ptrlist*
%5 = alloca i8*
%6 = alloca i32 (i8*, i8*, i8*)*
%7 = alloca i64
%8 = alloca i64
store %struct.ptrlist* %0, %struct.ptrlist** %4
store i8* %1, i8** %5
store i32 (i8*, i8*, i8*)* %2, i32 (i8*, i8*, i8*)** %6
%9 = load %struct.ptrlist*, %struct.ptrlist** %4
%10 = call i32 @assert(%struct.ptrlist* %9)
%11 = load %struct.ptrlist*, %struct.ptrlist** %4
%12 = call i64 @ptrlist_len(%struct.ptrlist* %11)
store i64 %12, i64* %7
%13 = load i64, i64* %7
%14 = icmp ult i64 %13, 2
br i1 %14, label %15, label %16
15: ; preds = %3
br label %30
16: ; preds = %3
store i64 1, i64* %8
br label %17
17: ; preds = %27, %16
%18 = load i64, i64* %8
%19 = load i64, i64* %7
%20 = icmp ult i64 %18, %19
br i1 %20, label %21, label %30
21: ; preds = %17
%22 = load %struct.ptrlist*, %struct.ptrlist** %4
%23 = load i8*, i8** %5
%24 = load i64, i64* %8
%25 = load i32 (i8*, i8*, i8*)*, i32 (i8*, i8*, i8*)** %6
%26 = call i32 @merge_chunk(%struct.ptrlist* %22, i8* %23, i64 %24, i32 (i8*, i8*, i8*)* %25)
br label %27
27: ; preds = %21
%28 = load i64, i64* %8
%29 = mul i64 %28, 2
store i64 %29, i64* %8
br label %17
30: ; preds = %15, %17
ret void
}

LLVM IR (-O2):
%struct.ptrlist = type { i32 }
define void @ptrlist_sort(%struct.ptrlist* %0, i8* %1, i32 (i8*, i8*, i8*)* %2) {
%4 = tail call i32 @assert(%struct.ptrlist* %0)
%5 = tail call i64 @ptrlist_len(%struct.ptrlist* %0)
%6 = icmp ugt i64 %5, 1
br i1 %6, label %7, label %12
7: ; preds = %3, %7
%8 = phi i64 [ %10, %7 ], [ 1, %3 ]
%9 = tail call i32 @merge_chunk(%struct.ptrlist* %0, i8* %1, i64 %8, i32 (i8*, i8*, i8*)* %2)
%10 = shl i64 %8, 1
%11 = icmp ult i64 %10, %5
br i1 %11, label %7, label %12
12: ; preds = %7, %3
ret void
}