#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*/ printf(char *, ...);
void bubble_sort(int *a, int len) {
  int x, y, nswaps;
  do {
    nswaps = 0;
    x = 0;
    y = 1;
    printf("scanning\n");
    while (y < len) {
      if (a[x] > a[y]) {
        int t = a[x];
        a[x] = a[y];
        a[y] = t;
        printf("swap: %i <-> %i\n", x, y);
        nswaps++;
      }
      x++;
      y++;
    }
  } while (nswaps != 0);
  printf("done\n");
}


x86 assembly (clang, -O0):
<bubble_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
lea    0xec5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11d0 <bubble_sort+0xc0>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x8(%rbp),%rcx
movslq -0x14(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jle    11b9 <bubble_sort+0xa9>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%edx
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1142 <bubble_sort+0x32>
jmp    11d5 <bubble_sort+0xc5>
cmpl   $0x0,-0x18(%rbp)
jne    111f <bubble_sort+0xf>
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
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
mov    %esi,%r14d
mov    %rdi,%r13
lea    -0x1(%r14),%ebp
lea    0xec4(%rip),%r12
jmp    1145 <bubble_sort+0x25>
xchg   %ax,%ax
test   %r15d,%r15d
je     1196 <bubble_sort+0x76>
lea    0xec5(%rip),%rdi
call   1030 <puts@plt>
cmp    $0x2,%r14d
jl     1196 <bubble_sort+0x76>
xor    %ebx,%ebx
xor    %r15d,%r15d
jmp    1169 <bubble_sort+0x49>
xchg   %ax,%ax
add    $0x1,%rbx
cmp    %rbx,%rbp
je     1140 <bubble_sort+0x20>
mov    0x0(%r13,%rbx,4),%eax
mov    0x4(%r13,%rbx,4),%ecx
cmp    %ecx,%eax
jle    1160 <bubble_sort+0x40>
mov    %ecx,0x0(%r13,%rbx,4)
mov    %eax,0x4(%r13,%rbx,4)
lea    0x1(%rbx),%edx
mov    %r12,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%r15d
jmp    1160 <bubble_sort+0x40>
lea    0xe7d(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"scanning\0A\00"
@.str.1 = [17 x i8] c"swap: %i <-> %i\0A\00"
@.str.2 = [6 x i8] c"done\0A\00"
define void @bubble_sort(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
br label %9
9: ; preds = %58, %2
store i32 0, i32* %7
store i32 0, i32* %5
store i32 1, i32* %6
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %52, %9
%12 = load i32, i32* %6
%13 = load i32, i32* %4
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %57
15: ; preds = %11
%16 = load i32*, i32** %3
%17 = load i32, i32* %5
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i32, i32* %16, i64 %18
%20 = load i32, i32* %19
%21 = load i32*, i32** %3
%22 = load i32, i32* %6
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %21, i64 %23
%25 = load i32, i32* %24
%26 = icmp sgt i32 %20, %25
br i1 %26, label %27, label %52
27: ; preds = %15
%28 = load i32*, i32** %3
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %28, i64 %30
%32 = load i32, i32* %31
store i32 %32, i32* %8
%33 = load i32*, i32** %3
%34 = load i32, i32* %6
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i32, i32* %33, i64 %35
%37 = load i32, i32* %36
%38 = load i32*, i32** %3
%39 = load i32, i32* %5
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i32, i32* %38, i64 %40
store i32 %37, i32* %41
%42 = load i32, i32* %8
%43 = load i32*, i32** %3
%44 = load i32, i32* %6
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i32, i32* %43, i64 %45
store i32 %42, i32* %46
%47 = load i32, i32* %5
%48 = load i32, i32* %6
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %47, i32 %48)
%50 = load i32, i32* %7
%51 = add nsw i32 %50, 1
store i32 %51, i32* %7
br label %52
52: ; preds = %27, %15
%53 = load i32, i32* %5
%54 = add nsw i32 %53, 1
store i32 %54, i32* %5
%55 = load i32, i32* %6
%56 = add nsw i32 %55, 1
store i32 %56, i32* %6
br label %11
57: ; preds = %11
br label %58
58: ; preds = %57
%59 = load i32, i32* %7
%60 = icmp ne i32 %59, 0
br i1 %60, label %9, label %61
61: ; preds = %58
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str.1 = [17 x i8] c"swap: %i <-> %i\0A\00"
@str = [9 x i8] c"scanning\00"
@str.3 = [5 x i8] c"done\00"
define void @bubble_sort(i32* nocapture %0, i32 %1) {
%3 = icmp sgt i32 %1, 1
%4 = add i32 %1, -1
%5 = zext i32 %4 to i64
br label %6
6: ; preds = %27, %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
br i1 %3, label %8, label %29
8: ; preds = %6, %22
%9 = phi i64 [ %25, %22 ], [ 1, %6 ]
%10 = phi i64 [ %24, %22 ], [ 0, %6 ]
%11 = phi i32 [ %23, %22 ], [ 0, %6 ]
%12 = getelementptr inbounds i32, i32* %0, i64 %10
%13 = load i32, i32* %12
%14 = getelementptr inbounds i32, i32* %0, i64 %9
%15 = load i32, i32* %14
%16 = icmp sgt i32 %13, %15
br i1 %16, label %17, label %22
17: ; preds = %8
store i32 %15, i32* %12
store i32 %13, i32* %14
%18 = trunc i64 %10 to i32
%19 = trunc i64 %9 to i32
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 %19)
%21 = add nsw i32 %11, 1
br label %22
22: ; preds = %17, %8
%23 = phi i32 [ %21, %17 ], [ %11, %8 ]
%24 = add nuw nsw i64 %10, 1
%25 = add nuw nsw i64 %9, 1
%26 = icmp eq i64 %24, %5
br i1 %26, label %27, label %8
27: ; preds = %22
%28 = icmp eq i32 %23, 0
br i1 %28, label %29, label %6
29: ; preds = %6, %27
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.3, i64 0, i64 0))
ret void
}