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


x86 assembly (clang, -O0):
<bubble_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movb   $0x1,-0x15(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%ecx
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x16(%rbp)
jge    114d <bubble_sort+0x3d>
movsbl -0x15(%rbp),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x16(%rbp)
mov    -0x16(%rbp),%al
test   $0x1,%al
jne    115d <bubble_sort+0x4d>
jmp    11ee <bubble_sort+0xde>
movb   $0x0,-0x15(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
sub    -0x10(%rbp),%ecx
cmp    %ecx,%eax
jge    11db <bubble_sort+0xcb>
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    11c8 <bubble_sort+0xb8>
mov    -0x8(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
add    $0x4,%rsi
call   1030 <swap@plt>
movb   $0x1,-0x15(%rbp)
jmp    11cd <bubble_sort+0xbd>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1168 <bubble_sort+0x58>
jmp    11e0 <bubble_sort+0xd0>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    112a <bubble_sort+0x1a>
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
cmp    $0x2,%esi
jl     11a1 <bubble_sort+0x91>
mov    %esi,%r15d
mov    %rdi,%r14
add    $0xffffffff,%r15d
add    $0x4,%r14
xor    %r12d,%r12d
mov    %r15d,%r13d
nopl   0x0(%rax,%rax,1)
cmp    %r12d,%r15d
jle    11a1 <bubble_sort+0x91>
mov    %r13d,%r13d
xor    %eax,%eax
mov    %r13,%rbp
mov    %r14,%rbx
jmp    116a <bubble_sort+0x5a>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x4,%rbx
add    $0xffffffffffffffff,%rbp
je     1190 <bubble_sort+0x80>
mov    -0x4(%rbx),%ecx
cmp    (%rbx),%ecx
jle    1160 <bubble_sort+0x50>
lea    -0x4(%rbx),%rdi
mov    %rbx,%rsi
call   1030 <swap@plt>
mov    $0x1,%al
jmp    1160 <bubble_sort+0x50>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%r12d
cmp    %r12d,%r15d
jle    11a1 <bubble_sort+0x91>
add    $0xffffffff,%r13d
test   %al,%al
jne    1140 <bubble_sort+0x30>
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
%7 = alloca i8
store i32* %0, i32** %3
store i32 %1, i32* %4
store i8 1, i8* %7
store i32 0, i32* %5
br label %8
8: ; preds = %56, %2
%9 = load i32, i32* %5
%10 = load i32, i32* %4
%11 = sub nsw i32 %10, 1
%12 = icmp slt i32 %9, %11
br i1 %12, label %13, label %17
13: ; preds = %8
%14 = load i8, i8* %7
%15 = sext i8 %14 to i32
%16 = icmp ne i32 %15, 0
br label %17
17: ; preds = %13, %8
%18 = phi i1 [ false, %8 ], [ %16, %13 ]
br i1 %18, label %19, label %59
19: ; preds = %17
store i8 0, i8* %7
store i32 0, i32* %6
br label %20
20: ; preds = %52, %19
%21 = load i32, i32* %6
%22 = load i32, i32* %4
%23 = sub nsw i32 %22, 1
%24 = load i32, i32* %5
%25 = sub nsw i32 %23, %24
%26 = icmp slt i32 %21, %25
br i1 %26, label %27, label %55
27: ; preds = %20
%28 = load i32*, i32** %3
%29 = load i32, i32* %6
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %28, i64 %30
%32 = load i32, i32* %31
%33 = load i32*, i32** %3
%34 = load i32, i32* %6
%35 = add nsw i32 %34, 1
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %33, i64 %36
%38 = load i32, i32* %37
%39 = icmp sgt i32 %32, %38
br i1 %39, label %40, label %51
40: ; preds = %27
%41 = load i32*, i32** %3
%42 = load i32, i32* %6
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %41, i64 %43
%45 = load i32*, i32** %3
%46 = load i32, i32* %6
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %45, i64 %47
%49 = getelementptr inbounds i32, i32* %48, i64 1
%50 = call i32 @swap(i32* %44, i32* %49)
store i8 1, i8* %7
br label %51
51: ; preds = %40, %27
br label %52
52: ; preds = %51
%53 = load i32, i32* %6
%54 = add nsw i32 %53, 1
store i32 %54, i32* %6
br label %20
55: ; preds = %20
br label %56
56: ; preds = %55
%57 = load i32, i32* %5
%58 = add nsw i32 %57, 1
store i32 %58, i32* %5
br label %8
59: ; preds = %17
ret void
}

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