#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 *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  int terms = 0, i = 0, d = 1;
  float pi = 4;
  printf("Enter number of terms used to approximate pi: ");
  scanf("%d", &terms);
  if (terms > 1000)
    printf("ERROR: Value too large");
  else if (terms < 0)
    printf("ERROR: negative value");
  else {
    printf("pi = 4");
    for (i = 1; i < terms; i++) {
      d += 2;
      if ((i + 1) % 2) {
        pi += 4.0 / d;
        printf(" + 4/%d", d);
      } else {
        pi -= 4.0 / d;
        printf(" - 4/%d", d);
      }
    }
    printf("\n=%.21f", pi);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x1,-0x10(%rbp)
movss  0xeb4(%rip),%xmm0
movss  %xmm0,-0x14(%rbp)
lea    0xeb8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xf12(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1040 <scanf@plt>
cmpl   $0x3e8,-0x8(%rbp)
jle    118f <main+0x6f>
lea    0xebc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1291 <main+0x171>
cmpl   $0x0,-0x8(%rbp)
jge    11ac <main+0x8c>
lea    0xeb6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    128c <main+0x16c>
lea    0xeb9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1275 <main+0x155>
mov    -0x10(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     122a <main+0x10a>
cvtsi2sdl -0x10(%rbp),%xmm0
movsd  0xe0e(%rip),%xmm1
divsd  %xmm0,%xmm1
movss  -0x14(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
addsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x14(%rbp)
mov    -0x10(%rbp),%esi
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1262 <main+0x142>
cvtsi2sdl -0x10(%rbp),%xmm0
movsd  0xdd1(%rip),%xmm1
divsd  %xmm0,%xmm1
movss  -0x14(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
subsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x14(%rbp)
mov    -0x10(%rbp),%esi
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1267 <main+0x147>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11c1 <main+0xa1>
movss  -0x14(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xdfe(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
jmp    1291 <main+0x171>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
movl   $0x0,0xc(%rsp)
lea    0xee7(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xf41(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1040 <scanf@plt>
mov    0xc(%rsp),%eax
cmp    $0x3e9,%eax
jl     1168 <main+0x48>
lea    0xeec(%rip),%rdi
jmp    120e <main+0xee>
test   %eax,%eax
js     1207 <main+0xe7>
lea    0xf05(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
cmpl   $0x2,0xc(%rsp)
jl     1217 <main+0xf7>
mov    $0x1,%ebp
movsd  0xe6a(%rip),%xmm0
mov    $0x3,%ebx
lea    0xee9(%rip),%r14
lea    0xeda(%rip),%r15
jmp    11da <main+0xba>
nopl   0x0(%rax,%rax,1)
addsd  %xmm2,%xmm0
movsd  %xmm0,0x10(%rsp)
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
movsd  0x10(%rsp),%xmm0
cvtsd2ss %xmm0,%xmm0
cvtss2sd %xmm0,%xmm0
add    $0x2,%ebx
cmp    0xc(%rsp),%ebp
jge    121f <main+0xff>
add    $0x1,%ebp
xorps  %xmm1,%xmm1
cvtsi2sd %ebx,%xmm1
movsd  0xe14(%rip),%xmm2
divsd  %xmm1,%xmm2
mov    %r15,%rdi
test   $0x1,%bpl
cmove  %r14,%rdi
jne    11b0 <main+0x90>
xorpd  0xe0b(%rip),%xmm2
jmp    11b0 <main+0x90>
lea    0xe58(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    122d <main+0x10d>
movsd  0xde1(%rip),%xmm0
lea    0xe6d(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [47 x i8] c"Enter number of terms used to approximate pi: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [23 x i8] c"ERROR: Value too large\00"
@.str.3 = [22 x i8] c"ERROR: negative value\00"
@.str.4 = [7 x i8] c"pi = 4\00"
@.str.5 = [8 x i8] c" + 4/%d\00"
@.str.6 = [8 x i8] c" - 4/%d\00"
@.str.7 = [8 x i8] c"\0A=%.21f\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca float
store i32 0, i32* %1
store i32 0, i32* %2
store i32 0, i32* %3
store i32 1, i32* %4
store float 4.000000e+00, float* %5
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%8 = load i32, i32* %2
%9 = icmp sgt i32 %8, 1000
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %59
12: ; preds = %0
%13 = load i32, i32* %2
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %58
17: ; preds = %12
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %19
19: ; preds = %51, %17
%20 = load i32, i32* %3
%21 = load i32, i32* %2
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %54
23: ; preds = %19
%24 = load i32, i32* %4
%25 = add nsw i32 %24, 2
store i32 %25, i32* %4
%26 = load i32, i32* %3
%27 = add nsw i32 %26, 1
%28 = srem i32 %27, 2
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %40
30: ; preds = %23
%31 = load i32, i32* %4
%32 = sitofp i32 %31 to double
%33 = fdiv double 4.000000e+00, %32
%34 = load float, float* %5
%35 = fpext float %34 to double
%36 = fadd double %35, %33
%37 = fptrunc double %36 to float
store float %37, float* %5
%38 = load i32, i32* %4
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 %38)
br label %50
40: ; preds = %23
%41 = load i32, i32* %4
%42 = sitofp i32 %41 to double
%43 = fdiv double 4.000000e+00, %42
%44 = load float, float* %5
%45 = fpext float %44 to double
%46 = fsub double %45, %43
%47 = fptrunc double %46 to float
store float %47, float* %5
%48 = load i32, i32* %4
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i32 %48)
br label %50
50: ; preds = %40, %30
br label %51
51: ; preds = %50
%52 = load i32, i32* %3
%53 = add nsw i32 %52, 1
store i32 %53, i32* %3
br label %19
54: ; preds = %19
%55 = load float, float* %5
%56 = fpext float %55 to double
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), double %56)
br label %58
58: ; preds = %54, %15
br label %59
59: ; preds = %58, %10
ret i32 0
}

LLVM IR (-O2):
@.str = [47 x i8] c"Enter number of terms used to approximate pi: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [23 x i8] c"ERROR: Value too large\00"
@.str.3 = [22 x i8] c"ERROR: negative value\00"
@.str.4 = [7 x i8] c"pi = 4\00"
@.str.5 = [8 x i8] c" + 4/%d\00"
@.str.6 = [8 x i8] c" - 4/%d\00"
@.str.7 = [8 x i8] c"\0A=%.21f\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
%4 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%5 = load i32, i32* %1
%6 = icmp sgt i32 %5, 1000
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %39
9: ; preds = %0
%10 = icmp slt i32 %5, 0
br i1 %10, label %11, label %13
11: ; preds = %9
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %39
13: ; preds = %9
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%15 = load i32, i32* %1
%16 = icmp sgt i32 %15, 1
br i1 %16, label %17, label %36
17: ; preds = %13, %17
%18 = phi double [ %33, %17 ], [ 4.000000e+00, %13 ]
%19 = phi i32 [ %21, %17 ], [ 1, %13 ]
%20 = phi i32 [ %22, %17 ], [ 1, %13 ]
%21 = add nuw nsw i32 %19, 2
%22 = add nuw nsw i32 %20, 1
%23 = and i32 %22, 1
%24 = icmp eq i32 %23, 0
%25 = sitofp i32 %21 to double
%26 = fdiv double 4.000000e+00, %25
%27 = select i1 %24, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0)
%28 = fneg double %26
%29 = select i1 %24, double %28, double %26
%30 = fadd double %18, %29
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) %27, i32 %21)
%32 = fptrunc double %30 to float
%33 = fpext float %32 to double
%34 = load i32, i32* %1
%35 = icmp slt i32 %22, %34
br i1 %35, label %17, label %36
36: ; preds = %17, %13
%37 = phi double [ 4.000000e+00, %13 ], [ %33, %17 ]
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), double %37)
br label %39
39: ; preds = %11, %36, %7
ret i32 0
}