#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);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  int input = 0, i = 0, sign = -1;
  double sum = 1.0, term = 0.0;
  printf("This series prints the sum of the series 1 - 1/3 + 1/9 - 1/27  .... "
         "upto n terms. \n");
  printf("Enter the value of n.\n");
  scanf("%d", &input);
  for (i = 1; i < input; i++) {
    if (i % 2 != 0) {
      term = sign * (1 / pow(3, i));
    } else {
      term = 1 / (pow(3, i));
    }
    sum = sum + term;
  }
  printf("The sum of the series till n is %2.4f. \n", sum);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
movsd  0xea4(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
lea    0xe98(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xede(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xee7(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1040 <scanf@plt>
movl   $0x1,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    122c <main+0xfc>
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     11ef <main+0xbf>
mov    -0x10(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%esi
mov    $0x3,%edi
call   1050 <pow@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
imul   %ecx,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    120f <main+0xdf>
mov    -0xc(%rbp),%esi
mov    $0x3,%edi
call   1050 <pow@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cltd
idiv   %ecx
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x18(%rbp),%xmm0
addsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    119c <main+0x6c>
movsd  -0x18(%rbp),%xmm0
lea    0xe3e(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movl   $0x0,0xc(%rsp)
lea    0xedf(%rip),%rdi
call   1030 <puts@plt>
lea    0xf26(%rip),%rdi
call   1030 <puts@plt>
lea    0xe9b(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1050 <scanf@plt>
cmpl   $0x2,0xc(%rsp)
jl     11dd <main+0x9d>
mov    $0x1,%ebx
movsd  0xe75(%rip),%xmm0
xor    %ebp,%ebp
nopl   (%rax)
movsd  %xmm0,0x10(%rsp)
mov    $0x3,%edi
mov    %ebx,%esi
call   1060 <pow@plt>
lea    0x1(%rax),%ecx
cmp    $0x3,%ecx
cmovae %ebp,%eax
mov    %eax,%ecx
neg    %ecx
test   $0x1,%bl
cmove  %eax,%ecx
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
movsd  0x10(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x10(%rsp)
movsd  0x10(%rsp),%xmm0
add    $0x1,%ebx
cmp    0xc(%rsp),%ebx
jl     1190 <main+0x50>
jmp    11e5 <main+0xa5>
movsd  0xe1b(%rip),%xmm0
lea    0xe1f(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [84 x i8] c"This series prints the sum of the series 1 - 1/3 + 1/9 - 1/27 .... upto n terms. \0A\00"
@.str.1 = [23 x i8] c"Enter the value of n.\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [41 x i8] c"The sum of the series till n is %2.4f. \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca double
%6 = alloca double
store i32 0, i32* %1
store i32 0, i32* %2
store i32 0, i32* %3
store i32 -1, i32* %4
store double 1.000000e+00, double* %5
store double 0.000000e+00, double* %6
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([84 x i8], [84 x i8]* @.str, i64 0, i64 0))
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
store i32 1, i32* %3
br label %10
10: ; preds = %34, %0
%11 = load i32, i32* %3
%12 = load i32, i32* %2
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %37
14: ; preds = %10
%15 = load i32, i32* %3
%16 = srem i32 %15, 2
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %14
%19 = load i32, i32* %4
%20 = load i32, i32* %3
%21 = call i32 @pow(i32 3, i32 %20)
%22 = sdiv i32 1, %21
%23 = mul nsw i32 %19, %22
%24 = sitofp i32 %23 to double
store double %24, double* %6
br label %30
25: ; preds = %14
%26 = load i32, i32* %3
%27 = call i32 @pow(i32 3, i32 %26)
%28 = sdiv i32 1, %27
%29 = sitofp i32 %28 to double
store double %29, double* %6
br label %30
30: ; preds = %25, %18
%31 = load double, double* %5
%32 = load double, double* %6
%33 = fadd double %31, %32
store double %33, double* %5
br label %34
34: ; preds = %30
%35 = load i32, i32* %3
%36 = add nsw i32 %35, 1
store i32 %36, i32* %3
br label %10
37: ; preds = %10
%38 = load double, double* %5
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), double %38)
ret i32 0
}

LLVM IR (-O2):
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [41 x i8] c"The sum of the series till n is %2.4f. \0A\00"
@str = [83 x i8] c"This series prints the sum of the series 1 - 1/3 + 1/9 - 1/27 .... upto n terms. \00"
@str.4 = [22 x i8] c"Enter the value of n.\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([83 x i8], [83 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.4, i64 0, i64 0))
%5 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%6 = load i32, i32* %1
%7 = icmp sgt i32 %6, 1
br i1 %7, label %8, label %24
8: ; preds = %0, %8
%9 = phi double [ %20, %8 ], [ 1.000000e+00, %0 ]
%10 = phi i32 [ %21, %8 ], [ 1, %0 ]
%11 = and i32 %10, 1
%12 = icmp eq i32 %11, 0
%13 = call i32 @pow(i32 3, i32 %10)
%14 = add i32 %13, 1
%15 = icmp ult i32 %14, 3
%16 = select i1 %15, i32 %13, i32 0
%17 = sub nsw i32 0, %16
%18 = select i1 %12, i32 %16, i32 %17
%19 = sitofp i32 %18 to double
%20 = fadd double %9, %19
%21 = add nuw nsw i32 %10, 1
%22 = load i32, i32* %1
%23 = icmp slt i32 %21, %22
br i1 %23, label %8, label %24
24: ; preds = %8, %0
%25 = phi double [ 1.000000e+00, %0 ], [ %20, %8 ]
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), double %25)
ret i32 0
}