#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*/ swap(int *, int *);
void bubble_sort(int A[], int n) {
  int i, j, k;
  int flag = 1;
  for (i = 0; i < n && flag; i++) {
    flag = 0;
    for (j = n - 1; j > i; j--) {
      if (A[j - 1] > A[j]) {
        swap(&A[j], &A[j - 1]);
        flag = 1;
      }
    }
  }
}


x86 assembly (clang, -O0):
<bubble_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x1,-0x1c(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%ecx
xor    %eax,%eax
cmp    -0xc(%rbp),%ecx
mov    %al,-0x1d(%rbp)
jge    1148 <bubble_sort+0x38>
cmpl   $0x0,-0x1c(%rbp)
setne  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1158 <bubble_sort+0x48>
jmp    11e9 <bubble_sort+0xd9>
movl   $0x0,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
jle    11d6 <bubble_sort+0xc6>
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    11c3 <bubble_sort+0xb3>
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   1030 <swap@plt>
movl   $0x1,-0x1c(%rbp)
jmp    11c8 <bubble_sort+0xb8>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1168 <bubble_sort+0x58>
jmp    11db <bubble_sort+0xcb>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    112d <bubble_sort+0x1d>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bubble_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
test   %esi,%esi
jle    11bf <bubble_sort+0xaf>
mov    %esi,%r14d
mov    %rdi,%r15
lea    -0x1(%r14),%eax
mov    %rax,0x20(%rsp)
mov    %esi,%eax
lea    -0x1(%rax),%rcx
mov    %rcx,0x10(%rsp)
add    $0xfffffffe,%r14d
mov    %rax,0x18(%rsp)
lea    (%rdi,%rax,4),%rax
add    $0xfffffffffffffffc,%rax
mov    %rax,0x8(%rsp)
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
cmp    0x20(%rsp),%rbp
jae    11bf <bubble_sort+0xaf>
xor    %eax,%eax
mov    0x8(%rsp),%rbx
mov    %r14d,%r12d
mov    0x10(%rsp),%r13
jmp    1191 <bubble_sort+0x81>
nopl   0x0(%rax,%rax,1)
add    $0xffffffffffffffff,%r13
add    $0xffffffff,%r12d
add    $0xfffffffffffffffc,%rbx
cmp    %rbp,%r13
jle    11b0 <bubble_sort+0xa0>
movslq %r12d,%rcx
mov    (%r15,%rcx,4),%edx
cmp    (%rbx),%edx
jle    1180 <bubble_sort+0x70>
lea    (%r15,%rcx,4),%rsi
mov    %rbx,%rdi
call   1030 <swap@plt>
mov    $0x1,%eax
jmp    1180 <bubble_sort+0x70>
nop
add    $0x1,%rbp
cmp    0x18(%rsp),%rbp
jae    11bf <bubble_sort+0xaf>
test   %eax,%eax
jne    1160 <bubble_sort+0x50>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @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** %3
store i32 %1, i32* %4
store i32 1, i32* %8
store i32 0, i32* %5
br label %9
9: ; preds = %54, %2
%10 = load i32, i32* %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i32, i32* %8
%15 = icmp ne i32 %14, 0
br label %16
16: ; preds = %13, %9
%17 = phi i1 [ false, %9 ], [ %15, %13 ]
br i1 %17, label %18, label %57
18: ; preds = %16
store i32 0, i32* %8
%19 = load i32, i32* %4
%20 = sub nsw i32 %19, 1
store i32 %20, i32* %6
br label %21
21: ; preds = %50, %18
%22 = load i32, i32* %6
%23 = load i32, i32* %5
%24 = icmp sgt i32 %22, %23
br i1 %24, label %25, label %53
25: ; preds = %21
%26 = load i32*, i32** %3
%27 = load i32, i32* %6
%28 = sub nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %26, i64 %29
%31 = load i32, i32* %30
%32 = load i32*, i32** %3
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i32, i32* %32, i64 %34
%36 = load i32, i32* %35
%37 = icmp sgt i32 %31, %36
br i1 %37, label %38, label %49
38: ; preds = %25
%39 = load i32*, i32** %3
%40 = load i32, i32* %6
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32, i32* %39, i64 %41
%43 = load i32*, i32** %3
%44 = load i32, i32* %6
%45 = sub nsw i32 %44, 1
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i32, i32* %43, i64 %46
%48 = call i32 @swap(i32* %42, i32* %47)
store i32 1, i32* %8
br label %49
49: ; preds = %38, %25
br label %50
50: ; preds = %49
%51 = load i32, i32* %6
%52 = add nsw i32 %51, -1
store i32 %52, i32* %6
br label %21
53: ; preds = %21
br label %54
54: ; preds = %53
%55 = load i32, i32* %5
%56 = add nsw i32 %55, 1
store i32 %56, i32* %5
br label %9
57: ; preds = %16
ret void
}

LLVM IR (-O2):
define void @bubble_sort(i32* %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %35
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 = %4, %30
%10 = phi i64 [ 0, %4 ], [ %31, %30 ]
%11 = icmp ult i64 %10, %8
br i1 %11, label %12, label %35
12: ; preds = %9, %26
%13 = phi i64 [ %28, %26 ], [ %7, %9 ]
%14 = phi i32 [ %27, %26 ], [ 0, %9 ]
%15 = phi i32 [ %16, %26 ], [ %1, %9 ]
%16 = trunc i64 %13 to i32
%17 = add nsw i32 %15, -2
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i32, i32* %0, i64 %18
%20 = load i32, i32* %19
%21 = getelementptr inbounds i32, i32* %0, i64 %13
%22 = load i32, i32* %21
%23 = icmp sgt i32 %20, %22
br i1 %23, label %24, label %26
24: ; preds = %12
%25 = tail call i32 @swap(i32* %21, i32* %19)
br label %26
26: ; preds = %12, %24
%27 = phi i32 [ 1, %24 ], [ %14, %12 ]
%28 = add nsw i64 %13, -1
%29 = icmp sgt i64 %28, %10
br i1 %29, label %12, label %30
30: ; preds = %26
%31 = add nuw nsw i64 %10, 1
%32 = icmp ult i64 %31, %6
%33 = icmp ne i32 %27, 0
%34 = select i1 %32, i1 %33, i1 false
br i1 %34, label %9, label %35
35: ; preds = %9, %30, %2
ret void
}