#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 *, unsigned int, unsigned int);
void bubble_sort(int *arr, unsigned int len) {
  bool swapped = true;
  for (unsigned int i = len - 1; i >= 0 && swapped; i--) {
    swapped = false;
    for (unsigned int j = 0; j < i; j++) {
      if (arr[j] > arr[j + 1]) {
        swap(arr, j, j + 1);
        swapped = true;
      }
    }
  }
}


x86 assembly (clang, -O0):
<bubble_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x1,-0x10(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x19(%rbp)
jb     1148 <bubble_sort+0x38>
cmpl   $0x0,-0x10(%rbp)
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1158 <bubble_sort+0x48>
jmp    11d0 <bubble_sort+0xc0>
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    11bd <bubble_sort+0xad>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    (%rax,%rcx,4),%eax
mov    -0x8(%rbp),%rcx
mov    -0x18(%rbp),%edx
add    $0x1,%edx
mov    %edx,%edx
cmp    (%rcx,%rdx,4),%eax
jle    11aa <bubble_sort+0x9a>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x18(%rbp),%edx
add    $0x1,%edx
call   1030 <swap@plt>
movl   $0x1,-0x10(%rbp)
jmp    11af <bubble_sort+0x9f>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1166 <bubble_sort+0x56>
jmp    11c2 <bubble_sort+0xb2>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    112f <bubble_sort+0x1f>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bubble_sort>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r12
lea    -0x1(%r14),%r15d
jmp    1138 <bubble_sort+0x28>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0xffffffff,%r15d
test   %eax,%eax
je     1179 <bubble_sort+0x69>
add    $0xffffffff,%r14d
je     1179 <bubble_sort+0x69>
mov    %r15d,%r15d
xor    %esi,%esi
xor    %eax,%eax
jmp    1158 <bubble_sort+0x48>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rsi
cmp    %rbx,%r15
je     1130 <bubble_sort+0x20>
mov    (%r12,%rsi,4),%ecx
lea    0x1(%rsi),%rbx
cmp    0x4(%r12,%rsi,4),%ecx
jle    1150 <bubble_sort+0x40>
lea    0x1(%rsi),%edx
mov    %r12,%rdi
call   1030 <swap@plt>
mov    $0x1,%eax
jmp    1150 <bubble_sort+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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