#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 calcMax(char *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int, int, int, int);
size_t strlen(char *);
int write_image(char *file_name, int width, int height, char *image_data,
                int data_length) {
  FILE *fp = NULL;
  size_t arraySize = 0;
  size_t written = 0;
  char header[64 + 1];
  int type = 5;
  int maxVal = 0;
  if ((file_name == NULL) || (image_data == NULL)) {
    return -1;
  }
  if ((data_length < 1) || (width < 1) || (height < 1)) {
    return -1;
  }
  maxVal = calcMax(image_data, data_length);
  arraySize = width * height;
  if (arraySize == data_length) {
    type = 5;
  } else if ((arraySize * 3) == data_length) {
    type = 6;
  } else {
    return -1;
  }
  memset(header, 0, sizeof(char) * (64 + 1));
  snprintf(header, 64, "P%d %d %d %d\n", type, width, height, maxVal);
  fp = fopen(file_name, "wb");
  if (fp == NULL) {
    return -1;
  }
  written = fwrite(header, sizeof(char), strlen(header), fp);
  if (written != strlen(header)) {
    do {
      if (fp != NULL) {
        fclose(fp);
        fp = NULL;
      }
    } while (0);
    ;
    return -1;
  }
  written = fwrite(image_data, sizeof(char), (size_t)data_length, fp);
  if (written != (size_t)data_length) {
    do {
      if (fp != NULL) {
        fclose(fp);
        fp = NULL;
      }
    } while (0);
    ;
    return -1;
  }
  do {
    if (fp != NULL) {
      fclose(fp);
      fp = NULL;
    }
  } while (0);
  ;
  return 0;
}


x86 assembly (clang, -O0):
<write_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movl   $0x5,-0x94(%rbp)
movl   $0x0,-0x98(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11cf <write_image+0x5f>
cmpq   $0x0,-0x20(%rbp)
jne    11db <write_image+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ff <write_image+0x28f>
cmpl   $0x1,-0x24(%rbp)
jl     11f9 <write_image+0x89>
cmpl   $0x1,-0x14(%rbp)
jl     11f9 <write_image+0x89>
cmpl   $0x1,-0x18(%rbp)
jge    1205 <write_image+0x95>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ff <write_image+0x28f>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1070 <calcMax@plt>
mov    %eax,-0x98(%rbp)
mov    -0x14(%rbp),%eax
imul   -0x18(%rbp),%eax
cltq
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmp    %rcx,%rax
jne    1244 <write_image+0xd4>
movl   $0x5,-0x94(%rbp)
jmp    1276 <write_image+0x106>
imul   $0x3,-0x38(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmp    %rcx,%rax
jne    1265 <write_image+0xf5>
movl   $0x6,-0x94(%rbp)
jmp    1271 <write_image+0x101>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ff <write_image+0x28f>
jmp    1276 <write_image+0x106>
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x41,%edx
call   1060 <memset@plt>
lea    -0x90(%rbp),%rdi
mov    -0x94(%rbp),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x18(%rbp),%r9d
mov    -0x98(%rbp),%eax
mov    $0x40,%esi
lea    0xd50(%rip),%rdx
mov    %eax,(%rsp)
call   1050 <snprintf@plt>
mov    -0x10(%rbp),%rdi
lea    0xd4b(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12e3 <write_image+0x173>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ff <write_image+0x28f>
lea    -0x90(%rbp),%rax
mov    %rax,-0xa8(%rbp)
lea    -0x90(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xa8(%rbp),%rdi
mov    %rax,%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0xa0(%rbp)
lea    -0x90(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0xa0(%rbp),%rax
cmp    %rcx,%rax
je     1375 <write_image+0x205>
jmp    1348 <write_image+0x1d8>
cmpq   $0x0,-0x30(%rbp)
je     1364 <write_image+0x1f4>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x30(%rbp)
jmp    1369 <write_image+0x1f9>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ff <write_image+0x28f>
mov    -0x20(%rbp),%rdi
movslq -0x24(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmp    %rcx,%rax
je     13d2 <write_image+0x262>
jmp    13a5 <write_image+0x235>
cmpq   $0x0,-0x30(%rbp)
je     13c1 <write_image+0x251>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x30(%rbp)
jmp    13c6 <write_image+0x256>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ff <write_image+0x28f>
jmp    13d7 <write_image+0x267>
cmpq   $0x0,-0x30(%rbp)
je     13f3 <write_image+0x283>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x30(%rbp)
jmp    13f8 <write_image+0x288>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_image>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    $0xffffffff,%r15d
test   %rdi,%rdi
je     12af <write_image+0x13f>
mov    %rcx,%r12
test   %rcx,%rcx
je     12af <write_image+0x13f>
mov    %edx,%ebx
test   %edx,%edx
jle    12af <write_image+0x13f>
mov    %esi,%r13d
test   %esi,%esi
jle    12af <write_image+0x13f>
mov    %r8d,%r14d
test   %r8d,%r8d
jle    12af <write_image+0x13f>
mov    %rdi,%rbp
mov    %r12,%rdi
mov    %r14d,%esi
call   1070 <calcMax@plt>
mov    %ebx,%edx
imul   %r13d,%edx
mov    %r14d,%ecx
mov    $0x5,%esi
cmp    %r14d,%edx
je     11f0 <write_image+0x80>
movslq %edx,%rdx
lea    (%rdx,%rdx,2),%rdx
mov    $0x6,%esi
cmp    %rcx,%rdx
jne    12af <write_image+0x13f>
mov    %esi,0x14(%rsp)
mov    %rcx,0x18(%rsp)
lea    0x20(%rsp),%rdi
xor    %esi,%esi
mov    %eax,%r14d
mov    $0x41,%edx
call   1060 <memset@plt>
mov    %r14d,(%rsp)
lea    0xde8(%rip),%rdx
lea    0x20(%rsp),%rdi
mov    $0x40,%esi
mov    0x14(%rsp),%ecx
mov    %r13d,%r8d
mov    %ebx,%r9d
call   1050 <snprintf@plt>
lea    0xdd6(%rip),%rsi
mov    %rbp,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     12af <write_image+0x13f>
mov    %rax,%rbx
lea    0x20(%rsp),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
mov    %rax,%rbp
mov    %r14,%rdi
call   1040 <strlen@plt>
cmp    %rax,%rbp
jne    12a7 <write_image+0x137>
mov    %r12,%rdi
mov    $0x1,%esi
mov    0x18(%rsp),%rbp
mov    %rbp,%rdx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
xor    %r15d,%r15d
cmp    %rbp,%rax
setne  %r15b
mov    %rbx,%rdi
call   1030 <fclose@plt>
neg    %r15d
jmp    12af <write_image+0x13f>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r15d,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"P%d %d %d %d\0A\00"
@.str.1 = [3 x i8] c"wb\00"
define i32 @write_image(i8* %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i64
%14 = alloca i64
%15 = alloca [65 x i8]
%16 = alloca i32
%17 = alloca i32
store i8* %0, i8** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i32 %4, i32* %11
store i32* null, i32** %12
store i64 0, i64* %13
store i64 0, i64* %14
store i32 5, i32* %16
store i32 0, i32* %17
%18 = load i8*, i8** %7
%19 = icmp eq i8* %18, null
br i1 %19, label %23, label %20
20: ; preds = %5
%21 = load i8*, i8** %10
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %24
23: ; preds = %20, %5
store i32 -1, i32* %6
br label %117
24: ; preds = %20
%25 = load i32, i32* %11
%26 = icmp slt i32 %25, 1
br i1 %26, label %33, label %27
27: ; preds = %24
%28 = load i32, i32* %8
%29 = icmp slt i32 %28, 1
br i1 %29, label %33, label %30
30: ; preds = %27
%31 = load i32, i32* %9
%32 = icmp slt i32 %31, 1
br i1 %32, label %33, label %34
33: ; preds = %30, %27, %24
store i32 -1, i32* %6
br label %117
34: ; preds = %30
%35 = load i8*, i8** %10
%36 = load i32, i32* %11
%37 = call i32 @calcMax(i8* %35, i32 %36)
store i32 %37, i32* %17
%38 = load i32, i32* %8
%39 = load i32, i32* %9
%40 = mul nsw i32 %38, %39
%41 = sext i32 %40 to i64
store i64 %41, i64* %13
%42 = load i64, i64* %13
%43 = load i32, i32* %11
%44 = sext i32 %43 to i64
%45 = icmp eq i64 %42, %44
br i1 %45, label %46, label %47
46: ; preds = %34
store i32 5, i32* %16
br label %56
47: ; preds = %34
%48 = load i64, i64* %13
%49 = mul i64 %48, 3
%50 = load i32, i32* %11
%51 = sext i32 %50 to i64
%52 = icmp eq i64 %49, %51
br i1 %52, label %53, label %54
53: ; preds = %47
store i32 6, i32* %16
br label %55
54: ; preds = %47
store i32 -1, i32* %6
br label %117
55: ; preds = %53
br label %56
56: ; preds = %55, %46
%57 = getelementptr inbounds [65 x i8], [65 x i8]* %15, i64 0, i64 0
%58 = call i32 @memset(i8* %57, i32 0, i32 65)
%59 = getelementptr inbounds [65 x i8], [65 x i8]* %15, i64 0, i64 0
%60 = load i32, i32* %16
%61 = load i32, i32* %8
%62 = load i32, i32* %9
%63 = load i32, i32* %17
%64 = call i32 @snprintf(i8* %59, i32 64, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %60, i32 %61, i32 %62, i32 %63)
%65 = load i8*, i8** %7
%66 = call i32* @fopen(i8* %65, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %66, i32** %12
%67 = load i32*, i32** %12
%68 = icmp eq i32* %67, null
br i1 %68, label %69, label %70
69: ; preds = %56
store i32 -1, i32* %6
br label %117
70: ; preds = %56
%71 = getelementptr inbounds [65 x i8], [65 x i8]* %15, i64 0, i64 0
%72 = getelementptr inbounds [65 x i8], [65 x i8]* %15, i64 0, i64 0
%73 = call i64 @strlen(i8* %72)
%74 = load i32*, i32** %12
%75 = call i64 @fwrite(i8* %71, i32 1, i64 %73, i32* %74)
store i64 %75, i64* %14
%76 = load i64, i64* %14
%77 = getelementptr inbounds [65 x i8], [65 x i8]* %15, i64 0, i64 0
%78 = call i64 @strlen(i8* %77)
%79 = icmp ne i64 %76, %78
br i1 %79, label %80, label %89
80: ; preds = %70
br label %81
81: ; preds = %80
%82 = load i32*, i32** %12
%83 = icmp ne i32* %82, null
br i1 %83, label %84, label %87
84: ; preds = %81
%85 = load i32*, i32** %12
%86 = call i32 @fclose(i32* %85)
store i32* null, i32** %12
br label %87
87: ; preds = %84, %81
br label %88
88: ; preds = %87
store i32 -1, i32* %6
br label %117
89: ; preds = %70
%90 = load i8*, i8** %10
%91 = load i32, i32* %11
%92 = sext i32 %91 to i64
%93 = load i32*, i32** %12
%94 = call i64 @fwrite(i8* %90, i32 1, i64 %92, i32* %93)
store i64 %94, i64* %14
%95 = load i64, i64* %14
%96 = load i32, i32* %11
%97 = sext i32 %96 to i64
%98 = icmp ne i64 %95, %97
br i1 %98, label %99, label %108
99: ; preds = %89
br label %100
100: ; preds = %99
%101 = load i32*, i32** %12
%102 = icmp ne i32* %101, null
br i1 %102, label %103, label %106
103: ; preds = %100
%104 = load i32*, i32** %12
%105 = call i32 @fclose(i32* %104)
store i32* null, i32** %12
br label %106
106: ; preds = %103, %100
br label %107
107: ; preds = %106
store i32 -1, i32* %6
br label %117
108: ; preds = %89
br label %109
109: ; preds = %108
%110 = load i32*, i32** %12
%111 = icmp ne i32* %110, null
br i1 %111, label %112, label %115
112: ; preds = %109
%113 = load i32*, i32** %12
%114 = call i32 @fclose(i32* %113)
store i32* null, i32** %12
br label %115
115: ; preds = %112, %109
br label %116
116: ; preds = %115
store i32 0, i32* %6
br label %117
117: ; preds = %116, %107, %88, %69, %54, %33, %23
%118 = load i32, i32* %6
ret i32 %118
}

LLVM IR (-O2):
@.str = [14 x i8] c"P%d %d %d %d\0A\00"
@.str.1 = [3 x i8] c"wb\00"
define i32 @write_image(i8* %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca [65 x i8]
%7 = getelementptr inbounds [65 x i8], [65 x i8]* %6, i64 0, i64 0
%8 = icmp eq i8* %0, null
%9 = icmp eq i8* %3, null
%10 = or i1 %8, %9
br i1 %10, label %44, label %11
11: ; preds = %5
%12 = icmp slt i32 %4, 1
%13 = icmp slt i32 %1, 1
%14 = or i1 %13, %12
%15 = icmp slt i32 %2, 1
%16 = or i1 %15, %14
br i1 %16, label %44, label %17
17: ; preds = %11
%18 = tail call i32 @calcMax(i8* %3, i32 %4)
%19 = mul nsw i32 %2, %1
%20 = zext i32 %4 to i64
%21 = icmp eq i32 %19, %4
br i1 %21, label %26, label %22
22: ; preds = %17
%23 = sext i32 %19 to i64
%24 = mul nsw i64 %23, 3
%25 = icmp eq i64 %24, %20
br i1 %25, label %26, label %44
26: ; preds = %22, %17
%27 = phi i32 [ 5, %17 ], [ 6, %22 ]
%28 = call i32 @memset(i8* %7, i32 0, i32 65)
%29 = call i32 @snprintf(i8* %7, i32 64, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %27, i32 %1, i32 %2, i32 %18)
%30 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%31 = icmp eq i32* %30, null
br i1 %31, label %44, label %32
32: ; preds = %26
%33 = call i64 @strlen(i8* %7)
%34 = call i64 @fwrite(i8* %7, i32 1, i64 %33, i32* %30)
%35 = call i64 @strlen(i8* %7)
%36 = icmp eq i64 %34, %35
br i1 %36, label %39, label %37
37: ; preds = %32
%38 = call i32 @fclose(i32* %30)
br label %44
39: ; preds = %32
%40 = call i64 @fwrite(i8* %3, i32 1, i64 %20, i32* %30)
%41 = icmp ne i64 %40, %20
%42 = call i32 @fclose(i32* %30)
%43 = sext i1 %41 to i32
br label %44
44: ; preds = %39, %26, %22, %11, %5, %37
%45 = phi i32 [ -1, %37 ], [ -1, %5 ], [ -1, %11 ], [ -1, %22 ], [ -1, %26 ], [ %43, %39 ]
ret i32 %45
}