#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 strcasecmp(char *, char *);
void bubble_sort(char *str[], int length) {
  int hasSwaped = 1, compare = 0;
  while (hasSwaped) {
    hasSwaped = 0;
    int i;
    for (i = 0; i < length - 1; i++) {
      char *temp;
      compare = strcasecmp(str[i], str[i + 1]);
      if (compare >= 0) {
        temp = str[i + 1];
        str[i + 1] = str[i];
        str[i] = temp;
        hasSwaped = 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,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     11e6 <bubble_sort+0xd6>
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11e1 <bubble_sort+0xd1>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rsi
call   1030 <strcasecmp@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11ce <bubble_sort+0xbe>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x20(%rbp),%rdx
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
movl   $0x1,-0x10(%rbp)
jmp    11d3 <bubble_sort+0xc3>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1145 <bubble_sort+0x35>
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    $0x18,%rsp
mov    %rsi,0x10(%rsp)
cmp    $0x2,%esi
jl     119e <bubble_sort+0x8e>
mov    %rdi,%r15
mov    0x10(%rsp),%rax
lea    -0x1(%rax),%r13d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r15),%r12
xor    %r14d,%r14d
movl   $0x0,0xc(%rsp)
jmp    1169 <bubble_sort+0x59>
mov    %r12,0x8(%r15,%r14,8)
mov    %rbx,(%r15,%r14,8)
movl   $0x1,0xc(%rsp)
mov    %rbp,%r14
cmp    %rbp,%r13
je     1190 <bubble_sort+0x80>
lea    0x1(%r14),%rbp
mov    0x8(%r15,%r14,8),%rbx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1030 <strcasecmp@plt>
test   %eax,%eax
jns    1150 <bubble_sort+0x40>
mov    %rbx,%r12
jmp    1161 <bubble_sort+0x51>
cs nopw 0x0(%rax,%rax,1)
cmpl   $0x2,0x10(%rsp)
jl     119e <bubble_sort+0x8e>
cmpl   $0x0,0xc(%rsp)
jne    1140 <bubble_sort+0x30>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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