#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*/ fcitx_msort_r_with_tmp(void *, size_t, size_t,
                                          int (*)(void const *, void const *,
                                                  void *),
                                          void *, char *);
int /*<<< orphan*/ fcitx_qsort_r(void *, size_t, size_t,
                                 int (*)(void const *, void const *, void *),
                                 void *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ iswap(char *, void *, size_t);
char *malloc(size_t const);
__attribute__((visibility("default"))) void
fcitx_msort_r(void *b, size_t n, size_t s,
              int (*cmp)(const void *, const void *, void *), void *thunk) {
  if (n < 10) {
    size_t i;
    while (n > 1) {
      char *min = b;
      char *tmp = min + s;
      for (i = 1; i < n; ++i) {
        if (cmp(tmp, min, thunk) < 0)
          min = tmp;
        tmp += s;
      }
      iswap(min, b, s);
      b += s;
      n -= 1;
    }
  } else {
    const size_t size = n * s;
    char *tmp = malloc(size);
    if (tmp == NULL) {
      fcitx_qsort_r(b, n, s, cmp, thunk);
    } else {
      fcitx_msort_r_with_tmp(b, n, s, cmp, thunk, tmp);
      free(tmp);
    }
  }
}


x86 assembly (clang, -O0):
<fcitx_msort_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
cmpq   $0xa,-0x10(%rbp)
jae    1224 <fcitx_msort_r+0xd4>
jmp    117c <fcitx_msort_r+0x2c>
cmpq   $0x1,-0x10(%rbp)
jbe    121f <fcitx_msort_r+0xcf>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
movq   $0x1,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11f1 <fcitx_msort_r+0xa1>
mov    -0x20(%rbp),%rax
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   *%rax
cmp    $0x0,%eax
jge    11d4 <fcitx_msort_r+0x84>
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
add    -0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11a3 <fcitx_msort_r+0x53>
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1070 <iswap@plt>
mov    -0x18(%rbp),%rax
add    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    117c <fcitx_msort_r+0x2c>
jmp    1292 <fcitx_msort_r+0x142>
mov    -0x10(%rbp),%rax
imul   -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    1267 <fcitx_msort_r+0x117>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   1040 <fcitx_qsort_r@plt>
jmp    128d <fcitx_msort_r+0x13d>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    -0x50(%rbp),%r9
call   1050 <fcitx_msort_r_with_tmp@plt>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
jmp    1292 <fcitx_msort_r+0x142>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fcitx_msort_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,(%rsp)
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r13
cmp    $0x9,%rsi
ja     11f8 <fcitx_msort_r+0xa8>
cmp    $0x2,%rbx
jae    11ac <fcitx_msort_r+0x5c>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
nopl   (%rax)
mov    0x10(%rsp),%rsi
mov    %r12,%rdx
call   1070 <iswap@plt>
add    $0xffffffffffffffff,%rbx
mov    0x8(%rsp),%r13
cmp    $0x1,%rbx
jbe    117e <fcitx_msort_r+0x2e>
lea    (%r12,%r13,1),%rbp
mov    $0x1,%r14d
mov    %rbp,0x8(%rsp)
mov    %r13,0x10(%rsp)
jmp    11df <fcitx_msort_r+0x8f>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    %r12,%rbp
add    $0x1,%r14
mov    %rdi,%r13
cmp    %rbx,%r14
jae    1190 <fcitx_msort_r+0x40>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    (%rsp),%rdx
call   *%r15
mov    %rbp,%rdi
test   %eax,%eax
js     11d0 <fcitx_msort_r+0x80>
mov    %r13,%rdi
jmp    11d0 <fcitx_msort_r+0x80>
mov    %r12,%rdi
imul   %rbx,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     123a <fcitx_msort_r+0xea>
mov    %rax,%rbp
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
mov    %r15,%rcx
mov    (%rsp),%r8
mov    %rax,%r9
call   1050 <fcitx_msort_r_with_tmp@plt>
mov    %rbp,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
mov    %r15,%rcx
mov    (%rsp),%r8
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fcitx_qsort_r@plt>

LLVM IR (-O0):
define void @fcitx_msort_r(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 i64
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i64
%15 = alloca i8*
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
%16 = load i64, i64* %7
%17 = icmp ult i64 %16, 10
br i1 %17, label %18, label %58
18: ; preds = %5
br label %19
19: ; preds = %47, %18
%20 = load i64, i64* %7
%21 = icmp ugt i64 %20, 1
br i1 %21, label %22, label %57
22: ; preds = %19
%23 = load i8*, i8** %6
store i8* %23, i8** %12
%24 = load i8*, i8** %12
%25 = load i64, i64* %8
%26 = getelementptr inbounds i8, i8* %24, i64 %25
store i8* %26, i8** %13
store i64 1, i64* %11
br label %27
27: ; preds = %44, %22
%28 = load i64, i64* %11
%29 = load i64, i64* %7
%30 = icmp ult i64 %28, %29
br i1 %30, label %31, label %47
31: ; preds = %27
%32 = load i32 (i8*, i8*, i8*)*, i32 (i8*, i8*, i8*)** %9
%33 = load i8*, i8** %13
%34 = load i8*, i8** %12
%35 = load i8*, i8** %10
%36 = call i32 %32(i8* %33, i8* %34, i8* %35)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %31
%39 = load i8*, i8** %13
store i8* %39, i8** %12
br label %40
40: ; preds = %38, %31
%41 = load i64, i64* %8
%42 = load i8*, i8** %13
%43 = getelementptr inbounds i8, i8* %42, i64 %41
store i8* %43, i8** %13
br label %44
44: ; preds = %40
%45 = load i64, i64* %11
%46 = add i64 %45, 1
store i64 %46, i64* %11
br label %27
47: ; preds = %27
%48 = load i8*, i8** %12
%49 = load i8*, i8** %6
%50 = load i64, i64* %8
%51 = call i32 @iswap(i8* %48, i8* %49, i64 %50)
%52 = load i64, i64* %8
%53 = load i8*, i8** %6
%54 = getelementptr i8, i8* %53, i64 %52
store i8* %54, i8** %6
%55 = load i64, i64* %7
%56 = sub i64 %55, 1
store i64 %56, i64* %7
br label %19
57: ; preds = %19
br label %84
58: ; preds = %5
%59 = load i64, i64* %7
%60 = load i64, i64* %8
%61 = mul i64 %59, %60
store i64 %61, i64* %14
%62 = load i64, i64* %14
%63 = call i8* @malloc(i64 %62)
store i8* %63, i8** %15
%64 = load i8*, i8** %15
%65 = icmp eq i8* %64, null
br i1 %65, label %66, label %73
66: ; preds = %58
%67 = load i8*, i8** %6
%68 = load i64, i64* %7
%69 = load i64, i64* %8
%70 = load i32 (i8*, i8*, i8*)*, i32 (i8*, i8*, i8*)** %9
%71 = load i8*, i8** %10
%72 = call i32 @fcitx_qsort_r(i8* %67, i64 %68, i64 %69, i32 (i8*, i8*, i8*)* %70, i8* %71)
br label %83
73: ; preds = %58
%74 = load i8*, i8** %6
%75 = load i64, i64* %7
%76 = load i64, i64* %8
%77 = load i32 (i8*, i8*, i8*)*, i32 (i8*, i8*, i8*)** %9
%78 = load i8*, i8** %10
%79 = load i8*, i8** %15
%80 = call i32 @fcitx_msort_r_with_tmp(i8* %74, i64 %75, i64 %76, i32 (i8*, i8*, i8*)* %77, i8* %78, i8* %79)
%81 = load i8*, i8** %15
%82 = call i32 @free(i8* %81)
br label %83
83: ; preds = %73, %66
br label %84
84: ; preds = %83, %57
ret void
}

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