#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*/ assign_class_id_compute_total_cost(int, int, double **,
                                                      int *, int *, double *);
int /*<<< orphan*/ classify_by_EDM(int, int, double **, int *, int *, int *);
int /*<<< orphan*/ srswor(int, int, int *);
void assign_class_by_k_medoids(int N_X, int K, double **EDM, int *center_id,
                               int *class_id) {
  int max_kmed_iter = 100;
  int i, n_X, k;
  int new_center_id[K], new_class_id[N_X];
  double total_cost = 0.0, new_total_cost = 0.0;
  srswor(N_X, K, center_id);
  assign_class_id_compute_total_cost(N_X, K, EDM, center_id, class_id,
                                     &total_cost);
  for (i = 0; i < max_kmed_iter; i++) {
    classify_by_EDM(N_X, K, EDM, center_id, class_id, new_center_id);
    assign_class_id_compute_total_cost(N_X, K, EDM, new_center_id, new_class_id,
                                       &new_total_cost);
    if (new_total_cost < total_cost) {
      total_cost = new_total_cost;
      for (k = 0; k < K; k++) {
        center_id[k] = new_center_id[k];
      }
      for (n_X = 0; n_X < N_X; n_X++) {
        class_id[n_X] = new_class_id[n_X];
      }
    } else {
      break;
    }
  }
  for (n_X = 0; n_X < N_X; n_X++) {
    for (k = 0; k < K; k++) {
      if (class_id[n_X] == center_id[k]) {
        class_id[n_X] = k;
        break;
      }
    }
  }
}


x86 assembly (clang, -O0):
<assign_class_by_k_medoids>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
movl   $0x64,-0x24(%rbp)
mov    -0x8(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x4(%rbp),%eax
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x48(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x50(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x58(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1030 <srswor@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    -0x50(%rbp),%r9
call   1050 <assign_class_id_compute_total_cost@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    12c4 <assign_class_by_k_medoids+0x194>
mov    -0x68(%rbp),%r9
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
call   1040 <classify_by_EDM@plt>
mov    -0x68(%rbp),%rcx
mov    -0x60(%rbp),%r8
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
lea    -0x58(%rbp),%r9
call   1050 <assign_class_id_compute_total_cost@plt>
movsd  -0x58(%rbp),%xmm1
movsd  -0x50(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    12ac <assign_class_by_k_medoids+0x17c>
movsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1270 <assign_class_by_k_medoids+0x140>
mov    -0x68(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1240 <assign_class_by_k_medoids+0x110>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    12a7 <assign_class_by_k_medoids+0x177>
mov    -0x60(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1277 <assign_class_by_k_medoids+0x147>
jmp    12b1 <assign_class_by_k_medoids+0x181>
jmp    12c4 <assign_class_by_k_medoids+0x194>
jmp    12b6 <assign_class_by_k_medoids+0x186>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11d9 <assign_class_by_k_medoids+0xa9>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    133f <assign_class_by_k_medoids+0x20f>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    132c <assign_class_by_k_medoids+0x1fc>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x18(%rbp),%rcx
movslq -0x30(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jne    1319 <assign_class_by_k_medoids+0x1e9>
mov    -0x30(%rbp),%edx
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    132c <assign_class_by_k_medoids+0x1fc>
jmp    131e <assign_class_by_k_medoids+0x1ee>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    12de <assign_class_by_k_medoids+0x1ae>
jmp    1331 <assign_class_by_k_medoids+0x201>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    12cb <assign_class_by_k_medoids+0x19b>
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<assign_class_by_k_medoids>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %r8,-0x38(%rbp)
mov    %rcx,%rbx
mov    %rdx,-0x50(%rbp)
mov    %esi,%r15d
mov    %edi,%r13d
mov    %esi,%r14d
mov    %rsp,%rcx
lea    0xf(,%r14,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %edi,%r12d
mov    %rsp,%rcx
lea    0xf(,%r12,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x68(%rbp)
mov    %rbx,%rdx
call   1030 <srswor@plt>
lea    -0x40(%rbp),%r9
mov    %r13d,-0x2c(%rbp)
mov    %r13d,%edi
mov    %r15d,%esi
mov    -0x50(%rbp),%rdx
mov    %rbx,%rcx
mov    -0x38(%rbp),%r8
call   1060 <assign_class_id_compute_total_cost@plt>
mov    %r14,-0x70(%rbp)
lea    0x0(,%r14,4),%rax
mov    %rax,-0x88(%rbp)
mov    %r12,-0x78(%rbp)
lea    0x0(,%r12,4),%rax
mov    %rax,-0x80(%rbp)
movl   $0x64,-0x44(%rbp)
mov    -0x50(%rbp),%r13
mov    -0x60(%rbp),%r14
jmp    121a <assign_class_by_k_medoids+0xda>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
addl   $0xffffffff,-0x44(%rbp)
je     129e <assign_class_by_k_medoids+0x15e>
mov    %r15d,%r12d
mov    -0x2c(%rbp),%r15d
mov    %r15d,%edi
mov    %r12d,%esi
mov    %r13,%rdx
mov    %rbx,%rcx
mov    -0x38(%rbp),%r8
mov    %r14,%r9
call   1040 <classify_by_EDM@plt>
mov    %r15d,%edi
mov    %r12d,%r15d
mov    %r12d,%esi
mov    %r13,%rdx
mov    %r14,%rcx
mov    -0x58(%rbp),%r8
lea    -0x68(%rbp),%r9
call   1060 <assign_class_id_compute_total_cost@plt>
movsd  -0x68(%rbp),%xmm0
movsd  -0x40(%rbp),%xmm1
ucomisd %xmm0,%xmm1
jbe    129e <assign_class_by_k_medoids+0x15e>
movsd  %xmm0,-0x40(%rbp)
test   %r15d,%r15d
jle    1282 <assign_class_by_k_medoids+0x142>
mov    %rbx,%rdi
mov    -0x60(%rbp),%rsi
mov    -0x88(%rbp),%rdx
call   1050 <memcpy@plt>
cmpl   $0x0,-0x2c(%rbp)
jle    1210 <assign_class_by_k_medoids+0xd0>
mov    -0x38(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    -0x80(%rbp),%rdx
call   1050 <memcpy@plt>
jmp    1210 <assign_class_by_k_medoids+0xd0>
cmpl   $0x0,-0x2c(%rbp)
mov    -0x78(%rbp),%rsi
mov    -0x70(%rbp),%rdi
jle    12e0 <assign_class_by_k_medoids+0x1a0>
xor    %eax,%eax
jmp    12c0 <assign_class_by_k_medoids+0x180>
mov    -0x38(%rbp),%rdx
mov    %ecx,(%rdx,%rax,4)
add    $0x1,%rax
cmp    %rsi,%rax
je     12e0 <assign_class_by_k_medoids+0x1a0>
test   %r15d,%r15d
jle    12b7 <assign_class_by_k_medoids+0x177>
mov    -0x38(%rbp),%rcx
mov    (%rcx,%rax,4),%edx
xor    %ecx,%ecx
xchg   %ax,%ax
cmp    (%rbx,%rcx,4),%edx
je     12b0 <assign_class_by_k_medoids+0x170>
add    $0x1,%rcx
cmp    %rcx,%rdi
jne    12d0 <assign_class_by_k_medoids+0x190>
jmp    12b7 <assign_class_by_k_medoids+0x177>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @assign_class_by_k_medoids(i32 %0, i32 %1, double** %2, i32* %3, i32* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca double**
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i8*
%16 = alloca i64
%17 = alloca i64
%18 = alloca double
%19 = alloca double
store i32 %0, i32* %6
store i32 %1, i32* %7
store double** %2, double*** %8
store i32* %3, i32** %9
store i32* %4, i32** %10
store i32 100, i32* %11
%20 = load i32, i32* %7
%21 = zext i32 %20 to i64
%22 = call i8* @llvm.stacksave()
store i8* %22, i8** %15
%23 = alloca i32, i64 %21
store i64 %21, i64* %16
%24 = load i32, i32* %6
%25 = zext i32 %24 to i64
%26 = alloca i32, i64 %25
store i64 %25, i64* %17
store double 0.000000e+00, double* %18
store double 0.000000e+00, double* %19
%27 = load i32, i32* %6
%28 = load i32, i32* %7
%29 = load i32*, i32** %9
%30 = call i32 @srswor(i32 %27, i32 %28, i32* %29)
%31 = load i32, i32* %6
%32 = load i32, i32* %7
%33 = load double**, double*** %8
%34 = load i32*, i32** %9
%35 = load i32*, i32** %10
%36 = call i32 @assign_class_id_compute_total_cost(i32 %31, i32 %32, double** %33, i32* %34, i32* %35, double* %18)
store i32 0, i32* %12
br label %37
37: ; preds = %93, %5
%38 = load i32, i32* %12
%39 = load i32, i32* %11
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %96
41: ; preds = %37
%42 = load i32, i32* %6
%43 = load i32, i32* %7
%44 = load double**, double*** %8
%45 = load i32*, i32** %9
%46 = load i32*, i32** %10
%47 = call i32 @classify_by_EDM(i32 %42, i32 %43, double** %44, i32* %45, i32* %46, i32* %23)
%48 = load i32, i32* %6
%49 = load i32, i32* %7
%50 = load double**, double*** %8
%51 = call i32 @assign_class_id_compute_total_cost(i32 %48, i32 %49, double** %50, i32* %23, i32* %26, double* %19)
%52 = load double, double* %19
%53 = load double, double* %18
%54 = fcmp olt double %52, %53
br i1 %54, label %55, label %91
55: ; preds = %41
%56 = load double, double* %19
store double %56, double* %18
store i32 0, i32* %14
br label %57
57: ; preds = %70, %55
%58 = load i32, i32* %14
%59 = load i32, i32* %7
%60 = icmp slt i32 %58, %59
br i1 %60, label %61, label %73
61: ; preds = %57
%62 = load i32, i32* %14
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i32, i32* %23, i64 %63
%65 = load i32, i32* %64
%66 = load i32*, i32** %9
%67 = load i32, i32* %14
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i32, i32* %66, i64 %68
store i32 %65, i32* %69
br label %70
70: ; preds = %61
%71 = load i32, i32* %14
%72 = add nsw i32 %71, 1
store i32 %72, i32* %14
br label %57
73: ; preds = %57
store i32 0, i32* %13
br label %74
74: ; preds = %87, %73
%75 = load i32, i32* %13
%76 = load i32, i32* %6
%77 = icmp slt i32 %75, %76
br i1 %77, label %78, label %90
78: ; preds = %74
%79 = load i32, i32* %13
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds i32, i32* %26, i64 %80
%82 = load i32, i32* %81
%83 = load i32*, i32** %10
%84 = load i32, i32* %13
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds i32, i32* %83, i64 %85
store i32 %82, i32* %86
br label %87
87: ; preds = %78
%88 = load i32, i32* %13
%89 = add nsw i32 %88, 1
store i32 %89, i32* %13
br label %74
90: ; preds = %74
br label %92
91: ; preds = %41
br label %96
92: ; preds = %90
br label %93
93: ; preds = %92
%94 = load i32, i32* %12
%95 = add nsw i32 %94, 1
store i32 %95, i32* %12
br label %37
96: ; preds = %91, %37
store i32 0, i32* %13
br label %97
97: ; preds = %129, %96
%98 = load i32, i32* %13
%99 = load i32, i32* %6
%100 = icmp slt i32 %98, %99
br i1 %100, label %101, label %132
101: ; preds = %97
store i32 0, i32* %14
br label %102
102: ; preds = %125, %101
%103 = load i32, i32* %14
%104 = load i32, i32* %7
%105 = icmp slt i32 %103, %104
br i1 %105, label %106, label %128
106: ; preds = %102
%107 = load i32*, i32** %10
%108 = load i32, i32* %13
%109 = sext i32 %108 to i64
%110 = getelementptr inbounds i32, i32* %107, i64 %109
%111 = load i32, i32* %110
%112 = load i32*, i32** %9
%113 = load i32, i32* %14
%114 = sext i32 %113 to i64
%115 = getelementptr inbounds i32, i32* %112, i64 %114
%116 = load i32, i32* %115
%117 = icmp eq i32 %111, %116
br i1 %117, label %118, label %124
118: ; preds = %106
%119 = load i32, i32* %14
%120 = load i32*, i32** %10
%121 = load i32, i32* %13
%122 = sext i32 %121 to i64
%123 = getelementptr inbounds i32, i32* %120, i64 %122
store i32 %119, i32* %123
br label %128
124: ; preds = %106
br label %125
125: ; preds = %124
%126 = load i32, i32* %14
%127 = add nsw i32 %126, 1
store i32 %127, i32* %14
br label %102
128: ; preds = %118, %102
br label %129
129: ; preds = %128
%130 = load i32, i32* %13
%131 = add nsw i32 %130, 1
store i32 %131, i32* %13
br label %97
132: ; preds = %97
%133 = load i8*, i8** %15
ret void
}

LLVM IR (-O2):
define void @assign_class_by_k_medoids(i32 %0, i32 %1, double** %2, i32* %3, i32* %4) {
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = alloca double
%9 = alloca double
%10 = zext i32 %1 to i64
%11 = alloca i32, i64 %10
%12 = bitcast i32* %11 to i8*
%13 = zext i32 %0 to i64
%14 = alloca i32, i64 %13
%15 = bitcast i32* %14 to i8*
%16 = bitcast double* %8 to i8*
store double 0.000000e+00, double* %8
%17 = bitcast double* %9 to i8*
store double 0.000000e+00, double* %9
%18 = tail call i32 @srswor(i32 %0, i32 %1, i32* %3)
%19 = call i32 @assign_class_id_compute_total_cost(i32 %0, i32 %1, double** %2, i32* %3, i32* %4, double* %8)
%20 = icmp sgt i32 %1, 0
%21 = icmp sgt i32 %0, 0
%22 = shl nuw nsw i64 %10, 2
%23 = shl nuw nsw i64 %13, 2
br label %24
24: ; preds = %5, %35
%25 = phi i32 [ 0, %5 ], [ %36, %35 ]
%26 = call i32 @classify_by_EDM(i32 %0, i32 %1, double** %2, i32* %3, i32* %4, i32* %11)
%27 = call i32 @assign_class_id_compute_total_cost(i32 %0, i32 %1, double** %2, i32* %11, i32* %14, double* %9)
%28 = load double, double* %9
%29 = load double, double* %8
%30 = fcmp olt double %28, %29
br i1 %30, label %31, label %38
31: ; preds = %24
store double %28, double* %8
br i1 %20, label %32, label %33
32: ; preds = %31
br label %33
33: ; preds = %32, %31
br i1 %21, label %34, label %35
34: ; preds = %33
br label %35
35: ; preds = %34, %33
%36 = add nuw nsw i32 %25, 1
%37 = icmp eq i32 %36, 100
br i1 %37, label %38, label %24
38: ; preds = %24, %35
br i1 %21, label %39, label %57
39: ; preds = %38, %54
%40 = phi i64 [ %55, %54 ], [ 0, %38 ]
br i1 %20, label %41, label %54
41: ; preds = %39
%42 = getelementptr inbounds i32, i32* %4, i64 %40
%43 = load i32, i32* %42
br label %44
44: ; preds = %41, %51
%45 = phi i64 [ 0, %41 ], [ %52, %51 ]
%46 = getelementptr inbounds i32, i32* %3, i64 %45
%47 = load i32, i32* %46
%48 = icmp eq i32 %43, %47
br i1 %48, label %49, label %51
49: ; preds = %44
%50 = trunc i64 %45 to i32
store i32 %50, i32* %42
br label %54
51: ; preds = %44
%52 = add nuw nsw i64 %45, 1
%53 = icmp eq i64 %52, %10
br i1 %53, label %54, label %44
54: ; preds = %51, %39, %49
%55 = add nuw nsw i64 %40, 1
%56 = icmp eq i64 %55, %13
br i1 %56, label %57, label %39
57: ; preds = %54, %38
ret void
}