int sample_Mult_labeled(int w, double **post_beta, double *post_theta, int V,
                        int K, double alpha, double eta, int *labels) {
  int *labelIdx = (int *)malloc(K * sizeof(int));
  double *post_z = (double *)malloc((K + 1) * sizeof(double));
  int nLabels = 0;
  int i, j, z, result;
  j = 0;
  for (i = 1; i <= K; i++) {
    if (labels[i - 1]) {
      labelIdx[j] = i;
      post_z[j] = (ldf_Mult_smooth(0, eta, w, post_beta[i - 1], 1, V)) +
                  (ldf_Mult_smooth(0, alpha, i, post_theta, 1, K));
      j += 1;
    }
  }
  normalizeLog(post_z, 1, j);
  z = sample_Mult(post_z, 1, j);
  result = labelIdx[z - 1];
  free(labelIdx);
  free(post_z);
  return result;
}


x86 assembly (clang, -O0):
<sample_Mult_labeled>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
movsd  %xmm0,-0x28(%rbp)
movsd  %xmm1,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
movslq -0x20(%rbp),%rdi
shl    $0x2,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
movslq %eax,%rdi
shl    $0x3,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x48(%rbp)
movl   $0x0,-0x4c(%rbp)
movl   $0x0,-0x54(%rbp)
movl   $0x1,-0x50(%rbp)
mov    -0x50(%rbp),%eax
cmp    -0x20(%rbp),%eax
jg     1265 <sample_Mult_labeled+0x115>
mov    -0x38(%rbp),%rax
mov    -0x50(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
cmpl   $0x0,(%rax,%rcx,4)
je     1252 <sample_Mult_labeled+0x102>
mov    -0x50(%rbp),%edx
mov    -0x40(%rbp),%rax
movslq -0x54(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
movsd  -0x30(%rbp),%xmm0
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    -0x50(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdx
mov    -0x1c(%rbp),%r8d
xor    %edi,%edi
mov    $0x1,%ecx
mov    $0x1,%al
call   1070 <ldf_Mult_smooth@plt>
mov    %eax,-0x60(%rbp)
movsd  -0x28(%rbp),%xmm0
mov    -0x50(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%r8d
xor    %edi,%edi
mov    $0x1,%ecx
mov    $0x1,%al
call   1070 <ldf_Mult_smooth@plt>
mov    %eax,%ecx
mov    -0x60(%rbp),%eax
add    %ecx,%eax
cvtsi2sd %eax,%xmm0
mov    -0x48(%rbp),%rax
movslq -0x54(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    1257 <sample_Mult_labeled+0x107>
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
jmp    11b4 <sample_Mult_labeled+0x64>
mov    -0x48(%rbp),%rdi
mov    -0x54(%rbp),%edx
mov    $0x1,%esi
mov    $0x0,%al
call   1040 <normalizeLog@plt>
mov    -0x48(%rbp),%rdi
mov    -0x54(%rbp),%edx
mov    $0x1,%esi
mov    $0x0,%al
call   1050 <sample_Mult@plt>
mov    %eax,-0x58(%rbp)
mov    -0x40(%rbp),%rax
mov    -0x58(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x5c(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sample_Mult_labeled>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r9,%rbx
movsd  %xmm1,0x30(%rsp)
movsd  %xmm0,0x28(%rsp)
mov    %ecx,0x8(%rsp)
mov    %rdx,0x20(%rsp)
mov    %rsi,0x18(%rsp)
mov    %edi,0x4(%rsp)
mov    %r8d,0xc(%rsp)
movslq %r8d,%rbp
lea    0x0(,%rbp,4),%rdi
call   1060 <malloc@plt>
mov    %rax,0x10(%rsp)
lea    0x1(%rbp),%r14d
movslq %r14d,%rdi
shl    $0x3,%rdi
call   1060 <malloc@plt>
mov    %rax,%r15
test   %ebp,%ebp
jle    124e <sample_Mult_labeled+0xfe>
mov    %r14d,%ebp
xor    %r13d,%r13d
mov    $0x1,%r14d
jmp    11d9 <sample_Mult_labeled+0x89>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%r14
cmp    %r14,%rbp
je     1251 <sample_Mult_labeled+0x101>
cmpl   $0x0,-0x4(%rbx,%r14,4)
je     11d0 <sample_Mult_labeled+0x80>
movslq %r13d,%r13
mov    0x10(%rsp),%rax
mov    %r14d,(%rax,%r13,4)
mov    0x18(%rsp),%rax
mov    -0x8(%rax,%r14,8),%rdx
xor    %edi,%edi
movsd  0x30(%rsp),%xmm0
mov    0x4(%rsp),%esi
mov    $0x1,%ecx
mov    0x8(%rsp),%r8d
mov    $0x1,%al
call   1070 <ldf_Mult_smooth@plt>
mov    %eax,%r12d
xor    %edi,%edi
movsd  0x28(%rsp),%xmm0
mov    %r14d,%esi
mov    0x20(%rsp),%rdx
mov    $0x1,%ecx
mov    0xc(%rsp),%r8d
mov    $0x1,%al
call   1070 <ldf_Mult_smooth@plt>
add    %r12d,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  %xmm0,(%r15,%r13,8)
add    $0x1,%r13d
jmp    11d0 <sample_Mult_labeled+0x80>
xor    %r13d,%r13d
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r13d,%edx
xor    %eax,%eax
call   1040 <normalizeLog@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r13d,%edx
xor    %eax,%eax
call   1050 <sample_Mult@plt>
cltq
mov    0x10(%rsp),%rdi
mov    -0x4(%rdi,%rax,4),%ebx
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @sample_Mult_labeled(i32 %0, double** nocapture %1, double* %2, i32 %3, i32 %4, double %5, double %6, i32* nocapture %7) {
%9 = sext i32 %4 to i64
%10 = shl nsw i64 %9, 2
%11 = tail call i8* @malloc(i64 %10)
%12 = bitcast i8* %11 to i32*
%13 = add i32 %4, 1
%14 = sext i32 %13 to i64
%15 = shl nsw i64 %14, 3
%16 = tail call i8* @malloc(i64 %15)
%17 = bitcast i8* %16 to double*
%18 = icmp slt i32 %4, 1
br i1 %18, label %44, label %19
19: ; preds = %8
%20 = zext i32 %13 to i64
br label %21
21: ; preds = %19, %40
%22 = phi i64 [ 1, %19 ], [ %42, %40 ]
%23 = phi i32 [ 0, %19 ], [ %41, %40 ]
%24 = add nsw i64 %22, -1
%25 = getelementptr inbounds i32, i32* %7, i64 %24
%26 = load i32, i32* %25
%27 = icmp eq i32 %26, 0
br i1 %27, label %40, label %28
28: ; preds = %21
%29 = sext i32 %23 to i64
%30 = getelementptr inbounds i32, i32* %12, i64 %29
%31 = trunc i64 %22 to i32
store i32 %31, i32* %30
%32 = getelementptr inbounds double*, double** %1, i64 %24
%33 = load double*, double** %32
%34 = tail call i32 (i32, double, i32, double*, i32, i32, ...) bitcast (i32 (...)* @ldf_Mult_smooth to i32 (i32, double, i32, double*, i32, i32, ...)*)(i32 0, double %6, i32 %0, double* %33, i32 1, i32 %3)
%35 = tail call i32 (i32, double, i32, double*, i32, i32, ...) bitcast (i32 (...)* @ldf_Mult_smooth to i32 (i32, double, i32, double*, i32, i32, ...)*)(i32 0, double %5, i32 %31, double* %2, i32 1, i32 %4)
%36 = add nsw i32 %35, %34
%37 = sitofp i32 %36 to double
%38 = getelementptr inbounds double, double* %17, i64 %29
store double %37, double* %38
%39 = add nsw i32 %23, 1
br label %40
40: ; preds = %21, %28
%41 = phi i32 [ %39, %28 ], [ %23, %21 ]
%42 = add nuw nsw i64 %22, 1
%43 = icmp eq i64 %42, %20
br i1 %43, label %44, label %21
44: ; preds = %40, %8
%45 = phi i32 [ 0, %8 ], [ %41, %40 ]
%46 = tail call i32 (double*, i32, i32, ...) bitcast (i32 (...)* @normalizeLog to i32 (double*, i32, i32, ...)*)(double* %17, i32 1, i32 %45)
%47 = tail call i32 (double*, i32, i32, ...) bitcast (i32 (...)* @sample_Mult to i32 (double*, i32, i32, ...)*)(double* %17, i32 1, i32 %45)
%48 = add nsw i32 %47, -1
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i32, i32* %12, i64 %49
%51 = load i32, i32* %50
tail call void @free(i8* %11)
tail call void @free(i8* %16)
ret i32 %51
}