#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 */
typedef int uint64_t;
struct timeval {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ bubble_sort(int *, int);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int);
int usec_diff(struct timeval);
int main() {
  struct timeval start_time;
  int array_length = 10000;
  int *arr;
  int i, j;
  uint64_t msec_time = 0;
  arr = (int *)malloc(sizeof(int) * array_length);
  memset(arr, 0, array_length);
  for (i = 0, j = array_length; i < array_length && j > 0; i++, j--) {
    arr[i] = j;
  }
  gettimeofday(&start_time, NULL);
  bubble_sort(arr, array_length);
  msec_time = usec_diff(start_time) / 1000;
  printf("Worst case time in ms is %d\n", msec_time);
  free(arr);
  arr = (int *)malloc(sizeof(int) * array_length);
  memset(arr, 0, array_length);
  gettimeofday(&start_time, NULL);
  bubble_sort(arr, array_length);
  msec_time = usec_diff(start_time) / 1000;
  printf("Best case time in ms is %d\n", msec_time);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x2710,-0xc(%rbp)
movl   $0x0,-0x24(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%ecx
xor    %eax,%eax
cmp    -0xc(%rbp),%ecx
mov    %al,-0x25(%rbp)
jge    11d6 <main+0x66>
cmpl   $0x0,-0x20(%rbp)
setg   %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    11e6 <main+0x76>
jmp    120b <main+0x9b>
mov    -0x20(%rbp),%edx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
jmp    11bb <main+0x4b>
lea    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1090 <bubble_sort@plt>
mov    -0x8(%rbp),%edi
call   1080 <usec_diff@plt>
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xdbf(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
lea    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1090 <bubble_sort@plt>
mov    -0x8(%rbp),%edi
call   1080 <usec_diff@plt>
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xd77(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x9c40,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x2710,%edx
call   1060 <memset@plt>
movdqa 0xe68(%rip),%xmm0
mov    $0x24,%eax
movdqa 0xe6a(%rip),%xmm8
movdqa 0xe71(%rip),%xmm9
movdqa 0xe78(%rip),%xmm10
movdqa 0xe7f(%rip),%xmm11
movdqa 0xe86(%rip),%xmm12
movdqa 0xe8e(%rip),%xmm6
movdqa 0xe96(%rip),%xmm7
movdqa 0xe9e(%rip),%xmm1
movdqa 0xea6(%rip),%xmm2
movdqa 0xeae(%rip),%xmm3
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movdqa %xmm0,%xmm4
paddd  %xmm8,%xmm4
movdqu %xmm0,-0x90(%rbx,%rax,4)
ff ff
movdqu %xmm4,-0x80(%rbx,%rax,4)
movdqa %xmm0,%xmm4
paddd  %xmm9,%xmm4
movdqa %xmm0,%xmm5
paddd  %xmm10,%xmm5
movdqu %xmm4,-0x70(%rbx,%rax,4)
movdqu %xmm5,-0x60(%rbx,%rax,4)
movdqa %xmm0,%xmm4
paddd  %xmm11,%xmm4
movdqa %xmm0,%xmm5
paddd  %xmm12,%xmm5
movdqu %xmm4,-0x50(%rbx,%rax,4)
movdqu %xmm5,-0x40(%rbx,%rax,4)
movdqa %xmm0,%xmm4
paddd  %xmm6,%xmm4
movdqa %xmm0,%xmm5
paddd  %xmm7,%xmm5
movdqu %xmm4,-0x30(%rbx,%rax,4)
movdqu %xmm5,-0x20(%rbx,%rax,4)
movdqa %xmm0,%xmm4
paddd  %xmm1,%xmm4
movdqa %xmm0,%xmm5
paddd  %xmm2,%xmm5
movdqu %xmm4,-0x10(%rbx,%rax,4)
movdqu %xmm5,(%rbx,%rax,4)
paddd  %xmm3,%xmm0
add    $0x28,%rax
cmp    $0x2734,%rax
jne    1200 <main+0x90>
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
mov    %rbx,%rdi
mov    $0x2710,%esi
call   1090 <bubble_sort@plt>
mov    (%rsp),%edi
call   1080 <usec_diff@plt>
cltq
imul   $0x10624dd3,%rax,%rsi
mov    %rsi,%rax
shr    $0x3f,%rax
sar    $0x26,%rsi
add    %eax,%esi
lea    0xdd2(%rip),%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x9c40,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x2710,%edx
call   1060 <memset@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
mov    %rbx,%rdi
mov    $0x2710,%esi
call   1090 <bubble_sort@plt>
mov    (%rsp),%edi
call   1080 <usec_diff@plt>
cltq
imul   $0x10624dd3,%rax,%rsi
mov    %rsi,%rax
shr    $0x3f,%rax
sar    $0x26,%rsi
add    %eax,%esi
lea    0xd8a(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
@.str = [29 x i8] c"Worst case time in ms is %d\0A\00"
@.str.1 = [28 x i8] c"Best case time in ms is %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
store i32 10000, i32* %3
store i32 0, i32* %7
%8 = load i32, i32* %3
%9 = sext i32 %8 to i64
%10 = mul i64 4, %9
%11 = trunc i64 %10 to i32
%12 = call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i32*
store i32* %13, i32** %4
%14 = load i32*, i32** %4
%15 = load i32, i32* %3
%16 = call i32 @memset(i32* %14, i32 0, i32 %15)
store i32 0, i32* %5
%17 = load i32, i32* %3
store i32 %17, i32* %6
br label %18
18: ; preds = %33, %0
%19 = load i32, i32* %5
%20 = load i32, i32* %3
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32, i32* %6
%24 = icmp sgt i32 %23, 0
br label %25
25: ; preds = %22, %18
%26 = phi i1 [ false, %18 ], [ %24, %22 ]
br i1 %26, label %27, label %38
27: ; preds = %25
%28 = load i32, i32* %6
%29 = load i32*, i32** %4
%30 = load i32, i32* %5
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %29, i64 %31
store i32 %28, i32* %32
br label %33
33: ; preds = %27
%34 = load i32, i32* %5
%35 = add nsw i32 %34, 1
store i32 %35, i32* %5
%36 = load i32, i32* %6
%37 = add nsw i32 %36, -1
store i32 %37, i32* %6
br label %18
38: ; preds = %25
%39 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%40 = load i32*, i32** %4
%41 = load i32, i32* %3
%42 = call i32 @bubble_sort(i32* %40, i32 %41)
%43 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i32 @usec_diff(i32 %44)
%46 = sdiv i32 %45, 1000
store i32 %46, i32* %7
%47 = load i32, i32* %7
%48 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %47)
%49 = load i32*, i32** %4
%50 = call i32 @free(i32* %49)
%51 = load i32, i32* %3
%52 = sext i32 %51 to i64
%53 = mul i64 4, %52
%54 = trunc i64 %53 to i32
%55 = call i64 @malloc(i32 %54)
%56 = inttoptr i64 %55 to i32*
store i32* %56, i32** %4
%57 = load i32*, i32** %4
%58 = load i32, i32* %3
%59 = call i32 @memset(i32* %57, i32 0, i32 %58)
%60 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%61 = load i32*, i32** %4
%62 = load i32, i32* %3
%63 = call i32 @bubble_sort(i32* %61, i32 %62)
%64 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
%65 = load i32, i32* %64
%66 = call i32 @usec_diff(i32 %65)
%67 = sdiv i32 %66, 1000
store i32 %67, i32* %7
%68 = load i32, i32* %7
%69 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %68)
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@.str = [29 x i8] c"Worst case time in ms is %d\0A\00"
@.str.1 = [28 x i8] c"Best case time in ms is %d\0A\00"
define i32 @main() {
%1 = alloca %struct.timeval
%2 = bitcast %struct.timeval* %1 to i8*
%3 = tail call i64 @malloc(i32 40000)
%4 = inttoptr i64 %3 to i32*
%5 = tail call i32 @memset(i32* %4, i32 0, i32 10000)
br label %6
6: ; preds = %6, %0
%7 = phi i64 [ 0, %0 ], [ %42, %6 ]
%8 = phi <4 x i32> [ <i32 10000, i32 9999, i32 9998, i32 9997>, %0 ], [ %43, %6 ]
%9 = add <4 x i32> %8, <i32 -4, i32 -4, i32 -4, i32 -4>
%10 = getelementptr inbounds i32, i32* %4, i64 %7
%11 = bitcast i32* %10 to <4 x i32>*
store <4 x i32> %8, <4 x i32>* %11
%12 = getelementptr inbounds i32, i32* %10, i64 4
%13 = bitcast i32* %12 to <4 x i32>*
store <4 x i32> %9, <4 x i32>* %13
%14 = add nuw nsw i64 %7, 8
%15 = add <4 x i32> %8, <i32 -8, i32 -8, i32 -8, i32 -8>
%16 = add <4 x i32> %8, <i32 -12, i32 -12, i32 -12, i32 -12>
%17 = getelementptr inbounds i32, i32* %4, i64 %14
%18 = bitcast i32* %17 to <4 x i32>*
store <4 x i32> %15, <4 x i32>* %18
%19 = getelementptr inbounds i32, i32* %17, i64 4
%20 = bitcast i32* %19 to <4 x i32>*
store <4 x i32> %16, <4 x i32>* %20
%21 = add nuw nsw i64 %7, 16
%22 = add <4 x i32> %8, <i32 -16, i32 -16, i32 -16, i32 -16>
%23 = add <4 x i32> %8, <i32 -20, i32 -20, i32 -20, i32 -20>
%24 = getelementptr inbounds i32, i32* %4, i64 %21
%25 = bitcast i32* %24 to <4 x i32>*
store <4 x i32> %22, <4 x i32>* %25
%26 = getelementptr inbounds i32, i32* %24, i64 4
%27 = bitcast i32* %26 to <4 x i32>*
store <4 x i32> %23, <4 x i32>* %27
%28 = add nuw nsw i64 %7, 24
%29 = add <4 x i32> %8, <i32 -24, i32 -24, i32 -24, i32 -24>
%30 = add <4 x i32> %8, <i32 -28, i32 -28, i32 -28, i32 -28>
%31 = getelementptr inbounds i32, i32* %4, i64 %28
%32 = bitcast i32* %31 to <4 x i32>*
store <4 x i32> %29, <4 x i32>* %32
%33 = getelementptr inbounds i32, i32* %31, i64 4
%34 = bitcast i32* %33 to <4 x i32>*
store <4 x i32> %30, <4 x i32>* %34
%35 = add nuw nsw i64 %7, 32
%36 = add <4 x i32> %8, <i32 -32, i32 -32, i32 -32, i32 -32>
%37 = add <4 x i32> %8, <i32 -36, i32 -36, i32 -36, i32 -36>
%38 = getelementptr inbounds i32, i32* %4, i64 %35
%39 = bitcast i32* %38 to <4 x i32>*
store <4 x i32> %36, <4 x i32>* %39
%40 = getelementptr inbounds i32, i32* %38, i64 4
%41 = bitcast i32* %40 to <4 x i32>*
store <4 x i32> %37, <4 x i32>* %41
%42 = add nuw nsw i64 %7, 40
%43 = add <4 x i32> %8, <i32 -40, i32 -40, i32 -40, i32 -40>
%44 = icmp eq i64 %42, 10000
br i1 %44, label %45, label %6
45: ; preds = %6
%46 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%47 = tail call i32 @bubble_sort(i32* %4, i32 10000)
%48 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%49 = load i32, i32* %48
%50 = tail call i32 @usec_diff(i32 %49)
%51 = sdiv i32 %50, 1000
%52 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %51)
%53 = tail call i32 @free(i32* %4)
%54 = tail call i64 @malloc(i32 40000)
%55 = inttoptr i64 %54 to i32*
%56 = tail call i32 @memset(i32* %55, i32 0, i32 10000)
%57 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%58 = tail call i32 @bubble_sort(i32* %55, i32 10000)
%59 = load i32, i32* %48
%60 = tail call i32 @usec_diff(i32 %59)
%61 = sdiv i32 %60, 1000
%62 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %61)
ret i32 0
}