#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*/ FILE;

/* Variables and functions */
void *alloca(int);
float atof(char *);
void *atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
float perlin_two(float, float, float, int, int);
int /*<<< orphan*/ print_usage();
int /*<<< orphan*/ *stderr;
int main(int argc, char *argv[]) {
  if (argc != 6) {
    print_usage();
    exit(1);
  } else {
    size_t width = atoi(argv[1]);
    size_t height = atoi(argv[2]);
    float gain = atof(argv[3]);
    int octaves = atoi(argv[4]);
    int hgrid = atoi(argv[5]);
    FILE *image_file = fopen("output.txt", "w");
    if (image_file == NULL) {
      fprintf(stderr, "Failed open filestream.\n");
      exit(1);
    }
    float **array = alloca(sizeof(float *) * height);
    size_t row, col;
    for (row = 0; row < height; row++) {
      array[row] = alloca(sizeof(float) * width);
      for (col = 0; col < width; col++) {
        array[row][col] =
            perlin_two((float)col, (float)row, gain, octaves, hgrid);
        fprintf(image_file, "%f ", array[row][col]);
      }
      fprintf(image_file, "\n");
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x6,-0x8(%rbp)
je     11b1 <main+0x31>
mov    $0x0,%al
call   10a0 <print_usage@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1030 <atof@plt>
movss  %xmm0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x28(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x2c(%rbp)
lea    0xdf4(%rip),%rdi
lea    0xdf8(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1249 <main+0xc9>
mov    0x2db2(%rip),%rax
mov    (%rax),%rdi
lea    0xdd5(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <alloca@plt>
mov    %rax,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    13b8 <main+0x238>
mov    -0x18(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <alloca@plt>
mov    %rax,%rdx
mov    -0x40(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
movq   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1395 <main+0x215>
mov    -0x50(%rbp),%rax
mov    %rax,%rdx
shr    %rdx
mov    %eax,%ecx
and    $0x1,%ecx
or     %rdx,%rcx
cvtsi2ss %rcx,%xmm0
addss  %xmm0,%xmm0
cvtsi2ss %rax,%xmm1
movss  %xmm1,-0x58(%rbp)
test   %rax,%rax
movss  %xmm0,-0x54(%rbp)
js     12e3 <main+0x163>
movss  -0x58(%rbp),%xmm0
movss  %xmm0,-0x54(%rbp)
movss  -0x54(%rbp),%xmm0
movss  %xmm0,-0x64(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,%rdx
shr    %rdx
mov    %eax,%ecx
and    $0x1,%ecx
or     %rdx,%rcx
cvtsi2ss %rcx,%xmm0
addss  %xmm0,%xmm0
cvtsi2ss %rax,%xmm1
movss  %xmm1,-0x60(%rbp)
test   %rax,%rax
movss  %xmm0,-0x5c(%rbp)
js     132a <main+0x1aa>
movss  -0x60(%rbp),%xmm0
movss  %xmm0,-0x5c(%rbp)
movss  -0x64(%rbp),%xmm0
movss  -0x5c(%rbp),%xmm1
movss  -0x24(%rbp),%xmm2
mov    -0x28(%rbp),%edi
mov    -0x2c(%rbp),%esi
call   1040 <perlin_two@plt>
mov    -0x40(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    -0x50(%rbp),%rcx
movss  %xmm0,(%rax,%rcx,4)
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    -0x50(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xca9(%rip),%rsi
mov    $0x1,%al
call   1050 <fprintf@plt>
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
jmp    1298 <main+0x118>
mov    -0x38(%rbp),%rdi
lea    0xc84(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    1264 <main+0xe4>
jmp    13bd <main+0x23d>
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x6,%edi
jne    1360 <main+0x1c0>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
mov    %rax,0x10(%rsp)
mov    0x10(%rbx),%rdi
call   1090 <atoi@plt>
mov    %rax,%r14
mov    0x18(%rbx),%rdi
call   1030 <atof@plt>
movss  %xmm0,0xc(%rsp)
mov    0x20(%rbx),%rdi
call   1090 <atoi@plt>
mov    %rax,0x20(%rsp)
mov    0x28(%rbx),%rdi
call   1090 <atoi@plt>
mov    %rax,%r12
lea    0xdfc(%rip),%rdi
lea    0xe00(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1371 <main+0x1d1>
mov    %rax,%r13
lea    0x0(,%r14,8),%edi
call   1070 <alloca@plt>
test   %r14,%r14
je     134f <main+0x1af>
mov    %rax,%rbx
mov    0x10(%rsp),%rax
lea    0x0(,%rax,4),%ecx
mov    %ecx,0x8(%rsp)
cmp    $0x2,%rax
mov    $0x1,%ecx
mov    $0x1,%ebp
cmovae %rax,%rbp
cmp    $0x2,%r14
cmovae %r14,%rcx
mov    %rcx,0x18(%rsp)
xor    %r14d,%r14d
jmp    128c <main+0xec>
nopl   0x0(%rax)
mov    $0xa,%edi
mov    %r13,%rsi
call   1040 <fputc@plt>
add    $0x1,%r14
cmp    0x18(%rsp),%r14
je     134f <main+0x1af>
mov    0x8(%rsp),%edi
call   1070 <alloca@plt>
mov    %rax,(%rbx,%r14,8)
cmpq   $0x0,0x10(%rsp)
je     1270 <main+0xd0>
test   %r14,%r14
js     12b0 <main+0x110>
xorps  %xmm0,%xmm0
cvtsi2ss %r14,%xmm0
jmp    12cb <main+0x12b>
mov    %r14,%rax
shr    %rax
mov    %r14d,%ecx
and    $0x1,%ecx
or     %rax,%rcx
xorps  %xmm0,%xmm0
cvtsi2ss %rcx,%xmm0
addss  %xmm0,%xmm0
movss  %xmm0,0x4(%rsp)
xor    %r15d,%r15d
jmp    132d <main+0x18d>
cs nopw 0x0(%rax,%rax,1)
xorps  %xmm0,%xmm0
cvtsi2ss %r15,%xmm0
movss  0x4(%rsp),%xmm1
movss  0xc(%rsp),%xmm2
mov    0x20(%rsp),%rdi
mov    %r12d,%esi
call   1050 <perlin_two@plt>
mov    (%rbx,%r14,8),%rax
movss  %xmm0,(%rax,%r15,4)
cvtss2sd %xmm0,%xmm0
mov    %r13,%rdi
lea    0xd0d(%rip),%rsi
mov    $0x1,%al
call   1060 <fprintf@plt>
add    $0x1,%r15
cmp    %r15,%rbp
je     1270 <main+0xd0>
test   %r15,%r15
jns    12e0 <main+0x140>
mov    %r15,%rax
shr    %rax
mov    %r15d,%ecx
and    $0x1,%ecx
or     %rax,%rcx
xorps  %xmm0,%xmm0
cvtsi2ss %rcx,%xmm0
addss  %xmm0,%xmm0
jmp    12e8 <main+0x148>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   10c0 <print_usage@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2c68(%rip),%rax
mov    (%rax),%rcx
lea    0xc8b(%rip),%rdi
mov    $0x18,%esi
mov    $0x1,%edx
call   10b0 <fwrite@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"output.txt\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32* null
@.str.2 = [25 x i8] c"Failed open filestream.\0A\00"
@.str.3 = [4 x i8] c"%f \00"
@.str.4 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca float
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca float**
%13 = alloca i64
%14 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 6
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 (...) @print_usage()
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i8* @atoi(i8* %23)
%25 = ptrtoint i8* %24 to i64
store i64 %25, i64* %6
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = call i8* @atoi(i8* %28)
%30 = ptrtoint i8* %29 to i64
store i64 %30, i64* %7
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 3
%33 = load i8*, i8** %32
%34 = call float @atof(i8* %33)
store float %34, float* %8
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 4
%37 = load i8*, i8** %36
%38 = call i8* @atoi(i8* %37)
%39 = ptrtoint i8* %38 to i32
store i32 %39, i32* %9
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 5
%42 = load i8*, i8** %41
%43 = call i8* @atoi(i8* %42)
%44 = ptrtoint i8* %43 to i32
store i32 %44, i32* %10
%45 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %45, i32** %11
%46 = load i32*, i32** %11
%47 = icmp eq i32* %46, null
br i1 %47, label %48, label %52
48: ; preds = %20
%49 = load i32*, i32** @stderr
%50 = call i32 (i32*, i8*, ...) @fprintf(i32* %49, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %20
%53 = load i64, i64* %7
%54 = mul i64 8, %53
%55 = trunc i64 %54 to i32
%56 = call i8* @alloca(i32 %55)
%57 = bitcast i8* %56 to float**
store float** %57, float*** %12
store i64 0, i64* %13
br label %58
58: ; preds = %106, %52
%59 = load i64, i64* %13
%60 = load i64, i64* %7
%61 = icmp ult i64 %59, %60
br i1 %61, label %62, label %109
62: ; preds = %58
%63 = load i64, i64* %6
%64 = mul i64 4, %63
%65 = trunc i64 %64 to i32
%66 = call i8* @alloca(i32 %65)
%67 = bitcast i8* %66 to float*
%68 = load float**, float*** %12
%69 = load i64, i64* %13
%70 = getelementptr inbounds float*, float** %68, i64 %69
store float* %67, float** %70
store i64 0, i64* %14
br label %71
71: ; preds = %100, %62
%72 = load i64, i64* %14
%73 = load i64, i64* %6
%74 = icmp ult i64 %72, %73
br i1 %74, label %75, label %103
75: ; preds = %71
%76 = load i64, i64* %14
%77 = uitofp i64 %76 to float
%78 = load i64, i64* %13
%79 = uitofp i64 %78 to float
%80 = load float, float* %8
%81 = load i32, i32* %9
%82 = load i32, i32* %10
%83 = call float @perlin_two(float %77, float %79, float %80, i32 %81, i32 %82)
%84 = load float**, float*** %12
%85 = load i64, i64* %13
%86 = getelementptr inbounds float*, float** %84, i64 %85
%87 = load float*, float** %86
%88 = load i64, i64* %14
%89 = getelementptr inbounds float, float* %87, i64 %88
store float %83, float* %89
%90 = load i32*, i32** %11
%91 = load float**, float*** %12
%92 = load i64, i64* %13
%93 = getelementptr inbounds float*, float** %91, i64 %92
%94 = load float*, float** %93
%95 = load i64, i64* %14
%96 = getelementptr inbounds float, float* %94, i64 %95
%97 = load float, float* %96
%98 = fpext float %97 to double
%99 = call i32 (i32*, i8*, ...) @fprintf(i32* %90, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), double %98)
br label %100
100: ; preds = %75
%101 = load i64, i64* %14
%102 = add i64 %101, 1
store i64 %102, i64* %14
br label %71
103: ; preds = %71
%104 = load i32*, i32** %11
%105 = call i32 (i32*, i8*, ...) @fprintf(i32* %104, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %106
106: ; preds = %103
%107 = load i64, i64* %13
%108 = add i64 %107, 1
store i64 %108, i64* %13
br label %58
109: ; preds = %58
br label %110
110: ; preds = %109
%111 = load i32, i32* %3
ret i32 %111
}

LLVM IR (-O2):
@.str = [11 x i8] c"output.txt\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32* null
@.str.2 = [25 x i8] c"Failed open filestream.\0A\00"
@.str.3 = [4 x i8] c"%f \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 6
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = tail call i32 (...) @print_usage()
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i8* @atoi(i8* %9)
%11 = ptrtoint i8* %10 to i64
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i8* @atoi(i8* %13)
%15 = ptrtoint i8* %14 to i64
%16 = getelementptr inbounds i8*, i8** %1, i64 3
%17 = load i8*, i8** %16
%18 = tail call float @atof(i8* %17)
%19 = getelementptr inbounds i8*, i8** %1, i64 4
%20 = load i8*, i8** %19
%21 = tail call i8* @atoi(i8* %20)
%22 = ptrtoint i8* %21 to i64
%23 = trunc i64 %22 to i32
%24 = getelementptr inbounds i8*, i8** %1, i64 5
%25 = load i8*, i8** %24
%26 = tail call i8* @atoi(i8* %25)
%27 = ptrtoint i8* %26 to i64
%28 = trunc i64 %27 to i32
%29 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %35
31: ; preds = %7
%32 = load i32*, i32** @stderr
%33 = tail call i64 @fwrite(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i64 24, i64 1, i32* %32)
%34 = tail call i32 @exit(i32 1)
unreachable
35: ; preds = %7
%36 = trunc i64 %15 to i32
%37 = shl i32 %36, 3
%38 = tail call i8* @alloca(i32 %37)
%39 = bitcast i8* %38 to float**
%40 = icmp eq i8* %14, null
br i1 %40, label %68, label %41
41: ; preds = %35
%42 = trunc i64 %11 to i32
%43 = shl i32 %42, 2
%44 = icmp eq i8* %10, null
%45 = call i64 @llvm.umax.i64(i64 %11, i64 1)
%46 = call i64 @llvm.umax.i64(i64 %15, i64 1)
br label %47
47: ; preds = %41, %64
%48 = phi i64 [ 0, %41 ], [ %66, %64 ]
%49 = tail call i8* @alloca(i32 %43)
%50 = getelementptr inbounds float*, float** %39, i64 %48
%51 = bitcast float** %50 to i8**
store i8* %49, i8** %51
br i1 %44, label %64, label %52
52: ; preds = %47
%53 = uitofp i64 %48 to float
br label %54
54: ; preds = %52, %54
%55 = phi i64 [ 0, %52 ], [ %62, %54 ]
%56 = uitofp i64 %55 to float
%57 = tail call float @perlin_two(float %56, float %53, float %18, i32 %23, i32 %28)
%58 = load float*, float** %50
%59 = getelementptr inbounds float, float* %58, i64 %55
store float %57, float* %59
%60 = fpext float %57 to double
%61 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), double %60)
%62 = add nuw i64 %55, 1
%63 = icmp eq i64 %62, %45
br i1 %63, label %64, label %54
64: ; preds = %54, %47
%65 = tail call i32 @fputc(i32 10, i32* %29)
%66 = add nuw i64 %48, 1
%67 = icmp eq i64 %66, %46
br i1 %67, label %68, label %47
68: ; preds = %64, %35
ret i32 0
}