#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 */
typedef size_t uint;

/* Variables and functions */
int random_double_uniform();
int sqrt(double);
void random_dvec_uniform_direction(uint ndim, double *v) {
  uint i;
  double len2, scale, tmp;
  while (1) {
    len2 = 0;
    for (i = 0; i < ndim; i++) {
      tmp = 2 * random_double_uniform() - 1;
      v[i] = tmp;
      len2 += tmp * tmp;
    }
    if (len2 <= 1 && len2 != 0) {
      scale = 1 / sqrt(len2);
      for (i = 0; i < ndim; i++)
        v[i] *= scale;
      break;
    }
  }
}


x86 assembly (clang, -O0):
<random_dvec_uniform_direction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
jae    11a3 <random_dvec_uniform_direction+0x83>
mov    $0x0,%al
call   1030 <random_double_uniform@plt>
shl    $0x1,%eax
sub    $0x1,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
movsd  -0x30(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm2
movsd  -0x20(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1140 <random_dvec_uniform_direction+0x20>
movsd  0xe55(%rip),%xmm0
ucomisd -0x20(%rbp),%xmm0
jb     1233 <random_dvec_uniform_direction+0x113>
movsd  -0x20(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11d3 <random_dvec_uniform_direction+0xb3>
jp     11d3 <random_dvec_uniform_direction+0xb3>
jmp    1233 <random_dvec_uniform_direction+0x113>
movsd  -0x20(%rbp),%xmm0
call   1040 <sqrt@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cltd
idiv   %ecx
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x28(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
jae    122e <random_dvec_uniform_direction+0x10e>
movsd  -0x28(%rbp),%xmm0
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mulsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11f8 <random_dvec_uniform_direction+0xd8>
jmp    1238 <random_dvec_uniform_direction+0x118>
jmp    1130 <random_dvec_uniform_direction+0x10>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_dvec_uniform_direction>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
nop
xorpd  %xmm1,%xmm1
test   %r14,%r14
je     1176 <random_dvec_uniform_direction+0x56>
xorpd  %xmm1,%xmm1
xor    %ebx,%ebx
nop
movsd  %xmm1,0x8(%rsp)
xor    %eax,%eax
call   1030 <random_double_uniform@plt>
movsd  0x8(%rsp),%xmm1
add    %eax,%eax
add    $0xffffffff,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  %xmm0,(%r15,%rbx,8)
mulsd  %xmm0,%xmm0
addsd  %xmm0,%xmm1
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1140 <random_dvec_uniform_direction+0x20>
movsd  0xe82(%rip),%xmm0
ucomisd %xmm1,%xmm0
jb     1130 <random_dvec_uniform_direction+0x10>
ucomisd 0xe7c(%rip),%xmm1
jne    1190 <random_dvec_uniform_direction+0x70>
jnp    1130 <random_dvec_uniform_direction+0x10>
movapd %xmm1,%xmm0
call   1040 <sqrt@plt>
lea    0x1(%rax),%edx
xor    %ecx,%ecx
cmp    $0x3,%edx
cmovb  %eax,%ecx
test   %r14,%r14
je     1299 <random_dvec_uniform_direction+0x179>
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
cmp    $0x4,%r14
jae    11c1 <random_dvec_uniform_direction+0xa1>
xor    %eax,%eax
jmp    1280 <random_dvec_uniform_direction+0x160>
mov    %r14,%rax
and    $0xfffffffffffffffc,%rax
movapd %xmm0,%xmm1
unpcklpd %xmm0,%xmm1
lea    -0x4(%rax),%rcx
mov    %rcx,%rdx
shr    $0x2,%rdx
add    $0x1,%rdx
test   %rcx,%rcx
je     12a3 <random_dvec_uniform_direction+0x183>
mov    %rdx,%rsi
and    $0xfffffffffffffffe,%rsi
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movupd (%r15,%rcx,8),%xmm2
movupd 0x10(%r15,%rcx,8),%xmm3
movupd 0x20(%r15,%rcx,8),%xmm4
movupd 0x30(%r15,%rcx,8),%xmm5
mulpd  %xmm1,%xmm2
mulpd  %xmm1,%xmm3
movupd %xmm2,(%r15,%rcx,8)
movupd %xmm3,0x10(%r15,%rcx,8)
mulpd  %xmm1,%xmm4
mulpd  %xmm1,%xmm5
movupd %xmm4,0x20(%r15,%rcx,8)
movupd %xmm5,0x30(%r15,%rcx,8)
add    $0x8,%rcx
add    $0xfffffffffffffffe,%rsi
jne    1200 <random_dvec_uniform_direction+0xe0>
test   $0x1,%dl
je     1277 <random_dvec_uniform_direction+0x157>
movupd (%r15,%rcx,8),%xmm2
movupd 0x10(%r15,%rcx,8),%xmm3
mulpd  %xmm1,%xmm2
mulpd  %xmm1,%xmm3
movupd %xmm2,(%r15,%rcx,8)
movupd %xmm3,0x10(%r15,%rcx,8)
cmp    %r14,%rax
je     1299 <random_dvec_uniform_direction+0x179>
nopl   0x0(%rax)
movsd  (%r15,%rax,8),%xmm1
mulsd  %xmm0,%xmm1
movsd  %xmm1,(%r15,%rax,8)
add    $0x1,%rax
cmp    %rax,%r14
jne    1280 <random_dvec_uniform_direction+0x160>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %ecx,%ecx
test   $0x1,%dl
jne    1255 <random_dvec_uniform_direction+0x135>
jmp    1277 <random_dvec_uniform_direction+0x157>

LLVM IR (-O0):
define void @random_dvec_uniform_direction(i64 %0, double* %1) {
%3 = alloca i64
%4 = alloca double*
%5 = alloca i64
%6 = alloca double
%7 = alloca double
%8 = alloca double
store i64 %0, i64* %3
store double* %1, double** %4
br label %9
9: ; preds = %2, %56
store double 0.000000e+00, double* %6
store i64 0, i64* %5
br label %10
10: ; preds = %27, %9
%11 = load i64, i64* %5
%12 = load i64, i64* %3
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %30
14: ; preds = %10
%15 = call i32 (...) @random_double_uniform()
%16 = mul nsw i32 2, %15
%17 = sub nsw i32 %16, 1
%18 = sitofp i32 %17 to double
store double %18, double* %8
%19 = load double, double* %8
%20 = load double*, double** %4
%21 = load i64, i64* %5
%22 = getelementptr inbounds double, double* %20, i64 %21
store double %19, double* %22
%23 = load double, double* %8
%24 = load double, double* %8
%25 = load double, double* %6
%26 = call double @llvm.fmuladd.f64(double %23, double %24, double %25)
store double %26, double* %6
br label %27
27: ; preds = %14
%28 = load i64, i64* %5
%29 = add i64 %28, 1
store i64 %29, i64* %5
br label %10
30: ; preds = %10
%31 = load double, double* %6
%32 = fcmp ole double %31, 1.000000e+00
br i1 %32, label %33, label %56
33: ; preds = %30
%34 = load double, double* %6
%35 = fcmp une double %34, 0.000000e+00
br i1 %35, label %36, label %56
36: ; preds = %33
%37 = load double, double* %6
%38 = call i32 @sqrt(double %37)
%39 = sdiv i32 1, %38
%40 = sitofp i32 %39 to double
store double %40, double* %7
store i64 0, i64* %5
br label %41
41: ; preds = %52, %36
%42 = load i64, i64* %5
%43 = load i64, i64* %3
%44 = icmp ult i64 %42, %43
br i1 %44, label %45, label %55
45: ; preds = %41
%46 = load double, double* %7
%47 = load double*, double** %4
%48 = load i64, i64* %5
%49 = getelementptr inbounds double, double* %47, i64 %48
%50 = load double, double* %49
%51 = fmul double %50, %46
store double %51, double* %49
br label %52
52: ; preds = %45
%53 = load i64, i64* %5
%54 = add i64 %53, 1
store i64 %54, i64* %5
br label %41
55: ; preds = %41
br label %57
56: ; preds = %33, %30
br label %9
57: ; preds = %55
ret void
}

LLVM IR (-O2):
define void @random_dvec_uniform_direction(i64 %0, double* nocapture %1) {
%3 = icmp eq i64 %0, 0
br label %4
4: ; preds = %16, %2
br i1 %3, label %16, label %5
5: ; preds = %4, %5
%6 = phi i64 [ %14, %5 ], [ 0, %4 ]
%7 = phi double [ %13, %5 ], [ 0.000000e+00, %4 ]
%8 = tail call i32 (...) @random_double_uniform()
%9 = shl nsw i32 %8, 1
%10 = add nsw i32 %9, -1
%11 = sitofp i32 %10 to double
%12 = getelementptr inbounds double, double* %1, i64 %6
store double %11, double* %12
%13 = tail call double @llvm.fmuladd.f64(double %11, double %11, double %7)
%14 = add nuw i64 %6, 1
%15 = icmp eq i64 %14, %0
br i1 %15, label %16, label %5
16: ; preds = %5, %4
%17 = phi double [ 0.000000e+00, %4 ], [ %13, %5 ]
%18 = fcmp ole double %17, 1.000000e+00
%19 = fcmp une double %17, 0.000000e+00
%20 = and i1 %18, %19
br i1 %20, label %21, label %4
21: ; preds = %16
%22 = tail call i32 @sqrt(double %17)
%23 = add i32 %22, 1
%24 = icmp ult i32 %23, 3
%25 = select i1 %24, i32 %22, i32 0
%26 = sitofp i32 %25 to double
br i1 %3, label %94, label %27
27: ; preds = %21
%28 = icmp ult i64 %0, 4
br i1 %28, label %85, label %29
29: ; preds = %27
%30 = and i64 %0, -4
%31 = insertelement <2 x double> poison, double %26, i64 0
%32 = shufflevector <2 x double> %31, <2 x double> poison, <2 x i32> zeroinitializer
%33 = insertelement <2 x double> poison, double %26, i64 0
%34 = shufflevector <2 x double> %33, <2 x double> poison, <2 x i32> zeroinitializer
%35 = add i64 %30, -4
%36 = lshr exact i64 %35, 2
%37 = add nuw nsw i64 %36, 1
%38 = and i64 %37, 1
%39 = icmp eq i64 %35, 0
br i1 %39, label %69, label %40
40: ; preds = %29
%41 = and i64 %37, 9223372036854775806
br label %42
42: ; preds = %42, %40
%43 = phi i64 [ 0, %40 ], [ %66, %42 ]
%44 = phi i64 [ 0, %40 ], [ %67, %42 ]
%45 = getelementptr inbounds double, double* %1, i64 %43
%46 = bitcast double* %45 to <2 x double>*
%47 = load <2 x double>, <2 x double>* %46
%48 = getelementptr inbounds double, double* %45, i64 2
%49 = bitcast double* %48 to <2 x double>*
%50 = load <2 x double>, <2 x double>* %49
%51 = fmul <2 x double> %47, %32
%52 = fmul <2 x double> %50, %34
%53 = bitcast double* %45 to <2 x double>*
store <2 x double> %51, <2 x double>* %53
%54 = bitcast double* %48 to <2 x double>*
store <2 x double> %52, <2 x double>* %54
%55 = or i64 %43, 4
%56 = getelementptr inbounds double, double* %1, i64 %55
%57 = bitcast double* %56 to <2 x double>*
%58 = load <2 x double>, <2 x double>* %57
%59 = getelementptr inbounds double, double* %56, i64 2
%60 = bitcast double* %59 to <2 x double>*
%61 = load <2 x double>, <2 x double>* %60
%62 = fmul <2 x double> %58, %32
%63 = fmul <2 x double> %61, %34
%64 = bitcast double* %56 to <2 x double>*
store <2 x double> %62, <2 x double>* %64
%65 = bitcast double* %59 to <2 x double>*
store <2 x double> %63, <2 x double>* %65
%66 = add nuw i64 %43, 8
%67 = add i64 %44, 2
%68 = icmp eq i64 %67, %41
br i1 %68, label %69, label %42
69: ; preds = %42, %29
%70 = phi i64 [ 0, %29 ], [ %66, %42 ]
%71 = icmp eq i64 %38, 0
br i1 %71, label %83, label %72
72: ; preds = %69
%73 = getelementptr inbounds double, double* %1, i64 %70
%74 = bitcast double* %73 to <2 x double>*
%75 = load <2 x double>, <2 x double>* %74
%76 = getelementptr inbounds double, double* %73, i64 2
%77 = bitcast double* %76 to <2 x double>*
%78 = load <2 x double>, <2 x double>* %77
%79 = fmul <2 x double> %75, %32
%80 = fmul <2 x double> %78, %34
%81 = bitcast double* %73 to <2 x double>*
store <2 x double> %79, <2 x double>* %81
%82 = bitcast double* %76 to <2 x double>*
store <2 x double> %80, <2 x double>* %82
br label %83
83: ; preds = %69, %72
%84 = icmp eq i64 %30, %0
br i1 %84, label %94, label %85
85: ; preds = %27, %83
%86 = phi i64 [ 0, %27 ], [ %30, %83 ]
br label %87
87: ; preds = %85, %87
%88 = phi i64 [ %92, %87 ], [ %86, %85 ]
%89 = getelementptr inbounds double, double* %1, i64 %88
%90 = load double, double* %89
%91 = fmul double %90, %26
store double %91, double* %89
%92 = add nuw i64 %88, 1
%93 = icmp eq i64 %92, %0
br i1 %93, label %94, label %87
94: ; preds = %87, %83, %21
ret void
}