#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 acos(double);
double atan2(double, double);
double sqrt(double);
void nsph2(int *di, double *x, double *res) {
  int i;
  if (*di > 0 && *di < 2)
    *res = *x;
  if (*di >= 2) {
    *res = *x * *x;
    *res += x[1] * x[1];
    res[1] = atan2(*x, x[1]);
    for (i = 2; i < *di; i++) {
      *res += x[i] * x[i];
      res[i] = acos(x[i] / sqrt(*res));
    }
    *res = sqrt(*res);
  }
}


x86 assembly (clang, -O0):
<nsph2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
cmpl   $0x0,(%rax)
jle    116e <nsph2+0x3e>
mov    -0x8(%rbp),%rax
cmpl   $0x2,(%rax)
jge    116e <nsph2+0x3e>
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
mov    -0x18(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x8(%rbp),%rax
cmpl   $0x2,(%rax)
jl     127c <nsph2+0x14c>
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
mov    -0x10(%rbp),%rax
mulsd  (%rax),%xmm0
mov    -0x18(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x10(%rbp),%rax
movsd  0x8(%rax),%xmm0
mov    -0x10(%rbp),%rax
movsd  0x8(%rax),%xmm2
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,(%rax)
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
mov    -0x10(%rbp),%rax
movsd  0x8(%rax),%xmm1
call   1030 <atan2@plt>
mov    -0x18(%rbp),%rax
movsd  %xmm0,0x8(%rax)
movl   $0x2,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x8(%rbp),%rcx
cmp    (%rcx),%eax
jge    1267 <nsph2+0x137>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,(%rax)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm0
call   1050 <sqrt@plt>
movaps %xmm0,%xmm1
movsd  -0x28(%rbp),%xmm0
divsd  %xmm1,%xmm0
call   1040 <acos@plt>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11df <nsph2+0xaf>
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm0
call   1050 <sqrt@plt>
mov    -0x18(%rbp),%rax
movsd  %xmm0,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nsph2>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r14
mov    (%rdi),%eax
cmp    $0x1,%eax
jne    114f <nsph2+0x1f>
movsd  (%r14),%xmm0
jmp    1215 <nsph2+0xe5>
jle    121b <nsph2+0xeb>
mov    %rdi,%r15
movsd  (%r14),%xmm0
mulsd  %xmm0,%xmm0
movsd  %xmm0,(%r12)
movsd  0x8(%r14),%xmm1
mulsd  %xmm1,%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%r12)
movsd  (%r14),%xmm0
movsd  0x8(%r14),%xmm1
call   1030 <atan2@plt>
movsd  %xmm0,0x8(%r12)
cmpl   $0x3,(%r15)
jl     11fa <nsph2+0xca>
mov    $0x2,%ebx
jmp    11ce <nsph2+0x9e>
nop
movsd  %xmm1,(%rsp)
call   1050 <sqrt@plt>
movsd  (%rsp),%xmm1
divsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
call   1040 <acos@plt>
movsd  %xmm0,(%r12,%rbx,8)
add    $0x1,%rbx
movslq (%r15),%rax
cmp    %rax,%rbx
jge    11fa <nsph2+0xca>
movsd  (%r14,%rbx,8),%xmm0
mulsd  %xmm0,%xmm0
addsd  (%r12),%xmm0
movsd  %xmm0,(%r12)
movsd  (%r14,%rbx,8),%xmm1
ucomisd 0xe0e(%rip),%xmm0
jb     11a0 <nsph2+0x70>
sqrtsd %xmm0,%xmm0
jmp    11af <nsph2+0x7f>
movsd  (%r12),%xmm0
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     1210 <nsph2+0xe0>
sqrtsd %xmm0,%xmm0
jmp    1215 <nsph2+0xe5>
call   1050 <sqrt@plt>
movsd  %xmm0,(%r12)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @nsph2(i32* %0, double* %1, double* %2) {
%4 = alloca i32*
%5 = alloca double*
%6 = alloca double*
%7 = alloca i32
store i32* %0, i32** %4
store double* %1, double** %5
store double* %2, double** %6
%8 = load i32*, i32** %4
%9 = load i32, i32* %8
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %19
11: ; preds = %3
%12 = load i32*, i32** %4
%13 = load i32, i32* %12
%14 = icmp slt i32 %13, 2
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = load double*, double** %5
%17 = load double, double* %16
%18 = load double*, double** %6
store double %17, double* %18
br label %19
19: ; preds = %15, %11, %3
%20 = load i32*, i32** %4
%21 = load i32, i32* %20
%22 = icmp sge i32 %21, 2
br i1 %22, label %23, label %88
23: ; preds = %19
%24 = load double*, double** %5
%25 = load double, double* %24
%26 = load double*, double** %5
%27 = load double, double* %26
%28 = fmul double %25, %27
%29 = load double*, double** %6
store double %28, double* %29
%30 = load double*, double** %5
%31 = getelementptr inbounds double, double* %30, i64 1
%32 = load double, double* %31
%33 = load double*, double** %5
%34 = getelementptr inbounds double, double* %33, i64 1
%35 = load double, double* %34
%36 = load double*, double** %6
%37 = load double, double* %36
%38 = call double @llvm.fmuladd.f64(double %32, double %35, double %37)
store double %38, double* %36
%39 = load double*, double** %5
%40 = load double, double* %39
%41 = load double*, double** %5
%42 = getelementptr inbounds double, double* %41, i64 1
%43 = load double, double* %42
%44 = call double @atan2(double %40, double %43)
%45 = load double*, double** %6
%46 = getelementptr inbounds double, double* %45, i64 1
store double %44, double* %46
store i32 2, i32* %7
br label %47
47: ; preds = %80, %23
%48 = load i32, i32* %7
%49 = load i32*, i32** %4
%50 = load i32, i32* %49
%51 = icmp slt i32 %48, %50
br i1 %51, label %52, label %83
52: ; preds = %47
%53 = load double*, double** %5
%54 = load i32, i32* %7
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds double, double* %53, i64 %55
%57 = load double, double* %56
%58 = load double*, double** %5
%59 = load i32, i32* %7
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds double, double* %58, i64 %60
%62 = load double, double* %61
%63 = load double*, double** %6
%64 = load double, double* %63
%65 = call double @llvm.fmuladd.f64(double %57, double %62, double %64)
store double %65, double* %63
%66 = load double*, double** %5
%67 = load i32, i32* %7
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds double, double* %66, i64 %68
%70 = load double, double* %69
%71 = load double*, double** %6
%72 = load double, double* %71
%73 = call double @sqrt(double %72)
%74 = fdiv double %70, %73
%75 = call double @acos(double %74)
%76 = load double*, double** %6
%77 = load i32, i32* %7
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds double, double* %76, i64 %78
store double %75, double* %79
br label %80
80: ; preds = %52
%81 = load i32, i32* %7
%82 = add nsw i32 %81, 1
store i32 %82, i32* %7
br label %47
83: ; preds = %47
%84 = load double*, double** %6
%85 = load double, double* %84
%86 = call double @sqrt(double %85)
%87 = load double*, double** %6
store double %86, double* %87
br label %88
88: ; preds = %83, %19
ret void
}

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