#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 cos(double);
double sin(double);
double sqrt(double);
void hammersley2_sphere(double *out, int n) {
  double p, t, sint, phi;
  int k, kk;
  for (k = 0; k < n; k++) {
    t = 0;
    for (p = 0.5, kk = k; kk; p *= 0.5, kk >>= 1) {
      if (kk & 1) {
        t += p;
      }
    }
    t = 2.0 * t - 1.0;
    phi = (k + 0.5) / n;
    phi *= 2.0 * 3.1415926535;
    sint = sqrt(1.0 - t * t);
    out[3 * k + 0] = sint * cos(phi);
    out[3 * k + 1] = sint * sin(phi);
    out[3 * k + 2] = t;
  }
}


x86 assembly (clang, -O0):
<hammersley2_sphere>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    12cc <hammersley2_sphere+0x19c>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  0xe9e(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
je     11ba <hammersley2_sphere+0x8a>
mov    -0x38(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1195 <hammersley2_sphere+0x65>
movsd  -0x18(%rbp),%xmm0
addsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    119a <hammersley2_sphere+0x6a>
movsd  0xe5e(%rip),%xmm0
mulsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x38(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    116d <hammersley2_sphere+0x3d>
movsd  -0x20(%rbp),%xmm0
addsd  %xmm0,%xmm0
movsd  0xe4d(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
cvtsi2sdl -0x34(%rbp),%xmm0
movsd  0xe1f(%rip),%xmm1
addsd  %xmm1,%xmm0
cvtsi2sdl -0xc(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  0xe15(%rip),%xmm0
mulsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm1,%xmm0
movsd  0xdd6(%rip),%xmm1
addsd  %xmm1,%xmm0
call   1050 <sqrt@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x30(%rbp),%xmm0
call   1040 <cos@plt>
movaps %xmm0,%xmm1
movsd  -0x48(%rbp),%xmm0
mulsd  %xmm1,%xmm0
mov    -0x8(%rbp),%rax
imul   $0x3,-0x34(%rbp),%ecx
add    $0x0,%ecx
movslq %ecx,%rcx
movsd  %xmm0,(%rax,%rcx,8)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x30(%rbp),%xmm0
call   1030 <sin@plt>
movaps %xmm0,%xmm1
movsd  -0x40(%rbp),%xmm0
mulsd  %xmm1,%xmm0
mov    -0x8(%rbp),%rax
imul   $0x3,-0x34(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movsd  %xmm0,(%rax,%rcx,8)
movsd  -0x20(%rbp),%xmm0
mov    -0x8(%rbp),%rax
imul   $0x3,-0x34(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1146 <hammersley2_sphere+0x16>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hammersley2_sphere>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
test   %esi,%esi
jle    1290 <hammersley2_sphere+0x160>
mov    %rdi,%r15
cvtsi2sd %esi,%xmm0
movsd  %xmm0,0x10(%rsp)
mov    %esi,%r14d
xor    %r13d,%r13d
movsd  0xea0(%rip),%xmm2
movsd  0xea0(%rip),%xmm3
jmp    11d9 <hammersley2_sphere+0xa9>
nopw   0x0(%rax,%rax,1)
call   1050 <sqrt@plt>
movsd  0x8(%rsp),%xmm3
movsd  %xmm0,(%rsp)
movapd %xmm3,%xmm0
call   1040 <cos@plt>
mulsd  (%rsp),%xmm0
lea    (%r12,%r12,2),%ebx
movsd  %xmm0,(%r15,%rbx,8)
movsd  0x8(%rsp),%xmm0
call   1030 <sin@plt>
mulsd  (%rsp),%xmm0
movsd  %xmm0,0x8(%r15,%rbx,8)
movsd  0x18(%rsp),%xmm0
movsd  %xmm0,0x10(%r15,%rbx,8)
add    $0x1,%r13
cmp    %r14,%r13
movsd  0xe35(%rip),%xmm2
movsd  0xe35(%rip),%xmm3
je     1290 <hammersley2_sphere+0x160>
mov    $0x0,%r12d
xorpd  %xmm4,%xmm4
test   %r13,%r13
je     1233 <hammersley2_sphere+0x103>
xorpd  %xmm4,%xmm4
mov    %r13d,%eax
movapd %xmm2,%xmm0
jmp    1213 <hammersley2_sphere+0xe3>
cs nopw 0x0(%rax,%rax,1)
nop
addsd  %xmm1,%xmm4
mulsd  %xmm2,%xmm0
mov    %eax,%ecx
shr    %ecx
cmp    $0x1,%eax
mov    %ecx,%eax
jbe    1230 <hammersley2_sphere+0x100>
movapd %xmm3,%xmm1
test   $0x1,%al
je     1200 <hammersley2_sphere+0xd0>
movapd %xmm0,%xmm1
jmp    1200 <hammersley2_sphere+0xd0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r13d,%r12d
addsd  %xmm4,%xmm4
xorps  %xmm3,%xmm3
cvtsi2sd %r12d,%xmm3
addsd  0xdc9(%rip),%xmm4
addsd  %xmm2,%xmm3
divsd  0x10(%rsp),%xmm3
mulsd  0xdbf(%rip),%xmm3
movapd %xmm4,%xmm1
mulsd  %xmm4,%xmm1
movsd  0xdb7(%rip),%xmm0
subsd  %xmm1,%xmm0
ucomisd 0xdb3(%rip),%xmm0
movsd  %xmm4,0x18(%rsp)
movsd  %xmm3,0x8(%rsp)
jb     1170 <hammersley2_sphere+0x40>
sqrtsd %xmm0,%xmm0
jmp    117b <hammersley2_sphere+0x4b>
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @hammersley2_sphere(double* %0, i32 %1) {
%3 = alloca double*
%4 = alloca i32
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca i32
%10 = alloca i32
store double* %0, double** %3
store i32 %1, i32* %4
store i32 0, i32* %9
br label %11
11: ; preds = %77, %2
%12 = load i32, i32* %9
%13 = load i32, i32* %4
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %80
15: ; preds = %11
store double 0.000000e+00, double* %6
store double 5.000000e-01, double* %5
%16 = load i32, i32* %9
store i32 %16, i32* %10
br label %17
17: ; preds = %29, %15
%18 = load i32, i32* %10
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %34
20: ; preds = %17
%21 = load i32, i32* %10
%22 = and i32 %21, 1
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = load double, double* %5
%26 = load double, double* %6
%27 = fadd double %26, %25
store double %27, double* %6
br label %28
28: ; preds = %24, %20
br label %29
29: ; preds = %28
%30 = load double, double* %5
%31 = fmul double %30, 5.000000e-01
store double %31, double* %5
%32 = load i32, i32* %10
%33 = ashr i32 %32, 1
store i32 %33, i32* %10
br label %17
34: ; preds = %17
%35 = load double, double* %6
%36 = call double @llvm.fmuladd.f64(double 2.000000e+00, double %35, double -1.000000e+00)
store double %36, double* %6
%37 = load i32, i32* %9
%38 = sitofp i32 %37 to double
%39 = fadd double %38, 5.000000e-01
%40 = load i32, i32* %4
%41 = sitofp i32 %40 to double
%42 = fdiv double %39, %41
store double %42, double* %8
%43 = load double, double* %8
%44 = fmul double %43, 0x401921FB54411744
store double %44, double* %8
%45 = load double, double* %6
%46 = load double, double* %6
%47 = fneg double %45
%48 = call double @llvm.fmuladd.f64(double %47, double %46, double 1.000000e+00)
%49 = call double @sqrt(double %48)
store double %49, double* %7
%50 = load double, double* %7
%51 = load double, double* %8
%52 = call double @cos(double %51)
%53 = fmul double %50, %52
%54 = load double*, double** %3
%55 = load i32, i32* %9
%56 = mul nsw i32 3, %55
%57 = add nsw i32 %56, 0
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds double, double* %54, i64 %58
store double %53, double* %59
%60 = load double, double* %7
%61 = load double, double* %8
%62 = call double @sin(double %61)
%63 = fmul double %60, %62
%64 = load double*, double** %3
%65 = load i32, i32* %9
%66 = mul nsw i32 3, %65
%67 = add nsw i32 %66, 1
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds double, double* %64, i64 %68
store double %63, double* %69
%70 = load double, double* %6
%71 = load double*, double** %3
%72 = load i32, i32* %9
%73 = mul nsw i32 3, %72
%74 = add nsw i32 %73, 2
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds double, double* %71, i64 %75
store double %70, double* %76
br label %77
77: ; preds = %34
%78 = load i32, i32* %9
%79 = add nsw i32 %78, 1
store i32 %79, i32* %9
br label %11
80: ; preds = %11
ret void
}

LLVM IR (-O2):
define void @hammersley2_sphere(double* nocapture writeonly %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %49
4: ; preds = %2
%5 = sitofp i32 %1 to double
%6 = zext i32 %1 to i64
br label %7
7: ; preds = %4, %23
%8 = phi i64 [ 0, %4 ], [ %47, %23 ]
%9 = icmp eq i64 %8, 0
br i1 %9, label %23, label %10
10: ; preds = %7
%11 = trunc i64 %8 to i32
br label %12
12: ; preds = %10, %12
%13 = phi i32 [ %21, %12 ], [ %11, %10 ]
%14 = phi double [ %20, %12 ], [ 5.000000e-01, %10 ]
%15 = phi double [ %19, %12 ], [ 0.000000e+00, %10 ]
%16 = and i32 %13, 1
%17 = icmp eq i32 %16, 0
%18 = select i1 %17, double -0.000000e+00, double %14
%19 = fadd double %15, %18
%20 = fmul double %14, 5.000000e-01
%21 = lshr i32 %13, 1
%22 = icmp ult i32 %13, 2
br i1 %22, label %23, label %12
23: ; preds = %12, %7
%24 = phi i32 [ 0, %7 ], [ %11, %12 ]
%25 = phi double [ 0.000000e+00, %7 ], [ %19, %12 ]
%26 = tail call double @llvm.fmuladd.f64(double %25, double 2.000000e+00, double -1.000000e+00)
%27 = sitofp i32 %24 to double
%28 = fadd double %27, 5.000000e-01
%29 = fdiv double %28, %5
%30 = fmul double %29, 0x401921FB54411744
%31 = fneg double %26
%32 = tail call double @llvm.fmuladd.f64(double %31, double %26, double 1.000000e+00)
%33 = tail call double @sqrt(double %32)
%34 = tail call double @cos(double %30)
%35 = fmul double %33, %34
%36 = mul nsw i32 %24, 3
%37 = zext i32 %36 to i64
%38 = getelementptr inbounds double, double* %0, i64 %37
store double %35, double* %38
%39 = tail call double @sin(double %30)
%40 = fmul double %33, %39
%41 = add nuw nsw i32 %36, 1
%42 = zext i32 %41 to i64
%43 = getelementptr inbounds double, double* %0, i64 %42
store double %40, double* %43
%44 = add nuw nsw i32 %36, 2
%45 = zext i32 %44 to i64
%46 = getelementptr inbounds double, double* %0, i64 %45
store double %26, double* %46
%47 = add nuw nsw i64 %8, 1
%48 = icmp eq i64 %47, %6
br i1 %48, label %49, label %7
49: ; preds = %23, %2
ret void
}