#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 */
typedef int /*<<< orphan*/ vectors;

/* Variables and functions */
int /*<<< orphan*/ perlin_gen_vectors(short *, int, unsigned int *);
int /*<<< orphan*/ perlin_point(unsigned int, unsigned int, unsigned int,
                                unsigned int, unsigned int, unsigned int,
                                short *);
scalar_t__ to_amplitude(int /*<<< orphan*/, unsigned int);
void perlin_noise_st(unsigned *dst, unsigned w, unsigned h, unsigned freq,
                     unsigned amp, unsigned seed) {
  signed short vectors[freq * freq * 2];
  unsigned xwl = w / freq, ywl = h / freq;
  unsigned x, y;
  perlin_gen_vectors(vectors, (sizeof(vectors) / sizeof(vectors[0])), &seed);
  for (y = 0; y < h; ++y)
    for (x = 0; x < w; ++x)
      dst[y * w + x] +=
          to_amplitude(perlin_point(x, y, xwl, ywl, freq, freq, vectors), amp);
}


x86 assembly (clang, -O0):
<perlin_noise_st>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
mov    %r9d,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
imul   %eax,%eax
add    %eax,%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
lea    0xf(%rax,%rax,1),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x50(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    -0xc(%rbp),%eax
xor    %edx,%edx
divl   -0x14(%rbp)
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%eax
xor    %edx,%edx
divl   -0x14(%rbp)
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
mov    %ecx,-0x38(%rbp)
shl    $0x1,%rax
shr    $0x1,%rax
mov    %eax,%esi
lea    -0x1c(%rbp),%rdx
call   1040 <perlin_gen_vectors@plt>
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x10(%rbp),%eax
jae    123f <perlin_noise_st+0x10f>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    122c <perlin_noise_st+0xfc>
mov    -0x50(%rbp),%rax
mov    -0x3c(%rbp),%edi
mov    -0x40(%rbp),%esi
mov    -0x34(%rbp),%edx
mov    -0x38(%rbp),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x14(%rbp),%r9d
sub    $0x10,%rsp
mov    %rax,(%rsp)
call   1050 <perlin_point@plt>
add    $0x10,%rsp
mov    %eax,%edi
mov    -0x18(%rbp),%esi
call   1030 <to_amplitude@plt>
mov    %rax,%rsi
mov    -0x8(%rbp),%rax
mov    -0x40(%rbp),%ecx
imul   -0xc(%rbp),%ecx
add    -0x3c(%rbp),%ecx
mov    %ecx,%ecx
mov    (%rax,%rcx,4),%edx
add    %rsi,%rdx
mov    %edx,(%rax,%rcx,4)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11c3 <perlin_noise_st+0x93>
jmp    1231 <perlin_noise_st+0x101>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    11b0 <perlin_noise_st+0x80>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<perlin_noise_st>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %r8d,-0x40(%rbp)
mov    %edx,%r14d
mov    %esi,%eax
mov    %rdi,-0x58(%rbp)
mov    %r9d,-0x44(%rbp)
mov    %ecx,%esi
imul   %esi,%esi
add    %esi,%esi
mov    %rsp,%rdi
lea    (%rsi,%rsi,1),%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
sub    %rdx,%rdi
mov    %rdi,%rsp
mov    %eax,-0x2c(%rbp)
xor    %edx,%edx
div    %ecx
mov    %eax,-0x30(%rbp)
mov    %r14d,%eax
xor    %edx,%edx
mov    %ecx,-0x38(%rbp)
div    %ecx
mov    %eax,-0x34(%rbp)
lea    -0x44(%rbp),%rdx
mov    %rdi,-0x50(%rbp)
call   1040 <perlin_gen_vectors@plt>
xor    %ebx,%ebx
mov    %r14d,-0x3c(%rbp)
test   %r14d,%r14d
je     1229 <perlin_noise_st+0xf9>
mov    -0x2c(%rbp),%eax
xor    %esi,%esi
mov    %rax,-0x60(%rbp)
mov    -0x58(%rbp),%r14
mov    -0x38(%rbp),%r13d
mov    -0x34(%rbp),%r15d
jmp    11cb <perlin_noise_st+0x9b>
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebx
add    %rax,%rsi
cmp    -0x3c(%rbp),%ebx
je     1229 <perlin_noise_st+0xf9>
cmpl   $0x0,-0x2c(%rbp)
mov    -0x50(%rbp),%r10
mov    -0x30(%rbp),%edx
je     11c0 <perlin_noise_st+0x90>
xor    %r12d,%r12d
mov    %rsi,-0x68(%rbp)
nop
sub    $0x8,%rsp
mov    %r12d,%edi
mov    %ebx,%esi
mov    %r15d,%ecx
mov    %r13d,%r8d
mov    %r13d,%r9d
push   %r10
call   1050 <perlin_point@plt>
add    $0x10,%rsp
mov    %eax,%edi
mov    -0x40(%rbp),%esi
call   1030 <to_amplitude@plt>
mov    -0x50(%rbp),%r10
mov    -0x68(%rbp),%rsi
lea    (%rsi,%r12,1),%ecx
add    %eax,(%r14,%rcx,4)
mov    -0x30(%rbp),%edx
mov    -0x60(%rbp),%rax
add    $0x1,%r12
cmp    %r12,%rax
jne    11e0 <perlin_noise_st+0xb0>
jmp    11c0 <perlin_noise_st+0x90>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @perlin_noise_st(i32* %0, i32 %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i64
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
store i32* %0, i32** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 %5, i32* %12
%19 = load i32, i32* %10
%20 = load i32, i32* %10
%21 = mul i32 %19, %20
%22 = mul i32 %21, 2
%23 = zext i32 %22 to i64
%24 = call i8* @llvm.stacksave()
store i8* %24, i8** %13
%25 = alloca i16, i64 %23
store i64 %23, i64* %14
%26 = load i32, i32* %8
%27 = load i32, i32* %10
%28 = udiv i32 %26, %27
store i32 %28, i32* %15
%29 = load i32, i32* %9
%30 = load i32, i32* %10
%31 = udiv i32 %29, %30
store i32 %31, i32* %16
%32 = mul nuw i64 2, %23
%33 = udiv i64 %32, 2
%34 = trunc i64 %33 to i32
%35 = call i32 @perlin_gen_vectors(i16* %25, i32 %34, i32* %12)
store i32 0, i32* %18
br label %36
36: ; preds = %71, %6
%37 = load i32, i32* %18
%38 = load i32, i32* %9
%39 = icmp ult i32 %37, %38
br i1 %39, label %40, label %74
40: ; preds = %36
store i32 0, i32* %17
br label %41
41: ; preds = %67, %40
%42 = load i32, i32* %17
%43 = load i32, i32* %8
%44 = icmp ult i32 %42, %43
br i1 %44, label %45, label %70
45: ; preds = %41
%46 = load i32, i32* %17
%47 = load i32, i32* %18
%48 = load i32, i32* %15
%49 = load i32, i32* %16
%50 = load i32, i32* %10
%51 = load i32, i32* %10
%52 = call i32 @perlin_point(i32 %46, i32 %47, i32 %48, i32 %49, i32 %50, i32 %51, i16* %25)
%53 = load i32, i32* %11
%54 = call i64 @to_amplitude(i32 %52, i32 %53)
%55 = load i32*, i32** %7
%56 = load i32, i32* %18
%57 = load i32, i32* %8
%58 = mul i32 %56, %57
%59 = load i32, i32* %17
%60 = add i32 %58, %59
%61 = zext i32 %60 to i64
%62 = getelementptr inbounds i32, i32* %55, i64 %61
%63 = load i32, i32* %62
%64 = zext i32 %63 to i64
%65 = add nsw i64 %64, %54
%66 = trunc i64 %65 to i32
store i32 %66, i32* %62
br label %67
67: ; preds = %45
%68 = load i32, i32* %17
%69 = add i32 %68, 1
store i32 %69, i32* %17
br label %41
70: ; preds = %41
br label %71
71: ; preds = %70
%72 = load i32, i32* %18
%73 = add i32 %72, 1
store i32 %73, i32* %18
br label %36
74: ; preds = %36
%75 = load i8*, i8** %13
ret void
}

LLVM IR (-O2):
define void @perlin_noise_st(i32* nocapture %0, i32 %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i32
store i32 %5, i32* %7
%8 = shl i32 %3, 1
%9 = mul i32 %8, %3
%10 = zext i32 %9 to i64
%11 = alloca i16, i64 %10
%12 = udiv i32 %1, %3
%13 = udiv i32 %2, %3
%14 = call i32 @perlin_gen_vectors(i16* %11, i32 %9, i32* %7)
%15 = icmp eq i32 %2, 0
br i1 %15, label %39, label %16
16: ; preds = %6
%17 = icmp eq i32 %1, 0
%18 = zext i32 %1 to i64
br label %19
19: ; preds = %16, %36
%20 = phi i32 [ 0, %16 ], [ %37, %36 ]
br i1 %17, label %36, label %21
21: ; preds = %19
%22 = mul i32 %20, %1
br label %23
23: ; preds = %21, %23
%24 = phi i64 [ 0, %21 ], [ %34, %23 ]
%25 = trunc i64 %24 to i32
%26 = call i32 @perlin_point(i32 %25, i32 %20, i32 %12, i32 %13, i32 %3, i32 %3, i16* %11)
%27 = call i64 @to_amplitude(i32 %26, i32 %4)
%28 = add i32 %22, %25
%29 = zext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %0, i64 %29
%31 = load i32, i32* %30
%32 = trunc i64 %27 to i32
%33 = add i32 %31, %32
store i32 %33, i32* %30
%34 = add nuw nsw i64 %24, 1
%35 = icmp eq i64 %34, %18
br i1 %35, label %36, label %23
36: ; preds = %23, %19
%37 = add nuw i32 %20, 1
%38 = icmp eq i32 %37, %2
br i1 %38, label %39, label %19
39: ; preds = %36, %6
ret void
}