#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 */
char getchar();
int /*<<< orphan*/ pause(int *);
int /*<<< orphan*/ printArray(int, int *);
int /*<<< orphan*/ printBubbleSort(int, int *, int);
int /*<<< orphan*/ printf(char *);
void bubblesort(int length, int array[]) {
  printf("BUBBLE SORT ");
  printArray(length, array);
  printf("Press ENTER to step through, enter s to (s)kip: ");
  int skip = getchar() == 's' ? 1 : 0;
  if (skip) {
    getchar();
  }
  int swapped = 1;
  while (swapped) {
    swapped = 0;
    for (int i = 1; i < length; i = i + 1) {
      printBubbleSort(length, array, i);
      if (array[i] < array[i - 1]) {
        int tmp = array[i];
        array[i] = array[i - 1];
        array[i - 1] = tmp;
        swapped = 1;
      }
      pause(&skip);
    }
  }
}


x86 assembly (clang, -O0):
<bubblesort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe9a(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1060 <printArray@plt>
lea    0xe8f(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
movsbl %al,%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x73,%edx
cmove  %ecx,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11ae <bubblesort+0x5e>
mov    $0x0,%al
call   1040 <getchar@plt>
movl   $0x1,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1268 <bubblesort+0x118>
movl   $0x0,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1263 <bubblesort+0x113>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1070 <printBubbleSort@plt>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x10(%rbp),%rcx
mov    -0x1c(%rbp),%edx
sub    $0x1,%edx
movslq %edx,%rdx
cmp    (%rcx,%rdx,4),%eax
jge    124c <bubblesort+0xfc>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x20(%rbp),%edx
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
movl   $0x1,-0x18(%rbp)
lea    -0x14(%rbp),%rdi
call   1050 <pause@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11cd <bubblesort+0x7d>
jmp    11b5 <bubblesort+0x65>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bubblesort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
mov    %edi,%r15d
lea    0xe98(%rip),%rdi
call   1030 <printf@plt>
mov    %r15d,%edi
mov    %r13,%rsi
call   1060 <printArray@plt>
lea    0xe8e(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
call   1040 <getchar@plt>
xor    %ecx,%ecx
cmp    $0x73,%al
sete   %cl
mov    %ecx,0x4(%rsp)
jne    119f <bubblesort+0x4f>
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    $0x2,%r15d
jl     120f <bubblesort+0xbf>
mov    %r15d,%r12d
xor    %ebx,%ebx
mov    $0x1,%ebp
lea    0x4(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15d,%edi
mov    %r13,%rsi
mov    %ebp,%edx
call   1070 <printBubbleSort@plt>
mov    -0x4(%r13,%rbp,4),%ecx
mov    0x0(%r13,%rbp,4),%eax
cmp    %ecx,%eax
jge    11ea <bubblesort+0x9a>
mov    %ecx,0x0(%r13,%rbp,4)
mov    %eax,-0x4(%r13,%rbp,4)
mov    $0x1,%ebx
mov    %r14,%rdi
call   1050 <pause@plt>
add    $0x1,%rbp
cmp    %r12,%rbp
jne    11c0 <bubblesort+0x70>
cmp    $0x2,%r15d
jl     120f <bubblesort+0xbf>
mov    $0x1,%ebp
test   %ebx,%ebx
mov    $0x0,%ebx
jne    11c0 <bubblesort+0x70>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"BUBBLE SORT \00"
@.str.1 = [49 x i8] c"Press ENTER to step through, enter s to (s)kip: \00"
define void @bubblesort(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
%9 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* %3
%11 = load i32*, i32** %4
%12 = call i32 @printArray(i32 %10, i32* %11)
%13 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%14 = call signext i8 (...) @getchar()
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 115
%17 = zext i1 %16 to i64
%18 = select i1 %16, i32 1, i32 0
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %2
%22 = call signext i8 (...) @getchar()
br label %23
23: ; preds = %21, %2
store i32 1, i32* %6
br label %24
24: ; preds = %76, %23
%25 = load i32, i32* %6
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %77
27: ; preds = %24
store i32 0, i32* %6
store i32 1, i32* %7
br label %28
28: ; preds = %73, %27
%29 = load i32, i32* %7
%30 = load i32, i32* %3
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %76
32: ; preds = %28
%33 = load i32, i32* %3
%34 = load i32*, i32** %4
%35 = load i32, i32* %7
%36 = call i32 @printBubbleSort(i32 %33, i32* %34, i32 %35)
%37 = load i32*, i32** %4
%38 = load i32, i32* %7
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i32, i32* %37, i64 %39
%41 = load i32, i32* %40
%42 = load i32*, i32** %4
%43 = load i32, i32* %7
%44 = sub nsw i32 %43, 1
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i32, i32* %42, i64 %45
%47 = load i32, i32* %46
%48 = icmp slt i32 %41, %47
br i1 %48, label %49, label %71
49: ; preds = %32
%50 = load i32*, i32** %4
%51 = load i32, i32* %7
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i32, i32* %50, i64 %52
%54 = load i32, i32* %53
store i32 %54, i32* %8
%55 = load i32*, i32** %4
%56 = load i32, i32* %7
%57 = sub nsw i32 %56, 1
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i32, i32* %55, i64 %58
%60 = load i32, i32* %59
%61 = load i32*, i32** %4
%62 = load i32, i32* %7
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i32, i32* %61, i64 %63
store i32 %60, i32* %64
%65 = load i32, i32* %8
%66 = load i32*, i32** %4
%67 = load i32, i32* %7
%68 = sub nsw i32 %67, 1
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds i32, i32* %66, i64 %69
store i32 %65, i32* %70
store i32 1, i32* %6
br label %71
71: ; preds = %49, %32
%72 = call i32 @pause(i32* %5)
br label %73
73: ; preds = %71
%74 = load i32, i32* %7
%75 = add nsw i32 %74, 1
store i32 %75, i32* %7
br label %28
76: ; preds = %28
br label %24
77: ; preds = %24
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"BUBBLE SORT \00"
@.str.1 = [49 x i8] c"Press ENTER to step through, enter s to (s)kip: \00"
define void @bubblesort(i32 %0, i32* %1) {
%3 = alloca i32
%4 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @printArray(i32 %0, i32* %1)
%6 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%7 = bitcast i32* %3 to i8*
%8 = tail call signext i8 (...) @getchar()
%9 = icmp eq i8 %8, 115
%10 = zext i1 %9 to i32
store i32 %10, i32* %3
br i1 %9, label %11, label %13
11: ; preds = %2
%12 = tail call signext i8 (...) @getchar()
br label %13
13: ; preds = %11, %2
%14 = icmp sgt i32 %0, 1
%15 = zext i32 %0 to i64
br i1 %14, label %19, label %39
16: ; preds = %31
%17 = icmp ne i32 %32, 0
%18 = and i1 %14, %17
br i1 %18, label %36, label %39
19: ; preds = %13, %36
%20 = phi i64 [ %37, %36 ], [ 1, %13 ]
%21 = phi i32 [ %38, %36 ], [ 0, %13 ]
%22 = trunc i64 %20 to i32
%23 = call i32 @printBubbleSort(i32 %0, i32* %1, i32 %22)
%24 = getelementptr inbounds i32, i32* %1, i64 %20
%25 = load i32, i32* %24
%26 = add nsw i64 %20, -1
%27 = getelementptr inbounds i32, i32* %1, i64 %26
%28 = load i32, i32* %27
%29 = icmp slt i32 %25, %28
br i1 %29, label %30, label %31
30: ; preds = %19
store i32 %28, i32* %24
store i32 %25, i32* %27
br label %31
31: ; preds = %30, %19
%32 = phi i32 [ 1, %30 ], [ %21, %19 ]
%33 = call i32 @pause(i32* %3)
%34 = add nuw nsw i64 %20, 1
%35 = icmp eq i64 %34, %15
br i1 %35, label %16, label %36
36: ; preds = %31, %16
%37 = phi i64 [ %34, %31 ], [ 1, %16 ]
%38 = phi i32 [ %32, %31 ], [ 0, %16 ]
br label %19
39: ; preds = %16, %13
ret void
}