#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*/ free(void *);
void *malloc(size_t);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, size_t);
void bubbleSortM(void *a, size_t count, size_t size,
                 int (*comp)(const void *, const void *)) {
  size_t i, j;
  const size_t j_temp = count - 1;
  void *current;
  void *prev;
  void *buffer = malloc(count * size);
  memset(buffer, 0, count * size);
  for (i = 0; i < count; ++i) {
    for (j = j_temp; j > i; --j) {
      current = a + j * size;
      prev = current - size;
      if (comp(prev, current) > 0) {
        memcpy(buffer, prev, size);
        memcpy(prev, current, size);
        memcpy(current, buffer, size);
      }
    }
  }
  free(buffer);
}


x86 assembly (clang, -O0):
<bubbleSortM>:
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    -0x10(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
imul   -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rdx
imul   -0x18(%rbp),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1251 <bubbleSortM+0x111>
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
jbe    123b <bubbleSortM+0xfb>
mov    -0x8(%rbp),%rax
mov    -0x30(%rbp),%rcx
imul   -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
xor    %ecx,%ecx
sub    -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
jle    1225 <bubbleSortM+0xe5>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x40(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
jmp    122a <bubbleSortM+0xea>
mov    -0x30(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
jmp    11a8 <bubbleSortM+0x68>
jmp    1240 <bubbleSortM+0x100>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1192 <bubbleSortM+0x52>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bubbleSortM>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rcx,0x40(%rsp)
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rdx,0x18(%rsp)
imul   %rsi,%rbp
mov    %rbp,%rdi
call   1060 <malloc@plt>
mov    %rax,0x20(%rsp)
mov    %rax,%rdi
xor    %esi,%esi
mov    %rbp,%rdx
call   1040 <memset@plt>
mov    %rbx,0x8(%rsp)
test   %rbx,%rbx
je     125e <bubbleSortM+0x11e>
mov    0x8(%rsp),%rax
lea    -0x1(%rax),%rdx
mov    0x18(%rsp),%rcx
mov    %rcx,%rsi
neg    %rsi
mov    %rsi,0x38(%rsp)
add    $0xfffffffffffffffe,%rax
imul   %rcx,%rax
add    %r14,%rax
mov    %rax,0x30(%rsp)
mov    %rdx,0x10(%rsp)
imul   %rdx,%rcx
add    %r14,%rcx
mov    %rcx,0x28(%rsp)
xor    %r13d,%r13d
mov    0x20(%rsp),%r15
mov    0x18(%rsp),%r14
jmp    11eb <bubbleSortM+0xab>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%r13
cmp    0x8(%rsp),%r13
je     125e <bubbleSortM+0x11e>
cmp    %r13,0x10(%rsp)
jbe    11e0 <bubbleSortM+0xa0>
mov    0x28(%rsp),%rbx
mov    0x30(%rsp),%rbp
mov    0x10(%rsp),%r12
jmp    1224 <bubbleSortM+0xe4>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0xffffffffffffffff,%r12
mov    0x38(%rsp),%rax
add    %rax,%rbp
add    %rax,%rbx
cmp    %r13,%r12
jbe    11e0 <bubbleSortM+0xa0>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   *0x40(%rsp)
test   %eax,%eax
jle    1210 <bubbleSortM+0xd0>
mov    %r15,%rdi
mov    %rbp,%rsi
mov    %r14,%rdx
call   1050 <memcpy@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1050 <memcpy@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1050 <memcpy@plt>
jmp    1210 <bubbleSortM+0xd0>
mov    0x20(%rsp),%rdi
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
define void @bubbleSortM(i8* %0, i64 %1, i64 %2, i32 (i8*, i8*)* %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32 (i8*, i8*)*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i32 (i8*, i8*)* %3, i32 (i8*, i8*)** %8
%15 = load i64, i64* %6
%16 = sub i64 %15, 1
store i64 %16, i64* %11
%17 = load i64, i64* %6
%18 = load i64, i64* %7
%19 = mul i64 %17, %18
%20 = call i8* @malloc(i64 %19)
store i8* %20, i8** %14
%21 = load i8*, i8** %14
%22 = load i64, i64* %6
%23 = load i64, i64* %7
%24 = mul i64 %22, %23
%25 = call i32 @memset(i8* %21, i32 0, i64 %24)
store i64 0, i64* %9
br label %26
26: ; preds = %69, %4
%27 = load i64, i64* %9
%28 = load i64, i64* %6
%29 = icmp ult i64 %27, %28
br i1 %29, label %30, label %72
30: ; preds = %26
%31 = load i64, i64* %11
store i64 %31, i64* %10
br label %32
32: ; preds = %65, %30
%33 = load i64, i64* %10
%34 = load i64, i64* %9
%35 = icmp ugt i64 %33, %34
br i1 %35, label %36, label %68
36: ; preds = %32
%37 = load i8*, i8** %5
%38 = load i64, i64* %10
%39 = load i64, i64* %7
%40 = mul i64 %38, %39
%41 = getelementptr i8, i8* %37, i64 %40
store i8* %41, i8** %12
%42 = load i8*, i8** %12
%43 = load i64, i64* %7
%44 = sub i64 0, %43
%45 = getelementptr i8, i8* %42, i64 %44
store i8* %45, i8** %13
%46 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %8
%47 = load i8*, i8** %13
%48 = load i8*, i8** %12
%49 = call i32 %46(i8* %47, i8* %48)
%50 = icmp sgt i32 %49, 0
br i1 %50, label %51, label %64
51: ; preds = %36
%52 = load i8*, i8** %14
%53 = load i8*, i8** %13
%54 = load i64, i64* %7
%55 = call i32 @memcpy(i8* %52, i8* %53, i64 %54)
%56 = load i8*, i8** %13
%57 = load i8*, i8** %12
%58 = load i64, i64* %7
%59 = call i32 @memcpy(i8* %56, i8* %57, i64 %58)
%60 = load i8*, i8** %12
%61 = load i8*, i8** %14
%62 = load i64, i64* %7
%63 = call i32 @memcpy(i8* %60, i8* %61, i64 %62)
br label %64
64: ; preds = %51, %36
br label %65
65: ; preds = %64
%66 = load i64, i64* %10
%67 = add i64 %66, -1
store i64 %67, i64* %10
br label %32
68: ; preds = %32
br label %69
69: ; preds = %68
%70 = load i64, i64* %9
%71 = add i64 %70, 1
store i64 %71, i64* %9
br label %26
72: ; preds = %26
%73 = load i8*, i8** %14
%74 = call i32 @free(i8* %73)
ret void
}

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