#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*/ printarray(int *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ swap(int *, int *);
void bubblesort(int A[], int n) {
  int i, j;
  for (i = 0; i < n - 1; i++) {
    for (j = 0; j < n - 1 - i; j++) {
      if (A[j] > A[j + 1]) {
        printarray(A, n);
        printf("%d~%d\t", j, j + 1);
        printf("%d~%d\n", A[j], A[j + 1]);
        swap(&A[j], &A[j + 1]);
        printarray(A, n);
        printf("-----------------------\n");
      }
    }
  }
}


x86 assembly (clang, -O0):
<bubblesort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1246 <bubblesort+0x116>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
sub    -0x10(%rbp),%ecx
cmp    %ecx,%eax
jge    1233 <bubblesort+0x103>
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    1220 <bubblesort+0xf0>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <printarray@plt>
mov    -0x14(%rbp),%esi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
call   1050 <swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <printarray@plt>
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1225 <bubblesort+0xf5>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    115e <bubblesort+0x2e>
jmp    1238 <bubblesort+0x108>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1146 <bubblesort+0x16>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bubblesort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdi,0x10(%rsp)
mov    %rsi,0x18(%rsp)
cmp    $0x2,%esi
jl     1246 <bubblesort+0x106>
mov    0x18(%rsp),%rax
lea    -0x1(%rax),%ecx
mov    0x10(%rsp),%rax
add    $0x4,%rax
mov    %rax,0x20(%rsp)
mov    %ecx,%edx
xor    %eax,%eax
mov    %ecx,0x8(%rsp)
mov    0x10(%rsp),%r12
mov    0x18(%rsp),%r14
jmp    11a6 <bubblesort+0x66>
nopl   0x0(%rax,%rax,1)
mov    0xc(%rsp),%eax
add    $0x1,%eax
add    $0xffffffff,%edx
mov    0x8(%rsp),%ecx
cmp    %ecx,%eax
je     1246 <bubblesort+0x106>
mov    %edx,%edx
mov    %eax,0xc(%rsp)
cmp    %eax,%ecx
jle    1190 <bubblesort+0x50>
mov    0x20(%rsp),%rbp
xor    %r15d,%r15d
mov    %rdx,0x28(%rsp)
jmp    11cc <bubblesort+0x8c>
nop
add    $0x4,%rbp
mov    %rbx,%r15
cmp    %rbx,%rdx
je     1190 <bubblesort+0x50>
mov    -0x4(%rbp),%eax
lea    0x1(%r15),%rbx
cmp    0x0(%rbp),%eax
jle    11c0 <bubblesort+0x80>
lea    -0x4(%rbp),%rax
mov    %rax,0x30(%rsp)
lea    0x1(%r15),%r13d
mov    %r12,%rdi
mov    %r14d,%esi
call   1050 <printarray@plt>
lea    0xe09(%rip),%rdi
mov    %r15d,%esi
mov    %r13d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    -0x4(%rbp),%esi
mov    0x0(%rbp),%edx
lea    0xdf6(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x30(%rsp),%rdi
mov    %rbp,%rsi
call   1060 <swap@plt>
mov    %r12,%rdi
mov    %r14d,%esi
call   1050 <printarray@plt>
lea    0xdd7(%rip),%rdi
call   1030 <puts@plt>
mov    0x28(%rsp),%rdx
jmp    11c0 <bubblesort+0x80>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%d~%d\09\00"
@.str.1 = [7 x i8] c"%d~%d\0A\00"
@.str.2 = [25 x i8] c"-----------------------\0A\00"
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* %5
br label %7
7: ; preds = %72, %2
%8 = load i32, i32* %5
%9 = load i32, i32* %4
%10 = sub nsw i32 %9, 1
%11 = icmp slt i32 %8, %10
br i1 %11, label %12, label %75
12: ; preds = %7
store i32 0, i32* %6
br label %13
13: ; preds = %68, %12
%14 = load i32, i32* %6
%15 = load i32, i32* %4
%16 = sub nsw i32 %15, 1
%17 = load i32, i32* %5
%18 = sub nsw i32 %16, %17
%19 = icmp slt i32 %14, %18
br i1 %19, label %20, label %71
20: ; preds = %13
%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 = load i32*, i32** %3
%27 = load i32, i32* %6
%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 %67
33: ; preds = %20
%34 = load i32*, i32** %3
%35 = load i32, i32* %4
%36 = call i32 @printarray(i32* %34, i32 %35)
%37 = load i32, i32* %6
%38 = load i32, i32* %6
%39 = add nsw i32 %38, 1
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %37, i32 %39)
%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* %44
%46 = load i32*, i32** %3
%47 = load i32, i32* %6
%48 = add nsw i32 %47, 1
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i32, i32* %46, i64 %49
%51 = load i32, i32* %50
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %45, i32 %51)
%53 = load i32*, i32** %3
%54 = load i32, i32* %6
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i32, i32* %53, i64 %55
%57 = load i32*, i32** %3
%58 = load i32, i32* %6
%59 = add nsw i32 %58, 1
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i32, i32* %57, i64 %60
%62 = call i32 @swap(i32* %56, i32* %61)
%63 = load i32*, i32** %3
%64 = load i32, i32* %4
%65 = call i32 @printarray(i32* %63, i32 %64)
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %67
67: ; preds = %33, %20
br label %68
68: ; preds = %67
%69 = load i32, i32* %6
%70 = add nsw i32 %69, 1
store i32 %70, i32* %6
br label %13
71: ; preds = %13
br label %72
72: ; preds = %71
%73 = load i32, i32* %5
%74 = add nsw i32 %73, 1
store i32 %74, i32* %5
br label %7
75: ; preds = %7
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"%d~%d\09\00"
@.str.1 = [7 x i8] c"%d~%d\0A\00"
@str = [24 x i8] c"-----------------------\00"
define void @bubblesort(i32* %0, i32 %1) {
%3 = add i32 %1, -1
%4 = icmp sgt i32 %1, 1
br i1 %4, label %5, label %36
5: ; preds = %2, %32
%6 = phi i32 [ %34, %32 ], [ %3, %2 ]
%7 = phi i32 [ %33, %32 ], [ 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, %30
%12 = phi i64 [ 0, %9 ], [ %15, %30 ]
%13 = getelementptr inbounds i32, i32* %0, i64 %12
%14 = load i32, i32* %13
%15 = add nuw nsw i64 %12, 1
%16 = getelementptr inbounds i32, i32* %0, i64 %15
%17 = load i32, i32* %16
%18 = icmp sgt i32 %14, %17
br i1 %18, label %19, label %30
19: ; preds = %11
%20 = trunc i64 %15 to i32
%21 = tail call i32 @printarray(i32* %0, i32 %1)
%22 = trunc i64 %12 to i32
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %22, i32 %20)
%24 = load i32, i32* %13
%25 = load i32, i32* %16
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %24, i32 %25)
%27 = tail call i32 @swap(i32* %13, i32* %16)
%28 = tail call i32 @printarray(i32* %0, i32 %1)
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %30
30: ; preds = %11, %19
%31 = icmp eq i64 %15, %10
br i1 %31, label %32, label %11
32: ; preds = %30, %5
%33 = add nuw nsw i32 %7, 1
%34 = add i32 %6, -1
%35 = icmp eq i32 %33, %3
br i1 %35, label %36, label %5
36: ; preds = %32, %2
ret void
}