#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*/ print_array(int *, int);
int /*<<< orphan*/ printf(char *, ...);
int bubble_sort(int *arr, int len) {
  int i, j, swap;
  if (arr == NULL || len < 2) {
    return 0;
  }
  for (i = 0; i < len; i++) {
    printf("origin array: ");
    print_array(arr, len);
    for (j = 0; j < len - i - 1; j++) {
      if (arr[j + 1] > arr[j]) {
        swap = arr[j];
        arr[j] = arr[j + 1];
        arr[j + 1] = swap;
      }
      printf("i=%d, j=%d : ", i, j);
      print_array(arr, len);
    }
    printf("\nsorting array: i = %d:  ", i);
    print_array(arr, len);
    printf("\n------------------------------\n\n");
  }
  printf("\n\n sorted array: ");
  print_array(arr, len);
  printf("sort done!\n");
}


x86 assembly (clang, -O0):
<bubble_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1144 <bubble_sort+0x24>
cmpl   $0x2,-0x14(%rbp)
jge    1150 <bubble_sort+0x30>
movl   $0x0,-0x4(%rbp)
jmp    1284 <bubble_sort+0x164>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    125c <bubble_sort+0x13c>
lea    0xe96(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <print_array@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    -0x18(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1223 <bubble_sort+0x103>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x10(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jle    11f5 <bubble_sort+0xd5>
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
add    $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
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <print_array@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1184 <bubble_sort+0x64>
mov    -0x18(%rbp),%esi
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <print_array@plt>
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1157 <bubble_sort+0x37>
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <print_array@plt>
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
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
test   %rdi,%rdi
je     1241 <bubble_sort+0x111>
mov    %esi,%r14d
cmp    $0x2,%esi
jl     1241 <bubble_sort+0x111>
mov    %rdi,%rbx
lea    -0x1(%r14),%r13d
lea    0xeb1(%rip),%r12
xor    %r15d,%r15d
jmp    11a5 <bubble_sort+0x75>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0xea6(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <print_array@plt>
lea    0xec1(%rip),%rdi
call   1030 <puts@plt>
add    $0x1,%r15d
add    $0xffffffff,%r13d
cmp    %r14d,%r15d
je     121c <bubble_sort+0xec>
mov    %r13d,%r13d
lea    0xe51(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <print_array@plt>
mov    %r15d,%eax
not    %eax
add    %r14d,%eax
test   %eax,%eax
jle    1170 <bubble_sort+0x40>
xor    %edx,%edx
jmp    1204 <bubble_sort+0xd4>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <print_array@plt>
mov    %rbp,%rdx
cmp    %rbp,%r13
je     1170 <bubble_sort+0x40>
lea    0x1(%rdx),%rbp
mov    (%rbx,%rdx,4),%eax
mov    0x4(%rbx,%rdx,4),%ecx
cmp    %eax,%ecx
jle    11e0 <bubble_sort+0xb0>
mov    %ecx,(%rbx,%rdx,4)
mov    %eax,0x4(%rbx,%rdx,4)
jmp    11e0 <bubble_sort+0xb0>
lea    0xe14(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <print_array@plt>
lea    0xe0d(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"origin array: \00"
@.str.1 = [14 x i8] c"i=%d, j=%d : \00"
@.str.2 = [26 x i8] c"\0Asorting array: i = %d: \00"
@.str.3 = [34 x i8] c"\0A------------------------------\0A\0A\00"
@.str.4 = [18 x i8] c"\0A\0A sorted array: \00"
@.str.5 = [12 x i8] c"sort done!\0A\00"
define i32 @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** %4
store i32 %1, i32* %5
%9 = load i32*, i32** %4
%10 = icmp eq i32* %9, null
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %15
14: ; preds = %11, %2
store i32 0, i32* %3
br label %93
15: ; preds = %11
store i32 0, i32* %6
br label %16
16: ; preds = %84, %15
%17 = load i32, i32* %6
%18 = load i32, i32* %5
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %87
20: ; preds = %16
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%22 = load i32*, i32** %4
%23 = load i32, i32* %5
%24 = call i32 @print_array(i32* %22, i32 %23)
store i32 0, i32* %7
br label %25
25: ; preds = %74, %20
%26 = load i32, i32* %7
%27 = load i32, i32* %5
%28 = load i32, i32* %6
%29 = sub nsw i32 %27, %28
%30 = sub nsw i32 %29, 1
%31 = icmp slt i32 %26, %30
br i1 %31, label %32, label %77
32: ; preds = %25
%33 = load i32*, i32** %4
%34 = load i32, i32* %7
%35 = add nsw i32 %34, 1
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %33, i64 %36
%38 = load i32, i32* %37
%39 = load i32*, i32** %4
%40 = load i32, i32* %7
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32, i32* %39, i64 %41
%43 = load i32, i32* %42
%44 = icmp sgt i32 %38, %43
br i1 %44, label %45, label %67
45: ; preds = %32
%46 = load i32*, i32** %4
%47 = load i32, i32* %7
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %46, i64 %48
%50 = load i32, i32* %49
store i32 %50, i32* %8
%51 = load i32*, i32** %4
%52 = load i32, i32* %7
%53 = add nsw i32 %52, 1
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i32, i32* %51, i64 %54
%56 = load i32, i32* %55
%57 = load i32*, i32** %4
%58 = load i32, i32* %7
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i32, i32* %57, i64 %59
store i32 %56, i32* %60
%61 = load i32, i32* %8
%62 = load i32*, i32** %4
%63 = load i32, i32* %7
%64 = add nsw i32 %63, 1
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i32, i32* %62, i64 %65
store i32 %61, i32* %66
br label %67
67: ; preds = %45, %32
%68 = load i32, i32* %6
%69 = load i32, i32* %7
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %68, i32 %69)
%71 = load i32*, i32** %4
%72 = load i32, i32* %5
%73 = call i32 @print_array(i32* %71, i32 %72)
br label %74
74: ; preds = %67
%75 = load i32, i32* %7
%76 = add nsw i32 %75, 1
store i32 %76, i32* %7
br label %25
77: ; preds = %25
%78 = load i32, i32* %6
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %78)
%80 = load i32*, i32** %4
%81 = load i32, i32* %5
%82 = call i32 @print_array(i32* %80, i32 %81)
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
br label %84
84: ; preds = %77
%85 = load i32, i32* %6
%86 = add nsw i32 %85, 1
store i32 %86, i32* %6
br label %16
87: ; preds = %16
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%89 = load i32*, i32** %4
%90 = load i32, i32* %5
%91 = call i32 @print_array(i32* %89, i32 %90)
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %93
93: ; preds = %87, %14
%94 = load i32, i32* %3
ret i32 %94
}

LLVM IR (-O2):
@.str = [15 x i8] c"origin array: \00"
@.str.1 = [14 x i8] c"i=%d, j=%d : \00"
@.str.2 = [26 x i8] c"\0Asorting array: i = %d: \00"
@.str.4 = [18 x i8] c"\0A\0A sorted array: \00"
@str = [11 x i8] c"sort done!\00"
@str.6 = [33 x i8] c"\0A------------------------------\0A\00"
define i32 @bubble_sort(i32* %0, i32 %1) {
%3 = icmp eq i32* %0, null
%4 = icmp slt i32 %1, 2
%5 = or i1 %3, %4
br i1 %5, label %41, label %6
6: ; preds = %2, %31
%7 = phi i32 [ %9, %31 ], [ %1, %2 ]
%8 = phi i32 [ %35, %31 ], [ 0, %2 ]
%9 = add i32 %7, -1
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @print_array(i32* %0, i32 %1)
%12 = xor i32 %8, -1
%13 = add i32 %12, %1
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %31
15: ; preds = %6
%16 = zext i32 %9 to i64
br label %17
17: ; preds = %15, %26
%18 = phi i64 [ 0, %15 ], [ %19, %26 ]
%19 = add nuw nsw i64 %18, 1
%20 = getelementptr inbounds i32, i32* %0, i64 %19
%21 = load i32, i32* %20
%22 = getelementptr inbounds i32, i32* %0, i64 %18
%23 = load i32, i32* %22
%24 = icmp sgt i32 %21, %23
br i1 %24, label %25, label %26
25: ; preds = %17
store i32 %21, i32* %22
store i32 %23, i32* %20
br label %26
26: ; preds = %25, %17
%27 = trunc i64 %18 to i32
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %8, i32 %27)
%29 = tail call i32 @print_array(i32* %0, i32 %1)
%30 = icmp eq i64 %19, %16
br i1 %30, label %31, label %17
31: ; preds = %26, %6
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %8)
%33 = tail call i32 @print_array(i32* %0, i32 %1)
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.6, i64 0, i64 0))
%35 = add nuw nsw i32 %8, 1
%36 = icmp eq i32 %35, %1
br i1 %36, label %37, label %6
37: ; preds = %31
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%39 = tail call i32 @print_array(i32* %0, i32 %1)
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %41
41: ; preds = %2, %37
ret i32 0
}