#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 pow(int, int);
double pitoprecision(int digits) {
  double pi = 0, pi0 = 0, denom = 1.0, diff;
  double dec = pow(10, digits + 1) * 4;
  char sign = 1;
  while (1) {
    pi += sign / denom;
    diff = dec * (pi - pi0);
    if (diff < 1 && diff > -1) {
      break;
    }
    sign *= -1;
    denom += 2;
    pi0 = pi;
  }
  return pi * 4;
}


x86 assembly (clang, -O0):
<pitoprecision>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  0xecd(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x4(%rbp),%esi
add    $0x1,%esi
mov    $0xa,%edi
call   1030 <pow@plt>
shl    $0x2,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x30(%rbp)
movb   $0x1,-0x31(%rbp)
movsbl -0x31(%rbp),%eax
cvtsi2sd %eax,%xmm0
divsd  -0x20(%rbp),%xmm0
addsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm1
subsd  -0x18(%rbp),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  0xe71(%rip),%xmm0
ucomisd -0x28(%rbp),%xmm0
jbe    11b6 <pitoprecision+0xa6>
movsd  -0x28(%rbp),%xmm0
movsd  0xe61(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    11b6 <pitoprecision+0xa6>
jmp    11e1 <pitoprecision+0xd1>
movsbl -0x31(%rbp),%eax
imul   $0xffffffff,%eax,%eax
mov    %al,-0x31(%rbp)
movsd  0xe48(%rip),%xmm0
addsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    1158 <pitoprecision+0x48>
movsd  0xe2f(%rip),%xmm0
mulsd  -0x10(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pitoprecision>:
push   %rax
lea    0x1(%rdi),%esi
mov    $0xa,%edi
call   1030 <pow@plt>
test   %eax,%eax
je     1191 <pitoprecision+0x81>
shl    $0x2,%eax
cvtsi2sd %eax,%xmm1
mov    $0x1,%eax
movsd  0xed2(%rip),%xmm2
movsd  0xed2(%rip),%xmm3
movsd  0xed2(%rip),%xmm4
movapd %xmm2,%xmm0
movapd %xmm2,%xmm5
xchg   %ax,%ax
movapd %xmm0,%xmm7
addsd  %xmm3,%xmm5
shl    $0x18,%eax
neg    %eax
sar    $0x18,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
divsd  %xmm5,%xmm0
addsd  %xmm7,%xmm0
movapd %xmm0,%xmm6
subsd  %xmm7,%xmm6
mulsd  %xmm1,%xmm6
ucomisd %xmm6,%xmm2
jbe    1150 <pitoprecision+0x40>
ucomisd %xmm4,%xmm6
jbe    1150 <pitoprecision+0x40>
mulsd  0xe71(%rip),%xmm0
pop    %rax
ret
movsd  0xe67(%rip),%xmm0
pop    %rax
ret

LLVM IR (-O0):
define double @pitoprecision(i32 %0) {
%2 = alloca i32
%3 = alloca double
%4 = alloca double
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca i8
store i32 %0, i32* %2
store double 0.000000e+00, double* %3
store double 0.000000e+00, double* %4
store double 1.000000e+00, double* %5
%9 = load i32, i32* %2
%10 = add nsw i32 %9, 1
%11 = call i32 @pow(i32 10, i32 %10)
%12 = mul nsw i32 %11, 4
%13 = sitofp i32 %12 to double
store double %13, double* %7
store i8 1, i8* %8
br label %14
14: ; preds = %1, %33
%15 = load i8, i8* %8
%16 = sext i8 %15 to i32
%17 = sitofp i32 %16 to double
%18 = load double, double* %5
%19 = fdiv double %17, %18
%20 = load double, double* %3
%21 = fadd double %20, %19
store double %21, double* %3
%22 = load double, double* %7
%23 = load double, double* %3
%24 = load double, double* %4
%25 = fsub double %23, %24
%26 = fmul double %22, %25
store double %26, double* %6
%27 = load double, double* %6
%28 = fcmp olt double %27, 1.000000e+00
br i1 %28, label %29, label %33
29: ; preds = %14
%30 = load double, double* %6
%31 = fcmp ogt double %30, -1.000000e+00
br i1 %31, label %32, label %33
32: ; preds = %29
br label %41
33: ; preds = %29, %14
%34 = load i8, i8* %8
%35 = sext i8 %34 to i32
%36 = mul nsw i32 %35, -1
%37 = trunc i32 %36 to i8
store i8 %37, i8* %8
%38 = load double, double* %5
%39 = fadd double %38, 2.000000e+00
store double %39, double* %5
%40 = load double, double* %3
store double %40, double* %4
br label %14
41: ; preds = %32
%42 = load double, double* %3
%43 = fmul double %42, 4.000000e+00
ret double %43
}

LLVM IR (-O2):
define double @pitoprecision(i32 %0) {
%2 = add nsw i32 %0, 1
%3 = tail call i32 @pow(i32 10, i32 %2)
%4 = shl nsw i32 %3, 2
%5 = sitofp i32 %4 to double
%6 = icmp eq i32 %3, 0
br i1 %6, label %24, label %7
7: ; preds = %1, %7
%8 = phi double [ %16, %7 ], [ 1.000000e+00, %1 ]
%9 = phi i32 [ %13, %7 ], [ 1, %1 ]
%10 = phi double [ %11, %7 ], [ 1.000000e+00, %1 ]
%11 = fadd double %10, 2.000000e+00
%12 = mul i32 %9, -16777216
%13 = ashr exact i32 %12, 24
%14 = sitofp i32 %13 to double
%15 = fdiv double %14, %11
%16 = fadd double %8, %15
%17 = fsub double %16, %8
%18 = fmul double %17, %5
%19 = fcmp olt double %18, 1.000000e+00
%20 = fcmp ogt double %18, -1.000000e+00
%21 = and i1 %19, %20
br i1 %21, label %22, label %7
22: ; preds = %7
%23 = fmul double %16, 4.000000e+00
br label %24
24: ; preds = %22, %1
%25 = phi double [ 4.000000e+00, %1 ], [ %23, %22 ]
ret double %25
}