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

/* Variables and functions */
void *cctools_list_pop_head(struct list *);
int /*<<< orphan*/ cctools_list_push_tail(struct list *, void *);
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;
  int size, i = 0;
  size = cctools_list_size(list);
  array = malloc(size * sizeof(*array));
  while (cctools_list_size(list)) {
    array[i] = cctools_list_pop_head(list);
    i++;
  }
  qsort(array, size, sizeof(*array), comparator);
  for (i = 0; i < size; i++) {
    cctools_list_push_tail(list, array[i]);
  }
  free(array);
  return list;
}


x86 assembly (clang, -O0):
<list_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <cctools_list_size@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <cctools_list_size@plt>
cmp    $0x0,%eax
je     11ce <list_sort+0x6e>
mov    -0x8(%rbp),%rdi
call   1080 <cctools_list_pop_head@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1196 <list_sort+0x36>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rcx
mov    $0x8,%edx
call   1040 <qsort@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1219 <list_sort+0xb9>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1070 <cctools_list_push_tail@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11ea <list_sort+0x8a>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<list_sort>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r12
mov    %rdi,%r13
call   1050 <cctools_list_size@plt>
mov    %eax,%r15d
lea    0x0(,%r15,8),%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %r13,%rdi
call   1050 <cctools_list_size@plt>
test   %eax,%eax
je     11bb <list_sort+0x5b>
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1080 <cctools_list_pop_head@plt>
mov    %rax,(%rbx)
mov    %r13,%rdi
call   1050 <cctools_list_size@plt>
add    $0x8,%rbx
test   %eax,%eax
jne    11a0 <list_sort+0x40>
mov    %r14,%rdi
mov    %r15d,%esi
mov    $0x8,%edx
mov    %r12,%rcx
call   1040 <qsort@plt>
test   %r15d,%r15d
jle    11f5 <list_sort+0x95>
mov    %r15d,%r15d
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    (%r14,%rbx,8),%rsi
mov    %r13,%rdi
call   1070 <cctools_list_push_tail@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    11e0 <list_sort+0x80>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.list = 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
store %struct.list* %0, %struct.list** %3
store i32 (i8*, i8*)* %1, i32 (i8*, i8*)** %4
store i32 0, i32* %7
%8 = load %struct.list*, %struct.list** %3
%9 = call i32 @cctools_list_size(%struct.list* %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = sext i32 %10 to i64
%12 = mul i64 %11, 8
%13 = trunc i64 %12 to i32
%14 = call i8** @malloc(i32 %13)
store i8** %14, i8*** %5
br label %15
15: ; preds = %19, %2
%16 = load %struct.list*, %struct.list** %3
%17 = call i32 @cctools_list_size(%struct.list* %16)
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %15
%20 = load %struct.list*, %struct.list** %3
%21 = call i8* @cctools_list_pop_head(%struct.list* %20)
%22 = load i8**, i8*** %5
%23 = load i32, i32* %7
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8*, i8** %22, i64 %24
store i8* %21, i8** %25
%26 = load i32, i32* %7
%27 = add nsw i32 %26, 1
store i32 %27, i32* %7
br label %15
28: ; preds = %15
%29 = load i8**, i8*** %5
%30 = load i32, i32* %6
%31 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %4
%32 = call i32 @qsort(i8** %29, i32 %30, i32 8, i32 (i8*, i8*)* %31)
store i32 0, i32* %7
br label %33
33: ; preds = %45, %28
%34 = load i32, i32* %7
%35 = load i32, i32* %6
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %48
37: ; preds = %33
%38 = load %struct.list*, %struct.list** %3
%39 = load i8**, i8*** %5
%40 = load i32, i32* %7
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8*, i8** %39, i64 %41
%43 = load i8*, i8** %42
%44 = call i32 @cctools_list_push_tail(%struct.list* %38, i8* %43)
br label %45
45: ; preds = %37
%46 = load i32, i32* %7
%47 = add nsw i32 %46, 1
store i32 %47, i32* %7
br label %33
48: ; preds = %33
%49 = load i8**, i8*** %5
%50 = call i32 @free(i8** %49)
%51 = load %struct.list*, %struct.list** %3
ret %struct.list* %51
}

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