#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*/ puts(char *);
int /*<<< orphan*/ swap(int *, int, int);
int bubble_sort(int *array, int size) {
  if (!array) {
    puts("Empty array has been passed\nNothing to sort");
    return -1;
  }
  if (size <= 0) {
    puts("Size passed is invalid");
    return -2;
  }
  int i = 0, j = 0;
  for (; i <= size - 2; i++) {
    for (j = 0; j <= size - 1 - i; j++) {
      if (array[j + 1] < array[j]) {
        swap(array, j, j + 1);
      }
    }
  }
  return 0;
}


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)
jne    1152 <bubble_sort+0x32>
lea    0xebf(%rip),%rdi
call   1030 <puts@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120e <bubble_sort+0xee>
cmpl   $0x0,-0x14(%rbp)
jg     1174 <bubble_sort+0x54>
lea    0xec9(%rip),%rdi
call   1030 <puts@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    120e <bubble_sort+0xee>
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x2,%ecx
cmp    %ecx,%eax
jg     1207 <bubble_sort+0xe7>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
sub    -0x18(%rbp),%ecx
cmp    %ecx,%eax
jg     11f4 <bubble_sort+0xd4>
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
jge    11e1 <bubble_sort+0xc1>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
call   1040 <swap@plt>
jmp    11e6 <bubble_sort+0xc6>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    119a <bubble_sort+0x7a>
jmp    11f9 <bubble_sort+0xd9>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1182 <bubble_sort+0x62>
movl   $0x0,-0x4(%rbp)
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     1192 <bubble_sort+0x72>
mov    %esi,%r14d
test   %esi,%esi
jle    11a5 <bubble_sort+0x85>
xor    %ebp,%ebp
cmp    $0x1,%r14d
je     11ba <bubble_sort+0x9a>
mov    %rdi,%rbx
lea    -0x1(%r14),%r15d
xor    %r12d,%r12d
mov    %r14d,%r13d
jmp    115d <bubble_sort+0x3d>
xchg   %ax,%ax
add    $0x1,%r12d
add    $0xffffffff,%r13d
cmp    %r15d,%r12d
je     11b8 <bubble_sort+0x98>
mov    %r13d,%r13d
mov    %r12d,%eax
not    %eax
add    %r14d,%eax
js     1150 <bubble_sort+0x30>
xor    %esi,%esi
jmp    1178 <bubble_sort+0x58>
xchg   %ax,%ax
mov    %rbp,%rsi
cmp    %rbp,%r13
je     1150 <bubble_sort+0x30>
lea    0x1(%rsi),%rbp
mov    0x4(%rbx,%rsi,4),%eax
cmp    (%rbx,%rsi,4),%eax
jge    1170 <bubble_sort+0x50>
lea    0x1(%rsi),%edx
mov    %rbx,%rdi
call   1040 <swap@plt>
jmp    1170 <bubble_sort+0x50>
mov    $0xffffffff,%ebp
lea    0xe62(%rip),%rdi
call   1030 <puts@plt>
jmp    11ba <bubble_sort+0x9a>
mov    $0xfffffffe,%ebp
lea    0xe7b(%rip),%rdi
call   1030 <puts@plt>
jmp    11ba <bubble_sort+0x9a>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [44 x i8] c"Empty array has been passed\0ANothing to sort\00"
@.str.1 = [23 x i8] c"Size passed is invalid\00"
define i32 @bubble_sort(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
%8 = load i32*, i32** %4
%9 = icmp ne i32* %8, null
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = call i32 @puts(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %59
12: ; preds = %2
%13 = load i32, i32* %5
%14 = icmp sle i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @puts(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 -2, i32* %3
br label %59
17: ; preds = %12
store i32 0, i32* %6
store i32 0, i32* %7
br label %18
18: ; preds = %55, %17
%19 = load i32, i32* %6
%20 = load i32, i32* %5
%21 = sub nsw i32 %20, 2
%22 = icmp sle i32 %19, %21
br i1 %22, label %23, label %58
23: ; preds = %18
store i32 0, i32* %7
br label %24
24: ; preds = %51, %23
%25 = load i32, i32* %7
%26 = load i32, i32* %5
%27 = sub nsw i32 %26, 1
%28 = load i32, i32* %6
%29 = sub nsw i32 %27, %28
%30 = icmp sle i32 %25, %29
br i1 %30, label %31, label %54
31: ; preds = %24
%32 = load i32*, i32** %4
%33 = load i32, i32* %7
%34 = add nsw i32 %33, 1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i32, i32* %32, i64 %35
%37 = load i32, i32* %36
%38 = load i32*, i32** %4
%39 = load i32, i32* %7
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i32, i32* %38, i64 %40
%42 = load i32, i32* %41
%43 = icmp slt i32 %37, %42
br i1 %43, label %44, label %50
44: ; preds = %31
%45 = load i32*, i32** %4
%46 = load i32, i32* %7
%47 = load i32, i32* %7
%48 = add nsw i32 %47, 1
%49 = call i32 @swap(i32* %45, i32 %46, i32 %48)
br label %50
50: ; preds = %44, %31
br label %51
51: ; preds = %50
%52 = load i32, i32* %7
%53 = add nsw i32 %52, 1
store i32 %53, i32* %7
br label %24
54: ; preds = %24
br label %55
55: ; preds = %54
%56 = load i32, i32* %6
%57 = add nsw i32 %56, 1
store i32 %57, i32* %6
br label %18
58: ; preds = %18
store i32 0, i32* %3
br label %59
59: ; preds = %58, %15, %10
%60 = load i32, i32* %3
ret i32 %60
}

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