#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 */
long CSPL_Sort_gnomesort(double *, long);
scalar_t__ calloc(long, int);
int /*<<< orphan*/ free(double *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ rand();
int main(void) {
  double *dat1;
  long i, j;
  long lengths[10] = {10, 50, 100, 200, 300, 400, 500, 700, 800, 1000};
  long nswaps[10];
  char *msg1 = "Better";
  char *msg2 = "Worse";
  char *msg;
  for (i = 0; i < 10; i++) {
    dat1 = (double *)calloc(lengths[i], sizeof(double));
    for (j = 0; j < lengths[i]; j++) {
      dat1[j] = ((double)rand()) / 12.34;
    }
    nswaps[i] = CSPL_Sort_gnomesort(dat1, lengths[i]);
    printf("Random array gnomesorted, %ld elements, %ld swaps\n", lengths[i],
           nswaps[i]);
    msg = msg2;
    if (lengths[i] * lengths[i] > nswaps[i])
      msg = msg1;
    printf("\tExpected is n**2 performance: it was %s\n", msg);
    free(dat1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x70(%rbp),%rdi
lea    0xe93(%rip),%rsi
mov    $0x50,%edx
call   1070 <memcpy@plt>
lea    0xed2(%rip),%rax
mov    %rax,-0xc8(%rbp)
lea    0xecb(%rip),%rax
mov    %rax,-0xd0(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0xa,-0x18(%rbp)
jge    12d2 <main+0x172>
mov    -0x18(%rbp),%rax
mov    -0x70(%rbp,%rax,8),%rdi
mov    $0x8,%esi
call   1060 <calloc@plt>
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmp    -0x70(%rbp,%rcx,8),%rax
jge    121e <main+0xbe>
mov    $0x0,%al
call   1080 <rand@plt>
cvtsi2sd %rax,%xmm0
movsd  0xe04(%rip),%xmm1
divsd  %xmm1,%xmm0
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11d5 <main+0x75>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    -0x70(%rbp,%rax,8),%rsi
call   1050 <CSPL_Sort_gnomesort@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,-0xc0(%rbp,%rax,8)
ff
mov    -0x18(%rbp),%rax
mov    -0x70(%rbp,%rax,8),%rsi
mov    -0x18(%rbp),%rax
mov    -0xc0(%rbp,%rax,8),%rdx
ff
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xd0(%rbp),%rax
mov    %rax,-0xd8(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x70(%rbp,%rax,8),%rax
mov    -0x18(%rbp),%rcx
imul   -0x70(%rbp,%rcx,8),%rax
mov    -0x18(%rbp),%rcx
cmp    -0xc0(%rbp,%rcx,8),%rax
ff
jle    12a3 <main+0x143>
mov    -0xc8(%rbp),%rax
mov    %rax,-0xd8(%rbp)
mov    -0xd8(%rbp),%rsi
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11ab <main+0x4b>
xor    %eax,%eax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %r14d,%r14d
xchg   %ax,%ax
lea    0xea9(%rip),%rax
mov    (%rax,%r14,8),%r12
mov    %r12,%rdi
mov    $0x8,%esi
call   1060 <calloc@plt>
mov    %rax,%r15
cmp    $0x2,%r12
mov    $0x1,%ebp
cmovge %r12,%rbp
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1070 <rand@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
divsd  0xe59(%rip),%xmm0
movsd  %xmm0,(%r15,%rbx,8)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1190 <main+0x40>
mov    %r15,%rdi
mov    %r12,%rsi
call   1050 <CSPL_Sort_gnomesort@plt>
mov    %rax,%r13
lea    0xea2(%rip),%rdi
mov    %r12,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
imul   %r12,%r12
cmp    %r13,%r12
lea    0xe81(%rip),%rsi
lea    0xe73(%rip),%rax
cmovg  %rax,%rsi
lea    0xea8(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
add    $0x1,%r14
cmp    $0xa,%r14
jne    1160 <main+0x10>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.lengths = [10 x i64] [i64 10, i64 50, i64 100, i64 200, i64 300, i64 400, i64 500, i64 700, i64 800, i64 1000]
@.str = [7 x i8] c"Better\00"
@.str.1 = [6 x i8] c"Worse\00"
@.str.2 = [51 x i8] c"Random array gnomesorted, %ld elements, %ld swaps\0A\00"
@.str.3 = [42 x i8] c"\09Expected is n**2 performance: it was %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca double*
%3 = alloca i64
%4 = alloca i64
%5 = alloca [10 x i64]
%6 = alloca [10 x i64]
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i32 0, i32* %1
%10 = bitcast [10 x i64]* %5 to i8*
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8** %7
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8** %8
store i64 0, i64* %3
br label %11
11: ; preds = %70, %0
%12 = load i64, i64* %3
%13 = icmp slt i64 %12, 10
br i1 %13, label %14, label %73
14: ; preds = %11
%15 = load i64, i64* %3
%16 = getelementptr inbounds [10 x i64], [10 x i64]* %5, i64 0, i64 %15
%17 = load i64, i64* %16
%18 = call i64 @calloc(i64 %17, i32 8)
%19 = inttoptr i64 %18 to double*
store double* %19, double** %2
store i64 0, i64* %4
br label %20
20: ; preds = %33, %14
%21 = load i64, i64* %4
%22 = load i64, i64* %3
%23 = getelementptr inbounds [10 x i64], [10 x i64]* %5, i64 0, i64 %22
%24 = load i64, i64* %23
%25 = icmp slt i64 %21, %24
br i1 %25, label %26, label %36
26: ; preds = %20
%27 = call i64 (...) @rand()
%28 = sitofp i64 %27 to double
%29 = fdiv double %28, 1.234000e+01
%30 = load double*, double** %2
%31 = load i64, i64* %4
%32 = getelementptr inbounds double, double* %30, i64 %31
store double %29, double* %32
br label %33
33: ; preds = %26
%34 = load i64, i64* %4
%35 = add nsw i64 %34, 1
store i64 %35, i64* %4
br label %20
36: ; preds = %20
%37 = load double*, double** %2
%38 = load i64, i64* %3
%39 = getelementptr inbounds [10 x i64], [10 x i64]* %5, i64 0, i64 %38
%40 = load i64, i64* %39
%41 = call i64 @CSPL_Sort_gnomesort(double* %37, i64 %40)
%42 = load i64, i64* %3
%43 = getelementptr inbounds [10 x i64], [10 x i64]* %6, i64 0, i64 %42
store i64 %41, i64* %43
%44 = load i64, i64* %3
%45 = getelementptr inbounds [10 x i64], [10 x i64]* %5, i64 0, i64 %44
%46 = load i64, i64* %45
%47 = load i64, i64* %3
%48 = getelementptr inbounds [10 x i64], [10 x i64]* %6, i64 0, i64 %47
%49 = load i64, i64* %48
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i64 %46, i64 %49)
%51 = load i8*, i8** %8
store i8* %51, i8** %9
%52 = load i64, i64* %3
%53 = getelementptr inbounds [10 x i64], [10 x i64]* %5, i64 0, i64 %52
%54 = load i64, i64* %53
%55 = load i64, i64* %3
%56 = getelementptr inbounds [10 x i64], [10 x i64]* %5, i64 0, i64 %55
%57 = load i64, i64* %56
%58 = mul nsw i64 %54, %57
%59 = load i64, i64* %3
%60 = getelementptr inbounds [10 x i64], [10 x i64]* %6, i64 0, i64 %59
%61 = load i64, i64* %60
%62 = icmp sgt i64 %58, %61
br i1 %62, label %63, label %65
63: ; preds = %36
%64 = load i8*, i8** %7
store i8* %64, i8** %9
br label %65
65: ; preds = %63, %36
%66 = load i8*, i8** %9
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i8* %66)
%68 = load double*, double** %2
%69 = call i32 @free(double* %68)
br label %70
70: ; preds = %65
%71 = load i64, i64* %3
%72 = add nsw i64 %71, 1
store i64 %72, i64* %3
br label %11
73: ; preds = %11
ret i32 0
}

LLVM IR (-O2):
@__const.main.lengths = [10 x i64] [i64 10, i64 50, i64 100, i64 200, i64 300, i64 400, i64 500, i64 700, i64 800, i64 1000]
@.str = [7 x i8] c"Better\00"
@.str.1 = [6 x i8] c"Worse\00"
@.str.2 = [51 x i8] c"Random array gnomesorted, %ld elements, %ld swaps\0A\00"
@.str.3 = [42 x i8] c"\09Expected is n**2 performance: it was %s\0A\00"
define i32 @main() {
br label %1
1: ; preds = %16, %0
%2 = phi i64 [ 0, %0 ], [ %24, %16 ]
%3 = getelementptr inbounds [10 x i64], [10 x i64]* @__const.main.lengths, i64 0, i64 %2
%4 = load i64, i64* %3
%5 = tail call i64 @calloc(i64 %4, i32 8)
%6 = inttoptr i64 %5 to double*
%7 = call i64 @llvm.smax.i64(i64 %4, i64 1)
br label %8
8: ; preds = %1, %8
%9 = phi i64 [ %14, %8 ], [ 0, %1 ]
%10 = tail call i64 (...) @rand()
%11 = sitofp i64 %10 to double
%12 = fdiv double %11, 1.234000e+01
%13 = getelementptr inbounds double, double* %6, i64 %9
store double %12, double* %13
%14 = add nuw nsw i64 %9, 1
%15 = icmp eq i64 %14, %7
br i1 %15, label %16, label %8
16: ; preds = %8
%17 = tail call i64 @CSPL_Sort_gnomesort(double* %6, i64 %4)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i64 %4, i64 %17)
%19 = mul nsw i64 %4, %4
%20 = icmp sgt i64 %19, %17
%21 = select i1 %20, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0)
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i8* %21)
%23 = tail call i32 @free(double* %6)
%24 = add nuw nsw i64 %2, 1
%25 = icmp eq i64 %24, 10
br i1 %25, label %26, label %1
26: ; preds = %16
ret i32 0
}