#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 */
float InterpolatedNoise_1(float, float);
float PerlinNoise_2D(float x, float y, float persistence, int octaves) {
  float total, p, frequency, amplitude;
  int i;
  total = 0;
  octaves--;
  for (i = 0; i < octaves; i++) {
    frequency = 2 * i;
    amplitude = persistence * i;
    total += InterpolatedNoise_1(x * frequency, y * frequency) * amplitude;
  }
  return total;
}


x86 assembly (clang, -O0):
<PerlinNoise_2D>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movss  %xmm0,-0x4(%rbp)
movss  %xmm1,-0x8(%rbp)
movss  %xmm2,-0xc(%rbp)
mov    %edi,-0x10(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x14(%rbp)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11ae <PerlinNoise_2D+0x9e>
mov    -0x24(%rbp),%eax
shl    $0x1,%eax
cvtsi2ss %eax,%xmm0
movss  %xmm0,-0x1c(%rbp)
movss  -0xc(%rbp),%xmm0
cvtsi2ssl -0x24(%rbp),%xmm1
mulss  %xmm1,%xmm0
movss  %xmm0,-0x20(%rbp)
movss  -0x4(%rbp),%xmm0
mulss  -0x1c(%rbp),%xmm0
movss  -0x8(%rbp),%xmm1
mulss  -0x1c(%rbp),%xmm1
call   1030 <InterpolatedNoise_1@plt>
movss  -0x20(%rbp),%xmm2
movss  -0x14(%rbp),%xmm1
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
movss  %xmm0,-0x14(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1142 <PerlinNoise_2D+0x32>
movss  -0x14(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PerlinNoise_2D>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
movaps %xmm0,%xmm4
xorps  %xmm0,%xmm0
cmp    $0x2,%edi
jl     11b7 <PerlinNoise_2D+0xa7>
mov    %edi,%r14d
movaps %xmm1,%xmm3
add    $0xffffffff,%r14d
xorps  %xmm0,%xmm0
xor    %ebp,%ebp
xor    %ebx,%ebx
movss  %xmm2,0x18(%rsp)
movss  %xmm1,0x14(%rsp)
movss  %xmm4,0x10(%rsp)
nopw   0x0(%rax,%rax,1)
movss  %xmm0,0xc(%rsp)
xorps  %xmm1,%xmm1
cvtsi2ss %ebp,%xmm1
xorps  %xmm0,%xmm0
cvtsi2ss %ebx,%xmm0
mulss  %xmm2,%xmm0
movss  %xmm0,0x1c(%rsp)
movaps %xmm1,%xmm0
mulss  %xmm4,%xmm0
mulss  %xmm3,%xmm1
call   1030 <InterpolatedNoise_1@plt>
movss  0x10(%rsp),%xmm4
movss  0x14(%rsp),%xmm3
movss  0x18(%rsp),%xmm2
mulss  0x1c(%rsp),%xmm0
movss  0xc(%rsp),%xmm1
addss  %xmm0,%xmm1
movss  %xmm1,0xc(%rsp)
movss  0xc(%rsp),%xmm0
add    $0x1,%ebx
add    $0x2,%ebp
cmp    %ebx,%r14d
jne    1150 <PerlinNoise_2D+0x40>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define float @PerlinNoise_2D(float %0, float %1, float %2, i32 %3) {
%5 = alloca float
%6 = alloca float
%7 = alloca float
%8 = alloca i32
%9 = alloca float
%10 = alloca float
%11 = alloca float
%12 = alloca float
%13 = alloca i32
store float %0, float* %5
store float %1, float* %6
store float %2, float* %7
store i32 %3, i32* %8
store float 0.000000e+00, float* %9
%14 = load i32, i32* %8
%15 = add nsw i32 %14, -1
store i32 %15, i32* %8
store i32 0, i32* %13
br label %16
16: ; preds = %38, %4
%17 = load i32, i32* %13
%18 = load i32, i32* %8
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %41
20: ; preds = %16
%21 = load i32, i32* %13
%22 = mul nsw i32 2, %21
%23 = sitofp i32 %22 to float
store float %23, float* %11
%24 = load float, float* %7
%25 = load i32, i32* %13
%26 = sitofp i32 %25 to float
%27 = fmul float %24, %26
store float %27, float* %12
%28 = load float, float* %5
%29 = load float, float* %11
%30 = fmul float %28, %29
%31 = load float, float* %6
%32 = load float, float* %11
%33 = fmul float %31, %32
%34 = call float @InterpolatedNoise_1(float %30, float %33)
%35 = load float, float* %12
%36 = load float, float* %9
%37 = call float @llvm.fmuladd.f32(float %34, float %35, float %36)
store float %37, float* %9
br label %38
38: ; preds = %20
%39 = load i32, i32* %13
%40 = add nsw i32 %39, 1
store i32 %40, i32* %13
br label %16
41: ; preds = %16
%42 = load float, float* %9
ret float %42
}

LLVM IR (-O2):
define float @PerlinNoise_2D(float %0, float %1, float %2, i32 %3) {
%5 = add i32 %3, -1
%6 = icmp sgt i32 %3, 1
br i1 %6, label %7, label %20
7: ; preds = %4, %7
%8 = phi i32 [ %18, %7 ], [ 0, %4 ]
%9 = phi float [ %17, %7 ], [ 0.000000e+00, %4 ]
%10 = shl nuw nsw i32 %8, 1
%11 = sitofp i32 %10 to float
%12 = sitofp i32 %8 to float
%13 = fmul float %12, %2
%14 = fmul float %11, %0
%15 = fmul float %11, %1
%16 = tail call float @InterpolatedNoise_1(float %14, float %15)
%17 = tail call float @llvm.fmuladd.f32(float %16, float %13, float %9)
%18 = add nuw nsw i32 %8, 1
%19 = icmp eq i32 %18, %5
br i1 %19, label %20, label %7
20: ; preds = %7, %4
%21 = phi float [ 0.000000e+00, %4 ], [ %17, %7 ]
ret float %21
}