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


x86 assembly (clang, -O0):
<bubblesort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11af <bubblesort+0x9f>
movl   $0x0,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11aa <bubblesort+0x9a>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x8(%rbp),%rcx
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
movslq %edx,%rdx
cmp    (%rcx,%rdx,4),%eax
jle    1197 <bubblesort+0x87>
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
call   1030 <swap@plt>
movl   $0x1,-0x14(%rbp)
jmp    119c <bubblesort+0x8c>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1151 <bubblesort+0x41>
jmp    1130 <bubblesort+0x20>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bubblesort>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    1164 <bubblesort+0x54>
mov    %edx,%r14d
mov    %rdi,%rbx
mov    %edx,%r15d
xor    %esi,%esi
xor    %eax,%eax
nopl   0x0(%rax,%rax,1)
mov    (%rbx,%rsi,4),%ecx
lea    0x1(%rsi),%rbp
cmp    0x4(%rbx,%rsi,4),%ecx
jle    114c <bubblesort+0x3c>
mov    %rbx,%rdi
mov    %ebp,%edx
call   1030 <swap@plt>
mov    $0x1,%eax
mov    %rbp,%rsi
cmp    %r15,%rbp
jne    1130 <bubblesort+0x20>
test   %r14d,%r14d
jle    1164 <bubblesort+0x54>
xor    %esi,%esi
test   %eax,%eax
mov    $0x0,%eax
jne    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, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 1, i32* %7
store i32 0, i32* %8
br label %10
10: ; preds = %43, %3
%11 = load i32, i32* %7
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %44
13: ; preds = %10
store i32 0, i32* %7
%14 = load i32, i32* %8
%15 = add nsw i32 %14, 1
store i32 %15, i32* %8
store i32 0, i32* %9
br label %16
16: ; preds = %40, %13
%17 = load i32, i32* %9
%18 = load i32, i32* %6
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %43
20: ; preds = %16
%21 = load i32*, i32** %4
%22 = load i32, i32* %9
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %21, i64 %23
%25 = load i32, i32* %24
%26 = load i32*, i32** %4
%27 = load i32, i32* %9
%28 = add nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %26, i64 %29
%31 = load i32, i32* %30
%32 = icmp sgt i32 %25, %31
br i1 %32, label %33, label %39
33: ; preds = %20
%34 = load i32*, i32** %4
%35 = load i32, i32* %9
%36 = load i32, i32* %9
%37 = add nsw i32 %36, 1
%38 = call i32 @swap(i32* %34, i32 %35, i32 %37)
store i32 1, i32* %7
br label %39
39: ; preds = %33, %20
br label %40
40: ; preds = %39
%41 = load i32, i32* %9
%42 = add nsw i32 %41, 1
store i32 %42, i32* %9
br label %16
43: ; preds = %16
br label %10
44: ; preds = %10
ret void
}

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