#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 *, size_t, size_t);
void bubblesort(int arr[], size_t arr_sz) {
  int swapped = 1;
  while (swapped) {
    swapped = 0;
    size_t i;
    for (i = 1; i < arr_sz; i++) {
      if (arr[i] < arr[i - 1]) {
        swap(arr, i, i - 1);
        swapped = 1;
      }
    }
  }
}


x86 assembly (clang, -O0):
<bubblesort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11a5 <bubblesort+0x95>
movl   $0x0,-0x14(%rbp)
movq   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11a0 <bubblesort+0x90>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x8(%rbp),%rcx
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
cmp    (%rcx,%rdx,4),%eax
jge    118a <bubblesort+0x7a>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
call   1030 <swap@plt>
movl   $0x1,-0x14(%rbp)
jmp    118f <bubblesort+0x7f>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1140 <bubblesort+0x30>
jmp    1127 <bubblesort+0x17>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bubblesort>:
push   %r15
push   %r14
push   %rbx
cmp    $0x2,%rsi
jb     116c <bubblesort+0x5c>
mov    %rsi,%r14
mov    %rdi,%r15
xor    %eax,%eax
mov    $0x1,%ebx
nopl   0x0(%rax,%rax,1)
mov    (%r15,%rbx,4),%ecx
cmp    -0x4(%r15,%rbx,4),%ecx
jge    114f <bubblesort+0x3f>
lea    -0x1(%rbx),%rdx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <swap@plt>
mov    $0x1,%eax
add    $0x1,%rbx
cmp    %r14,%rbx
jne    1130 <bubblesort+0x20>
cmp    $0x2,%r14
jb     116c <bubblesort+0x5c>
mov    $0x1,%ebx
test   %eax,%eax
mov    $0x0,%eax
jne    1130 <bubblesort+0x20>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @bubblesort(i32* %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i64
store i32* %0, i32** %3
store i64 %1, i64* %4
store i32 1, i32* %5
br label %7
7: ; preds = %36, %2
%8 = load i32, i32* %5
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %37
10: ; preds = %7
store i32 0, i32* %5
store i64 1, i64* %6
br label %11
11: ; preds = %33, %10
%12 = load i64, i64* %6
%13 = load i64, i64* %4
%14 = icmp ult i64 %12, %13
br i1 %14, label %15, label %36
15: ; preds = %11
%16 = load i32*, i32** %3
%17 = load i64, i64* %6
%18 = getelementptr inbounds i32, i32* %16, i64 %17
%19 = load i32, i32* %18
%20 = load i32*, i32** %3
%21 = load i64, i64* %6
%22 = sub i64 %21, 1
%23 = getelementptr inbounds i32, i32* %20, i64 %22
%24 = load i32, i32* %23
%25 = icmp slt i32 %19, %24
br i1 %25, label %26, label %32
26: ; preds = %15
%27 = load i32*, i32** %3
%28 = load i64, i64* %6
%29 = load i64, i64* %6
%30 = sub i64 %29, 1
%31 = call i32 @swap(i32* %27, i64 %28, i64 %30)
store i32 1, i32* %5
br label %32
32: ; preds = %26, %15
br label %33
33: ; preds = %32
%34 = load i64, i64* %6
%35 = add i64 %34, 1
store i64 %35, i64* %6
br label %11
36: ; preds = %11
br label %7
37: ; preds = %7
ret void
}

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