#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 = n - 1; i > 0; i--) {
    for (j = n - 1; 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)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    1243 <bubblesort+0x113>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
sub    -0x10(%rbp),%ecx
cmp    %ecx,%eax
jle    1230 <bubblesort+0x100>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x8(%rbp),%rcx
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
movslq %edx,%rdx
cmp    (%rcx,%rdx,4),%eax
jge    121d <bubblesort+0xed>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <printarray@plt>
mov    -0x14(%rbp),%esi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
lea    0xe54(%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
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe32(%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
sub    $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    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1222 <bubblesort+0xf2>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    115b <bubblesort+0x2b>
jmp    1235 <bubblesort+0x105>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1148 <bubblesort+0x18>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bubblesort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsi,(%rsp)
cmp    $0x2,%esi
jl     1277 <bubblesort+0x137>
mov    %rdi,%rbx
mov    (%rsp),%rax
lea    -0x1(%rax),%ecx
mov    %rcx,0x28(%rsp)
mov    %eax,%eax
lea    -0x1(%rax),%rcx
lea    -0x4(,%rax,4),%rdx
ff
mov    %rdx,0x18(%rsp)
lea    -0x8(,%rax,4),%rax
ff
mov    %rax,0x10(%rsp)
mov    %rcx,0x20(%rsp)
jmp    11b6 <bubblesort+0x76>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x30(%rsp),%rcx
lea    -0x1(%rcx),%rax
cmp    $0x1,%rcx
mov    %rax,%rcx
jle    1277 <bubblesort+0x137>
mov    0x28(%rsp),%rdx
mov    %rcx,0x30(%rsp)
sub    %rcx,%rdx
mov    0x10(%rsp),%r14
mov    0x18(%rsp),%r12
mov    0x20(%rsp),%rbp
mov    %rdx,0x38(%rsp)
jmp    11f0 <bubblesort+0xb0>
nopl   0x0(%rax)
add    $0xfffffffffffffffc,%r12
add    $0xfffffffffffffffc,%r14
mov    %r13,%rbp
cmp    %rdx,%r13
jle    11a0 <bubblesort+0x60>
mov    (%rbx,%r12,1),%eax
lea    -0x1(%rbp),%r13
cmp    -0x4(%rbx,%r12,1),%eax
jge    11e0 <bubblesort+0xa0>
lea    (%rbx,%r12,1),%r15
lea    (%rbx,%r14,1),%rax
mov    %rax,0x40(%rsp)
lea    -0x1(%rbp),%eax
mov    %eax,0xc(%rsp)
mov    %rbx,%rdi
mov    (%rsp),%rsi
call   1050 <printarray@plt>
lea    0xdda(%rip),%rdi
mov    %ebp,%esi
mov    0xc(%rsp),%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    -0x4(%r15),%edx
mov    (%r15),%esi
lea    0xdc6(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r15,%rdi
mov    0x40(%rsp),%rsi
call   1060 <swap@plt>
mov    %rbx,%rdi
mov    (%rsp),%rsi
call   1050 <printarray@plt>
lea    0xda6(%rip),%rdi
call   1030 <puts@plt>
mov    0x38(%rsp),%rdx
jmp    11e0 <bubblesort+0xa0>
add    $0x48,%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
%7 = load i32, i32* %4
%8 = sub nsw i32 %7, 1
store i32 %8, i32* %5
br label %9
9: ; preds = %74, %2
%10 = load i32, i32* %5
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %77
12: ; preds = %9
%13 = load i32, i32* %4
%14 = sub nsw i32 %13, 1
store i32 %14, i32* %6
br label %15
15: ; preds = %70, %12
%16 = load i32, i32* %6
%17 = load i32, i32* %4
%18 = sub nsw i32 %17, 1
%19 = load i32, i32* %5
%20 = sub nsw i32 %18, %19
%21 = icmp sgt i32 %16, %20
br i1 %21, label %22, label %73
22: ; preds = %15
%23 = load i32*, i32** %3
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %23, i64 %25
%27 = load i32, i32* %26
%28 = load i32*, i32** %3
%29 = load i32, i32* %6
%30 = sub nsw i32 %29, 1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %28, i64 %31
%33 = load i32, i32* %32
%34 = icmp slt i32 %27, %33
br i1 %34, label %35, label %69
35: ; preds = %22
%36 = load i32*, i32** %3
%37 = load i32, i32* %4
%38 = call i32 @printarray(i32* %36, i32 %37)
%39 = load i32, i32* %6
%40 = load i32, i32* %6
%41 = sub nsw i32 %40, 1
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %39, i32 %41)
%43 = load i32*, i32** %3
%44 = load i32, i32* %6
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i32, i32* %43, i64 %45
%47 = load i32, i32* %46
%48 = load i32*, i32** %3
%49 = load i32, i32* %6
%50 = sub nsw i32 %49, 1
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i32, i32* %48, i64 %51
%53 = load i32, i32* %52
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %47, i32 %53)
%55 = load i32*, i32** %3
%56 = load i32, i32* %6
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i32, i32* %55, i64 %57
%59 = load i32*, i32** %3
%60 = load i32, i32* %6
%61 = sub nsw i32 %60, 1
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i32, i32* %59, i64 %62
%64 = call i32 @swap(i32* %58, i32* %63)
%65 = load i32*, i32** %3
%66 = load i32, i32* %4
%67 = call i32 @printarray(i32* %65, i32 %66)
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %69
69: ; preds = %35, %22
br label %70
70: ; preds = %69
%71 = load i32, i32* %6
%72 = add nsw i32 %71, -1
store i32 %72, i32* %6
br label %15
73: ; preds = %15
br label %74
74: ; preds = %73
%75 = load i32, i32* %5
%76 = add nsw i32 %75, -1
store i32 %76, i32* %5
br label %9
77: ; preds = %9
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 = icmp sgt i32 %1, 1
br i1 %3, label %4, label %36
4: ; preds = %2
%5 = add nsw i32 %1, -1
%6 = zext i32 %1 to i64
%7 = add nsw i64 %6, -1
%8 = zext i32 %5 to i64
br label %9
9: ; preds = %33, %4
%10 = phi i64 [ %7, %4 ], [ %34, %33 ]
%11 = sub nuw nsw i64 %8, %10
br label %12
12: ; preds = %9, %31
%13 = phi i64 [ %7, %9 ], [ %16, %31 ]
%14 = getelementptr inbounds i32, i32* %0, i64 %13
%15 = load i32, i32* %14
%16 = add nsw i64 %13, -1
%17 = getelementptr inbounds i32, i32* %0, i64 %16
%18 = load i32, i32* %17
%19 = icmp slt i32 %15, %18
br i1 %19, label %20, label %31
20: ; preds = %12
%21 = trunc i64 %16 to i32
%22 = tail call i32 @printarray(i32* %0, i32 %1)
%23 = trunc i64 %13 to i32
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %23, i32 %21)
%25 = load i32, i32* %14
%26 = load i32, i32* %17
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %25, i32 %26)
%28 = tail call i32 @swap(i32* %14, i32* %17)
%29 = tail call i32 @printarray(i32* %0, i32 %1)
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %31
31: ; preds = %12, %20
%32 = icmp sgt i64 %16, %11
br i1 %32, label %12, label %33
33: ; preds = %31
%34 = add nsw i64 %10, -1
%35 = icmp sgt i64 %10, 1
br i1 %35, label %9, label %36
36: ; preds = %33, %2
ret void
}