#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 noise_handler(float, float);
float perlin_two(float x, float y, float gain, int octaves, int hgrid) {
  int i;
  float total = 0.0f;
  float frequency = 1.0f / (float)hgrid;
  float amplitude = gain;
  float lacunarity = 2.0;
  for (i = 0; i < octaves; ++i) {
    total +=
        noise_handler((float)x * frequency, (float)y * frequency) * amplitude;
    frequency *= lacunarity;
    amplitude *= gain;
  }
  return total;
}


x86 assembly (clang, -O0):
<perlin_two>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movss  %xmm0,-0x4(%rbp)
movss  %xmm1,-0x8(%rbp)
movss  %xmm2,-0xc(%rbp)
mov    %edi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x1c(%rbp)
cvtsi2ssl -0x14(%rbp),%xmm1
movss  0xec2(%rip),%xmm0
divss  %xmm1,%xmm0
movss  %xmm0,-0x20(%rbp)
movss  -0xc(%rbp),%xmm0
movss  %xmm0,-0x24(%rbp)
movss  0xea3(%rip),%xmm0
movss  %xmm0,-0x28(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11d1 <perlin_two+0xc1>
movss  -0x4(%rbp),%xmm0
mulss  -0x20(%rbp),%xmm0
movss  -0x8(%rbp),%xmm1
mulss  -0x20(%rbp),%xmm1
call   1030 <noise_handler@plt>
movss  -0x24(%rbp),%xmm2
movss  -0x1c(%rbp),%xmm1
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
movss  %xmm0,-0x1c(%rbp)
movss  -0x28(%rbp),%xmm0
mulss  -0x20(%rbp),%xmm0
movss  %xmm0,-0x20(%rbp)
movss  -0xc(%rbp),%xmm0
mulss  -0x24(%rbp),%xmm0
movss  %xmm0,-0x24(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1169 <perlin_two+0x59>
movss  -0x1c(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<perlin_two>:
push   %rbx
sub    $0x20,%rsp
movss  %xmm2,0xc(%rsp)
test   %edi,%edi
jle    11c8 <perlin_two+0xb8>
mov    %edi,%ebx
movaps %xmm1,%xmm2
movaps %xmm0,%xmm1
xorps  %xmm0,%xmm0
cvtsi2ss %esi,%xmm0
movss  0xec6(%rip),%xmm3
divss  %xmm0,%xmm3
xorps  %xmm0,%xmm0
movss  0xc(%rsp),%xmm4
movss  %xmm2,0x14(%rsp)
movss  %xmm1,0x10(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movss  %xmm4,0x18(%rsp)
movss  %xmm3,0x1c(%rsp)
movss  %xmm0,0x8(%rsp)
movaps %xmm3,%xmm0
mulss  %xmm1,%xmm0
movaps %xmm3,%xmm1
mulss  %xmm2,%xmm1
call   1030 <noise_handler@plt>
movss  0x18(%rsp),%xmm4
movss  0x10(%rsp),%xmm1
movss  0x14(%rsp),%xmm2
mulss  %xmm4,%xmm0
movss  0x8(%rsp),%xmm3
addss  %xmm0,%xmm3
movss  %xmm3,0x8(%rsp)
movss  0x1c(%rsp),%xmm3
movss  0x8(%rsp),%xmm0
addss  %xmm3,%xmm3
mulss  0xc(%rsp),%xmm4
add    $0xffffffff,%ebx
jne    1160 <perlin_two+0x50>
jmp    11cb <perlin_two+0xbb>
xorps  %xmm0,%xmm0
add    $0x20,%rsp
pop    %rbx
ret

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

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