#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;
};

/* Variables and functions */
scalar_t__ imgCheckFileExtension(char const *, char *);
int imgWriteImgBmp(char const *, struct imgImage *);
int imgWriteImgPng(char const *, struct imgImage *);
int imgWriteImgTga(char const *, struct imgImage *);
int /*<<< orphan*/ printf(char *, ...);
int imgWriteFile(struct imgImage *img, const char *fn) {
  int err = -1;
  printf("PNG? %s\n", fn);
  if (imgCheckFileExtension(fn, ".png") == 0) {
    printf("good extension\n");
    if ((err = imgWriteImgPng(fn, img)) == 0) {
      printf("SUCCESS\n");
      return err;
    }
    printf("error!\n");
  }
  printf("NOT PNG\n");
  if (imgCheckFileExtension(fn, ".tga") == 0)
    if ((err = imgWriteImgTga(fn, img)) == 0)
      return err;
  if (imgCheckFileExtension(fn, ".bmp") == 0)
    if ((err = imgWriteImgBmp(fn, img)) == 0)
      return err;
  return err;
}


x86 assembly (clang, -O0):
<imgWriteFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe8e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1060 <imgCheckFileExtension@plt>
cmp    $0x0,%rax
jne    11e1 <imgWriteFile+0x91>
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <imgWriteImgPng@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    11d3 <imgWriteFile+0x83>
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    127b <imgWriteFile+0x12b>
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe3e(%rip),%rsi
call   1060 <imgCheckFileExtension@plt>
cmp    $0x0,%rax
jne    1232 <imgWriteFile+0xe2>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <imgWriteImgTga@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    122d <imgWriteFile+0xdd>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    127b <imgWriteFile+0x12b>
jmp    1232 <imgWriteFile+0xe2>
mov    -0x18(%rbp),%rdi
lea    0xe00(%rip),%rsi
call   1060 <imgCheckFileExtension@plt>
cmp    $0x0,%rax
jne    1275 <imgWriteFile+0x125>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <imgWriteImgBmp@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    1270 <imgWriteFile+0x120>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    127b <imgWriteFile+0x12b>
jmp    1275 <imgWriteFile+0x125>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<imgWriteFile>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xe8f(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe8a(%rip),%rsi
mov    %rbx,%rdi
call   1070 <imgCheckFileExtension@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    11ba <imgWriteFile+0x5a>
lea    0xe88(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <imgWriteImgPng@plt>
test   %eax,%eax
je     1212 <imgWriteFile+0xb2>
mov    %eax,%ebp
lea    0xe7a(%rip),%rdi
call   1030 <puts@plt>
lea    0xe57(%rip),%rdi
call   1030 <puts@plt>
lea    0xe41(%rip),%rsi
mov    %rbx,%rdi
call   1070 <imgCheckFileExtension@plt>
test   %rax,%rax
jne    11eb <imgWriteFile+0x8b>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <imgWriteImgTga@plt>
mov    %eax,%ebp
test   %eax,%eax
je     121e <imgWriteFile+0xbe>
lea    0xe21(%rip),%rsi
mov    %rbx,%rdi
call   1070 <imgCheckFileExtension@plt>
test   %rax,%rax
jne    1220 <imgWriteFile+0xc0>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <imgWriteImgBmp@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1220 <imgWriteFile+0xc0>
jmp    121e <imgWriteFile+0xbe>
lea    0xe1d(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.imgImage = type { i32 }
@.str = [9 x i8] c"PNG? %s\0A\00"
@.str.1 = [5 x i8] c".png\00"
@.str.2 = [16 x i8] c"good extension\0A\00"
@.str.3 = [9 x i8] c"SUCCESS\0A\00"
@.str.4 = [8 x i8] c"error!\0A\00"
@.str.5 = [9 x i8] c"NOT PNG\0A\00"
@.str.6 = [5 x i8] c".tga\00"
@.str.7 = [5 x i8] c".bmp\00"
define i32 @imgWriteFile(%struct.imgImage* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.imgImage*
%5 = alloca i8*
%6 = alloca i32
store %struct.imgImage* %0, %struct.imgImage** %4
store i8* %1, i8** %5
store i32 -1, i32* %6
%7 = load i8*, i8** %5
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i8*, i8** %5
%10 = call i64 @imgCheckFileExtension(i8* %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %23
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%14 = load i8*, i8** %5
%15 = load %struct.imgImage*, %struct.imgImage** %4
%16 = call i32 @imgWriteImgPng(i8* %14, %struct.imgImage* %15)
store i32 %16, i32* %6
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%20 = load i32, i32* %6
store i32 %20, i32* %3
br label %50
21: ; preds = %12
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %23
23: ; preds = %21, %2
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%25 = load i8*, i8** %5
%26 = call i64 @imgCheckFileExtension(i8* %25, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %36
28: ; preds = %23
%29 = load i8*, i8** %5
%30 = load %struct.imgImage*, %struct.imgImage** %4
%31 = call i32 @imgWriteImgTga(i8* %29, %struct.imgImage* %30)
store i32 %31, i32* %6
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = load i32, i32* %6
store i32 %34, i32* %3
br label %50
35: ; preds = %28
br label %36
36: ; preds = %35, %23
%37 = load i8*, i8** %5
%38 = call i64 @imgCheckFileExtension(i8* %37, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %48
40: ; preds = %36
%41 = load i8*, i8** %5
%42 = load %struct.imgImage*, %struct.imgImage** %4
%43 = call i32 @imgWriteImgBmp(i8* %41, %struct.imgImage* %42)
store i32 %43, i32* %6
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %40
%46 = load i32, i32* %6
store i32 %46, i32* %3
br label %50
47: ; preds = %40
br label %48
48: ; preds = %47, %36
%49 = load i32, i32* %6
store i32 %49, i32* %3
br label %50
50: ; preds = %48, %45, %33, %18
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
%struct.imgImage = type { i32 }
@.str = [9 x i8] c"PNG? %s\0A\00"
@.str.1 = [5 x i8] c".png\00"
@.str.6 = [5 x i8] c".tga\00"
@.str.7 = [5 x i8] c".bmp\00"
@str = [8 x i8] c"NOT PNG\00"
@str.8 = [15 x i8] c"good extension\00"
@str.9 = [7 x i8] c"error!\00"
@str.10 = [8 x i8] c"SUCCESS\00"
define i32 @imgWriteFile(%struct.imgImage* %0, i8* %1) {
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %1)
%4 = tail call i64 @imgCheckFileExtension(i8* %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %14
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.8, i64 0, i64 0))
%8 = tail call i32 @imgWriteImgPng(i8* %1, %struct.imgImage* %0)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str.10, i64 0, i64 0))
br label %31
12: ; preds = %6
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.9, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = phi i32 [ %8, %12 ], [ -1, %2 ]
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
%17 = tail call i64 @imgCheckFileExtension(i8* %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = tail call i32 @imgWriteImgTga(i8* %1, %struct.imgImage* %0)
%21 = icmp eq i32 %20, 0
br i1 %21, label %31, label %22
22: ; preds = %19, %14
%23 = phi i32 [ %20, %19 ], [ %15, %14 ]
%24 = tail call i64 @imgCheckFileExtension(i8* %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = tail call i32 @imgWriteImgBmp(i8* %1, %struct.imgImage* %0)
%28 = icmp eq i32 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %26, %22
%30 = phi i32 [ %27, %26 ], [ %23, %22 ]
br label %31
31: ; preds = %26, %19, %29, %10
%32 = phi i32 [ 0, %10 ], [ %30, %29 ], [ 0, %19 ], [ 0, %26 ]
ret i32 %32
}