#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 optimizedBubbleSortM(void *a, size_t count, size_t size,
                          int (*comp)(const void *, const void *)) {
  size_t i = 0;
  size_t j = 0;
  size_t temp = 0;
  char t = 1;
  void *current;
  void *next;
  void *buffer = malloc(count * size);
  memset(buffer, 0, count * size);
  while (t) {
    t = 0;
    temp = count - i - 1;
    for (j = 0; j < temp; ++j) {
      current = a + j * size;
      next = current + size;
      if (comp(current, next) > 0) {
        memcpy(buffer, next, size);
        memcpy(next, current, size);
        memcpy(current, buffer, size);
        t = 1;
      }
    }
    i += 1;
  }
  free(buffer);
}


x86 assembly (clang, -O0):
<optimizedBubbleSortM>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movb   $0x1,-0x39(%rbp)
mov    -0x10(%rbp),%rdi
imul   -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
mov    -0x10(%rbp),%rdx
imul   -0x18(%rbp),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
cmpb   $0x0,-0x39(%rbp)
je     1263 <optimizedBubbleSortM+0x123>
movb   $0x0,-0x39(%rbp)
mov    -0x10(%rbp),%rax
sub    -0x28(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x38(%rbp),%rax
jae    1252 <optimizedBubbleSortM+0x112>
mov    -0x8(%rbp),%rax
mov    -0x30(%rbp),%rcx
imul   -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
jle    123c <optimizedBubbleSortM+0xfc>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x48(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
movb   $0x1,-0x39(%rbp)
jmp    1241 <optimizedBubbleSortM+0x101>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11c0 <optimizedBubbleSortM+0x80>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    119a <optimizedBubbleSortM+0x5a>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<optimizedBubbleSortM>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,0x20(%rsp)
mov    %rdx,%r13
mov    %rdi,0x8(%rsp)
mov    %rdx,%rbx
mov    %rsi,0x10(%rsp)
imul   %rsi,%rbx
mov    %rbx,%rdi
call   1060 <malloc@plt>
xor    %ebp,%ebp
mov    %rax,(%rsp)
mov    %rax,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1040 <memset@plt>
jmp    119d <optimizedBubbleSortM+0x5d>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x18(%rsp),%rbp
add    $0x1,%rbp
test   %bl,%bl
je     1212 <optimizedBubbleSortM+0xd2>
mov    %rbp,%r15
not    %r15
add    0x10(%rsp),%r15
je     1212 <optimizedBubbleSortM+0xd2>
mov    %rbp,0x18(%rsp)
xor    %r14d,%r14d
mov    0x8(%rsp),%rbp
xor    %ebx,%ebx
jmp    11cc <optimizedBubbleSortM+0x8c>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r14
mov    %r12,%rbp
cmp    %r15,%r14
jae    1190 <optimizedBubbleSortM+0x50>
mov    %rbp,%r12
add    %r13,%r12
mov    %rbp,%rdi
mov    %r12,%rsi
call   *0x20(%rsp)
test   %eax,%eax
jle    11c0 <optimizedBubbleSortM+0x80>
mov    (%rsp),%rbx
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1050 <memcpy@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %r13,%rdx
call   1050 <memcpy@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1050 <memcpy@plt>
mov    $0x1,%bl
jmp    11c0 <optimizedBubbleSortM+0x80>
mov    (%rsp),%rdi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

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

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