#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 */
typedef struct TYPE_9__ TYPE_2__;
typedef struct TYPE_8__ TYPE_1__;

/* Type definitions */
struct TYPE_9__ {
  int fts_nitems;
  TYPE_1__ **fts_array;
  scalar_t__ fts_compar;
};
struct TYPE_8__ {
  struct TYPE_8__ *fts_link;
};
typedef TYPE_1__ FTSENT;
typedef TYPE_2__ FTS;

/* Variables and functions */
int /*<<< orphan*/ qsort(void *, int, int, int (*)(void const *, void const *));
TYPE_1__ **realloc(TYPE_1__ **, size_t);
FTSENT *fts_sort(FTS *sp, FTSENT *head, int nitems) {
  FTSENT **ap, *p;
  if (nitems > sp->fts_nitems) {
    sp->fts_nitems = nitems + 40;
    if ((sp->fts_array =
             realloc(sp->fts_array,
                     (size_t)(sp->fts_nitems * sizeof(FTSENT *)))) == NULL) {
      sp->fts_nitems = 0;
      return (head);
    }
  }
  for (ap = sp->fts_array, p = head; p; p = p->fts_link)
    *ap++ = p;
  qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *),
        (int (*)(const void *, const void *))sp->fts_compar);
  for (head = *(ap = sp->fts_array); --nitems; ++ap)
    ap[0]->fts_link = ap[1];
  ap[0]->fts_link = NULL;
  return (head);
}


x86 assembly (clang, -O0):
<fts_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
jle    1194 <fts_sort+0x74>
mov    -0x1c(%rbp),%ecx
add    $0x28,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
movslq (%rax),%rsi
shl    $0x3,%rsi
call   1040 <realloc@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,0x8(%rcx)
cmp    $0x0,%rax
jne    118f <fts_sort+0x6f>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1254 <fts_sort+0x134>
jmp    1194 <fts_sort+0x74>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11d9 <fts_sort+0xb9>
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x8,%rdx
mov    %rdx,-0x28(%rbp)
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    11a8 <fts_sort+0x88>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rcx
mov    $0x8,%edx
call   1030 <qsort@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     123e <fts_sort+0x11e>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rcx
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x28(%rbp)
jmp    1209 <fts_sort+0xe9>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fts_sort>:
push   %r15
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%r15
cmp    %edx,(%rdi)
jge    1165 <fts_sort+0x45>
lea    0x28(%r14),%eax
mov    %eax,(%r15)
mov    0x8(%r15),%rdi
movslq %r14d,%rax
lea    0x140(,%rax,8),%rsi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    %rax,0x8(%r15)
test   %rax,%rax
jne    1169 <fts_sort+0x49>
movl   $0x0,(%r15)
jmp    123e <fts_sort+0x11e>
mov    0x8(%r15),%rdi
test   %rbx,%rbx
je     1183 <fts_sort+0x63>
xchg   %ax,%ax
mov    %rbx,(%rdi)
add    $0x8,%rdi
mov    (%rbx),%rbx
test   %rbx,%rbx
jne    1170 <fts_sort+0x50>
mov    0x8(%r15),%rdi
mov    0x10(%r15),%rcx
mov    %r14d,%esi
mov    $0x8,%edx
call   1030 <qsort@plt>
mov    0x8(%r15),%rax
mov    (%rax),%rbx
mov    %rbx,%rcx
cmp    $0x1,%r14d
je     1237 <fts_sort+0x117>
mov    0x8(%rax),%rcx
add    $0x8,%rax
mov    %rcx,(%rbx)
mov    %r14d,%ecx
add    $0xfffffffe,%ecx
je     1234 <fts_sort+0x114>
lea    -0x3(%r14),%r8d
test   $0x3,%cl
je     11f9 <fts_sort+0xd9>
add    $0x2,%r14b
movzbl %r14b,%r9d
and    $0x3,%r9d
xor    %edi,%edi
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%rax),%rdx
mov    0x8(%rax),%rsi
add    $0x8,%rax
mov    %rsi,(%rdx)
add    $0x1,%rdi
cmp    %edi,%r9d
jne    11e0 <fts_sort+0xc0>
sub    %edi,%ecx
cmp    $0x3,%r8d
jb     1234 <fts_sort+0x114>
nop
mov    (%rax),%rdx
mov    0x8(%rax),%rsi
mov    %rsi,(%rdx)
mov    0x8(%rax),%rdx
mov    0x10(%rax),%rsi
mov    %rsi,(%rdx)
mov    0x10(%rax),%rdx
mov    0x18(%rax),%rsi
mov    %rsi,(%rdx)
mov    0x18(%rax),%rdx
mov    0x20(%rax),%rsi
add    $0x20,%rax
mov    %rsi,(%rdx)
add    $0xfffffffc,%ecx
jne    1200 <fts_sort+0xe0>
mov    (%rax),%rcx
movq   $0x0,(%rcx)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.TYPE_9__ = type { i32, %struct.TYPE_8__**, i64 }
%struct.TYPE_8__ = type { %struct.TYPE_8__* }
define %struct.TYPE_8__* @fts_sort(%struct.TYPE_9__* %0, %struct.TYPE_8__* %1, i32 %2) {
%4 = alloca %struct.TYPE_8__*
%5 = alloca %struct.TYPE_9__*
%6 = alloca %struct.TYPE_8__*
%7 = alloca i32
%8 = alloca %struct.TYPE_8__**
%9 = alloca %struct.TYPE_8__*
store %struct.TYPE_9__* %0, %struct.TYPE_9__** %5
store %struct.TYPE_8__* %1, %struct.TYPE_8__** %6
store i32 %2, i32* %7
%10 = load i32, i32* %7
%11 = load %struct.TYPE_9__*, %struct.TYPE_9__** %5
%12 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %11, i32 0, i32 0
%13 = load i32, i32* %12
%14 = icmp sgt i32 %10, %13
br i1 %14, label %15, label %37
15: ; preds = %3
%16 = load i32, i32* %7
%17 = add nsw i32 %16, 40
%18 = load %struct.TYPE_9__*, %struct.TYPE_9__** %5
%19 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %18, i32 0, i32 0
store i32 %17, i32* %19
%20 = load %struct.TYPE_9__*, %struct.TYPE_9__** %5
%21 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %20, i32 0, i32 1
%22 = load %struct.TYPE_8__**, %struct.TYPE_8__*** %21
%23 = load %struct.TYPE_9__*, %struct.TYPE_9__** %5
%24 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = sext i32 %25 to i64
%27 = mul i64 %26, 8
%28 = call %struct.TYPE_8__** @realloc(%struct.TYPE_8__** %22, i64 %27)
%29 = load %struct.TYPE_9__*, %struct.TYPE_9__** %5
%30 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %29, i32 0, i32 1
store %struct.TYPE_8__** %28, %struct.TYPE_8__*** %30
%31 = icmp eq %struct.TYPE_8__** %28, null
br i1 %31, label %32, label %36
32: ; preds = %15
%33 = load %struct.TYPE_9__*, %struct.TYPE_9__** %5
%34 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %33, i32 0, i32 0
store i32 0, i32* %34
%35 = load %struct.TYPE_8__*, %struct.TYPE_8__** %6
store %struct.TYPE_8__* %35, %struct.TYPE_8__** %4
br label %89
36: ; preds = %15
br label %37
37: ; preds = %36, %3
%38 = load %struct.TYPE_9__*, %struct.TYPE_9__** %5
%39 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %38, i32 0, i32 1
%40 = load %struct.TYPE_8__**, %struct.TYPE_8__*** %39
store %struct.TYPE_8__** %40, %struct.TYPE_8__*** %8
%41 = load %struct.TYPE_8__*, %struct.TYPE_8__** %6
store %struct.TYPE_8__* %41, %struct.TYPE_8__** %9
br label %42
42: ; preds = %49, %37
%43 = load %struct.TYPE_8__*, %struct.TYPE_8__** %9
%44 = icmp ne %struct.TYPE_8__* %43, null
br i1 %44, label %45, label %53
45: ; preds = %42
%46 = load %struct.TYPE_8__*, %struct.TYPE_8__** %9
%47 = load %struct.TYPE_8__**, %struct.TYPE_8__*** %8
%48 = getelementptr inbounds %struct.TYPE_8__*, %struct.TYPE_8__** %47, i32 1
store %struct.TYPE_8__** %48, %struct.TYPE_8__*** %8
store %struct.TYPE_8__* %46, %struct.TYPE_8__** %47
br label %49
49: ; preds = %45
%50 = load %struct.TYPE_8__*, %struct.TYPE_8__** %9
%51 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %50, i32 0, i32 0
%52 = load %struct.TYPE_8__*, %struct.TYPE_8__** %51
store %struct.TYPE_8__* %52, %struct.TYPE_8__** %9
br label %42
53: ; preds = %42
%54 = load %struct.TYPE_9__*, %struct.TYPE_9__** %5
%55 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %54, i32 0, i32 1
%56 = load %struct.TYPE_8__**, %struct.TYPE_8__*** %55
%57 = bitcast %struct.TYPE_8__** %56 to i8*
%58 = load i32, i32* %7
%59 = load %struct.TYPE_9__*, %struct.TYPE_9__** %5
%60 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %59, i32 0, i32 2
%61 = load i64, i64* %60
%62 = inttoptr i64 %61 to i32 (i8*, i8*)*
%63 = call i32 @qsort(i8* %57, i32 %58, i32 8, i32 (i8*, i8*)* %62)
%64 = load %struct.TYPE_9__*, %struct.TYPE_9__** %5
%65 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %64, i32 0, i32 1
%66 = load %struct.TYPE_8__**, %struct.TYPE_8__*** %65
store %struct.TYPE_8__** %66, %struct.TYPE_8__*** %8
%67 = load %struct.TYPE_8__*, %struct.TYPE_8__** %66
store %struct.TYPE_8__* %67, %struct.TYPE_8__** %6
br label %68
68: ; preds = %80, %53
%69 = load i32, i32* %7
%70 = add nsw i32 %69, -1
store i32 %70, i32* %7
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %83
72: ; preds = %68
%73 = load %struct.TYPE_8__**, %struct.TYPE_8__*** %8
%74 = getelementptr inbounds %struct.TYPE_8__*, %struct.TYPE_8__** %73, i64 1
%75 = load %struct.TYPE_8__*, %struct.TYPE_8__** %74
%76 = load %struct.TYPE_8__**, %struct.TYPE_8__*** %8
%77 = getelementptr inbounds %struct.TYPE_8__*, %struct.TYPE_8__** %76, i64 0
%78 = load %struct.TYPE_8__*, %struct.TYPE_8__** %77
%79 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %78, i32 0, i32 0
store %struct.TYPE_8__* %75, %struct.TYPE_8__** %79
br label %80
80: ; preds = %72
%81 = load %struct.TYPE_8__**, %struct.TYPE_8__*** %8
%82 = getelementptr inbounds %struct.TYPE_8__*, %struct.TYPE_8__** %81, i32 1
store %struct.TYPE_8__** %82, %struct.TYPE_8__*** %8
br label %68
83: ; preds = %68
%84 = load %struct.TYPE_8__**, %struct.TYPE_8__*** %8
%85 = getelementptr inbounds %struct.TYPE_8__*, %struct.TYPE_8__** %84, i64 0
%86 = load %struct.TYPE_8__*, %struct.TYPE_8__** %85
%87 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %86, i32 0, i32 0
store %struct.TYPE_8__* null, %struct.TYPE_8__** %87
%88 = load %struct.TYPE_8__*, %struct.TYPE_8__** %6
store %struct.TYPE_8__* %88, %struct.TYPE_8__** %4
br label %89
89: ; preds = %83, %32
%90 = load %struct.TYPE_8__*, %struct.TYPE_8__** %4
ret %struct.TYPE_8__* %90
}

LLVM IR (-O2):
%struct.TYPE_9__ = type { i32, %struct.TYPE_8__**, i64 }
%struct.TYPE_8__ = type { %struct.TYPE_8__* }
define %struct.TYPE_8__* @fts_sort(%struct.TYPE_9__* nocapture %0, %struct.TYPE_8__* %1, i32 %2) {
%4 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %0, i64 0, i32 0
%5 = load i32, i32* %4
%6 = icmp slt i32 %5, %2
br i1 %6, label %10, label %7
7: ; preds = %3
%8 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %0, i64 0, i32 1
%9 = load %struct.TYPE_8__**, %struct.TYPE_8__*** %8
br label %19
10: ; preds = %3
%11 = add nsw i32 %2, 40
store i32 %11, i32* %4
%12 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %0, i64 0, i32 1
%13 = load %struct.TYPE_8__**, %struct.TYPE_8__*** %12
%14 = sext i32 %11 to i64
%15 = shl nsw i64 %14, 3
%16 = tail call %struct.TYPE_8__** @realloc(%struct.TYPE_8__** %13, i64 %15)
store %struct.TYPE_8__** %16, %struct.TYPE_8__*** %12
%17 = icmp eq %struct.TYPE_8__** %16, null
br i1 %17, label %18, label %19
18: ; preds = %10
store i32 0, i32* %4
br label %97
19: ; preds = %7, %10
%20 = phi %struct.TYPE_8__** [ %9, %7 ], [ %16, %10 ]
%21 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %0, i64 0, i32 1
%22 = icmp eq %struct.TYPE_8__* %1, null
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = bitcast %struct.TYPE_8__** %20 to i8*
br label %35
25: ; preds = %19, %25
%26 = phi %struct.TYPE_8__* [ %30, %25 ], [ %1, %19 ]
%27 = phi %struct.TYPE_8__** [ %28, %25 ], [ %20, %19 ]
%28 = getelementptr inbounds %struct.TYPE_8__*, %struct.TYPE_8__** %27, i64 1
store %struct.TYPE_8__* %26, %struct.TYPE_8__** %27
%29 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %26, i64 0, i32 0
%30 = load %struct.TYPE_8__*, %struct.TYPE_8__** %29
%31 = icmp eq %struct.TYPE_8__* %30, null
br i1 %31, label %32, label %25
32: ; preds = %25
%33 = bitcast %struct.TYPE_8__*** %21 to i8**
%34 = load i8*, i8** %33
br label %35
35: ; preds = %23, %32
%36 = phi i8* [ %34, %32 ], [ %24, %23 ]
%37 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %0, i64 0, i32 2
%38 = load i64, i64* %37
%39 = inttoptr i64 %38 to i32 (i8*, i8*)*
%40 = tail call i32 @qsort(i8* %36, i32 %2, i32 8, i32 (i8*, i8*)* %39)
%41 = load %struct.TYPE_8__**, %struct.TYPE_8__*** %21
%42 = load %struct.TYPE_8__*, %struct.TYPE_8__** %41
%43 = icmp eq i32 %2, 1
br i1 %43, label %94, label %44
44: ; preds = %35
%45 = getelementptr inbounds %struct.TYPE_8__*, %struct.TYPE_8__** %41, i64 1
%46 = load %struct.TYPE_8__*, %struct.TYPE_8__** %45
%47 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %42, i64 0, i32 0
store %struct.TYPE_8__* %46, %struct.TYPE_8__** %47
%48 = add i32 %2, -2
%49 = icmp eq i32 %48, 0
br i1 %49, label %91, label %50
50: ; preds = %44
%51 = add i32 %2, -3
%52 = and i32 %48, 3
%53 = icmp eq i32 %52, 0
br i1 %53, label %65, label %54
54: ; preds = %50, %54
%55 = phi i32 [ %62, %54 ], [ %48, %50 ]
%56 = phi %struct.TYPE_8__** [ %59, %54 ], [ %45, %50 ]
%57 = phi i32 [ %63, %54 ], [ 0, %50 ]
%58 = load %struct.TYPE_8__*, %struct.TYPE_8__** %56
%59 = getelementptr inbounds %struct.TYPE_8__*, %struct.TYPE_8__** %56, i64 1
%60 = load %struct.TYPE_8__*, %struct.TYPE_8__** %59
%61 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %58, i64 0, i32 0
store %struct.TYPE_8__* %60, %struct.TYPE_8__** %61
%62 = add nsw i32 %55, -1
%63 = add i32 %57, 1
%64 = icmp eq i32 %63, %52
br i1 %64, label %65, label %54
65: ; preds = %54, %50
%66 = phi %struct.TYPE_8__** [ undef, %50 ], [ %59, %54 ]
%67 = phi i32 [ %48, %50 ], [ %62, %54 ]
%68 = phi %struct.TYPE_8__** [ %45, %50 ], [ %59, %54 ]
%69 = icmp ult i32 %51, 3
br i1 %69, label %91, label %70
70: ; preds = %65, %70
%71 = phi i32 [ %89, %70 ], [ %67, %65 ]
%72 = phi %struct.TYPE_8__** [ %86, %70 ], [ %68, %65 ]
%73 = load %struct.TYPE_8__*, %struct.TYPE_8__** %72
%74 = getelementptr inbounds %struct.TYPE_8__*, %struct.TYPE_8__** %72, i64 1
%75 = load %struct.TYPE_8__*, %struct.TYPE_8__** %74
%76 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %73, i64 0, i32 0
store %struct.TYPE_8__* %75, %struct.TYPE_8__** %76
%77 = load %struct.TYPE_8__*, %struct.TYPE_8__** %74
%78 = getelementptr inbounds %struct.TYPE_8__*, %struct.TYPE_8__** %72, i64 2
%79 = load %struct.TYPE_8__*, %struct.TYPE_8__** %78
%80 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %77, i64 0, i32 0
store %struct.TYPE_8__* %79, %struct.TYPE_8__** %80
%81 = load %struct.TYPE_8__*, %struct.TYPE_8__** %78
%82 = getelementptr inbounds %struct.TYPE_8__*, %struct.TYPE_8__** %72, i64 3
%83 = load %struct.TYPE_8__*, %struct.TYPE_8__** %82
%84 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %81, i64 0, i32 0
store %struct.TYPE_8__* %83, %struct.TYPE_8__** %84
%85 = load %struct.TYPE_8__*, %struct.TYPE_8__** %82
%86 = getelementptr inbounds %struct.TYPE_8__*, %struct.TYPE_8__** %72, i64 4
%87 = load %struct.TYPE_8__*, %struct.TYPE_8__** %86
%88 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %85, i64 0, i32 0
store %struct.TYPE_8__* %87, %struct.TYPE_8__** %88
%89 = add nsw i32 %71, -4
%90 = icmp eq i32 %89, 0
br i1 %90, label %91, label %70
91: ; preds = %65, %70, %44
%92 = phi %struct.TYPE_8__** [ %45, %44 ], [ %66, %65 ], [ %86, %70 ]
%93 = load %struct.TYPE_8__*, %struct.TYPE_8__** %92
br label %94
94: ; preds = %91, %35
%95 = phi %struct.TYPE_8__* [ %42, %35 ], [ %93, %91 ]
%96 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %95, i64 0, i32 0
store %struct.TYPE_8__* null, %struct.TYPE_8__** %96
br label %97
97: ; preds = %94, %18
%98 = phi %struct.TYPE_8__* [ %1, %18 ], [ %42, %94 ]
ret %struct.TYPE_8__* %98
}