#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 */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ printf(char *, float);
int /*<<< orphan*/ stderr;
void write_array(const char *filename, float *img, int width, int height) {
  int i, j;
  FILE *fp;
  float intensity;
  float maxval = -1.0;
  fp = fopen(filename, "wb");
  if (!fp) {
    fprintf(stderr, "can't open file [ %s ] to save.\n", filename);
    exit(-1);
  }
  for (j = 0; j < height; j++) {
    for (i = 0; i < width; i++) {
      intensity = (0.265074126 * (&img[3 * (j * width + i)])[0] +
                   0.670114631 * (&img[3 * (j * width + i)])[1] +
                   0.064811243 * (&img[3 * (j * width + i)])[2]);
      fprintf(fp, "%f ", intensity);
      if (maxval < intensity)
        maxval = intensity;
    }
    fprintf(fp, "\n");
  }
  printf("max = %f\n", maxval);
  fclose(fp);
}


x86 assembly (clang, -O0):
<write_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movss  0xe92(%rip),%xmm0
movss  %xmm0,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b8 <write_array+0x68>
mov    0x2e47(%rip),%rax
movslq (%rax),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe7c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12d7 <write_array+0x187>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12b7 <write_array+0x167>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
imul   -0x14(%rbp),%ecx
add    -0x1c(%rbp),%ecx
imul   $0x3,%ecx,%ecx
movslq %ecx,%rcx
movss  (%rax,%rcx,4),%xmm0
cvtss2sd %xmm0,%xmm1
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
imul   -0x14(%rbp),%ecx
add    -0x1c(%rbp),%ecx
imul   $0x3,%ecx,%ecx
movslq %ecx,%rcx
movss  0x4(%rax,%rcx,4),%xmm0
cvtss2sd %xmm0,%xmm2
movsd  0xdf7(%rip),%xmm0
mulsd  %xmm2,%xmm0
movsd  0xde3(%rip),%xmm2
mulsd  %xmm2,%xmm1
addsd  %xmm0,%xmm1
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
imul   -0x14(%rbp),%ecx
add    -0x1c(%rbp),%ecx
imul   $0x3,%ecx,%ecx
movslq %ecx,%rcx
movss  0x8(%rax,%rcx,4),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xdad(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
movss  -0x2c(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xdc4(%rip),%rsi
mov    $0x1,%al
call   1050 <fprintf@plt>
movss  -0x30(%rbp),%xmm1
movss  -0x2c(%rbp),%xmm0
ucomiss %xmm1,%xmm0
jbe    12a4 <write_array+0x154>
movss  -0x2c(%rbp),%xmm0
movss  %xmm0,-0x30(%rbp)
jmp    12a9 <write_array+0x159>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11d2 <write_array+0x82>
mov    -0x28(%rbp),%rdi
lea    0xd80(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11bf <write_array+0x6f>
movss  -0x30(%rbp),%xmm0
lea    0xd65(%rip),%rdi
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_array>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%r12d
mov    %edx,0x10(%rsp)
mov    %rsi,%rbp
mov    %rdi,%r15
lea    0xe9e(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     12e2 <write_array+0x182>
mov    %rax,%rbx
test   %r12d,%r12d
jle    12ac <write_array+0x14c>
movslq 0x10(%rsp),%rax
mov    %r12d,%ecx
mov    %rcx,0x20(%rsp)
mov    %eax,%ecx
add    $0x8,%rbp
shl    $0x2,%rax
lea    (%rax,%rax,2),%rax
mov    %rax,0x18(%rsp)
shl    $0x2,%rcx
lea    (%rcx,%rcx,2),%r14
movss  0xe34(%rip),%xmm0
xor    %r12d,%r12d
lea    0xe6e(%rip),%r15
jmp    120d <write_array+0xad>
nopl   0x0(%rax,%rax,1)
movss  %xmm0,0xc(%rsp)
mov    $0xa,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
add    $0x1,%r12
add    0x18(%rsp),%rbp
movss  0xc(%rsp),%xmm0
cmp    0x20(%rsp),%r12
je     12ba <write_array+0x15a>
cmpl   $0x0,0x10(%rsp)
jle    11e0 <write_array+0x80>
xor    %r13d,%r13d
nopw   0x0(%rax,%rax,1)
movss  %xmm0,0x14(%rsp)
movss  -0x8(%rbp,%r13,1),%xmm0
movss  -0x4(%rbp,%r13,1),%xmm1
cvtss2sd %xmm0,%xmm0
cvtss2sd %xmm1,%xmm1
mulsd  0xdc4(%rip),%xmm1
mulsd  0xdc4(%rip),%xmm0
addsd  %xmm1,%xmm0
movss  0x0(%rbp,%r13,1),%xmm1
cvtss2sd %xmm1,%xmm1
mulsd  0xdb5(%rip),%xmm1
addsd  %xmm0,%xmm1
xorps  %xmm0,%xmm0
cvtsd2ss %xmm1,%xmm0
movss  %xmm0,0xc(%rsp)
cvtss2sd %xmm0,%xmm0
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x1,%al
call   1060 <fprintf@plt>
movss  0xc(%rsp),%xmm3
maxss  0x14(%rsp),%xmm3
add    $0xc,%r13
movss  %xmm3,0xc(%rsp)
movaps %xmm3,%xmm0
cmp    %r13,%r14
jne    1220 <write_array+0xc0>
jmp    11e6 <write_array+0x86>
movss  0xd4c(%rip),%xmm0
movss  %xmm0,0xc(%rsp)
lea    0xd87(%rip),%rdi
movss  0xc(%rsp),%xmm0
call   1040 <printf@plt>
mov    %rbx,%rdi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>
mov    0x2cf7(%rip),%rax
movslq (%rax),%rdi
lea    0xd30(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [33 x i8] c"can't open file [ %s ] to save.\0A\00"
@.str.2 = [4 x i8] c"%f \00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [10 x i8] c"max = %f\0A\00"
define void @write_array(i8* %0, float* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca float*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca float
%13 = alloca float
store i8* %0, i8** %5
store float* %1, float** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store float -1.000000e+00, float* %13
%14 = load i8*, i8** %5
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %11
%16 = load i32*, i32** %11
%17 = icmp ne i32* %16, null
br i1 %17, label %25, label %18
18: ; preds = %4
%19 = load i32, i32* @stderr
%20 = sext i32 %19 to i64
%21 = inttoptr i64 %20 to i32*
%22 = load i8*, i8** %5
%23 = call i32 (i32*, i8*, ...) @fprintf(i32* %21, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = call i32 @exit(i32 -1)
unreachable
25: ; preds = %4
store i32 0, i32* %10
br label %26
26: ; preds = %92, %25
%27 = load i32, i32* %10
%28 = load i32, i32* %8
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %95
30: ; preds = %26
store i32 0, i32* %9
br label %31
31: ; preds = %86, %30
%32 = load i32, i32* %9
%33 = load i32, i32* %7
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %89
35: ; preds = %31
%36 = load float*, float** %6
%37 = load i32, i32* %10
%38 = load i32, i32* %7
%39 = mul nsw i32 %37, %38
%40 = load i32, i32* %9
%41 = add nsw i32 %39, %40
%42 = mul nsw i32 3, %41
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds float, float* %36, i64 %43
%45 = getelementptr inbounds float, float* %44, i64 0
%46 = load float, float* %45
%47 = fpext float %46 to double
%48 = load float*, float** %6
%49 = load i32, i32* %10
%50 = load i32, i32* %7
%51 = mul nsw i32 %49, %50
%52 = load i32, i32* %9
%53 = add nsw i32 %51, %52
%54 = mul nsw i32 3, %53
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds float, float* %48, i64 %55
%57 = getelementptr inbounds float, float* %56, i64 1
%58 = load float, float* %57
%59 = fpext float %58 to double
%60 = fmul double 0x3FE571943D16EA5B, %59
%61 = call double @llvm.fmuladd.f64(double 0x3FD0F6F9778BE3E0, double %47, double %60)
%62 = load float*, float** %6
%63 = load i32, i32* %10
%64 = load i32, i32* %7
%65 = mul nsw i32 %63, %64
%66 = load i32, i32* %9
%67 = add nsw i32 %65, %66
%68 = mul nsw i32 3, %67
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds float, float* %62, i64 %69
%71 = getelementptr inbounds float, float* %70, i64 2
%72 = load float, float* %71
%73 = fpext float %72 to double
%74 = call double @llvm.fmuladd.f64(double 0x3FB0977839191DAB, double %73, double %61)
%75 = fptrunc double %74 to float
store float %75, float* %12
%76 = load i32*, i32** %11
%77 = load float, float* %12
%78 = fpext float %77 to double
%79 = call i32 (i32*, i8*, ...) @fprintf(i32* %76, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), double %78)
%80 = load float, float* %13
%81 = load float, float* %12
%82 = fcmp olt float %80, %81
br i1 %82, label %83, label %85
83: ; preds = %35
%84 = load float, float* %12
store float %84, float* %13
br label %85
85: ; preds = %83, %35
br label %86
86: ; preds = %85
%87 = load i32, i32* %9
%88 = add nsw i32 %87, 1
store i32 %88, i32* %9
br label %31
89: ; preds = %31
%90 = load i32*, i32** %11
%91 = call i32 (i32*, i8*, ...) @fprintf(i32* %90, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %92
92: ; preds = %89
%93 = load i32, i32* %10
%94 = add nsw i32 %93, 1
store i32 %94, i32* %10
br label %26
95: ; preds = %26
%96 = load float, float* %13
%97 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), float %96)
%98 = load i32*, i32** %11
%99 = call i32 @fclose(i32* %98)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [33 x i8] c"can't open file [ %s ] to save.\0A\00"
@.str.2 = [4 x i8] c"%f \00"
@.str.4 = [10 x i8] c"max = %f\0A\00"
define void @write_array(i8* %0, float* nocapture %1, i32 %2, i32 %3) {
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %14, label %7
7: ; preds = %4
%8 = icmp sgt i32 %3, 0
br i1 %8, label %9, label %54
9: ; preds = %7
%10 = icmp sgt i32 %2, 0
%11 = sext i32 %2 to i64
%12 = zext i32 %3 to i64
%13 = zext i32 %2 to i64
br label %20
14: ; preds = %4
%15 = load i32, i32* @stderr
%16 = sext i32 %15 to i64
%17 = inttoptr i64 %16 to i32*
%18 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %17, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%19 = tail call i32 @exit(i32 -1)
unreachable
20: ; preds = %9, %49
%21 = phi i64 [ 0, %9 ], [ %52, %49 ]
%22 = phi float [ -1.000000e+00, %9 ], [ %50, %49 ]
br i1 %10, label %23, label %49
23: ; preds = %20
%24 = mul nsw i64 %21, %11
br label %25
25: ; preds = %23, %25
%26 = phi i64 [ 0, %23 ], [ %47, %25 ]
%27 = phi float [ %22, %23 ], [ %46, %25 ]
%28 = add nsw i64 %26, %24
%29 = mul nsw i64 %28, 3
%30 = getelementptr inbounds float, float* %1, i64 %29
%31 = load float, float* %30
%32 = fpext float %31 to double
%33 = getelementptr inbounds float, float* %30, i64 1
%34 = load float, float* %33
%35 = fpext float %34 to double
%36 = fmul double %35, 0x3FE571943D16EA5B
%37 = tail call double @llvm.fmuladd.f64(double %32, double 0x3FD0F6F9778BE3E0, double %36)
%38 = getelementptr inbounds float, float* %30, i64 2
%39 = load float, float* %38
%40 = fpext float %39 to double
%41 = tail call double @llvm.fmuladd.f64(double %40, double 0x3FB0977839191DAB, double %37)
%42 = fptrunc double %41 to float
%43 = fpext float %42 to double
%44 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), double %43)
%45 = fcmp olt float %27, %42
%46 = select i1 %45, float %42, float %27
%47 = add nuw nsw i64 %26, 1
%48 = icmp eq i64 %47, %13
br i1 %48, label %49, label %25
49: ; preds = %25, %20
%50 = phi float [ %22, %20 ], [ %46, %25 ]
%51 = tail call i32 @fputc(i32 10, i32* %5)
%52 = add nuw nsw i64 %21, 1
%53 = icmp eq i64 %52, %12
br i1 %53, label %54, label %20
54: ; preds = %49, %7
%55 = phi float [ -1.000000e+00, %7 ], [ %50, %49 ]
%56 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), float %55)
%57 = tail call i32 @fclose(i32* %5)
ret void
}