#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*/ insertion_sort(void *, size_t, size_t,
                                  int (*)(void const *, void const *, void *),
                                  void *);
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) {
    insertion_sort(b, n, s, cmp, thunk);
  } else {
    const size_t size = n * s;
    char *tmp = malloc(size);
    if (!tmp) {
      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    $0x40,%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    1195 <fcitx_msort_r+0x45>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   1070 <insertion_sort@plt>
jmp    1203 <fcitx_msort_r+0xb3>
mov    -0x10(%rbp),%rax
imul   -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11d8 <fcitx_msort_r+0x88>
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    11fe <fcitx_msort_r+0xae>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    -0x38(%rbp),%r9
call   1050 <fcitx_msort_r_with_tmp@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
jmp    1203 <fcitx_msort_r+0xb3>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fcitx_msort_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r13
cmp    $0x9,%rsi
ja     1192 <fcitx_msort_r+0x42>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
mov    %r15,%rcx
mov    %r14,%r8
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <insertion_sort@plt>
mov    %r12,%rdi
imul   %rbx,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11d3 <fcitx_msort_r+0x83>
mov    %rax,%rbp
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
mov    %r15,%rcx
mov    %r14,%r8
mov    %rax,%r9
call   1050 <fcitx_msort_r_with_tmp@plt>
mov    %rbp,%rdi
add    $0x8,%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    %r14,%r8
add    $0x8,%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*
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
%13 = load i64, i64* %7
%14 = icmp ult i64 %13, 10
br i1 %14, label %15, label %22
15: ; preds = %5
%16 = load i8*, i8** %6
%17 = load i64, i64* %7
%18 = load i64, i64* %8
%19 = load i32 (i8*, i8*, i8*)*, i32 (i8*, i8*, i8*)** %9
%20 = load i8*, i8** %10
%21 = call i32 @insertion_sort(i8* %16, i64 %17, i64 %18, i32 (i8*, i8*, i8*)* %19, i8* %20)
br label %48
22: ; preds = %5
%23 = load i64, i64* %7
%24 = load i64, i64* %8
%25 = mul i64 %23, %24
store i64 %25, i64* %11
%26 = load i64, i64* %11
%27 = call i8* @malloc(i64 %26)
store i8* %27, i8** %12
%28 = load i8*, i8** %12
%29 = icmp ne i8* %28, null
br i1 %29, label %37, label %30
30: ; preds = %22
%31 = load i8*, i8** %6
%32 = load i64, i64* %7
%33 = load i64, i64* %8
%34 = load i32 (i8*, i8*, i8*)*, i32 (i8*, i8*, i8*)** %9
%35 = load i8*, i8** %10
%36 = call i32 @fcitx_qsort_r(i8* %31, i64 %32, i64 %33, i32 (i8*, i8*, i8*)* %34, i8* %35)
br label %47
37: ; preds = %22
%38 = load i8*, i8** %6
%39 = load i64, i64* %7
%40 = load i64, i64* %8
%41 = load i32 (i8*, i8*, i8*)*, i32 (i8*, i8*, i8*)** %9
%42 = load i8*, i8** %10
%43 = load i8*, i8** %12
%44 = call i32 @fcitx_msort_r_with_tmp(i8* %38, i64 %39, i64 %40, i32 (i8*, i8*, i8*)* %41, i8* %42, i8* %43)
%45 = load i8*, i8** %12
%46 = call i32 @free(i8* %45)
br label %47
47: ; preds = %37, %30
br label %48
48: ; preds = %47, %15
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 %9
7: ; preds = %5
%8 = tail call i32 @insertion_sort(i8* %0, i64 %1, i64 %2, i32 (i8*, i8*, i8*)* %3, i8* %4)
br label %18
9: ; preds = %5
%10 = mul i64 %2, %1
%11 = tail call i8* @malloc(i64 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @fcitx_qsort_r(i8* %0, i64 %1, i64 %2, i32 (i8*, i8*, i8*)* %3, i8* %4)
br label %18
15: ; preds = %9
%16 = tail call i32 @fcitx_msort_r_with_tmp(i8* %0, i64 %1, i64 %2, i32 (i8*, i8*, i8*)* %3, i8* %4, i8* %11)
%17 = tail call i32 @free(i8* %11)
br label %18
18: ; preds = %13, %15, %7
ret void
}