#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 noise2(double *);
double PerlinNoise2D(double x, double y, double alpha, double beta, int n) {
  int i;
  double val, sum = 0;
  double p[2], scale = 1;
  p[0] = x;
  p[1] = y;
  for (i = 0; i < n; i++) {
    val = noise2(p);
    sum += val / scale;
    scale *= alpha;
    p[0] *= beta;
    p[1] *= beta;
  }
  return (sum);
}


x86 assembly (clang, -O0):
<PerlinNoise2D>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
movsd  %xmm2,-0x18(%rbp)
movsd  %xmm3,-0x20(%rbp)
mov    %edi,-0x24(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  0xec1(%rip),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    11c8 <PerlinNoise2D+0xb8>
lea    -0x50(%rbp),%rdi
call   1030 <noise2@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
divsd  -0x58(%rbp),%xmm0
addsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x18(%rbp),%xmm0
mulsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x20(%rbp),%xmm0
mulsd  -0x50(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x20(%rbp),%xmm0
mulsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    115f <PerlinNoise2D+0x4f>
movsd  -0x38(%rbp),%xmm0
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PerlinNoise2D>:
push   %r14
push   %rbx
sub    $0x38,%rsp
movapd %xmm3,0x10(%rsp)
movsd  %xmm0,(%rsp)
movsd  %xmm1,0x8(%rsp)
test   %edi,%edi
jle    11a9 <PerlinNoise2D+0x99>
mov    %edi,%ebx
movapd 0x10(%rsp),%xmm0
unpcklpd %xmm0,%xmm0
movapd %xmm0,0x10(%rsp)
xorpd  %xmm1,%xmm1
movsd  0xeb6(%rip),%xmm3
mov    %rsp,%r14
movsd  %xmm2,0x20(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsd  %xmm3,0x28(%rsp)
movsd  %xmm1,0x30(%rsp)
mov    %r14,%rdi
call   1030 <noise2@plt>
movsd  0x28(%rsp),%xmm3
movsd  0x30(%rsp),%xmm1
movsd  0x20(%rsp),%xmm2
divsd  %xmm3,%xmm0
addsd  %xmm0,%xmm1
mulsd  %xmm2,%xmm3
movapd (%rsp),%xmm0
mulpd  0x10(%rsp),%xmm0
movapd %xmm0,(%rsp)
add    $0xffffffff,%ebx
jne    1160 <PerlinNoise2D+0x50>
jmp    11ad <PerlinNoise2D+0x9d>
xorpd  %xmm1,%xmm1
movapd %xmm1,%xmm0
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define double @PerlinNoise2D(double %0, double %1, double %2, double %3, i32 %4) {
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca double
%10 = alloca i32
%11 = alloca i32
%12 = alloca double
%13 = alloca double
%14 = alloca [2 x double]
%15 = alloca double
store double %0, double* %6
store double %1, double* %7
store double %2, double* %8
store double %3, double* %9
store i32 %4, i32* %10
store double 0.000000e+00, double* %13
store double 1.000000e+00, double* %15
%16 = load double, double* %6
%17 = getelementptr inbounds [2 x double], [2 x double]* %14, i64 0, i64 0
store double %16, double* %17
%18 = load double, double* %7
%19 = getelementptr inbounds [2 x double], [2 x double]* %14, i64 0, i64 1
store double %18, double* %19
store i32 0, i32* %11
br label %20
20: ; preds = %43, %5
%21 = load i32, i32* %11
%22 = load i32, i32* %10
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %46
24: ; preds = %20
%25 = getelementptr inbounds [2 x double], [2 x double]* %14, i64 0, i64 0
%26 = call double @noise2(double* %25)
store double %26, double* %12
%27 = load double, double* %12
%28 = load double, double* %15
%29 = fdiv double %27, %28
%30 = load double, double* %13
%31 = fadd double %30, %29
store double %31, double* %13
%32 = load double, double* %8
%33 = load double, double* %15
%34 = fmul double %33, %32
store double %34, double* %15
%35 = load double, double* %9
%36 = getelementptr inbounds [2 x double], [2 x double]* %14, i64 0, i64 0
%37 = load double, double* %36
%38 = fmul double %37, %35
store double %38, double* %36
%39 = load double, double* %9
%40 = getelementptr inbounds [2 x double], [2 x double]* %14, i64 0, i64 1
%41 = load double, double* %40
%42 = fmul double %41, %39
store double %42, double* %40
br label %43
43: ; preds = %24
%44 = load i32, i32* %11
%45 = add nsw i32 %44, 1
store i32 %45, i32* %11
br label %20
46: ; preds = %20
%47 = load double, double* %13
ret double %47
}

LLVM IR (-O2):
define double @PerlinNoise2D(double %0, double %1, double %2, double %3, i32 %4) {
%6 = alloca <2 x double>
%7 = bitcast <2 x double>* %6 to i8*
%8 = getelementptr inbounds <2 x double>, <2 x double>* %6, i64 0, i64 0
store double %0, double* %8
%9 = getelementptr inbounds <2 x double>, <2 x double>* %6, i64 0, i64 1
store double %1, double* %9
%10 = icmp sgt i32 %4, 0
br i1 %10, label %11, label %26
11: ; preds = %5
%12 = insertelement <2 x double> poison, double %3, i64 0
%13 = shufflevector <2 x double> %12, <2 x double> poison, <2 x i32> zeroinitializer
br label %14
14: ; preds = %11, %14
%15 = phi double [ %21, %14 ], [ 1.000000e+00, %11 ]
%16 = phi double [ %20, %14 ], [ 0.000000e+00, %11 ]
%17 = phi i32 [ %24, %14 ], [ 0, %11 ]
%18 = call double @noise2(double* %8)
%19 = fdiv double %18, %15
%20 = fadd double %16, %19
%21 = fmul double %15, %2
%22 = load <2 x double>, <2 x double>* %6
%23 = fmul <2 x double> %22, %13
store <2 x double> %23, <2 x double>* %6
%24 = add nuw nsw i32 %17, 1
%25 = icmp eq i32 %24, %4
br i1 %25, label %26, label %14
26: ; preds = %14, %5
%27 = phi double [ 0.000000e+00, %5 ], [ %20, %14 ]
ret double %27
}