#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 fabs(float);
float noise2d(float, float);
float TURB_Turbulence2d(float x, float y, long octaves, long mode) {
  float t, s;
  t = 0.0;
  s = 1.0;
  while (octaves--) {
    s *= 0.5;
    if (mode & 1)
      t += fabs(noise2d(x, y) * s);
    else
      t += noise2d(x, y) * s;
    x *= 2.0;
    y *= 2.0;
  }
  if (t > 1.0)
    t = 1.0;
  if (t < -1.0)
    t = -1.0;
  if (mode & 2)
    t = fabs(t);
  else
    t = .5 + .5 * t;
  return (t);
}


x86 assembly (clang, -O0):
<TURB_Turbulence2d>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movss  %xmm0,-0x4(%rbp)
movss  %xmm1,-0x8(%rbp)
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x1c(%rbp)
movss  0xeb6(%rip),%xmm0
movss  %xmm0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x10(%rbp)
cmp    $0x0,%rax
je     1227 <TURB_Turbulence2d+0x107>
movss  -0x20(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe9f(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
je     11c0 <TURB_Turbulence2d+0xa0>
movss  -0x4(%rbp),%xmm0
movss  -0x8(%rbp),%xmm1
call   1040 <noise2d@plt>
mulss  -0x20(%rbp),%xmm0
call   1030 <fabs@plt>
addss  -0x1c(%rbp),%xmm0
movss  %xmm0,-0x1c(%rbp)
jmp    11e6 <TURB_Turbulence2d+0xc6>
movss  -0x4(%rbp),%xmm0
movss  -0x8(%rbp),%xmm1
call   1040 <noise2d@plt>
movss  -0x20(%rbp),%xmm2
movss  -0x1c(%rbp),%xmm1
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
movss  %xmm0,-0x1c(%rbp)
movss  -0x4(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe29(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x4(%rbp)
movss  -0x8(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe0b(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x8(%rbp)
jmp    114f <TURB_Turbulence2d+0x2f>
movss  -0x1c(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xdd0(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    124f <TURB_Turbulence2d+0x12f>
movss  0xdb6(%rip),%xmm0
movss  %xmm0,-0x1c(%rbp)
movss  -0x1c(%rbp),%xmm0
cvtss2sd %xmm0,%xmm1
movsd  0xdb0(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    1277 <TURB_Turbulence2d+0x157>
movss  0xd92(%rip),%xmm0
movss  %xmm0,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
and    $0x2,%rax
cmp    $0x0,%rax
je     129d <TURB_Turbulence2d+0x17d>
movss  -0x1c(%rbp),%xmm0
call   1030 <fabs@plt>
movss  %xmm0,-0x1c(%rbp)
jmp    12bf <TURB_Turbulence2d+0x19f>
movss  -0x1c(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xd6a(%rip),%xmm1
mulsd  %xmm1,%xmm0
addsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x1c(%rbp)
movss  -0x1c(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<TURB_Turbulence2d>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
test   %rdi,%rdi
je     11a6 <TURB_Turbulence2d+0x96>
mov    %rdi,%rbx
xorps  %xmm2,%xmm2
movss  0xecf(%rip),%xmm3
movss  %xmm3,0x4(%rsp)
jmp    1165 <TURB_Turbulence2d+0x55>
nopl   0x0(%rax)
movaps 0x10(%rsp),%xmm1
addss  %xmm0,%xmm1
movaps %xmm1,%xmm2
movss  0x8(%rsp),%xmm0
addss  %xmm0,%xmm0
movss  0xc(%rsp),%xmm1
addss  %xmm1,%xmm1
test   %rbx,%rbx
je     11a9 <TURB_Turbulence2d+0x99>
movaps %xmm2,0x10(%rsp)
add    $0xffffffffffffffff,%rbx
movss  %xmm0,0x8(%rsp)
movss  %xmm1,0xc(%rsp)
call   1030 <noise2d@plt>
movss  0x4(%rsp),%xmm1
mulss  0xe77(%rip),%xmm1
test   $0x1,%r14b
movss  %xmm1,0x4(%rsp)
mulss  %xmm1,%xmm0
je     1140 <TURB_Turbulence2d+0x30>
andps  0xe6c(%rip),%xmm0
jmp    1140 <TURB_Turbulence2d+0x30>
xorps  %xmm2,%xmm2
movss  0xe4f(%rip),%xmm1
minss  %xmm2,%xmm1
movss  0xe4b(%rip),%xmm0
maxss  %xmm1,%xmm0
test   $0x2,%r14b
jne    11e1 <TURB_Turbulence2d+0xd1>
cvtss2sd %xmm0,%xmm0
movsd  0xe4d(%rip),%xmm1
mulsd  %xmm1,%xmm0
addsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
jmp    11e8 <TURB_Turbulence2d+0xd8>
andps  0xe28(%rip),%xmm0
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define float @TURB_Turbulence2d(float %0, float %1, i64 %2, i64 %3) {
%5 = alloca float
%6 = alloca float
%7 = alloca i64
%8 = alloca i64
%9 = alloca float
%10 = alloca float
store float %0, float* %5
store float %1, float* %6
store i64 %2, i64* %7
store i64 %3, i64* %8
store float 0.000000e+00, float* %9
store float 1.000000e+00, float* %10
br label %11
11: ; preds = %39, %4
%12 = load i64, i64* %7
%13 = add nsw i64 %12, -1
store i64 %13, i64* %7
%14 = icmp ne i64 %12, 0
br i1 %14, label %15, label %48
15: ; preds = %11
%16 = load float, float* %10
%17 = fpext float %16 to double
%18 = fmul double %17, 5.000000e-01
%19 = fptrunc double %18 to float
store float %19, float* %10
%20 = load i64, i64* %8
%21 = and i64 %20, 1
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %32
23: ; preds = %15
%24 = load float, float* %5
%25 = load float, float* %6
%26 = call float @noise2d(float %24, float %25)
%27 = load float, float* %10
%28 = fmul float %26, %27
%29 = call float @fabs(float %28)
%30 = load float, float* %9
%31 = fadd float %30, %29
store float %31, float* %9
br label %39
32: ; preds = %15
%33 = load float, float* %5
%34 = load float, float* %6
%35 = call float @noise2d(float %33, float %34)
%36 = load float, float* %10
%37 = load float, float* %9
%38 = call float @llvm.fmuladd.f32(float %35, float %36, float %37)
store float %38, float* %9
br label %39
39: ; preds = %32, %23
%40 = load float, float* %5
%41 = fpext float %40 to double
%42 = fmul double %41, 2.000000e+00
%43 = fptrunc double %42 to float
store float %43, float* %5
%44 = load float, float* %6
%45 = fpext float %44 to double
%46 = fmul double %45, 2.000000e+00
%47 = fptrunc double %46 to float
store float %47, float* %6
br label %11
48: ; preds = %11
%49 = load float, float* %9
%50 = fpext float %49 to double
%51 = fcmp ogt double %50, 1.000000e+00
br i1 %51, label %52, label %53
52: ; preds = %48
store float 1.000000e+00, float* %9
br label %53
53: ; preds = %52, %48
%54 = load float, float* %9
%55 = fpext float %54 to double
%56 = fcmp olt double %55, -1.000000e+00
br i1 %56, label %57, label %58
57: ; preds = %53
store float -1.000000e+00, float* %9
br label %58
58: ; preds = %57, %53
%59 = load i64, i64* %8
%60 = and i64 %59, 2
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %58
%63 = load float, float* %9
%64 = call float @fabs(float %63)
store float %64, float* %9
br label %70
65: ; preds = %58
%66 = load float, float* %9
%67 = fpext float %66 to double
%68 = call double @llvm.fmuladd.f64(double 5.000000e-01, double %67, double 5.000000e-01)
%69 = fptrunc double %68 to float
store float %69, float* %9
br label %70
70: ; preds = %65, %62
%71 = load float, float* %9
ret float %71
}

LLVM IR (-O2):
define float @TURB_Turbulence2d(float %0, float %1, i64 %2, i64 %3) {
%5 = icmp eq i64 %2, 0
br i1 %5, label %29, label %6
6: ; preds = %4
%7 = and i64 %3, 1
%8 = icmp eq i64 %7, 0
br label %9
9: ; preds = %6, %24
%10 = phi i64 [ %2, %6 ], [ %15, %24 ]
%11 = phi float [ 1.000000e+00, %6 ], [ %16, %24 ]
%12 = phi float [ 0.000000e+00, %6 ], [ %25, %24 ]
%13 = phi float [ %0, %6 ], [ %26, %24 ]
%14 = phi float [ %1, %6 ], [ %27, %24 ]
%15 = add nsw i64 %10, -1
%16 = fmul float %11, 5.000000e-01
%17 = tail call float @noise2d(float %13, float %14)
br i1 %8, label %22, label %18
18: ; preds = %9
%19 = fmul float %16, %17
%20 = tail call float @llvm.fabs.f32(float %19)
%21 = fadd float %12, %20
br label %24
22: ; preds = %9
%23 = tail call float @llvm.fmuladd.f32(float %17, float %16, float %12)
br label %24
24: ; preds = %22, %18
%25 = phi float [ %21, %18 ], [ %23, %22 ]
%26 = fmul float %13, 2.000000e+00
%27 = fmul float %14, 2.000000e+00
%28 = icmp eq i64 %15, 0
br i1 %28, label %29, label %9
29: ; preds = %24, %4
%30 = phi float [ 0.000000e+00, %4 ], [ %25, %24 ]
%31 = fcmp ogt float %30, 1.000000e+00
%32 = select i1 %31, float 1.000000e+00, float %30
%33 = fcmp olt float %32, -1.000000e+00
%34 = select i1 %33, float -1.000000e+00, float %32
%35 = and i64 %3, 2
%36 = icmp eq i64 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %29
%38 = tail call float @llvm.fabs.f32(float %34)
br label %43
39: ; preds = %29
%40 = fpext float %34 to double
%41 = tail call double @llvm.fmuladd.f64(double %40, double 5.000000e-01, double 5.000000e-01)
%42 = fptrunc double %41 to float
br label %43
43: ; preds = %39, %37
%44 = phi float [ %38, %37 ], [ %42, %39 ]
ret float %44
}