#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 sqrt(double);
double agm(double x, double y) {
  double px, py;
  for (;;) {
    px = x;
    py = y;
    x = 0.5 * (px + py);
    y = sqrt(px * py);
    if ((x <= y) || (x >= px) || (y <= py))
      return x;
  }
}


x86 assembly (clang, -O0):
<agm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x18(%rbp),%xmm1
addsd  -0x20(%rbp),%xmm1
movsd  0xeb8(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x18(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
call   1030 <sqrt@plt>
movsd  %xmm0,-0x10(%rbp)
movsd  -0x8(%rbp),%xmm1
movsd  -0x10(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jae    119d <agm+0x8d>
movsd  -0x8(%rbp),%xmm0
ucomisd -0x18(%rbp),%xmm0
jae    119d <agm+0x8d>
movsd  -0x10(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     11a8 <agm+0x98>
movsd  -0x8(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret
jmp    1122 <agm+0x12>

x86 assembly (clang, -O2):
<agm>:
sub    $0x18,%rsp
movapd %xmm1,%xmm5
movapd %xmm0,%xmm3
movsd  0xedc(%rip),%xmm1
xorpd  %xmm2,%xmm2
nopl   0x0(%rax,%rax,1)
movapd %xmm5,%xmm4
addsd  %xmm3,%xmm4
mulsd  %xmm1,%xmm4
movapd %xmm5,%xmm0
mulsd  %xmm3,%xmm0
ucomisd %xmm2,%xmm0
jb     1160 <agm+0x50>
sqrtsd %xmm0,%xmm0
ucomisd %xmm0,%xmm5
jb     1199 <agm+0x89>
jmp    11ad <agm+0x9d>
cs nopw 0x0(%rax,%rax,1)
movsd  %xmm3,0x10(%rsp)
movsd  %xmm4,0x8(%rsp)
movsd  %xmm5,(%rsp)
call   1030 <sqrt@plt>
movsd  (%rsp),%xmm5
movsd  0x8(%rsp),%xmm4
movsd  0x10(%rsp),%xmm3
xorpd  %xmm2,%xmm2
movsd  0xe6d(%rip),%xmm1
ucomisd %xmm0,%xmm5
jae    11ad <agm+0x9d>
ucomisd %xmm4,%xmm0
jae    11ad <agm+0x9d>
ucomisd %xmm3,%xmm4
movapd %xmm0,%xmm5
movapd %xmm4,%xmm3
jb     1130 <agm+0x20>
movapd %xmm4,%xmm0
add    $0x18,%rsp
ret

LLVM IR (-O0):
define double @agm(double %0, double %1) {
%3 = alloca double
%4 = alloca double
%5 = alloca double
%6 = alloca double
store double %0, double* %3
store double %1, double* %4
br label %7
7: ; preds = %31, %2
%8 = load double, double* %3
store double %8, double* %5
%9 = load double, double* %4
store double %9, double* %6
%10 = load double, double* %5
%11 = load double, double* %6
%12 = fadd double %10, %11
%13 = fmul double 5.000000e-01, %12
store double %13, double* %3
%14 = load double, double* %5
%15 = load double, double* %6
%16 = fmul double %14, %15
%17 = call double @sqrt(double %16)
store double %17, double* %4
%18 = load double, double* %3
%19 = load double, double* %4
%20 = fcmp ole double %18, %19
br i1 %20, label %29, label %21
21: ; preds = %7
%22 = load double, double* %3
%23 = load double, double* %5
%24 = fcmp oge double %22, %23
br i1 %24, label %29, label %25
25: ; preds = %21
%26 = load double, double* %4
%27 = load double, double* %6
%28 = fcmp ole double %26, %27
br i1 %28, label %29, label %31
29: ; preds = %25, %21, %7
%30 = load double, double* %3
ret double %30
31: ; preds = %25
br label %7
}

LLVM IR (-O2):
define double @agm(double %0, double %1) {
br label %3
3: ; preds = %3, %2
%4 = phi double [ %1, %2 ], [ %9, %3 ]
%5 = phi double [ %0, %2 ], [ %7, %3 ]
%6 = fadd double %4, %5
%7 = fmul double %6, 5.000000e-01
%8 = fmul double %4, %5
%9 = tail call double @sqrt(double %8)
%10 = fcmp ugt double %7, %9
%11 = fcmp ult double %7, %5
%12 = and i1 %10, %11
%13 = fcmp ugt double %9, %4
%14 = and i1 %13, %12
br i1 %14, label %3, label %15
15: ; preds = %3
ret double %7
}