#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 */
double ComputeDJ(int, int);
int GetMedoid(int);
int GetNoOfCluster();
int /*<<< orphan*/ SwapCentroid(int, int);
int LloydsUpdate(double (*assign)(), int *confNums) {
  int i = 0;
  int medoid = 0;
  int flag = 0;
  static double oldJ = -1;
  double bestDJ = 0;
  for (i = 0; i < GetNoOfCluster(); i++) {
    medoid = GetMedoid(i);
    double dj = ComputeDJ(i, medoid);
    if (dj < bestDJ) {
      SwapCentroid(medoid, i);
      double j = assign(confNums[1], confNums[2]);
      if (oldJ != -1 && j > oldJ)
        return 0;
      oldJ = j;
      bestDJ = dj;
      flag = 1;
    }
  }
  return flag;
}


x86 assembly (clang, -O0):
<LloydsUpdate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    $0x0,%al
call   1060 <GetNoOfCluster@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
cmp    %ecx,%eax
jge    1258 <LloydsUpdate+0x118>
mov    -0x1c(%rbp),%edi
call   1030 <GetMedoid@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1040 <ComputeDJ@plt>
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm1
movsd  -0x30(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1245 <LloydsUpdate+0x105>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1050 <SwapCentroid@plt>
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%esi
mov    $0x0,%al
call   *%rcx
movsd  %xmm0,-0x40(%rbp)
movsd  0x2e55(%rip),%xmm0
movsd  0xe0d(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    1208 <LloydsUpdate+0xc8>
jp     1208 <LloydsUpdate+0xc8>
jmp    1227 <LloydsUpdate+0xe7>
movsd  -0x40(%rbp),%xmm0
ucomisd 0x2e2b(%rip),%xmm0
jbe    1227 <LloydsUpdate+0xe7>
movl   $0x0,-0x4(%rbp)
jmp    125e <LloydsUpdate+0x11e>
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,0x2e0c(%rip)
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    124a <LloydsUpdate+0x10a>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1174 <LloydsUpdate+0x34>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<LloydsUpdate>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
xor    %r12d,%r12d
xor    %eax,%eax
call   1060 <GetNoOfCluster@plt>
test   %eax,%eax
jle    11f7 <LloydsUpdate+0xb7>
xorpd  %xmm0,%xmm0
movsd  %xmm0,0x8(%rsp)
xor    %ebp,%ebp
xor    %r12d,%r12d
jmp    119c <LloydsUpdate+0x5c>
cs nopw 0x0(%rax,%rax,1)
nop
movsd  %xmm0,0x2eb8(%rip)
mov    $0x1,%r12d
add    $0x1,%ebp
xor    %eax,%eax
call   1060 <GetNoOfCluster@plt>
cmp    %eax,%ebp
jge    11f7 <LloydsUpdate+0xb7>
mov    %ebp,%edi
call   1030 <GetMedoid@plt>
mov    %eax,%ebx
mov    %ebp,%edi
mov    %eax,%esi
call   1040 <ComputeDJ@plt>
movapd %xmm0,%xmm1
movsd  0x8(%rsp),%xmm0
ucomisd %xmm1,%xmm0
jbe    118e <LloydsUpdate+0x4e>
movsd  %xmm1,0x8(%rsp)
mov    %ebx,%edi
mov    %ebp,%esi
call   1050 <SwapCentroid@plt>
mov    0x4(%r15),%edi
mov    0x8(%r15),%esi
xor    %r12d,%r12d
xor    %eax,%eax
call   *%r14
movsd  0x2e5b(%rip),%xmm1
ucomisd 0xe13(%rip),%xmm1
jne    11f1 <LloydsUpdate+0xb1>
jnp    1180 <LloydsUpdate+0x40>
ucomisd %xmm1,%xmm0
jbe    1180 <LloydsUpdate+0x40>
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@LloydsUpdate.oldJ = internal global double -1.000000e+00
define i32 @LloydsUpdate(double (...)* %0, i32* %1) {
%3 = alloca i32
%4 = alloca double (...)*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca double
%10 = alloca double
%11 = alloca double
store double (...)* %0, double (...)** %4
store i32* %1, i32** %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
store double 0.000000e+00, double* %9
store i32 0, i32* %6
br label %12
12: ; preds = %49, %2
%13 = load i32, i32* %6
%14 = call i32 (...) @GetNoOfCluster()
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %52
16: ; preds = %12
%17 = load i32, i32* %6
%18 = call i32 @GetMedoid(i32 %17)
store i32 %18, i32* %7
%19 = load i32, i32* %6
%20 = load i32, i32* %7
%21 = call double @ComputeDJ(i32 %19, i32 %20)
store double %21, double* %10
%22 = load double, double* %10
%23 = load double, double* %9
%24 = fcmp olt double %22, %23
br i1 %24, label %25, label %48
25: ; preds = %16
%26 = load i32, i32* %7
%27 = load i32, i32* %6
%28 = call i32 @SwapCentroid(i32 %26, i32 %27)
%29 = load double (...)*, double (...)** %4
%30 = load i32*, i32** %5
%31 = getelementptr inbounds i32, i32* %30, i64 1
%32 = load i32, i32* %31
%33 = load i32*, i32** %5
%34 = getelementptr inbounds i32, i32* %33, i64 2
%35 = load i32, i32* %34
%36 = bitcast double (...)* %29 to double (i32, i32, ...)*
%37 = call double (i32, i32, ...) %36(i32 %32, i32 %35)
store double %37, double* %11
%38 = load double, double* @LloydsUpdate.oldJ
%39 = fcmp une double %38, -1.000000e+00
br i1 %39, label %40, label %45
40: ; preds = %25
%41 = load double, double* %11
%42 = load double, double* @LloydsUpdate.oldJ
%43 = fcmp ogt double %41, %42
br i1 %43, label %44, label %45
44: ; preds = %40
store i32 0, i32* %3
br label %54
45: ; preds = %40, %25
%46 = load double, double* %11
store double %46, double* @LloydsUpdate.oldJ
%47 = load double, double* %10
store double %47, double* %9
store i32 1, i32* %8
br label %48
48: ; preds = %45, %16
br label %49
49: ; preds = %48
%50 = load i32, i32* %6
%51 = add nsw i32 %50, 1
store i32 %51, i32* %6
br label %12
52: ; preds = %12
%53 = load i32, i32* %8
store i32 %53, i32* %3
br label %54
54: ; preds = %52, %44
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@LloydsUpdate.oldJ = internal unnamed_addr global double -1.000000e+00
define i32 @LloydsUpdate(double (...)* nocapture %0, i32* nocapture %1) {
%3 = tail call i32 (...) @GetNoOfCluster()
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %32
5: ; preds = %2
%6 = getelementptr inbounds i32, i32* %1, i64 1
%7 = getelementptr inbounds i32, i32* %1, i64 2
%8 = bitcast double (...)* %0 to double (i32, i32, ...)*
br label %9
9: ; preds = %5, %26
%10 = phi i32 [ 0, %5 ], [ %29, %26 ]
%11 = phi double [ 0.000000e+00, %5 ], [ %28, %26 ]
%12 = phi i32 [ 0, %5 ], [ %27, %26 ]
%13 = tail call i32 @GetMedoid(i32 %10)
%14 = tail call double @ComputeDJ(i32 %10, i32 %13)
%15 = fcmp olt double %14, %11
br i1 %15, label %16, label %26
16: ; preds = %9
%17 = tail call i32 @SwapCentroid(i32 %13, i32 %10)
%18 = load i32, i32* %6
%19 = load i32, i32* %7
%20 = tail call double (i32, i32, ...) %8(i32 %18, i32 %19)
%21 = load double, double* @LloydsUpdate.oldJ
%22 = fcmp une double %21, -1.000000e+00
%23 = fcmp ogt double %20, %21
%24 = select i1 %22, i1 %23, i1 false
br i1 %24, label %32, label %25
25: ; preds = %16
store double %20, double* @LloydsUpdate.oldJ
br label %26
26: ; preds = %25, %9
%27 = phi i32 [ 1, %25 ], [ %12, %9 ]
%28 = phi double [ %14, %25 ], [ %11, %9 ]
%29 = add nuw nsw i32 %10, 1
%30 = tail call i32 (...) @GetNoOfCluster()
%31 = icmp slt i32 %29, %30
br i1 %31, label %9, label %32
32: ; preds = %26, %16, %2
%33 = phi i32 [ 0, %2 ], [ 0, %16 ], [ %27, %26 ]
ret i32 %33
}