#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 closest_centroid(double *, double *, int, int);
int /*<<< orphan*/ gradient_step(double *, double *, int, int);
void minibatch_iteration(double *data, double *centroids, int *sample_indicies,
                         int *centroid_counts, int *cluster_cache,
                         int n_samples, int k, int N, int D) {
  int idx, cur_cluster;
  do {
    ;
  } while (0);
  for (int i = 0; i < n_samples; i++) {
    idx = sample_indicies[i];
    cluster_cache[i] = closest_centroid(data + idx * D, centroids, k, D);
  }
  do {
    ;
  } while (0);
  for (int i = 0; i < n_samples; i++) {
    idx = sample_indicies[i];
    cur_cluster = cluster_cache[i];
    centroid_counts[cur_cluster] += 1;
    gradient_step(data + idx * D, centroids + cur_cluster * D,
                  centroid_counts[cur_cluster], D);
  }
}


x86 assembly (clang, -O0):
<minibatch_iteration>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x20(%rbp),%eax
mov    0x18(%rbp),%eax
mov    0x10(%rbp),%eax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
jmp    114e <minibatch_iteration+0x2e>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    11ad <minibatch_iteration+0x8d>
mov    -0x18(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%eax
imul   0x20(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    0x10(%rbp),%edx
mov    0x20(%rbp),%ecx
call   1040 <closest_centroid@plt>
mov    %eax,%edx
mov    -0x28(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1155 <minibatch_iteration+0x35>
jmp    11b2 <minibatch_iteration+0x92>
jmp    11b7 <minibatch_iteration+0x97>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    1240 <minibatch_iteration+0x120>
mov    -0x18(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
add    $0x1,%edx
mov    %edx,(%rax,%rcx,4)
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%eax
imul   0x20(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x34(%rbp),%eax
imul   0x20(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rsi
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    0x20(%rbp),%ecx
call   1030 <gradient_step@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11be <minibatch_iteration+0x9e>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<minibatch_iteration>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %r9d,%r9d
jle    11e9 <minibatch_iteration+0xc9>
mov    %r9d,%ebp
mov    %rcx,0x10(%rsp)
mov    0x60(%rsp),%eax
mov    %r9d,%r9d
xor    %ebx,%ebx
movslq %eax,%rcx
mov    %r8,%r15
mov    %rdx,0x8(%rsp)
mov    %r9,(%rsp)
nopw   0x0(%rax,%rax,1)
movslq (%rdx,%rbx,4),%rax
imul   %rcx,%rax
mov    %rdi,%r14
lea    (%rdi,%rax,8),%rdi
mov    %rsi,%r12
mov    0x50(%rsp),%edx
mov    %rcx,%r13
call   1040 <closest_centroid@plt>
mov    %r13,%rcx
mov    (%rsp),%r9
mov    %r14,%rdi
mov    %r12,%rsi
mov    0x8(%rsp),%rdx
mov    %eax,(%r15,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%r9
jne    1160 <minibatch_iteration+0x40>
test   %ebp,%ebp
mov    0x10(%rsp),%rbx
jle    11e9 <minibatch_iteration+0xc9>
mov    %r15,%rbp
xor    %r15d,%r15d
nopl   0x0(%rax)
movslq (%rdx,%r15,4),%rax
movslq 0x0(%rbp,%r15,4),%rsi
mov    (%rbx,%rsi,4),%edx
add    $0x1,%edx
mov    %edx,(%rbx,%rsi,4)
imul   %rcx,%rax
lea    (%r14,%rax,8),%rdi
imul   %rcx,%rsi
lea    (%r12,%rsi,8),%rsi
call   1030 <gradient_step@plt>
mov    %r13,%rcx
mov    0x8(%rsp),%rdx
add    $0x1,%r15
cmp    %r15,(%rsp)
jne    11b0 <minibatch_iteration+0x90>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @minibatch_iteration(double* %0, double* %1, i32* %2, i32* %3, i32* %4, i32 %5, i32 %6, i32 %7, i32 %8) {
%10 = alloca double*
%11 = alloca double*
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
%21 = alloca i32
%22 = alloca i32
store double* %0, double** %10
store double* %1, double** %11
store i32* %2, i32** %12
store i32* %3, i32** %13
store i32* %4, i32** %14
store i32 %5, i32* %15
store i32 %6, i32* %16
store i32 %7, i32* %17
store i32 %8, i32* %18
br label %23
23: ; preds = %9
br label %24
24: ; preds = %23
store i32 0, i32* %21
br label %25
25: ; preds = %49, %24
%26 = load i32, i32* %21
%27 = load i32, i32* %15
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %52
29: ; preds = %25
%30 = load i32*, i32** %12
%31 = load i32, i32* %21
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i32, i32* %30, i64 %32
%34 = load i32, i32* %33
store i32 %34, i32* %19
%35 = load double*, double** %10
%36 = load i32, i32* %19
%37 = load i32, i32* %18
%38 = mul nsw i32 %36, %37
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds double, double* %35, i64 %39
%41 = load double*, double** %11
%42 = load i32, i32* %16
%43 = load i32, i32* %18
%44 = call i32 @closest_centroid(double* %40, double* %41, i32 %42, i32 %43)
%45 = load i32*, i32** %14
%46 = load i32, i32* %21
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %45, i64 %47
store i32 %44, i32* %48
br label %49
49: ; preds = %29
%50 = load i32, i32* %21
%51 = add nsw i32 %50, 1
store i32 %51, i32* %21
br label %25
52: ; preds = %25
br label %53
53: ; preds = %52
br label %54
54: ; preds = %53
store i32 0, i32* %22
br label %55
55: ; preds = %95, %54
%56 = load i32, i32* %22
%57 = load i32, i32* %15
%58 = icmp slt i32 %56, %57
br i1 %58, label %59, label %98
59: ; preds = %55
%60 = load i32*, i32** %12
%61 = load i32, i32* %22
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i32, i32* %60, i64 %62
%64 = load i32, i32* %63
store i32 %64, i32* %19
%65 = load i32*, i32** %14
%66 = load i32, i32* %22
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i32, i32* %65, i64 %67
%69 = load i32, i32* %68
store i32 %69, i32* %20
%70 = load i32*, i32** %13
%71 = load i32, i32* %20
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i32, i32* %70, i64 %72
%74 = load i32, i32* %73
%75 = add nsw i32 %74, 1
store i32 %75, i32* %73
%76 = load double*, double** %10
%77 = load i32, i32* %19
%78 = load i32, i32* %18
%79 = mul nsw i32 %77, %78
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds double, double* %76, i64 %80
%82 = load double*, double** %11
%83 = load i32, i32* %20
%84 = load i32, i32* %18
%85 = mul nsw i32 %83, %84
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds double, double* %82, i64 %86
%88 = load i32*, i32** %13
%89 = load i32, i32* %20
%90 = sext i32 %89 to i64
%91 = getelementptr inbounds i32, i32* %88, i64 %90
%92 = load i32, i32* %91
%93 = load i32, i32* %18
%94 = call i32 @gradient_step(double* %81, double* %87, i32 %92, i32 %93)
br label %95
95: ; preds = %59
%96 = load i32, i32* %22
%97 = add nsw i32 %96, 1
store i32 %97, i32* %22
br label %55
98: ; preds = %55
ret void
}

LLVM IR (-O2):
define void @minibatch_iteration(double* %0, double* %1, i32* nocapture %2, i32* nocapture %3, i32* nocapture %4, i32 %5, i32 %6, i32 %7, i32 %8) {
%10 = icmp sgt i32 %5, 0
br i1 %10, label %11, label %27
11: ; preds = %9
%12 = zext i32 %5 to i64
br label %16
13: ; preds = %16
br i1 %10, label %14, label %27
14: ; preds = %13
%15 = zext i32 %5 to i64
br label %28
16: ; preds = %11, %16
%17 = phi i64 [ 0, %11 ], [ %25, %16 ]
%18 = getelementptr inbounds i32, i32* %2, i64 %17
%19 = load i32, i32* %18
%20 = mul nsw i32 %19, %8
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds double, double* %0, i64 %21
%23 = tail call i32 @closest_centroid(double* %22, double* %1, i32 %6, i32 %8)
%24 = getelementptr inbounds i32, i32* %4, i64 %17
store i32 %23, i32* %24
%25 = add nuw nsw i64 %17, 1
%26 = icmp eq i64 %25, %12
br i1 %26, label %13, label %16
27: ; preds = %28, %9, %13
ret void
28: ; preds = %14, %28
%29 = phi i64 [ 0, %14 ], [ %45, %28 ]
%30 = getelementptr inbounds i32, i32* %2, i64 %29
%31 = load i32, i32* %30
%32 = getelementptr inbounds i32, i32* %4, i64 %29
%33 = load i32, i32* %32
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i32, i32* %3, i64 %34
%36 = load i32, i32* %35
%37 = add nsw i32 %36, 1
store i32 %37, i32* %35
%38 = mul nsw i32 %31, %8
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds double, double* %0, i64 %39
%41 = mul nsw i32 %33, %8
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds double, double* %1, i64 %42
%44 = tail call i32 @gradient_step(double* %40, double* %43, i32 %37, i32 %8)
%45 = add nuw nsw i64 %29, 1
%46 = icmp eq i64 %45, %15
br i1 %46, label %27, label %28
}