#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 bubblesort(int array[], int length) {
  int i;
  int sorted = 0;
  while (!sorted) {
    sorted = 1;
    for (i = 1; i < length; i++) {
      if (array[i] < array[i - 1]) {
        swap(&array[i], &array[i - 1]);
        sorted = 0;
      }
    }
  }
}


x86 assembly (clang, -O0):
<bubblesort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    113c <bubblesort+0x2c>
jmp    11bd <bubblesort+0xad>
movl   $0x1,-0x14(%rbp)
movl   $0x1,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11b8 <bubblesort+0xa8>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%edx
sub    $0x1,%edx
movslq %edx,%rdx
cmp    (%rcx,%rdx,4),%eax
jge    11a5 <bubblesort+0x95>
mov    -0x8(%rbp),%rdi
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
call   1030 <swap@plt>
movl   $0x0,-0x14(%rbp)
jmp    11aa <bubblesort+0x9a>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    114a <bubblesort+0x3a>
jmp    1126 <bubblesort+0x16>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bubblesort>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%esi
jl     1169 <bubblesort+0x59>
mov    %esi,%r14d
mov    %rdi,%rbx
mov    %esi,%r15d
mov    $0x1,%eax
mov    $0x1,%ebp
nop
mov    (%rbx,%rbp,4),%ecx
cmp    -0x4(%rbx,%rbp,4),%ecx
jge    114c <bubblesort+0x3c>
lea    (%rbx,%rbp,4),%rdi
lea    (%rbx,%rbp,4),%rsi
add    $0xfffffffffffffffc,%rsi
call   1030 <swap@plt>
xor    %eax,%eax
add    $0x1,%rbp
cmp    %r15,%rbp
jne    1130 <bubblesort+0x20>
cmp    $0x2,%r14d
jl     1169 <bubblesort+0x59>
mov    $0x1,%ebp
test   %eax,%eax
mov    $0x1,%eax
je     1130 <bubblesort+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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