#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*/ printf(char *, float, float, ...);
void my_sqrt(float x) {
  float x1, x2, y, diff;
  if (x < 0)
    return;
  if (x >= 1) {
    x1 = 1;
    x2 = x;
  } else {
    x1 = x;
    x2 = 1;
  }
  y = x1;
  while (1) {
    diff = y * y - x;
    if (diff > 0.000001)
      x2 = y;
    else if ((-1 * diff) > 0.000001)
      x1 = y;
    else if ((x2 - x1) <= 0.000001)
      break;
    else
      break;
    y = (x1 + x2) / 2;
    printf("x1=%f x2=%f\n", x1, x2);
  }
  printf("%f %f %f\n", x1, y, x2);
}


x86 assembly (clang, -O0):
<my_sqrt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movss  %xmm0,-0x4(%rbp)
xorps  %xmm0,%xmm0
ucomiss -0x4(%rbp),%xmm0
jbe    112f <my_sqrt+0x1f>
jmp    128f <my_sqrt+0x17f>
movss  -0x4(%rbp),%xmm0
movss  0xec4(%rip),%xmm1
ucomiss %xmm1,%xmm0
jb     1161 <my_sqrt+0x51>
movss  0xeb3(%rip),%xmm0
movss  %xmm0,-0x8(%rbp)
movss  -0x4(%rbp),%xmm0
movss  %xmm0,-0xc(%rbp)
jmp    1178 <my_sqrt+0x68>
movss  -0x4(%rbp),%xmm0
movss  %xmm0,-0x8(%rbp)
movss  0xe8d(%rip),%xmm0
movss  %xmm0,-0xc(%rbp)
movss  -0x8(%rbp),%xmm0
movss  %xmm0,-0x10(%rbp)
movss  -0x10(%rbp),%xmm0
movss  -0x10(%rbp),%xmm2
movss  -0x4(%rbp),%xmm1
movd   %xmm1,%eax
xor    $0x80000000,%eax
movd   %eax,%xmm1
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
movss  %xmm0,-0x14(%rbp)
movss  -0x14(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe54(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    11d5 <my_sqrt+0xc5>
movss  -0x10(%rbp),%xmm0
movss  %xmm0,-0xc(%rbp)
jmp    1236 <my_sqrt+0x126>
movss  0xe27(%rip),%xmm0
mulss  -0x14(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe22(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1207 <my_sqrt+0xf7>
movss  -0x10(%rbp),%xmm0
movss  %xmm0,-0x8(%rbp)
jmp    1231 <my_sqrt+0x121>
movss  -0xc(%rbp),%xmm0
subss  -0x8(%rbp),%xmm0
cvtss2sd %xmm0,%xmm1
movsd  0xdf3(%rip),%xmm0
ucomisd %xmm1,%xmm0
jb     122c <my_sqrt+0x11c>
jmp    126e <my_sqrt+0x15e>
jmp    126e <my_sqrt+0x15e>
jmp    1236 <my_sqrt+0x126>
movss  -0x8(%rbp),%xmm0
addss  -0xc(%rbp),%xmm0
movss  0xdc0(%rip),%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0x10(%rbp)
movss  -0x8(%rbp),%xmm0
movss  -0xc(%rbp),%xmm1
lea    0xdb6(%rip),%rdi
mov    $0x2,%al
call   1030 <printf@plt>
jmp    1182 <my_sqrt+0x72>
movss  -0x8(%rbp),%xmm0
movss  -0x10(%rbp),%xmm1
movss  -0xc(%rbp),%xmm2
cvtss2sd %xmm2,%xmm2
lea    0xd9d(%rip),%rdi
mov    $0x3,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_sqrt>:
xorps  %xmm1,%xmm1
ucomiss %xmm0,%xmm1
jbe    1119 <my_sqrt+0x9>
ret
push   %rbx
sub    $0x40,%rsp
movss  0xeda(%rip),%xmm1
movaps %xmm1,%xmm3
cmpnless %xmm0,%xmm3
movaps %xmm3,%xmm2
andps  %xmm1,%xmm2
andnps %xmm0,%xmm3
orps   %xmm2,%xmm3
movaps %xmm3,0x10(%rsp)
minss  %xmm0,%xmm1
xorps  0xec6(%rip),%xmm0
movsd  0xece(%rip),%xmm3
lea    0xecf(%rip),%rbx
movss  %xmm1,0xc(%rsp)
movaps %xmm0,0x20(%rsp)
jmp    11b4 <my_sqrt+0xa4>
cs nopw 0x0(%rax,%rax,1)
movaps %xmm1,%xmm2
movss  0xc(%rsp),%xmm0
addss  %xmm0,%xmm1
mulss  0xe7f(%rip),%xmm1
movaps %xmm1,0x30(%rsp)
mov    %rbx,%rdi
movss  %xmm0,0xc(%rsp)
movaps %xmm2,0x10(%rsp)
movaps %xmm2,%xmm1
mov    $0x2,%al
call   1030 <printf@plt>
movaps 0x30(%rsp),%xmm1
movaps 0x20(%rsp),%xmm0
movsd  0xe6c(%rip),%xmm3
movaps %xmm1,%xmm2
mulss  %xmm1,%xmm2
addss  %xmm0,%xmm2
xorps  %xmm0,%xmm0
cvtss2sd %xmm2,%xmm0
ucomisd %xmm3,%xmm0
ja     1170 <my_sqrt+0x60>
xorps  0xe3d(%rip),%xmm2
xorps  %xmm0,%xmm0
cvtss2sd %xmm2,%xmm0
ucomisd %xmm3,%xmm0
jbe    11ed <my_sqrt+0xdd>
movaps %xmm1,%xmm0
movaps 0x10(%rsp),%xmm2
movaps %xmm2,%xmm1
jmp    1179 <my_sqrt+0x69>
movaps 0x10(%rsp),%xmm0
xorps  %xmm2,%xmm2
cvtss2sd %xmm0,%xmm2
lea    0xe35(%rip),%rdi
movss  0xc(%rsp),%xmm0
mov    $0x3,%al
add    $0x40,%rsp
pop    %rbx
jmp    1030 <printf@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"x1=%f x2=%f\0A\00"
@.str.1 = [10 x i8] c"%f %f %f\0A\00"
define void @my_sqrt(float %0) {
%2 = alloca float
%3 = alloca float
%4 = alloca float
%5 = alloca float
%6 = alloca float
store float %0, float* %2
%7 = load float, float* %2
%8 = fcmp olt float %7, 0.000000e+00
br i1 %8, label %9, label %10
9: ; preds = %1
br label %60
10: ; preds = %1
%11 = load float, float* %2
%12 = fcmp oge float %11, 1.000000e+00
br i1 %12, label %13, label %15
13: ; preds = %10
store float 1.000000e+00, float* %3
%14 = load float, float* %2
store float %14, float* %4
br label %17
15: ; preds = %10
%16 = load float, float* %2
store float %16, float* %3
store float 1.000000e+00, float* %4
br label %17
17: ; preds = %15, %13
%18 = load float, float* %3
store float %18, float* %5
br label %19
19: ; preds = %17, %46
%20 = load float, float* %5
%21 = load float, float* %5
%22 = load float, float* %2
%23 = fneg float %22
%24 = call float @llvm.fmuladd.f32(float %20, float %21, float %23)
store float %24, float* %6
%25 = load float, float* %6
%26 = fpext float %25 to double
%27 = fcmp ogt double %26, 0x3EB0C6F7A0B5ED8D
br i1 %27, label %28, label %30
28: ; preds = %19
%29 = load float, float* %5
store float %29, float* %4
br label %46
30: ; preds = %19
%31 = load float, float* %6
%32 = fmul float -1.000000e+00, %31
%33 = fpext float %32 to double
%34 = fcmp ogt double %33, 0x3EB0C6F7A0B5ED8D
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = load float, float* %5
store float %36, float* %3
br label %45
37: ; preds = %30
%38 = load float, float* %4
%39 = load float, float* %3
%40 = fsub float %38, %39
%41 = fpext float %40 to double
%42 = fcmp ole double %41, 0x3EB0C6F7A0B5ED8D
br i1 %42, label %43, label %44
43: ; preds = %37
br label %54
44: ; preds = %37
br label %54
45: ; preds = %35
br label %46
46: ; preds = %45, %28
%47 = load float, float* %3
%48 = load float, float* %4
%49 = fadd float %47, %48
%50 = fdiv float %49, 2.000000e+00
store float %50, float* %5
%51 = load float, float* %3
%52 = load float, float* %4
%53 = call i32 (i8*, float, float, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), float %51, float %52)
br label %19
54: ; preds = %44, %43
%55 = load float, float* %3
%56 = load float, float* %5
%57 = load float, float* %4
%58 = fpext float %57 to double
%59 = call i32 (i8*, float, float, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), float %55, float %56, double %58)
br label %60
60: ; preds = %54, %9
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"x1=%f x2=%f\0A\00"
@.str.1 = [10 x i8] c"%f %f %f\0A\00"
define void @my_sqrt(float %0) {
%2 = fcmp olt float %0, 0.000000e+00
br i1 %2, label %28, label %3
3: ; preds = %1
%4 = fcmp ult float %0, 1.000000e+00
%5 = select i1 %4, float 1.000000e+00, float %0
%6 = select i1 %4, float %0, float 1.000000e+00
%7 = fneg float %0
br label %8
8: ; preds = %19, %3
%9 = phi float [ %5, %3 ], [ %20, %19 ]
%10 = phi float [ %6, %3 ], [ %23, %19 ]
%11 = phi float [ %6, %3 ], [ %21, %19 ]
%12 = tail call float @llvm.fmuladd.f32(float %10, float %10, float %7)
%13 = fpext float %12 to double
%14 = fcmp ogt double %13, 0x3EB0C6F7A0B5ED8D
br i1 %14, label %19, label %15
15: ; preds = %8
%16 = fneg float %12
%17 = fpext float %16 to double
%18 = fcmp ogt double %17, 0x3EB0C6F7A0B5ED8D
br i1 %18, label %19, label %25
19: ; preds = %15, %8
%20 = phi float [ %10, %8 ], [ %9, %15 ]
%21 = phi float [ %11, %8 ], [ %10, %15 ]
%22 = fadd float %20, %21
%23 = fmul float %22, 5.000000e-01
%24 = tail call i32 (i8*, float, float, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), float %21, float %20)
br label %8
25: ; preds = %15
%26 = fpext float %9 to double
%27 = tail call i32 (i8*, float, float, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), float %11, float %10, double %26)
br label %28
28: ; preds = %1, %25
ret void
}