#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 /*<<< orphan*/ ApproximatelyEqual(double, double);
int /*<<< orphan*/ Error(char *, double);
double Sqrt(double x) {
  double g;
  if (x == 0)
    return (0);
  if (x < 0)
    Error("Sqrt called with negative argument %g", x);
  g = x;
  while (!ApproximatelyEqual(x, g * g)) {
    g = (g + x / g) / 2;
  }
  return (g);
}


x86 assembly (clang, -O0):
<Sqrt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1152 <Sqrt+0x32>
jp     1152 <Sqrt+0x32>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11d7 <Sqrt+0xb7>
xorps  %xmm0,%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    1171 <Sqrt+0x51>
movsd  -0x10(%rbp),%xmm0
lea    0xe9c(%rip),%rdi
call   1030 <Error@plt>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
mulsd  -0x18(%rbp),%xmm1
call   1040 <ApproximatelyEqual@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11a4 <Sqrt+0x84>
jmp    11cd <Sqrt+0xad>
movsd  -0x18(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm1
divsd  -0x18(%rbp),%xmm1
addsd  %xmm1,%xmm0
movsd  0xe41(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    117b <Sqrt+0x5b>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Sqrt>:
sub    $0x18,%rsp
xorpd  %xmm2,%xmm2
movsd  %xmm0,0x8(%rsp)
ucomisd %xmm2,%xmm0
jne    113a <Sqrt+0x1a>
jnp    11bb <Sqrt+0x9b>
xorpd  %xmm0,%xmm0
ucomisd 0x8(%rsp),%xmm0
jbe    1158 <Sqrt+0x38>
lea    0xebb(%rip),%rdi
movsd  0x8(%rsp),%xmm0
call   1030 <Error@plt>
movsd  0x8(%rsp),%xmm0
movapd %xmm0,%xmm1
mulsd  %xmm0,%xmm1
call   1040 <ApproximatelyEqual@plt>
test   %eax,%eax
je     1177 <Sqrt+0x57>
movsd  0x8(%rsp),%xmm2
jmp    11bb <Sqrt+0x9b>
movsd  0x8(%rsp),%xmm1
nopl   (%rax)
movsd  0x8(%rsp),%xmm0
movapd %xmm0,%xmm2
divsd  %xmm1,%xmm2
addsd  %xmm1,%xmm2
mulsd  0xe66(%rip),%xmm2
movsd  %xmm2,0x10(%rsp)
movapd %xmm2,%xmm1
mulsd  %xmm2,%xmm1
call   1040 <ApproximatelyEqual@plt>
movsd  0x10(%rsp),%xmm2
movapd %xmm2,%xmm1
test   %eax,%eax
je     1180 <Sqrt+0x60>
movapd %xmm2,%xmm0
add    $0x18,%rsp
ret

LLVM IR (-O0):
@.str = [38 x i8] c"Sqrt called with negative argument %g\00"
define double @Sqrt(double %0) {
%2 = alloca double
%3 = alloca double
%4 = alloca double
store double %0, double* %3
%5 = load double, double* %3
%6 = fcmp oeq double %5, 0.000000e+00
br i1 %6, label %7, label %8
7: ; preds = %1
store double 0.000000e+00, double* %2
br label %33
8: ; preds = %1
%9 = load double, double* %3
%10 = fcmp olt double %9, 0.000000e+00
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = load double, double* %3
%13 = call i32 @Error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), double %12)
br label %14
14: ; preds = %11, %8
%15 = load double, double* %3
store double %15, double* %4
br label %16
16: ; preds = %24, %14
%17 = load double, double* %3
%18 = load double, double* %4
%19 = load double, double* %4
%20 = fmul double %18, %19
%21 = call i32 @ApproximatelyEqual(double %17, double %20)
%22 = icmp ne i32 %21, 0
%23 = xor i1 %22, true
br i1 %23, label %24, label %31
24: ; preds = %16
%25 = load double, double* %4
%26 = load double, double* %3
%27 = load double, double* %4
%28 = fdiv double %26, %27
%29 = fadd double %25, %28
%30 = fdiv double %29, 2.000000e+00
store double %30, double* %4
br label %16
31: ; preds = %16
%32 = load double, double* %4
store double %32, double* %2
br label %33
33: ; preds = %31, %7
%34 = load double, double* %2
ret double %34
}

LLVM IR (-O2):
@.str = [38 x i8] c"Sqrt called with negative argument %g\00"
define double @Sqrt(double %0) {
%2 = fcmp oeq double %0, 0.000000e+00
br i1 %2, label %19, label %3
3: ; preds = %1
%4 = fcmp olt double %0, 0.000000e+00
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i32 @Error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), double %0)
br label %7
7: ; preds = %5, %3
%8 = fmul double %0, %0
%9 = tail call i32 @ApproximatelyEqual(double %0, double %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %19
11: ; preds = %7, %11
%12 = phi double [ %15, %11 ], [ %0, %7 ]
%13 = fdiv double %0, %12
%14 = fadd double %12, %13
%15 = fmul double %14, 5.000000e-01
%16 = fmul double %15, %15
%17 = tail call i32 @ApproximatelyEqual(double %0, double %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %11, label %19
19: ; preds = %11, %7, %1
%20 = phi double [ 0.000000e+00, %1 ], [ %0, %7 ], [ %15, %11 ]
ret double %20
}