#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 list_cursor {
  int dummy;
};
struct list {
  int dummy;
};

/* Variables and functions */
struct list_cursor *cctools_list_cursor_create(struct list *);
int /*<<< orphan*/ cctools_list_cursor_destroy(struct list_cursor *);
int /*<<< orphan*/ cctools_list_drop(struct list_cursor *);
scalar_t__ cctools_list_get(struct list_cursor *, void **);
int /*<<< orphan*/ cctools_list_insert(struct list_cursor *, void *);
int /*<<< orphan*/ cctools_list_next(struct list_cursor *);
int /*<<< orphan*/ cctools_list_seek(struct list_cursor *, int /*<<< orphan*/);
int cctools_list_size(struct list *);
int /*<<< orphan*/ free(void **);
void **malloc(int);
int /*<<< orphan*/ qsort(void **, int, int,
                         int (*)(void const *, void const *));
struct list *list_sort(struct list *list,
                       int (*comparator)(const void *, const void *)) {
  void **array = NULL;
  int size, i = 0;
  struct list_cursor *cur = cctools_list_cursor_create(list);
  if (!cctools_list_seek(cur, 0))
    goto DONE;
  size = cctools_list_size(list);
  array = malloc(size * sizeof(*array));
  while (cctools_list_get(cur, &array[i])) {
    cctools_list_drop(cur);
    cctools_list_next(cur);
    i++;
  }
  qsort(array, size, sizeof(*array), comparator);
  for (i = 0; i < size; i++) {
    cctools_list_insert(cur, array[i]);
  }
DONE:
  free(array);
  cctools_list_cursor_destroy(cur);
  return list;
}


x86 assembly (clang, -O0):
<list_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   10c0 <cctools_list_cursor_create@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
call   10b0 <cctools_list_seek@plt>
cmp    $0x0,%eax
jne    11f5 <list_sort+0x45>
jmp    12a6 <list_sort+0xf6>
mov    -0x8(%rbp),%rdi
call   1090 <cctools_list_size@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
movslq -0x20(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rsi
call   1060 <cctools_list_get@plt>
cmp    $0x0,%rax
je     1256 <list_sort+0xa6>
mov    -0x28(%rbp),%rdi
call   1080 <cctools_list_drop@plt>
mov    -0x28(%rbp),%rdi
call   10d0 <cctools_list_next@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1214 <list_sort+0x64>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rcx
mov    $0x8,%edx
call   1050 <qsort@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12a1 <list_sort+0xf1>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1040 <cctools_list_insert@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1272 <list_sort+0xc2>
jmp    12a6 <list_sort+0xf6>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1070 <cctools_list_cursor_destroy@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<list_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %rdi,%r14
call   10c0 <cctools_list_cursor_create@plt>
mov    %rax,%rbx
xor    %r15d,%r15d
mov    %rax,%rdi
xor    %esi,%esi
call   10b0 <cctools_list_seek@plt>
test   %eax,%eax
je     1275 <list_sort+0xc5>
mov    %r14,%rdi
call   1090 <cctools_list_size@plt>
mov    %eax,%r13d
lea    0x0(,%r13,8),%edi
call   10a0 <malloc@plt>
mov    %rax,%r15
mov    %rbx,%rdi
mov    %rax,%rsi
call   1060 <cctools_list_get@plt>
test   %rax,%rax
je     1234 <list_sort+0x84>
mov    %r15,%rbp
add    $0x8,%rbp
mov    %rbx,%rdi
call   1080 <cctools_list_drop@plt>
mov    %rbx,%rdi
call   10d0 <cctools_list_next@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1060 <cctools_list_get@plt>
add    $0x8,%rbp
test   %rax,%rax
jne    1210 <list_sort+0x60>
mov    %r15,%rdi
mov    %r13d,%esi
mov    $0x8,%edx
mov    %r12,%rcx
call   1050 <qsort@plt>
test   %r13d,%r13d
jle    1275 <list_sort+0xc5>
mov    %r13d,%r12d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r15,%rbp,8),%rsi
mov    %rbx,%rdi
call   1040 <cctools_list_insert@plt>
add    $0x1,%rbp
cmp    %rbp,%r12
jne    1260 <list_sort+0xb0>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1070 <cctools_list_cursor_destroy@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.list = type { i32 }
%struct.list_cursor = type { i32 }
define %struct.list* @list_sort(%struct.list* %0, i32 (i8*, i8*)* %1) {
%3 = alloca %struct.list*
%4 = alloca i32 (i8*, i8*)*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.list_cursor*
store %struct.list* %0, %struct.list** %3
store i32 (i8*, i8*)* %1, i32 (i8*, i8*)** %4
store i8** null, i8*** %5
store i32 0, i32* %7
%9 = load %struct.list*, %struct.list** %3
%10 = call %struct.list_cursor* @cctools_list_cursor_create(%struct.list* %9)
store %struct.list_cursor* %10, %struct.list_cursor** %8
%11 = load %struct.list_cursor*, %struct.list_cursor** %8
%12 = call i32 @cctools_list_seek(%struct.list_cursor* %11, i32 0)
%13 = icmp ne i32 %12, 0
br i1 %13, label %15, label %14
14: ; preds = %2
br label %59
15: ; preds = %2
%16 = load %struct.list*, %struct.list** %3
%17 = call i32 @cctools_list_size(%struct.list* %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = sext i32 %18 to i64
%20 = mul i64 %19, 8
%21 = trunc i64 %20 to i32
%22 = call i8** @malloc(i32 %21)
store i8** %22, i8*** %5
br label %23
23: ; preds = %31, %15
%24 = load %struct.list_cursor*, %struct.list_cursor** %8
%25 = load i8**, i8*** %5
%26 = load i32, i32* %7
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8*, i8** %25, i64 %27
%29 = call i64 @cctools_list_get(%struct.list_cursor* %24, i8** %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %38
31: ; preds = %23
%32 = load %struct.list_cursor*, %struct.list_cursor** %8
%33 = call i32 @cctools_list_drop(%struct.list_cursor* %32)
%34 = load %struct.list_cursor*, %struct.list_cursor** %8
%35 = call i32 @cctools_list_next(%struct.list_cursor* %34)
%36 = load i32, i32* %7
%37 = add nsw i32 %36, 1
store i32 %37, i32* %7
br label %23
38: ; preds = %23
%39 = load i8**, i8*** %5
%40 = load i32, i32* %6
%41 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %4
%42 = call i32 @qsort(i8** %39, i32 %40, i32 8, i32 (i8*, i8*)* %41)
store i32 0, i32* %7
br label %43
43: ; preds = %55, %38
%44 = load i32, i32* %7
%45 = load i32, i32* %6
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %58
47: ; preds = %43
%48 = load %struct.list_cursor*, %struct.list_cursor** %8
%49 = load i8**, i8*** %5
%50 = load i32, i32* %7
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8*, i8** %49, i64 %51
%53 = load i8*, i8** %52
%54 = call i32 @cctools_list_insert(%struct.list_cursor* %48, i8* %53)
br label %55
55: ; preds = %47
%56 = load i32, i32* %7
%57 = add nsw i32 %56, 1
store i32 %57, i32* %7
br label %43
58: ; preds = %43
br label %59
59: ; preds = %58, %14
%60 = load i8**, i8*** %5
%61 = call i32 @free(i8** %60)
%62 = load %struct.list_cursor*, %struct.list_cursor** %8
%63 = call i32 @cctools_list_cursor_destroy(%struct.list_cursor* %62)
%64 = load %struct.list*, %struct.list** %3
ret %struct.list* %64
}

LLVM IR (-O2):
%struct.list = type { i32 }
%struct.list_cursor = type { i32 }
define %struct.list* @list_sort(%struct.list* returned %0, i32 (i8*, i8*)* nocapture %1) {
%3 = tail call %struct.list_cursor* @cctools_list_cursor_create(%struct.list* %0)
%4 = tail call i32 @cctools_list_seek(%struct.list_cursor* %3, i32 0)
%5 = icmp eq i32 %4, 0
br i1 %5, label %32, label %6
6: ; preds = %2
%7 = tail call i32 @cctools_list_size(%struct.list* %0)
%8 = shl i32 %7, 3
%9 = tail call i8** @malloc(i32 %8)
%10 = tail call i64 @cctools_list_get(%struct.list_cursor* %3, i8** %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %20, label %12
12: ; preds = %6, %12
%13 = phi i64 [ %16, %12 ], [ 0, %6 ]
%14 = tail call i32 @cctools_list_drop(%struct.list_cursor* %3)
%15 = tail call i32 @cctools_list_next(%struct.list_cursor* %3)
%16 = add nuw i64 %13, 1
%17 = getelementptr inbounds i8*, i8** %9, i64 %16
%18 = tail call i64 @cctools_list_get(%struct.list_cursor* %3, i8** %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %12
20: ; preds = %12, %6
%21 = tail call i32 @qsort(i8** %9, i32 %7, i32 8, i32 (i8*, i8*)* %1)
%22 = icmp sgt i32 %7, 0
br i1 %22, label %23, label %32
23: ; preds = %20
%24 = zext i32 %7 to i64
br label %25
25: ; preds = %23, %25
%26 = phi i64 [ 0, %23 ], [ %30, %25 ]
%27 = getelementptr inbounds i8*, i8** %9, i64 %26
%28 = load i8*, i8** %27
%29 = tail call i32 @cctools_list_insert(%struct.list_cursor* %3, i8* %28)
%30 = add nuw nsw i64 %26, 1
%31 = icmp eq i64 %30, %24
br i1 %31, label %32, label %25
32: ; preds = %25, %20, %2
%33 = phi i8** [ null, %2 ], [ %9, %20 ], [ %9, %25 ]
%34 = tail call i32 @free(i8** %33)
%35 = tail call i32 @cctools_list_cursor_destroy(%struct.list_cursor* %3)
ret %struct.list* %0
}