#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 */
int dabs(double);
double dpow(double, int);
double surd(double x, int n) {
  int im = 0;
  int neg = 0;
  if (n % 2 == 0 && x < 0) {
    im = 1;
    x *= -1;
  } else if (x < 0) {
    neg = 1;
    x *= -1;
  }
  double xp = x / ((double)n);
  double x0 = x;
  double atol = 1;
  while (dabs(atol) > 1e-10) {
    double xn = (xp * (-1 + n + dpow(xp, -n) * x0)) / n;
    atol = xp - xn;
    xp = xn;
  }
  return xp;
}


x86 assembly (clang, -O0):
<surd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    117e <surd+0x5e>
xorps  %xmm0,%xmm0
ucomisd -0x8(%rbp),%xmm0
jbe    117e <surd+0x5e>
movl   $0x1,-0x10(%rbp)
movsd  0xe91(%rip),%xmm0
mulsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11aa <surd+0x8a>
xorps  %xmm0,%xmm0
ucomisd -0x8(%rbp),%xmm0
jbe    11a5 <surd+0x85>
movl   $0x1,-0x14(%rbp)
movsd  0xe65(%rip),%xmm0
mulsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11aa <surd+0x8a>
movsd  -0x8(%rbp),%xmm0
cvtsi2sdl -0xc(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  0xe39(%rip),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
call   1040 <dabs@plt>
cvtsi2sd %eax,%xmm0
movsd  0xe26(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1268 <surd+0x148>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x20(%rbp),%xmm0
xor    %edi,%edi
sub    -0xc(%rbp),%edi
call   1030 <dpow@plt>
movsd  -0x48(%rbp),%xmm2
movaps %xmm0,%xmm1
movsd  -0x40(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm3
mulsd  %xmm3,%xmm1
addsd  %xmm2,%xmm1
mulsd  %xmm1,%xmm0
cvtsi2sdl -0xc(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x20(%rbp),%xmm0
subsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    11d4 <surd+0xb4>
movsd  -0x20(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<surd>:
push   %rbx
sub    $0x30,%rsp
mov    %edi,%ebx
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
jbe    1138 <surd+0x18>
mov    %ebx,%eax
and    $0x1,%eax
je     113e <surd+0x1e>
ucomisd %xmm0,%xmm1
jbe    1146 <surd+0x26>
xorpd  0xeba(%rip),%xmm0
xorps  %xmm1,%xmm1
cvtsi2sd %ebx,%xmm1
movapd %xmm0,0x20(%rsp)
movsd  %xmm1,0x10(%rsp)
divsd  %xmm1,%xmm0
movsd  %xmm0,(%rsp)
movsd  0xea6(%rip),%xmm0
call   1040 <dabs@plt>
test   %eax,%eax
jle    11de <surd+0xbe>
lea    -0x1(%rbx),%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  %xmm0,0x8(%rsp)
neg    %ebx
movsd  (%rsp),%xmm1
nopw   0x0(%rax,%rax,1)
movapd %xmm1,%xmm0
movsd  %xmm1,(%rsp)
mov    %ebx,%edi
call   1030 <dpow@plt>
mulsd  0x20(%rsp),%xmm0
addsd  0x8(%rsp),%xmm0
movsd  (%rsp),%xmm1
mulsd  %xmm1,%xmm0
divsd  0x10(%rsp),%xmm0
movsd  %xmm0,0x18(%rsp)
subsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
call   1040 <dabs@plt>
movsd  0x18(%rsp),%xmm0
movapd %xmm0,%xmm1
test   %eax,%eax
jg     1190 <surd+0x70>
jmp    11e3 <surd+0xc3>
movsd  (%rsp),%xmm0
add    $0x30,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
define double @surd(double %0, i32 %1) {
%3 = and i32 %1, 1
%4 = icmp eq i32 %3, 0
%5 = fcmp olt double %0, 0.000000e+00
%6 = and i1 %5, %4
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = fneg double %0
br label %12
9: ; preds = %2
br i1 %5, label %10, label %12
10: ; preds = %9
%11 = fneg double %0
br label %12
12: ; preds = %9, %10, %7
%13 = phi double [ %8, %7 ], [ %11, %10 ], [ %0, %9 ]
%14 = sitofp i32 %1 to double
%15 = fdiv double %13, %14
%16 = tail call i32 @dabs(double 1.000000e+00)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %31
18: ; preds = %12
%19 = add nsw i32 %1, -1
%20 = sitofp i32 %19 to double
%21 = sub nsw i32 0, %1
br label %22
22: ; preds = %18, %22
%23 = phi double [ %15, %18 ], [ %27, %22 ]
%24 = tail call double @dpow(double %23, i32 %21)
%25 = tail call double @llvm.fmuladd.f64(double %24, double %13, double %20)
%26 = fmul double %23, %25
%27 = fdiv double %26, %14
%28 = fsub double %23, %27
%29 = tail call i32 @dabs(double %28)
%30 = icmp sgt i32 %29, 0
br i1 %30, label %22, label %31
31: ; preds = %22, %12
%32 = phi double [ %15, %12 ], [ %27, %22 ]
ret double %32
}