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


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
cmp    -0xc(%rbp),%eax
jge    11bb <bubble_sort+0xab>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    -0x10(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11a8 <bubble_sort+0x98>
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    1195 <bubble_sort+0x85>
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    119a <bubble_sort+0x8a>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1139 <bubble_sort+0x29>
jmp    11ad <bubble_sort+0x9d>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1126 <bubble_sort+0x16>
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
test   %esi,%esi
jle    118f <bubble_sort+0x7f>
mov    %esi,%r15d
mov    %rdi,%r14
add    $0x4,%r14
lea    -0x1(%r15),%r12d
xor    %r13d,%r13d
jmp    114d <bubble_sort+0x3d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%r13d
add    $0xffffffff,%r12d
cmp    %r15d,%r13d
je     118f <bubble_sort+0x7f>
mov    %r12d,%r12d
mov    %r13d,%eax
not    %eax
add    %r15d,%eax
test   %eax,%eax
jle    1140 <bubble_sort+0x30>
mov    %r12,%rbp
mov    %r14,%rbx
jmp    117a <bubble_sort+0x6a>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
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>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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