#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*/ PrintArray(int *, int, int);
int /*<<< orphan*/ SwapValue(int *, int, int);
int /*<<< orphan*/ printf(char *);
void BubbleSort(int a[], int n) {
  int i, j;
  printf("\n-----------START----------");
  PrintArray(a, n, -1);
  printf("\n-----------START----------\n");
  for (i = 0; i < n; i++) {
    for (j = 0; j < n - 1; j++) {
      if (a[j] > a[j + 1])
        SwapValue(a, j, j + 1);
    }
    PrintArray(a, n, i);
  }
  printf("\n-----------END----------");
  PrintArray(a, n, n - 1);
  printf("\n-----------END----------\n");
}


x86 assembly (clang, -O0):
<BubbleSort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    0xeba(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    $0xffffffff,%edx
call   1050 <PrintArray@plt>
lea    0xeb9(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11f6 <BubbleSort+0xc6>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11d9 <BubbleSort+0xa9>
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    11c6 <BubbleSort+0x96>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
call   1030 <SwapValue@plt>
jmp    11cb <BubbleSort+0x9b>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1182 <BubbleSort+0x52>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1050 <PrintArray@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    116f <BubbleSort+0x3f>
lea    0xe3c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
call   1050 <PrintArray@plt>
lea    0xe38(%rip),%rdi
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BubbleSort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0xea8(%rip),%rbp
mov    %rbp,%rdi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
mov    $0xffffffff,%edx
call   1060 <PrintArray@plt>
mov    %rbp,%rdi
call   1030 <puts@plt>
lea    -0x1(%r14),%r15d
test   %r14d,%r14d
jle    11e2 <BubbleSort+0xa2>
mov    %r15d,%r13d
xor    %r12d,%r12d
jmp    11a7 <BubbleSort+0x67>
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %r12d,%edx
call   1060 <PrintArray@plt>
add    $0x1,%r12d
cmp    %r14d,%r12d
je     11e2 <BubbleSort+0xa2>
cmp    $0x1,%r14d
je     1190 <BubbleSort+0x50>
xor    %esi,%esi
jmp    11c8 <BubbleSort+0x88>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rsi
cmp    %rbp,%r13
je     1190 <BubbleSort+0x50>
mov    (%rbx,%rsi,4),%eax
lea    0x1(%rsi),%rbp
cmp    0x4(%rbx,%rsi,4),%eax
jle    11c0 <BubbleSort+0x80>
lea    0x1(%rsi),%edx
mov    %rbx,%rdi
call   1040 <SwapValue@plt>
jmp    11c0 <BubbleSort+0x80>
lea    0xe33(%rip),%rbp
mov    %rbp,%rdi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %r15d,%edx
call   1060 <PrintArray@plt>
mov    %rbp,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [28 x i8] c"\0A-----------START----------\00"
@.str.1 = [29 x i8] c"\0A-----------START----------\0A\00"
@.str.2 = [26 x i8] c"\0A-----------END----------\00"
@.str.3 = [27 x i8] c"\0A-----------END----------\0A\00"
define void @BubbleSort(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
%7 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%8 = load i32*, i32** %3
%9 = load i32, i32* %4
%10 = call i32 @PrintArray(i32* %8, i32 %9, i32 -1)
%11 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %5
br label %12
12: ; preds = %50, %2
%13 = load i32, i32* %5
%14 = load i32, i32* %4
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %53
16: ; preds = %12
store i32 0, i32* %6
br label %17
17: ; preds = %42, %16
%18 = load i32, i32* %6
%19 = load i32, i32* %4
%20 = sub nsw i32 %19, 1
%21 = icmp slt i32 %18, %20
br i1 %21, label %22, label %45
22: ; preds = %17
%23 = load i32*, i32** %3
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %23, i64 %25
%27 = load i32, i32* %26
%28 = load i32*, i32** %3
%29 = load i32, i32* %6
%30 = add nsw i32 %29, 1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %28, i64 %31
%33 = load i32, i32* %32
%34 = icmp sgt i32 %27, %33
br i1 %34, label %35, label %41
35: ; preds = %22
%36 = load i32*, i32** %3
%37 = load i32, i32* %6
%38 = load i32, i32* %6
%39 = add nsw i32 %38, 1
%40 = call i32 @SwapValue(i32* %36, i32 %37, i32 %39)
br label %41
41: ; preds = %35, %22
br label %42
42: ; preds = %41
%43 = load i32, i32* %6
%44 = add nsw i32 %43, 1
store i32 %44, i32* %6
br label %17
45: ; preds = %17
%46 = load i32*, i32** %3
%47 = load i32, i32* %4
%48 = load i32, i32* %5
%49 = call i32 @PrintArray(i32* %46, i32 %47, i32 %48)
br label %50
50: ; preds = %45
%51 = load i32, i32* %5
%52 = add nsw i32 %51, 1
store i32 %52, i32* %5
br label %12
53: ; preds = %12
%54 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%55 = load i32*, i32** %3
%56 = load i32, i32* %4
%57 = load i32, i32* %4
%58 = sub nsw i32 %57, 1
%59 = call i32 @PrintArray(i32* %55, i32 %56, i32 %58)
%60 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@str = [28 x i8] c"\0A-----------START----------\00"
@str.4 = [26 x i8] c"\0A-----------END----------\00"
define void @BubbleSort(i32* %0, i32 %1) {
%3 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @PrintArray(i32* %0, i32 %1, i32 -1)
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%6 = icmp sgt i32 %1, 0
%7 = add nsw i32 %1, -1
br i1 %6, label %8, label %31
8: ; preds = %2
%9 = icmp eq i32 %1, 1
%10 = zext i32 %7 to i64
br label %11
11: ; preds = %8, %27
%12 = phi i32 [ 0, %8 ], [ %29, %27 ]
br i1 %9, label %27, label %13
13: ; preds = %11, %25
%14 = phi i64 [ %17, %25 ], [ 0, %11 ]
%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 %25
21: ; preds = %13
%22 = trunc i64 %17 to i32
%23 = trunc i64 %14 to i32
%24 = tail call i32 @SwapValue(i32* %0, i32 %23, i32 %22)
br label %25
25: ; preds = %13, %21
%26 = icmp eq i64 %17, %10
br i1 %26, label %27, label %13
27: ; preds = %25, %11
%28 = tail call i32 @PrintArray(i32* %0, i32 %1, i32 %12)
%29 = add nuw nsw i32 %12, 1
%30 = icmp eq i32 %29, %1
br i1 %30, label %31, label %11
31: ; preds = %27, %2
%32 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.4, i64 0, i64 0))
%33 = tail call i32 @PrintArray(i32* %0, i32 %1, i32 %7)
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.4, i64 0, i64 0))
ret void
}