#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 normalrng();
double sqrt(double);
void uniformdirectionrng(double *x, int n) {
  double sumv;
  int k;
  sumv = 0;
  for (k = 1; k <= n; k++) {
    x[k] = normalrng();
    sumv += x[k] * x[k];
  }
  for (k = 1; k <= n; k++)
    x[k] /= sqrt(sumv);
}


x86 assembly (clang, -O0):
<uniformdirectionrng>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jg     1198 <uniformdirectionrng+0x78>
mov    $0x0,%al
call   1030 <normalrng@plt>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
movsd  -0x18(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    113e <uniformdirectionrng+0x1e>
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jg     11dc <uniformdirectionrng+0xbc>
movsd  -0x18(%rbp),%xmm0
call   1040 <sqrt@plt>
movaps %xmm0,%xmm1
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    119f <uniformdirectionrng+0x7f>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uniformdirectionrng>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %esi,%esi
jle    127e <uniformdirectionrng+0x15e>
mov    %esi,%r14d
mov    %rdi,%r12
lea    0x1(%r14),%r15d
xorpd  %xmm2,%xmm2
mov    $0x1,%ebx
cs nopw 0x0(%rax,%rax,1)
movsd  %xmm2,0x8(%rsp)
xor    %eax,%eax
call   1030 <normalrng@plt>
movsd  0x8(%rsp),%xmm2
movsd  %xmm0,(%r12,%rbx,8)
mulsd  %xmm0,%xmm0
addsd  %xmm0,%xmm2
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1150 <uniformdirectionrng+0x30>
test   %r14d,%r14d
jle    127e <uniformdirectionrng+0x15e>
mov    %r15d,%eax
add    $0xffffffffffffffff,%r15
mov    $0x1,%ebx
cmp    $0x2,%eax
jne    11b8 <uniformdirectionrng+0x98>
test   $0x1,%r15b
je     127e <uniformdirectionrng+0x15e>
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm2
jb     1265 <uniformdirectionrng+0x145>
xorps  %xmm0,%xmm0
sqrtsd %xmm2,%xmm0
jmp    126e <uniformdirectionrng+0x14e>
mov    %r15,%r14
and    $0xfffffffffffffffe,%r14
neg    %r14
mov    $0x1,%ebx
sqrtsd %xmm2,%xmm3
xorpd  %xmm4,%xmm4
movsd  %xmm3,0x10(%rsp)
jmp    1204 <uniformdirectionrng+0xe4>
nopw   0x0(%rax,%rax,1)
movsd  0x8(%r12,%rbx,8),%xmm1
divsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%r12,%rbx,8)
lea    (%r14,%rbx,1),%rax
add    $0x2,%rax
add    $0x2,%rbx
cmp    $0x1,%rax
je     1194 <uniformdirectionrng+0x74>
ucomisd %xmm4,%xmm2
movapd %xmm3,%xmm0
movsd  %xmm2,0x8(%rsp)
jae    122d <uniformdirectionrng+0x10d>
movapd %xmm2,%xmm0
call   1040 <sqrt@plt>
xorpd  %xmm4,%xmm4
movsd  0x10(%rsp),%xmm3
movsd  0x8(%rsp),%xmm2
movsd  (%r12,%rbx,8),%xmm1
divsd  %xmm0,%xmm1
movsd  %xmm1,(%r12,%rbx,8)
ucomisd %xmm4,%xmm2
movapd %xmm3,%xmm0
jae    11e0 <uniformdirectionrng+0xc0>
movapd %xmm2,%xmm0
call   1040 <sqrt@plt>
xorpd  %xmm4,%xmm4
movsd  0x10(%rsp),%xmm3
movsd  0x8(%rsp),%xmm2
jmp    11e0 <uniformdirectionrng+0xc0>
movapd %xmm2,%xmm0
call   1040 <sqrt@plt>
movsd  (%r12,%rbx,8),%xmm1
divsd  %xmm0,%xmm1
movsd  %xmm1,(%r12,%rbx,8)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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