#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 const *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ remove(char const *);
int save_bmp_rgb565(int /*<<< orphan*/ *, int, int, void *);
int save_bmp_rgb888(int /*<<< orphan*/ *, int, int, void *);
int save_bmp(const char *path, int w, int h, void *pdata, int bpp) {
  int success = -1;
  FILE *hfile = NULL;
  do {
    if (path == NULL || w <= 0 || h <= 0 || pdata == NULL) {
      printf("if (path == NULL || w <= 0 || h <= 0 || pdata == NULL)\n");
      break;
    }
    remove(path);
    hfile = fopen(path, "wb");
    if (hfile == NULL) {
      printf("open(%s) failed!\n", path);
      break;
    }
    printf("%s,", path);
    switch (bpp) {
    case 16:
      success = save_bmp_rgb565(hfile, w, h, pdata);
      break;
    case 24:
      success = save_bmp_rgb888(hfile, w, h, pdata);
      break;
    default:
      printf("error: not support format!\n");
      break;
    }
  } while (0);
  if (hfile != NULL)
    fclose(hfile);
  return success;
}


x86 assembly (clang, -O0):
<save_bmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11b3 <save_bmp+0x53>
cmpl   $0x0,-0xc(%rbp)
jle    11b3 <save_bmp+0x53>
cmpl   $0x0,-0x10(%rbp)
jle    11b3 <save_bmp+0x53>
cmpq   $0x0,-0x18(%rbp)
jne    11c6 <save_bmp+0x66>
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1285 <save_bmp+0x125>
mov    -0x8(%rbp),%rdi
call   1030 <remove@plt>
mov    -0x8(%rbp),%rdi
lea    0xe5e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1205 <save_bmp+0xa5>
mov    -0x8(%rbp),%rsi
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1285 <save_bmp+0x125>
mov    -0x8(%rbp),%rsi
lea    0xe3d(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0x10,%eax
je     123c <save_bmp+0xdc>
jmp    122b <save_bmp+0xcb>
mov    -0x2c(%rbp),%eax
sub    $0x18,%eax
je     1257 <save_bmp+0xf7>
jmp    1272 <save_bmp+0x112>
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x18(%rbp),%rcx
call   1070 <save_bmp_rgb565@plt>
mov    %eax,-0x20(%rbp)
jmp    1280 <save_bmp+0x120>
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x18(%rbp),%rcx
call   1060 <save_bmp_rgb888@plt>
mov    %eax,-0x20(%rbp)
jmp    1280 <save_bmp+0x120>
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1285 <save_bmp+0x125>
cmpq   $0x0,-0x28(%rbp)
je     1299 <save_bmp+0x139>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_bmp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     11e8 <save_bmp+0x78>
mov    %esi,%ebx
test   %esi,%esi
jle    11e8 <save_bmp+0x78>
mov    %edx,%r13d
test   %edx,%edx
jle    11e8 <save_bmp+0x78>
mov    %rcx,%r15
test   %rcx,%rcx
je     11e8 <save_bmp+0x78>
mov    %r8d,%r12d
mov    %rdi,%rbp
call   1030 <remove@plt>
lea    0xe59(%rip),%rsi
mov    %rbp,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     11fb <save_bmp+0x8b>
mov    %rax,%r14
lea    0xe57(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
cmp    $0x18,%r12d
je     1213 <save_bmp+0xa3>
cmp    $0x10,%r12d
jne    1227 <save_bmp+0xb7>
mov    %r14,%rdi
mov    %ebx,%esi
mov    %r13d,%edx
mov    %r15,%rcx
call   1080 <save_bmp_rgb565@plt>
mov    %eax,%ebx
jmp    1238 <save_bmp+0xc8>
lea    0xe45(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebx
jmp    1240 <save_bmp+0xd0>
lea    0xe01(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%ebx
jmp    1240 <save_bmp+0xd0>
mov    %r14,%rdi
mov    %ebx,%esi
mov    %r13d,%edx
mov    %r15,%rcx
call   1070 <save_bmp_rgb888@plt>
mov    %eax,%ebx
jmp    1238 <save_bmp+0xc8>
lea    0xdeb(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebx
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [56 x i8] c"if (path == NULL || w <= 0 || h <= 0 || pdata == NULL)\0A\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [18 x i8] c"open(%s) failed!\0A\00"
@.str.3 = [4 x i8] c"%s,\00"
@.str.4 = [28 x i8] c"error: not support format!\0A\00"
define i32 @save_bmp(i8* %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 %4, i32* %10
store i32 -1, i32* %11
store i32* null, i32** %12
br label %13
13: ; preds = %5
%14 = load i8*, i8** %6
%15 = icmp eq i8* %14, null
br i1 %15, label %25, label %16
16: ; preds = %13
%17 = load i32, i32* %7
%18 = icmp sle i32 %17, 0
br i1 %18, label %25, label %19
19: ; preds = %16
%20 = load i32, i32* %8
%21 = icmp sle i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %19
%23 = load i8*, i8** %9
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %27
25: ; preds = %22, %19, %16, %13
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0))
br label %56
27: ; preds = %22
%28 = load i8*, i8** %6
%29 = call i32 @remove(i8* %28)
%30 = load i8*, i8** %6
%31 = call i32* @fopen(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %31, i32** %12
%32 = load i32*, i32** %12
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %37
34: ; preds = %27
%35 = load i8*, i8** %6
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %35)
br label %56
37: ; preds = %27
%38 = load i8*, i8** %6
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %38)
%40 = load i32, i32* %10
switch i32 %40, label %53 [
i32 16, label %41
i32 24, label %47
]
41: ; preds = %37
%42 = load i32*, i32** %12
%43 = load i32, i32* %7
%44 = load i32, i32* %8
%45 = load i8*, i8** %9
%46 = call i32 @save_bmp_rgb565(i32* %42, i32 %43, i32 %44, i8* %45)
store i32 %46, i32* %11
br label %55
47: ; preds = %37
%48 = load i32*, i32** %12
%49 = load i32, i32* %7
%50 = load i32, i32* %8
%51 = load i8*, i8** %9
%52 = call i32 @save_bmp_rgb888(i32* %48, i32 %49, i32 %50, i8* %51)
store i32 %52, i32* %11
br label %55
53: ; preds = %37
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
br label %55
55: ; preds = %53, %47, %41
br label %56
56: ; preds = %55, %34, %25
%57 = load i32*, i32** %12
%58 = icmp ne i32* %57, null
br i1 %58, label %59, label %62
59: ; preds = %56
%60 = load i32*, i32** %12
%61 = call i32 @fclose(i32* %60)
br label %62
62: ; preds = %59, %56
%63 = load i32, i32* %11
ret i32 %63
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [18 x i8] c"open(%s) failed!\0A\00"
@.str.3 = [4 x i8] c"%s,\00"
@str = [27 x i8] c"error: not support format!\00"
@str.5 = [55 x i8] c"if (path == NULL || w <= 0 || h <= 0 || pdata == NULL)\00"
define i32 @save_bmp(i8* %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = icmp eq i8* %0, null
%7 = icmp slt i32 %1, 1
%8 = or i1 %6, %7
%9 = icmp slt i32 %2, 1
%10 = or i1 %8, %9
%11 = icmp eq i8* %3, null
%12 = or i1 %10, %11
br i1 %12, label %13, label %15
13: ; preds = %5
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str.5, i64 0, i64 0))
br label %32
15: ; preds = %5
%16 = tail call i32 @remove(i8* %0)
%17 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %32
21: ; preds = %15
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %0)
switch i32 %4, label %27 [
i32 16, label %23
i32 24, label %25
]
23: ; preds = %21
%24 = tail call i32 @save_bmp_rgb565(i32* %17, i32 %1, i32 %2, i8* %3)
br label %29
25: ; preds = %21
%26 = tail call i32 @save_bmp_rgb888(i32* %17, i32 %1, i32 %2, i8* %3)
br label %29
27: ; preds = %21
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
br label %29
29: ; preds = %27, %25, %23
%30 = phi i32 [ %24, %23 ], [ %26, %25 ], [ -1, %27 ]
%31 = tail call i32 @fclose(i32* %17)
br label %32
32: ; preds = %13, %19, %29
%33 = phi i32 [ %30, %29 ], [ -1, %13 ], [ -1, %19 ]
ret i32 %33
}