#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 */
scalar_t__ compare_int(char *, char *);
int /*<<< orphan*/ swap(char *, char *, size_t);
void bubble_sort(void *base, size_t nmemb, size_t size,
                 int (*compar)(const void *, const void *)) {
  char *arr;
  char *a1, *a2;
  int i, n;
  int last;
  arr = (char *)base;
  n = nmemb - 1;
  while (n > 0) {
    last = 0;
    for (i = 0; i < n; i++) {
      a1 = arr + i * size;
      a2 = a1 + size;
      if (compare_int(a1, a2) > 0) {
        swap(a1, a2, size);
        last = i;
      }
    }
    n = last;
  }
}


x86 assembly (clang, -O0):
<bubble_sort>:
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    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
sub    $0x1,%rax
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jle    11db <bubble_sort+0xbb>
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x40(%rbp),%eax
jge    11d0 <bubble_sort+0xb0>
mov    -0x28(%rbp),%rax
movslq -0x3c(%rbp),%rcx
imul   -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1030 <compare_int@plt>
cmp    $0x0,%rax
jle    11bd <bubble_sort+0x9d>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <swap@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x44(%rbp)
jmp    11c2 <bubble_sort+0xa2>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1163 <bubble_sort+0x43>
mov    -0x44(%rbp),%eax
mov    %eax,-0x40(%rbp)
jmp    114b <bubble_sort+0x2b>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bubble_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,(%rsp)
add    $0xffffffff,%r14d
test   %r14d,%r14d
jle    1193 <bubble_sort+0x73>
mov    %rdx,%r12
jmp    1145 <bubble_sort+0x25>
test   %r14d,%r14d
jle    1193 <bubble_sort+0x73>
mov    %r14d,%ebp
xor    %r15d,%r15d
mov    (%rsp),%rbx
xor    %r14d,%r14d
jmp    116c <bubble_sort+0x4c>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%r15
mov    %r13,%rbx
cmp    %r15,%rbp
je     1140 <bubble_sort+0x20>
lea    (%rbx,%r12,1),%r13
mov    %rbx,%rdi
mov    %r13,%rsi
call   1030 <compare_int@plt>
test   %rax,%rax
jle    1160 <bubble_sort+0x40>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
call   1040 <swap@plt>
mov    %r15d,%r14d
jmp    1160 <bubble_sort+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @bubble_sort(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 i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
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 i8*, i8** %5
store i8* %15, i8** %9
%16 = load i64, i64* %6
%17 = sub i64 %16, 1
%18 = trunc i64 %17 to i32
store i32 %18, i32* %13
br label %19
19: ; preds = %51, %4
%20 = load i32, i32* %13
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %53
22: ; preds = %19
store i32 0, i32* %14
store i32 0, i32* %12
br label %23
23: ; preds = %48, %22
%24 = load i32, i32* %12
%25 = load i32, i32* %13
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %51
27: ; preds = %23
%28 = load i8*, i8** %9
%29 = load i32, i32* %12
%30 = sext i32 %29 to i64
%31 = load i64, i64* %7
%32 = mul i64 %30, %31
%33 = getelementptr inbounds i8, i8* %28, i64 %32
store i8* %33, i8** %10
%34 = load i8*, i8** %10
%35 = load i64, i64* %7
%36 = getelementptr inbounds i8, i8* %34, i64 %35
store i8* %36, i8** %11
%37 = load i8*, i8** %10
%38 = load i8*, i8** %11
%39 = call i64 @compare_int(i8* %37, i8* %38)
%40 = icmp sgt i64 %39, 0
br i1 %40, label %41, label %47
41: ; preds = %27
%42 = load i8*, i8** %10
%43 = load i8*, i8** %11
%44 = load i64, i64* %7
%45 = call i32 @swap(i8* %42, i8* %43, i64 %44)
%46 = load i32, i32* %12
store i32 %46, i32* %14
br label %47
47: ; preds = %41, %27
br label %48
48: ; preds = %47
%49 = load i32, i32* %12
%50 = add nsw i32 %49, 1
store i32 %50, i32* %12
br label %23
51: ; preds = %23
%52 = load i32, i32* %14
store i32 %52, i32* %13
br label %19
53: ; preds = %19
ret void
}

LLVM IR (-O2):
define void @bubble_sort(i8* %0, i64 %1, i64 %2, i32 (i8*, i8*)* nocapture readnone %3) {
%5 = trunc i64 %1 to i32
%6 = add i32 %5, -1
%7 = icmp sgt i32 %6, 0
br i1 %7, label %10, label %28
8: ; preds = %24
%9 = icmp sgt i32 %25, 0
br i1 %9, label %10, label %28
10: ; preds = %4, %8
%11 = phi i32 [ %25, %8 ], [ %6, %4 ]
%12 = zext i32 %11 to i64
br label %13
13: ; preds = %10, %24
%14 = phi i64 [ 0, %10 ], [ %26, %24 ]
%15 = phi i32 [ 0, %10 ], [ %25, %24 ]
%16 = mul i64 %14, %2
%17 = getelementptr inbounds i8, i8* %0, i64 %16
%18 = getelementptr inbounds i8, i8* %17, i64 %2
%19 = tail call i64 @compare_int(i8* %17, i8* %18)
%20 = icmp sgt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %13
%22 = tail call i32 @swap(i8* %17, i8* %18, i64 %2)
%23 = trunc i64 %14 to i32
br label %24
24: ; preds = %13, %21
%25 = phi i32 [ %23, %21 ], [ %15, %13 ]
%26 = add nuw nsw i64 %14, 1
%27 = icmp eq i64 %26, %12
br i1 %27, label %8, label %13
28: ; preds = %8, %4
ret void
}