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


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
cmp    -0xc(%rbp),%eax
jge    120e <bubbleSort+0xee>
mov    -0x10(%rbp),%esi
lea    0xeb4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    -0x10(%rbp),%ecx
cmp    %ecx,%eax
jge    11fb <bubbleSort+0xdb>
mov    -0x14(%rbp),%esi
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x8(%rbp),%rcx
movslq -0x14(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jle    11c6 <bubbleSort+0xa6>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%esi
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
cltq
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
call   1040 <swap@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    115a <bubbleSort+0x3a>
jmp    1200 <bubbleSort+0xe0>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1136 <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    $0x18,%rsp
mov    %rdi,0x10(%rsp)
test   %esi,%esi
jle    11d4 <bubbleSort+0xb4>
mov    %esi,%ebx
addq   $0x4,0x10(%rsp)
mov    %esi,%r13d
xor    %ebp,%ebp
mov    %esi,0xc(%rsp)
jmp    115f <bubbleSort+0x3f>
xchg   %ax,%ax
add    $0x1,%ebp
add    $0xffffffff,%r13d
mov    0xc(%rsp),%ebx
cmp    %ebx,%ebp
je     11d4 <bubbleSort+0xb4>
mov    %r13d,%r13d
lea    0xe97(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <printf@plt>
sub    %ebp,%ebx
cmp    $0x2,%ebx
lea    0xe89(%rip),%rbx
jl     1150 <bubbleSort+0x30>
mov    $0x1,%r15d
mov    0x10(%rsp),%r14
jmp    11a8 <bubbleSort+0x88>
nopl   (%rax)
mov    %r12,%rdi
mov    %r14,%rsi
call   1040 <swap@plt>
add    $0x1,%r15
add    $0x4,%r14
cmp    %r15,%r13
je     1150 <bubbleSort+0x30>
mov    %rbx,%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    -0x4(%r14),%r12
mov    -0x4(%r14),%esi
mov    (%r14),%edx
cmp    %edx,%esi
jle    1190 <bubbleSort+0x70>
lea    0xe43(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    1190 <bubbleSort+0x70>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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