#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 rand_gamma(double, int);
void rand_dirichlet(double *x, double *alpha, int K) {
  int i;
  double s = 0;
  for (i = 0; i < K; i++) {
    x[i] = rand_gamma(alpha[i], 1);
    s += x[i];
  }
  for (i = 0; i < K; i++)
    x[i] /= s;
}


x86 assembly (clang, -O0):
<rand_dirichlet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1187 <rand_dirichlet+0x77>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    $0x1,%edi
call   1030 <rand_gamma@plt>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
addsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1132 <rand_dirichlet+0x22>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11c3 <rand_dirichlet+0xb3>
movsd  -0x20(%rbp),%xmm1
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    118e <rand_dirichlet+0x7e>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_dirichlet>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %edx,%edx
jle    1207 <rand_dirichlet+0xf7>
mov    %edx,%r14d
mov    %rsi,%r12
mov    %rdi,%rbx
mov    %edx,%r15d
xorpd  %xmm3,%xmm3
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
movapd %xmm3,(%rsp)
movsd  (%r12,%rbp,8),%xmm0
mov    $0x1,%edi
call   1030 <rand_gamma@plt>
movapd (%rsp),%xmm3
movsd  %xmm0,(%rbx,%rbp,8)
addsd  %xmm0,%xmm3
add    $0x1,%rbp
cmp    %rbp,%r15
jne    1140 <rand_dirichlet+0x30>
test   %r14d,%r14d
jle    1207 <rand_dirichlet+0xf7>
cmp    $0x1,%r14d
jne    117f <rand_dirichlet+0x6f>
xor    %eax,%eax
jmp    11f0 <rand_dirichlet+0xe0>
mov    %r15d,%eax
and    $0xfffffffe,%eax
movapd %xmm3,%xmm0
unpcklpd %xmm3,%xmm0
lea    -0x2(%rax),%rdx
mov    %rdx,%rcx
shr    %rcx
add    $0x1,%rcx
test   %rdx,%rdx
je     1214 <rand_dirichlet+0x104>
mov    %rcx,%rsi
and    $0xfffffffffffffffe,%rsi
xor    %edx,%edx
nopl   0x0(%rax)
movupd (%rbx,%rdx,8),%xmm1
movupd 0x10(%rbx,%rdx,8),%xmm2
divpd  %xmm0,%xmm1
movupd %xmm1,(%rbx,%rdx,8)
divpd  %xmm0,%xmm2
movupd %xmm2,0x10(%rbx,%rdx,8)
add    $0x4,%rdx
add    $0xfffffffffffffffe,%rsi
jne    11b0 <rand_dirichlet+0xa0>
test   $0x1,%cl
je     11eb <rand_dirichlet+0xdb>
movupd (%rbx,%rdx,8),%xmm1
divpd  %xmm0,%xmm1
movupd %xmm1,(%rbx,%rdx,8)
cmp    %r15,%rax
je     1207 <rand_dirichlet+0xf7>
movsd  (%rbx,%rax,8),%xmm0
divsd  %xmm3,%xmm0
movsd  %xmm0,(%rbx,%rax,8)
add    $0x1,%rax
cmp    %rax,%r15
jne    11f0 <rand_dirichlet+0xe0>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edx,%edx
test   $0x1,%cl
jne    11dd <rand_dirichlet+0xcd>
jmp    11eb <rand_dirichlet+0xdb>

LLVM IR (-O0):
define void @rand_dirichlet(double* %0, double* %1, i32 %2) {
%4 = alloca double*
%5 = alloca double*
%6 = alloca i32
%7 = alloca i32
%8 = alloca double
store double* %0, double** %4
store double* %1, double** %5
store i32 %2, i32* %6
store double 0.000000e+00, double* %8
store i32 0, i32* %7
br label %9
9: ; preds = %31, %3
%10 = load i32, i32* %7
%11 = load i32, i32* %6
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %34
13: ; preds = %9
%14 = load double*, double** %5
%15 = load i32, i32* %7
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds double, double* %14, i64 %16
%18 = load double, double* %17
%19 = call double @rand_gamma(double %18, i32 1)
%20 = load double*, double** %4
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds double, double* %20, i64 %22
store double %19, double* %23
%24 = load double*, double** %4
%25 = load i32, i32* %7
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds double, double* %24, i64 %26
%28 = load double, double* %27
%29 = load double, double* %8
%30 = fadd double %29, %28
store double %30, double* %8
br label %31
31: ; preds = %13
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 1
store i32 %33, i32* %7
br label %9
34: ; preds = %9
store i32 0, i32* %7
br label %35
35: ; preds = %47, %34
%36 = load i32, i32* %7
%37 = load i32, i32* %6
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %50
39: ; preds = %35
%40 = load double, double* %8
%41 = load double*, double** %4
%42 = load i32, i32* %7
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds double, double* %41, i64 %43
%45 = load double, double* %44
%46 = fdiv double %45, %40
store double %46, double* %44
br label %47
47: ; preds = %39
%48 = load i32, i32* %7
%49 = add nsw i32 %48, 1
store i32 %49, i32* %7
br label %35
50: ; preds = %35
ret void
}

LLVM IR (-O2):
define void @rand_dirichlet(double* nocapture %0, double* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %69
5: ; preds = %3
%6 = zext i32 %2 to i64
br label %52
7: ; preds = %52
br i1 %4, label %8, label %69
8: ; preds = %7
%9 = zext i32 %2 to i64
%10 = icmp eq i32 %2, 1
br i1 %10, label %50, label %11
11: ; preds = %8
%12 = and i64 %6, 4294967294
%13 = insertelement <2 x double> poison, double %59, i64 0
%14 = shufflevector <2 x double> %13, <2 x double> poison, <2 x i32> zeroinitializer
%15 = add nsw i64 %12, -2
%16 = lshr exact i64 %15, 1
%17 = add nuw i64 %16, 1
%18 = and i64 %17, 1
%19 = icmp eq i64 %15, 0
br i1 %19, label %39, label %20
20: ; preds = %11
%21 = and i64 %17, -2
br label %22
22: ; preds = %22, %20
%23 = phi i64 [ 0, %20 ], [ %36, %22 ]
%24 = phi i64 [ 0, %20 ], [ %37, %22 ]
%25 = getelementptr inbounds double, double* %0, i64 %23
%26 = bitcast double* %25 to <2 x double>*
%27 = load <2 x double>, <2 x double>* %26
%28 = fdiv <2 x double> %27, %14
%29 = bitcast double* %25 to <2 x double>*
store <2 x double> %28, <2 x double>* %29
%30 = or i64 %23, 2
%31 = getelementptr inbounds double, double* %0, i64 %30
%32 = bitcast double* %31 to <2 x double>*
%33 = load <2 x double>, <2 x double>* %32
%34 = fdiv <2 x double> %33, %14
%35 = bitcast double* %31 to <2 x double>*
store <2 x double> %34, <2 x double>* %35
%36 = add nuw i64 %23, 4
%37 = add nuw i64 %24, 2
%38 = icmp eq i64 %37, %21
br i1 %38, label %39, label %22
39: ; preds = %22, %11
%40 = phi i64 [ 0, %11 ], [ %36, %22 ]
%41 = icmp eq i64 %18, 0
br i1 %41, label %48, label %42
42: ; preds = %39
%43 = getelementptr inbounds double, double* %0, i64 %40
%44 = bitcast double* %43 to <2 x double>*
%45 = load <2 x double>, <2 x double>* %44
%46 = fdiv <2 x double> %45, %14
%47 = bitcast double* %43 to <2 x double>*
store <2 x double> %46, <2 x double>* %47
br label %48
48: ; preds = %39, %42
%49 = icmp eq i64 %12, %6
br i1 %49, label %69, label %50
50: ; preds = %8, %48
%51 = phi i64 [ 0, %8 ], [ %12, %48 ]
br label %62
52: ; preds = %5, %52
%53 = phi i64 [ 0, %5 ], [ %60, %52 ]
%54 = phi double [ 0.000000e+00, %5 ], [ %59, %52 ]
%55 = getelementptr inbounds double, double* %1, i64 %53
%56 = load double, double* %55
%57 = tail call double @rand_gamma(double %56, i32 1)
%58 = getelementptr inbounds double, double* %0, i64 %53
store double %57, double* %58
%59 = fadd double %54, %57
%60 = add nuw nsw i64 %53, 1
%61 = icmp eq i64 %60, %6
br i1 %61, label %7, label %52
62: ; preds = %50, %62
%63 = phi i64 [ %67, %62 ], [ %51, %50 ]
%64 = getelementptr inbounds double, double* %0, i64 %63
%65 = load double, double* %64
%66 = fdiv double %65, %59
store double %66, double* %64
%67 = add nuw nsw i64 %63, 1
%68 = icmp eq i64 %67, %9
br i1 %68, label %69, label %62
69: ; preds = %62, %48, %3, %7
ret void
}