#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(unsigned int);
int /*<<< orphan*/ memcpy(void *, void *, unsigned int);
void sort_bubble(void *array, unsigned int length, unsigned int el_size,
                 int (*comp)(const void *, const void *)) {
  if (length < 2)
    return;
  int done = 0;
  int count = 0;
  void *hold = malloc(el_size);
  void *pos;
  while (!done) {
    done = 1;
    pos = array;
    for (int i = 0; i < length - 1 - count; i++) {
      if (comp(pos, pos + el_size) > 0) {
        memcpy(hold, pos, el_size);
        memcpy(pos, pos + el_size, el_size);
        memcpy(pos + el_size, hold, el_size);
        done = 0;
      }
      pos = ((char *)pos) + el_size;
    }
    count++;
  }
  free(hold);
}


x86 assembly (clang, -O0):
<sort_bubble>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
cmpl   $0x2,-0xc(%rbp)
jae    1155 <sort_bubble+0x25>
jmp    1242 <sort_bubble+0x112>
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x1c(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1185 <sort_bubble+0x55>
jmp    1239 <sort_bubble+0x109>
movl   $0x1,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
sub    -0x20(%rbp),%ecx
cmp    %ecx,%eax
jae    122b <sort_bubble+0xfb>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%ecx
add    %rcx,%rsi
call   *%rax
cmp    $0x0,%eax
jle    120f <sort_bubble+0xdf>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%eax
add    %rax,%rsi
mov    -0x10(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%eax
add    %rax,%rdi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1040 <memcpy@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    119b <sort_bubble+0x6b>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    116f <sort_bubble+0x3f>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sort_bubble>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rcx,0x28(%rsp)
mov    %rdi,0x20(%rsp)
cmp    $0x2,%esi
jae    115c <sort_bubble+0x2c>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %esi,%ebx
mov    %edx,%r13d
mov    %edx,0x14(%rsp)
mov    %edx,%edi
call   1050 <malloc@plt>
mov    %rax,0x18(%rsp)
mov    %ebx,%eax
neg    %eax
mov    %eax,0x8(%rsp)
xor    %ecx,%ecx
jmp    1195 <sort_bubble+0x65>
nopl   (%rax)
mov    0x10(%rsp),%ecx
add    $0x1,%ecx
mov    0xc(%rsp),%eax
mov    %eax,%ebx
test   %ebp,%ebp
jne    1228 <sort_bubble+0xf8>
mov    %ecx,%eax
not    %eax
cmp    0x8(%rsp),%eax
je     1228 <sort_bubble+0xf8>
mov    %ecx,0x10(%rsp)
lea    -0x1(%rbx),%eax
mov    %eax,0xc(%rsp)
mov    $0x1,%r12d
mov    0x20(%rsp),%r14
mov    $0x1,%ebp
mov    %rbx,0x30(%rsp)
jmp    11e1 <sort_bubble+0xb1>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%r12d
mov    %r15,%r14
mov    0x30(%rsp),%rbx
cmp    %r12d,%ebx
je     1180 <sort_bubble+0x50>
lea    (%r14,%r13,1),%r15
mov    %r14,%rdi
mov    %r15,%rsi
call   *0x28(%rsp)
test   %eax,%eax
jle    11d0 <sort_bubble+0xa0>
mov    0x18(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r13d,%edx
call   1040 <memcpy@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    0x14(%rsp),%ebp
mov    %ebp,%edx
call   1040 <memcpy@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <memcpy@plt>
xor    %ebp,%ebp
jmp    11d0 <sort_bubble+0xa0>
mov    0x18(%rsp),%rdi
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

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

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