#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 *pt, unsigned int elem_num) {
  int i, sorted;
  int *ptmp = pt;
  do {
    sorted = 1;
    ptmp = pt;
    for (i = 0; i < (elem_num - 1); i++) {
      if (*ptmp > *(ptmp + 1)) {
        swap(ptmp, (ptmp + 1));
        sorted = 0;
      }
      ptmp++;
    }
  } while (sorted != 1);
}


x86 assembly (clang, -O0):
<bubble_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jae    1193 <bubble_sort+0x83>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    -0x20(%rbp),%rcx
cmp    0x4(%rcx),%eax
jle    1179 <bubble_sort+0x69>
mov    -0x20(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x4,%rsi
call   1030 <swap@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x20(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    113d <bubble_sort+0x2d>
jmp    1198 <bubble_sort+0x88>
cmpl   $0x1,-0x14(%rbp)
jne    1127 <bubble_sort+0x17>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bubble_sort>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
add    $0xffffffff,%r15d
je     1167 <bubble_sort+0x57>
mov    %rdi,%r14
xor    %ebp,%ebp
mov    $0x1,%eax
nopw   0x0(%rax,%rax,1)
mov    (%rdi),%ecx
lea    0x4(%rdi),%rbx
cmp    0x4(%rdi),%ecx
jle    1145 <bubble_sort+0x35>
mov    %rbx,%rsi
call   1030 <swap@plt>
xor    %eax,%eax
add    $0x1,%ebp
mov    %rbx,%rdi
cmp    %r15d,%ebp
jne    1130 <bubble_sort+0x20>
cmp    $0x1,%eax
je     1167 <bubble_sort+0x57>
mov    %r14,%rdi
mov    $0x1,%eax
mov    $0x0,%ebp
test   %r15d,%r15d
jne    1130 <bubble_sort+0x20>
add    $0x8,%rsp
pop    %rbx
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*
store i32* %0, i32** %3
store i32 %1, i32* %4
%8 = load i32*, i32** %3
store i32* %8, i32** %7
br label %9
9: ; preds = %35, %2
store i32 1, i32* %6
%10 = load i32*, i32** %3
store i32* %10, i32** %7
store i32 0, i32* %5
br label %11
11: ; preds = %31, %9
%12 = load i32, i32* %5
%13 = load i32, i32* %4
%14 = sub i32 %13, 1
%15 = icmp ult i32 %12, %14
br i1 %15, label %16, label %34
16: ; preds = %11
%17 = load i32*, i32** %7
%18 = load i32, i32* %17
%19 = load i32*, i32** %7
%20 = getelementptr inbounds i32, i32* %19, i64 1
%21 = load i32, i32* %20
%22 = icmp sgt i32 %18, %21
br i1 %22, label %23, label %28
23: ; preds = %16
%24 = load i32*, i32** %7
%25 = load i32*, i32** %7
%26 = getelementptr inbounds i32, i32* %25, i64 1
%27 = call i32 @swap(i32* %24, i32* %26)
store i32 0, i32* %6
br label %28
28: ; preds = %23, %16
%29 = load i32*, i32** %7
%30 = getelementptr inbounds i32, i32* %29, i32 1
store i32* %30, i32** %7
br label %31
31: ; preds = %28
%32 = load i32, i32* %5
%33 = add nsw i32 %32, 1
store i32 %33, i32* %5
br label %11
34: ; preds = %11
br label %35
35: ; preds = %34
%36 = load i32, i32* %6
%37 = icmp ne i32 %36, 1
br i1 %37, label %9, label %38
38: ; preds = %35
ret void
}

LLVM IR (-O2):
define void @bubble_sort(i32* %0, i32 %1) {
%3 = add i32 %1, -1
%4 = icmp eq i32 %3, 0
br i1 %4, label %26, label %5
5: ; preds = %2, %19
%6 = phi i32* [ %20, %19 ], [ %0, %2 ]
%7 = phi i32 [ %21, %19 ], [ 1, %2 ]
%8 = phi i32 [ %22, %19 ], [ 0, %2 ]
%9 = load i32, i32* %6
%10 = getelementptr inbounds i32, i32* %6, i64 1
%11 = load i32, i32* %10
%12 = icmp sgt i32 %9, %11
br i1 %12, label %13, label %15
13: ; preds = %5
%14 = tail call i32 @swap(i32* %6, i32* %10)
br label %15
15: ; preds = %13, %5
%16 = phi i32 [ 0, %13 ], [ %7, %5 ]
%17 = add nuw i32 %8, 1
%18 = icmp eq i32 %17, %3
br i1 %18, label %23, label %19
19: ; preds = %15, %23
%20 = phi i32* [ %10, %15 ], [ %0, %23 ]
%21 = phi i32 [ %16, %15 ], [ 1, %23 ]
%22 = phi i32 [ %17, %15 ], [ 0, %23 ]
br label %5
23: ; preds = %15
%24 = icmp eq i32 %16, 1
%25 = or i1 %24, %4
br i1 %25, label %26, label %19
26: ; preds = %23, %2
ret void
}