#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*/ printf(char *);
int writeGREY(int /*<<< orphan*/ *, int, int, char *, int);
int writeRGB(int /*<<< orphan*/ *, int, int, char *, int);
int write_image(char *file_name, int width, int height, char *image_data,
                int data_length) {
  int *imagedata = NULL;
  int *imgcopy = NULL;
  int result = 0;
  FILE *img = fopen(file_name, "wb");
  if (img == NULL) {
    printf("Failed to open file.\n");
    return -1;
  }
  if (width * height != data_length) {
    result = writeRGB(img, width, height, image_data, data_length);
  } else {
    result = writeGREY(img, width, height, image_data, data_length);
  }
  fclose(img);
  img = NULL;
  return result;
}


x86 assembly (clang, -O0):
<write_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%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)
movl   $0x0,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe74(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    11b8 <write_image+0x68>
lea    0xe5c(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1218 <write_image+0xc8>
mov    -0x14(%rbp),%eax
imul   -0x18(%rbp),%eax
cmp    -0x24(%rbp),%eax
je     11e7 <write_image+0x97>
mov    -0x48(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    -0x24(%rbp),%r8d
call   1050 <writeRGB@plt>
mov    %eax,-0x3c(%rbp)
jmp    1201 <write_image+0xb1>
mov    -0x48(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    -0x24(%rbp),%r8d
call   1070 <writeGREY@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x48(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x48(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_image>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8d,%r15d
mov    %rcx,%r14
mov    %edx,%r12d
mov    %esi,%ebp
lea    0xe96(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1197 <write_image+0x47>
mov    %rax,%rbx
mov    %r12d,%eax
imul   %ebp,%eax
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r12d,%edx
mov    %r14,%rcx
mov    %r15d,%r8d
cmp    %r15d,%eax
jne    11aa <write_image+0x5a>
call   1070 <writeGREY@plt>
jmp    11af <write_image+0x5f>
lea    0xe65(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
jmp    11b9 <write_image+0x69>
call   1050 <writeRGB@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [22 x i8] c"Failed to open file.\0A\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 i32*
%14 = alloca i32
%15 = 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 i32* null, i32** %13
store i32 0, i32* %14
%16 = load i8*, i8** %7
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %15
%18 = load i32*, i32** %15
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %22
20: ; preds = %5
%21 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %6
br label %46
22: ; preds = %5
%23 = load i32, i32* %8
%24 = load i32, i32* %9
%25 = mul nsw i32 %23, %24
%26 = load i32, i32* %11
%27 = icmp ne i32 %25, %26
br i1 %27, label %28, label %35
28: ; preds = %22
%29 = load i32*, i32** %15
%30 = load i32, i32* %8
%31 = load i32, i32* %9
%32 = load i8*, i8** %10
%33 = load i32, i32* %11
%34 = call i32 @writeRGB(i32* %29, i32 %30, i32 %31, i8* %32, i32 %33)
store i32 %34, i32* %14
br label %42
35: ; preds = %22
%36 = load i32*, i32** %15
%37 = load i32, i32* %8
%38 = load i32, i32* %9
%39 = load i8*, i8** %10
%40 = load i32, i32* %11
%41 = call i32 @writeGREY(i32* %36, i32 %37, i32 %38, i8* %39, i32 %40)
store i32 %41, i32* %14
br label %42
42: ; preds = %35, %28
%43 = load i32*, i32** %15
%44 = call i32 @fclose(i32* %43)
store i32* null, i32** %15
%45 = load i32, i32* %14
store i32 %45, i32* %6
br label %46
46: ; preds = %42, %20
%47 = load i32, i32* %6
ret i32 %47
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@str = [21 x i8] c"Failed to open file.\00"
define i32 @write_image(i8* nocapture %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %20
10: ; preds = %5
%11 = mul nsw i32 %2, %1
%12 = icmp eq i32 %11, %4
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = tail call i32 @writeRGB(i32* %6, i32 %1, i32 %2, i8* %3, i32 %4)
br label %17
15: ; preds = %10
%16 = tail call i32 @writeGREY(i32* %6, i32 %1, i32 %2, i8* %3, i32 %4)
br label %17
17: ; preds = %15, %13
%18 = phi i32 [ %14, %13 ], [ %16, %15 ]
%19 = tail call i32 @fclose(i32* %6)
br label %20
20: ; preds = %17, %8
%21 = phi i32 [ -1, %8 ], [ %18, %17 ]
ret i32 %21
}