#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*/ swap(int *, int *);
int bubble_sort(int *a, int n) {
  for (int i = 0; i < n - 1; i++) {
    for (int j = 0; j < n - i - 1; j++) {
      if (a[j] > a[j + 1])
        swap(&a[j], &a[j + 1]);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<bubble_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11c0 <bubble_sort+0xb0>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    -0x10(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11ad <bubble_sort+0x9d>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x8(%rbp),%rcx
mov    -0x14(%rbp),%edx
add    $0x1,%edx
movslq %edx,%rdx
cmp    (%rcx,%rdx,4),%eax
jle    119a <bubble_sort+0x8a>
mov    -0x8(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
call   1030 <swap@plt>
jmp    119f <bubble_sort+0x8f>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    113e <bubble_sort+0x2e>
jmp    11b2 <bubble_sort+0xa2>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1126 <bubble_sort+0x16>
xor    %eax,%eax
add    $0x20,%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    %rdi,(%rsp)
cmp    $0x2,%esi
jl     118f <bubble_sort+0x7f>
mov    %esi,%r15d
lea    -0x1(%r15),%r12d
addq   $0x4,(%rsp)
xor    %r13d,%r13d
mov    %r12d,%r14d
jmp    114d <bubble_sort+0x3d>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r13d
add    $0xffffffff,%r14d
cmp    %r12d,%r13d
je     118f <bubble_sort+0x7f>
mov    %r14d,%r14d
mov    %r13d,%eax
not    %eax
add    %r15d,%eax
mov    %r14,%rbp
mov    (%rsp),%rbx
test   %eax,%eax
jg     117a <bubble_sort+0x6a>
jmp    1140 <bubble_sort+0x30>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x4,%rbx
add    $0xffffffffffffffff,%rbp
je     1140 <bubble_sort+0x30>
mov    -0x4(%rbx),%eax
cmp    (%rbx),%eax
jle    1170 <bubble_sort+0x60>
lea    -0x4(%rbx),%rdi
mov    %rbx,%rsi
call   1030 <swap@plt>
jmp    1170 <bubble_sort+0x60>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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