#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fwrite(float *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write_AVS_binary(int /*<<< orphan*/ *, float *, int, float);
int /*<<< orphan*/ write_AVS_header(int /*<<< orphan*/ *, int, float, float,
                                    float, float, float, float, float);
int write_avs(char *fname, float *grd, int dim, float xmin, float ymin,
              float zmin, float xmax, float ymax, float zmax) {
  FILE *fp;
  if ((fname == NULL) || (grd == NULL))
    return 0;
  if ((fp = fopen(fname, "wb")) == NULL) {
    fprintf(stderr, "Could not open file to write\n");
    return 0;
  }
  if (!write_AVS_header(fp, dim, xmin, ymin, zmin, xmax, ymax, zmax, 'f')) {
    fprintf(stderr, "Could not write AVS header information\n");
    return 0;
  }
  if (!write_AVS_binary(fp, grd, dim, 'f')) {
    fprintf(stderr, "Could not write AVS binary information\n");
    return 0;
  }
  fwrite(&xmin, sizeof(float), 1, fp);
  fwrite(&xmax, sizeof(float), 1, fp);
  fwrite(&ymin, sizeof(float), 1, fp);
  fwrite(&ymax, sizeof(float), 1, fp);
  fwrite(&zmin, sizeof(float), 1, fp);
  fwrite(&zmax, sizeof(float), 1, fp);
  fclose(fp);
  return 1;
}


x86 assembly (clang, -O0):
<write_avs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movss  %xmm0,-0x20(%rbp)
movss  %xmm1,-0x24(%rbp)
movss  %xmm2,-0x28(%rbp)
movss  %xmm3,-0x2c(%rbp)
movss  %xmm4,-0x30(%rbp)
movss  %xmm5,-0x34(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a7 <write_avs+0x47>
cmpq   $0x0,-0x18(%rbp)
jne    11b3 <write_avs+0x53>
movl   $0x0,-0x4(%rbp)
jmp    132a <write_avs+0x1ca>
mov    -0x10(%rbp),%rdi
lea    0xe46(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    11f2 <write_avs+0x92>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    132a <write_avs+0x1ca>
mov    -0x40(%rbp),%rdi
mov    -0x1c(%rbp),%esi
movss  -0x20(%rbp),%xmm0
movss  -0x24(%rbp),%xmm1
movss  -0x28(%rbp),%xmm2
movss  -0x2c(%rbp),%xmm3
movss  -0x30(%rbp),%xmm4
movss  -0x34(%rbp),%xmm5
movss  0xde1(%rip),%xmm6
call   1040 <write_AVS_header@plt>
cmp    $0x0,%eax
jne    124e <write_avs+0xee>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
lea    0xde8(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    132a <write_avs+0x1ca>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
movss  0xd9f(%rip),%xmm0
call   1060 <write_AVS_binary@plt>
cmp    $0x0,%eax
jne    1290 <write_avs+0x130>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
lea    0xdce(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    132a <write_avs+0x1ca>
mov    -0x40(%rbp),%rcx
lea    -0x20(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    -0x40(%rbp),%rcx
lea    -0x2c(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    -0x40(%rbp),%rcx
lea    -0x24(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    -0x40(%rbp),%rcx
lea    -0x30(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    -0x40(%rbp),%rcx
lea    -0x28(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    -0x40(%rbp),%rcx
lea    -0x34(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    -0x40(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_avs>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
movss  %xmm0,0x34(%rsp)
movss  %xmm1,0x30(%rsp)
movss  %xmm2,0x2c(%rsp)
movss  %xmm3,0x28(%rsp)
movss  %xmm4,0x24(%rsp)
movss  %xmm5,0x20(%rsp)
xor    %r14d,%r14d
test   %rdi,%rdi
je     1315 <write_avs+0x1b5>
mov    %rsi,%rbp
test   %rsi,%rsi
je     1315 <write_avs+0x1b5>
mov    %edx,%r15d
movss  %xmm0,0x8(%rsp)
movss  %xmm1,0xc(%rsp)
movss  %xmm2,0x10(%rsp)
movss  %xmm3,0x14(%rsp)
movss  %xmm4,0x18(%rsp)
movss  %xmm5,0x1c(%rsp)
lea    0xe30(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     12dc <write_avs+0x17c>
mov    %rax,%rbx
movss  0xe13(%rip),%xmm6
mov    %rax,%rdi
mov    %r15d,%esi
movss  0x8(%rsp),%xmm0
movss  0xc(%rsp),%xmm1
movss  0x10(%rsp),%xmm2
movss  0x14(%rsp),%xmm3
movss  0x18(%rsp),%xmm4
movss  0x1c(%rsp),%xmm5
call   1040 <write_AVS_header@plt>
test   %eax,%eax
je     12ee <write_avs+0x18e>
movss  0xdd4(%rip),%xmm0
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r15d,%edx
call   1060 <write_AVS_binary@plt>
test   %eax,%eax
je     1300 <write_avs+0x1a0>
lea    0x34(%rsp),%rdi
mov    $0x1,%r14d
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
lea    0x28(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
lea    0x30(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
lea    0x24(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
lea    0x2c(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
lea    0x20(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    1315 <write_avs+0x1b5>
mov    0x2cfd(%rip),%rax
mov    (%rax),%edi
lea    0xd1b(%rip),%rsi
jmp    1310 <write_avs+0x1b0>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
lea    0xd27(%rip),%rsi
jmp    1310 <write_avs+0x1b0>
mov    0x2cd9(%rip),%rax
mov    (%rax),%edi
lea    0xd3d(%rip),%rsi
call   1050 <fprintf@plt>
mov    %r14d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"Could not open file to write\0A\00"
@.str.2 = [40 x i8] c"Could not write AVS header information\0A\00"
@.str.3 = [40 x i8] c"Could not write AVS binary information\0A\00"
define i32 @write_avs(i8* %0, float* %1, i32 %2, float %3, float %4, float %5, float %6, float %7, float %8) {
%10 = alloca i32
%11 = alloca i8*
%12 = alloca float*
%13 = alloca i32
%14 = alloca float
%15 = alloca float
%16 = alloca float
%17 = alloca float
%18 = alloca float
%19 = alloca float
%20 = alloca i32*
store i8* %0, i8** %11
store float* %1, float** %12
store i32 %2, i32* %13
store float %3, float* %14
store float %4, float* %15
store float %5, float* %16
store float %6, float* %17
store float %7, float* %18
store float %8, float* %19
%21 = load i8*, i8** %11
%22 = icmp eq i8* %21, null
br i1 %22, label %26, label %23
23: ; preds = %9
%24 = load float*, float** %12
%25 = icmp eq float* %24, null
br i1 %25, label %26, label %27
26: ; preds = %23, %9
store i32 0, i32* %10
br label %72
27: ; preds = %23
%28 = load i8*, i8** %11
%29 = call i32* @fopen(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %29, i32** %20
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %10
br label %72
34: ; preds = %27
%35 = load i32*, i32** %20
%36 = load i32, i32* %13
%37 = load float, float* %14
%38 = load float, float* %15
%39 = load float, float* %16
%40 = load float, float* %17
%41 = load float, float* %18
%42 = load float, float* %19
%43 = call i32 @write_AVS_header(i32* %35, i32 %36, float %37, float %38, float %39, float %40, float %41, float %42, float 1.020000e+02)
%44 = icmp ne i32 %43, 0
br i1 %44, label %48, label %45
45: ; preds = %34
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %10
br label %72
48: ; preds = %34
%49 = load i32*, i32** %20
%50 = load float*, float** %12
%51 = load i32, i32* %13
%52 = call i32 @write_AVS_binary(i32* %49, float* %50, i32 %51, float 1.020000e+02)
%53 = icmp ne i32 %52, 0
br i1 %53, label %57, label %54
54: ; preds = %48
%55 = load i32, i32* @stderr
%56 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %10
br label %72
57: ; preds = %48
%58 = load i32*, i32** %20
%59 = call i32 @fwrite(float* %14, i32 4, i32 1, i32* %58)
%60 = load i32*, i32** %20
%61 = call i32 @fwrite(float* %17, i32 4, i32 1, i32* %60)
%62 = load i32*, i32** %20
%63 = call i32 @fwrite(float* %15, i32 4, i32 1, i32* %62)
%64 = load i32*, i32** %20
%65 = call i32 @fwrite(float* %18, i32 4, i32 1, i32* %64)
%66 = load i32*, i32** %20
%67 = call i32 @fwrite(float* %16, i32 4, i32 1, i32* %66)
%68 = load i32*, i32** %20
%69 = call i32 @fwrite(float* %19, i32 4, i32 1, i32* %68)
%70 = load i32*, i32** %20
%71 = call i32 @fclose(i32* %70)
store i32 1, i32* %10
br label %72
72: ; preds = %57, %54, %45, %31, %26
%73 = load i32, i32* %10
ret i32 %73
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"Could not open file to write\0A\00"
@.str.2 = [40 x i8] c"Could not write AVS header information\0A\00"
@.str.3 = [40 x i8] c"Could not write AVS binary information\0A\00"
define i32 @write_avs(i8* %0, float* %1, i32 %2, float %3, float %4, float %5, float %6, float %7, float %8) {
%10 = alloca float
%11 = alloca float
%12 = alloca float
%13 = alloca float
%14 = alloca float
%15 = alloca float
store float %3, float* %10
store float %4, float* %11
store float %5, float* %12
store float %6, float* %13
store float %7, float* %14
store float %8, float* %15
%16 = icmp eq i8* %0, null
%17 = icmp eq float* %1, null
%18 = or i1 %16, %17
br i1 %18, label %45, label %19
19: ; preds = %9
%20 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i32, i32* @stderr
%24 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %45
25: ; preds = %19
%26 = tail call i32 @write_AVS_header(i32* %20, i32 %2, float %3, float %4, float %5, float %6, float %7, float %8, float 1.020000e+02)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32, i32* @stderr
%30 = tail call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %45
31: ; preds = %25
%32 = tail call i32 @write_AVS_binary(i32* %20, float* %1, i32 %2, float 1.020000e+02)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i32, i32* @stderr
%36 = tail call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0))
br label %45
37: ; preds = %31
%38 = call i32 @fwrite(float* %10, i32 4, i32 1, i32* %20)
%39 = call i32 @fwrite(float* %13, i32 4, i32 1, i32* %20)
%40 = call i32 @fwrite(float* %11, i32 4, i32 1, i32* %20)
%41 = call i32 @fwrite(float* %14, i32 4, i32 1, i32* %20)
%42 = call i32 @fwrite(float* %12, i32 4, i32 1, i32* %20)
%43 = call i32 @fwrite(float* %15, i32 4, i32 1, i32* %20)
%44 = tail call i32 @fclose(i32* %20)
br label %45
45: ; preds = %9, %37, %34, %28, %22
%46 = phi i32 [ 0, %22 ], [ 1, %37 ], [ 0, %34 ], [ 0, %28 ], [ 0, %9 ]
ret i32 %46
}