#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 /*<<< orphan*/ swap(int *, int *);
int bubble_sort(int *array, int left, int right) {
  printf("this is bubble_sort\r\n");
  int i, j, length = right - left + 1;
  for (i = 0; i < length - 1; i++) {
    for (j = 0; j < length - 1 - i; j++) {
      if (array[j] > array[j + 1])
        swap(&array[j], &array[j + 1]);
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<bubble_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
lea    0xec7(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%eax
sub    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11eb <bubble_sort+0xcb>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
sub    -0x14(%rbp),%ecx
cmp    %ecx,%eax
jge    11d8 <bubble_sort+0xb8>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x8(%rbp),%rcx
mov    -0x18(%rbp),%edx
add    $0x1,%edx
movslq %edx,%rdx
cmp    (%rcx,%rdx,4),%eax
jle    11c5 <bubble_sort+0xa5>
mov    -0x8(%rbp),%rdi
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
call   1040 <swap@plt>
jmp    11ca <bubble_sort+0xaa>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1169 <bubble_sort+0x49>
jmp    11dd <bubble_sort+0xbd>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1151 <bubble_sort+0x31>
mov    $0x1,%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
mov    %edx,%r12d
mov    %esi,%ebx
mov    %rdi,(%rsp)
lea    0xec5(%rip),%rdi
call   1030 <puts@plt>
sub    %ebx,%r12d
test   %r12d,%r12d
jle    11a2 <bubble_sort+0x82>
mov    %r12d,%r15d
addq   $0x4,(%rsp)
xor    %r13d,%r13d
jmp    1169 <bubble_sort+0x49>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%r13
cmp    %r15,%r13
je     11a2 <bubble_sort+0x82>
mov    %r12d,%eax
sub    %r13d,%eax
jle    1160 <bubble_sort+0x40>
mov    %eax,%ebp
mov    (%rsp),%rbx
xor    %r14d,%r14d
jmp    1189 <bubble_sort+0x69>
nopl   0x0(%rax)
add    $0x4,%rbx
cmp    %rbp,%r14
jae    1160 <bubble_sort+0x40>
mov    -0x4(%rbx),%eax
add    $0x1,%r14
cmp    (%rbx),%eax
jle    1180 <bubble_sort+0x60>
lea    -0x4(%rbx),%rdi
mov    %rbx,%rsi
call   1040 <swap@plt>
jmp    1180 <bubble_sort+0x60>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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