#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 */
struct imgImage {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ imgCheckFileExtension(char const *, char *);
int write_img(int /*<<< orphan*/ *, struct imgImage *);
int imgWriteImgBmp(const char *filename, struct imgImage *img_data) {
  int err = 0;
  FILE *file = NULL;
  if (imgCheckFileExtension(filename, ".bmp") != 0)
    return -4;
  if (!img_data)
    return -1;
  file = fopen(filename, "wb");
  if (!file)
    return -1;
  err = write_img(file, img_data);
  fclose(file);
  return err;
}


x86 assembly (clang, -O0):
<imgWriteImgBmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe96(%rip),%rsi
call   1050 <imgCheckFileExtension@plt>
cmp    $0x0,%rax
je     1185 <imgWriteImgBmp+0x45>
movl   $0xfffffffc,-0x4(%rbp)
jmp    11e6 <imgWriteImgBmp+0xa6>
cmpq   $0x0,-0x18(%rbp)
jne    119c <imgWriteImgBmp+0x5c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e6 <imgWriteImgBmp+0xa6>
mov    -0x10(%rbp),%rdi
lea    0xe5e(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c7 <imgWriteImgBmp+0x87>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e6 <imgWriteImgBmp+0xa6>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <write_img@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<imgWriteImgBmp>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xeaf(%rip),%rsi
call   1050 <imgCheckFileExtension@plt>
mov    $0xfffffffc,%ebp
test   %rax,%rax
jne    1196 <imgWriteImgBmp+0x56>
mov    $0xffffffff,%ebp
test   %r14,%r14
je     1196 <imgWriteImgBmp+0x56>
lea    0xe94(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     1196 <imgWriteImgBmp+0x56>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <write_img@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.imgImage = type { i32 }
@.str = [5 x i8] c".bmp\00"
@.str.1 = [3 x i8] c"wb\00"
define i32 @imgWriteImgBmp(i8* %0, %struct.imgImage* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.imgImage*
%6 = alloca i32
%7 = alloca i32*
store i8* %0, i8** %4
store %struct.imgImage* %1, %struct.imgImage** %5
store i32 0, i32* %6
store i32* null, i32** %7
%8 = load i8*, i8** %4
%9 = call i64 @imgCheckFileExtension(i8* %8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -4, i32* %3
br label %29
12: ; preds = %2
%13 = load %struct.imgImage*, %struct.imgImage** %5
%14 = icmp ne %struct.imgImage* %13, null
br i1 %14, label %16, label %15
15: ; preds = %12
store i32 -1, i32* %3
br label %29
16: ; preds = %12
%17 = load i8*, i8** %4
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %7
%19 = load i32*, i32** %7
%20 = icmp ne i32* %19, null
br i1 %20, label %22, label %21
21: ; preds = %16
store i32 -1, i32* %3
br label %29
22: ; preds = %16
%23 = load i32*, i32** %7
%24 = load %struct.imgImage*, %struct.imgImage** %5
%25 = call i32 @write_img(i32* %23, %struct.imgImage* %24)
store i32 %25, i32* %6
%26 = load i32*, i32** %7
%27 = call i32 @fclose(i32* %26)
%28 = load i32, i32* %6
store i32 %28, i32* %3
br label %29
29: ; preds = %22, %21, %15, %11
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
%struct.imgImage = type { i32 }
@.str = [5 x i8] c".bmp\00"
@.str.1 = [3 x i8] c"wb\00"
define i32 @imgWriteImgBmp(i8* %0, %struct.imgImage* %1) {
%3 = tail call i64 @imgCheckFileExtension(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %13
5: ; preds = %2
%6 = icmp eq %struct.imgImage* %1, null
br i1 %6, label %13, label %7
7: ; preds = %5
%8 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %13, label %10
10: ; preds = %7
%11 = tail call i32 @write_img(i32* %8, %struct.imgImage* %1)
%12 = tail call i32 @fclose(i32* %8)
br label %13
13: ; preds = %7, %5, %2, %10
%14 = phi i32 [ %11, %10 ], [ -4, %2 ], [ -1, %5 ], [ -1, %7 ]
ret i32 %14
}