#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 RAND_MAX;
int /*<<< orphan*/ average(double *, double *, int, int);
double func(int);
scalar_t__ rand();
int /*<<< orphan*/ save_as_dat(char *, double *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int main() {
  int i, j, nums[] = {4, 10, 20};
  double x[512], out[512];
  for (i = 0; i < 512; i++) {
    x[i] = func(i * 2 * 3.14159265 / 512);
  }
  save_as_dat("51series_given.dat", x, 512);
  for (i = 0; i < 512; i++) {
    x[i] += (((double)rand() / RAND_MAX) - 0.5) / 3;
  }
  save_as_dat("51series_given_noise.dat", x, 512);
  for (j = 0; j < 3; j++) {
    int num = nums[j];
    char fname[100];
    sprintf(fname, "51averaged%d.dat", num);
    average(out, x, num, 512);
    save_as_dat(fname, out, 512);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2090,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xeb7(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xeb5(%rip),%eax
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x200,-0x8(%rbp)
jge    11d0 <main+0x80>
mov    -0x8(%rbp),%eax
shl    $0x1,%eax
cvtsi2sd %eax,%xmm0
movsd  0xe7c(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xe68(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
call   1050 <func@plt>
movslq -0x8(%rbp),%rax
movsd  %xmm0,-0x1020(%rbp,%rax,8)
ff ff
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    117d <main+0x2d>
lea    -0x1020(%rbp),%rsi
lea    0xe4e(%rip),%rdi
mov    $0x200,%edx
call   1030 <save_as_dat@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x200,-0x8(%rbp)
jge    124f <main+0xff>
mov    $0x0,%al
call   1070 <rand@plt>
cvtsi2sd %rax,%xmm0
mov    0x2dd9(%rip),%rax
divsd  (%rax),%xmm0
movsd  0xded(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  0xdd9(%rip),%xmm1
divsd  %xmm1,%xmm0
movslq -0x8(%rbp),%rax
addsd  -0x1020(%rbp,%rax,8),%xmm0
ff ff
movsd  %xmm0,-0x1020(%rbp,%rax,8)
ff ff
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11ef <main+0x9f>
lea    -0x1020(%rbp),%rsi
lea    0xde2(%rip),%rdi
mov    $0x200,%edx
call   1030 <save_as_dat@plt>
movl   $0x0,-0xc(%rbp)
cmpl   $0x3,-0xc(%rbp)
jge    12e3 <main+0x193>
movslq -0xc(%rbp),%rax
mov    -0x18(%rbp,%rax,4),%eax
mov    %eax,-0x2024(%rbp)
lea    -0x2090(%rbp),%rdi
mov    -0x2024(%rbp),%edx
lea    0xdbe(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x2020(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
mov    -0x2024(%rbp),%edx
mov    $0x200,%ecx
call   1040 <average@plt>
lea    -0x2090(%rbp),%rdi
lea    -0x2020(%rbp),%rsi
mov    $0x200,%edx
call   1030 <save_as_dat@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    126e <main+0x11e>
mov    -0x4(%rbp),%eax
add    $0x2090,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2078,%rsp
xor    %ebx,%ebx
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
mulsd  0xe91(%rip),%xmm0
mulsd  0xe91(%rip),%xmm0
cvttsd2si %xmm0,%edi
call   1050 <func@plt>
movsd  %xmm0,0x70(%rsp,%rbx,4)
add    $0x2,%rbx
cmp    $0x400,%rbx
jne    1160 <main+0x10>
lea    0xe86(%rip),%rdi
lea    0x70(%rsp),%rsi
mov    $0x200,%edx
call   1030 <save_as_dat@plt>
mov    0x2e38(%rip),%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1070 <rand@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
divsd  (%r14),%xmm0
addsd  0xe34(%rip),%xmm0
divsd  0xe34(%rip),%xmm0
addsd  0x70(%rsp,%rbx,8),%xmm0
movsd  %xmm0,0x70(%rsp,%rbx,8)
add    $0x1,%rbx
cmp    $0x200,%rbx
jne    11c0 <main+0x70>
lea    0xe2f(%rip),%rdi
lea    0x70(%rsp),%r14
mov    %r14,%rsi
mov    $0x200,%edx
call   1030 <save_as_dat@plt>
lea    0xe2f(%rip),%r12
mov    %rsp,%r15
mov    %r15,%rdi
mov    %r12,%rsi
mov    $0x4,%edx
call   1060 <sprintf@plt>
lea    0x1070(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x4,%edx
mov    $0x200,%ecx
call   1040 <average@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1030 <save_as_dat@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    $0xa,%edx
call   1060 <sprintf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0xa,%edx
mov    $0x200,%ecx
call   1040 <average@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1030 <save_as_dat@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    $0x14,%edx
call   1060 <sprintf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x14,%edx
mov    $0x200,%ecx
call   1040 <average@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1030 <save_as_dat@plt>
xor    %eax,%eax
add    $0x2078,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.main.nums = [3 x i32] [i32 4, i32 10, i32 20]
@.str = [19 x i8] c"51series_given.dat\00"
@RAND_MAX = global double 0.000000e+00
@.str.1 = [25 x i8] c"51series_given_noise.dat\00"
@.str.2 = [17 x i8] c"51averaged%d.dat\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [3 x i32]
%5 = alloca [512 x double]
%6 = alloca [512 x double]
%7 = alloca i32
%8 = alloca [100 x i8]
store i32 0, i32* %1
%9 = bitcast [3 x i32]* %4 to i8*
store i32 0, i32* %2
br label %10
10: ; preds = %24, %0
%11 = load i32, i32* %2
%12 = icmp slt i32 %11, 512
br i1 %12, label %13, label %27
13: ; preds = %10
%14 = load i32, i32* %2
%15 = mul nsw i32 %14, 2
%16 = sitofp i32 %15 to double
%17 = fmul double %16, 0x400921FB53C8D4F1
%18 = fdiv double %17, 5.120000e+02
%19 = fptosi double %18 to i32
%20 = call double @func(i32 %19)
%21 = load i32, i32* %2
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [512 x double], [512 x double]* %5, i64 0, i64 %22
store double %20, double* %23
br label %24
24: ; preds = %13
%25 = load i32, i32* %2
%26 = add nsw i32 %25, 1
store i32 %26, i32* %2
br label %10
27: ; preds = %10
%28 = getelementptr inbounds [512 x double], [512 x double]* %5, i64 0, i64 0
%29 = call i32 @save_as_dat(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), double* %28, i32 512)
store i32 0, i32* %2
br label %30
30: ; preds = %45, %27
%31 = load i32, i32* %2
%32 = icmp slt i32 %31, 512
br i1 %32, label %33, label %48
33: ; preds = %30
%34 = call i64 (...) @rand()
%35 = sitofp i64 %34 to double
%36 = load double, double* @RAND_MAX
%37 = fdiv double %35, %36
%38 = fsub double %37, 5.000000e-01
%39 = fdiv double %38, 3.000000e+00
%40 = load i32, i32* %2
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [512 x double], [512 x double]* %5, i64 0, i64 %41
%43 = load double, double* %42
%44 = fadd double %43, %39
store double %44, double* %42
br label %45
45: ; preds = %33
%46 = load i32, i32* %2
%47 = add nsw i32 %46, 1
store i32 %47, i32* %2
br label %30
48: ; preds = %30
%49 = getelementptr inbounds [512 x double], [512 x double]* %5, i64 0, i64 0
%50 = call i32 @save_as_dat(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), double* %49, i32 512)
store i32 0, i32* %3
br label %51
51: ; preds = %69, %48
%52 = load i32, i32* %3
%53 = icmp slt i32 %52, 3
br i1 %53, label %54, label %72
54: ; preds = %51
%55 = load i32, i32* %3
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds [3 x i32], [3 x i32]* %4, i64 0, i64 %56
%58 = load i32, i32* %57
store i32 %58, i32* %7
%59 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%60 = load i32, i32* %7
%61 = call i32 @sprintf(i8* %59, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %60)
%62 = getelementptr inbounds [512 x double], [512 x double]* %6, i64 0, i64 0
%63 = getelementptr inbounds [512 x double], [512 x double]* %5, i64 0, i64 0
%64 = load i32, i32* %7
%65 = call i32 @average(double* %62, double* %63, i32 %64, i32 512)
%66 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%67 = getelementptr inbounds [512 x double], [512 x double]* %6, i64 0, i64 0
%68 = call i32 @save_as_dat(i8* %66, double* %67, i32 512)
br label %69
69: ; preds = %54
%70 = load i32, i32* %3
%71 = add nsw i32 %70, 1
store i32 %71, i32* %3
br label %51
72: ; preds = %51
%73 = load i32, i32* %1
ret i32 %73
}

LLVM IR (-O2):
@.str = [19 x i8] c"51series_given.dat\00"
@RAND_MAX = global double 0.000000e+00
@.str.1 = [25 x i8] c"51series_given_noise.dat\00"
@.str.2 = [17 x i8] c"51averaged%d.dat\00"
define i32 @main() {
%1 = alloca [512 x double]
%2 = alloca [512 x double]
%3 = alloca [100 x i8]
%4 = bitcast [512 x double]* %1 to i8*
%5 = bitcast [512 x double]* %2 to i8*
br label %6
6: ; preds = %0, %6
%7 = phi i64 [ 0, %0 ], [ %16, %6 ]
%8 = trunc i64 %7 to i32
%9 = shl i32 %8, 1
%10 = sitofp i32 %9 to double
%11 = fmul double %10, 0x400921FB53C8D4F1
%12 = fmul double %11, 0x3F60000000000000
%13 = fptosi double %12 to i32
%14 = tail call double @func(i32 %13)
%15 = getelementptr inbounds [512 x double], [512 x double]* %1, i64 0, i64 %7
store double %14, double* %15
%16 = add nuw nsw i64 %7, 1
%17 = icmp eq i64 %16, 512
br i1 %17, label %18, label %6
18: ; preds = %6
%19 = getelementptr inbounds [512 x double], [512 x double]* %1, i64 0, i64 0
%20 = call i32 @save_as_dat(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), double* %19, i32 512)
br label %21
21: ; preds = %18, %21
%22 = phi i64 [ 0, %18 ], [ %32, %21 ]
%23 = call i64 (...) @rand()
%24 = sitofp i64 %23 to double
%25 = load double, double* @RAND_MAX
%26 = fdiv double %24, %25
%27 = fadd double %26, -5.000000e-01
%28 = fdiv double %27, 3.000000e+00
%29 = getelementptr inbounds [512 x double], [512 x double]* %1, i64 0, i64 %22
%30 = load double, double* %29
%31 = fadd double %30, %28
store double %31, double* %29
%32 = add nuw nsw i64 %22, 1
%33 = icmp eq i64 %32, 512
br i1 %33, label %34, label %21
34: ; preds = %21
%35 = call i32 @save_as_dat(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), double* %19, i32 512)
%36 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%37 = getelementptr inbounds [512 x double], [512 x double]* %2, i64 0, i64 0
%38 = call i32 @sprintf(i8* %36, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 4)
%39 = call i32 @average(double* %37, double* %19, i32 4, i32 512)
%40 = call i32 @save_as_dat(i8* %36, double* %37, i32 512)
%41 = call i32 @sprintf(i8* %36, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 10)
%42 = call i32 @average(double* %37, double* %19, i32 10, i32 512)
%43 = call i32 @save_as_dat(i8* %36, double* %37, i32 512)
%44 = call i32 @sprintf(i8* %36, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 20)
%45 = call i32 @average(double* %37, double* %19, i32 20, i32 512)
%46 = call i32 @save_as_dat(i8* %36, double* %37, i32 512)
ret i32 0
}