#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 */

/* Variables and functions */
int /*<<< orphan*/ _to_buffer(char const *, void *, int, int, int);
void *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
void *bmp_to_buffer(const char *path, int buffer_w, int buffer_h,
                    int buffer_bits) {
  void *buffer = NULL;
  int bytespp = 0;
  int linesize = 0;
  int buffer_size = 0;
  do {
    if (!path) {
      printf("bmp_to_buffer : path is null\n");
      break;
    }
    if (buffer_w <= 0 || buffer_h <= 0) {
      printf("bmp_to_buffer : buffer's width <= 0 or height <= 0\n");
      break;
    }
    if (buffer_bits != 16 && buffer_bits != 24) {
      printf(
          "bmp_to_buffer : buffer bits (%d) only support 16 bits or 24 bits\n");
      break;
    }
    bytespp = buffer_bits / 8;
    linesize = (buffer_w * bytespp + 3) & ~3;
    buffer_size = linesize * buffer_h;
    buffer = malloc(buffer_size);
    if (!buffer) {
      printf("bmp_to_buffer : buffer = malloc(%) failed!\n", buffer_size);
      break;
    }
    _to_buffer(path, buffer, buffer_w, buffer_h, buffer_bits);
  } while (0);
  return buffer;
}


x86 assembly (clang, -O0):
<bmp_to_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1180 <bmp_to_buffer+0x50>
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    123a <bmp_to_buffer+0x10a>
cmpl   $0x0,-0xc(%rbp)
jle    1194 <bmp_to_buffer+0x64>
cmpl   $0x0,-0x10(%rbp)
jg     11a7 <bmp_to_buffer+0x77>
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    123a <bmp_to_buffer+0x10a>
cmpl   $0x10,-0x14(%rbp)
je     11ce <bmp_to_buffer+0x9e>
cmpl   $0x18,-0x14(%rbp)
je     11ce <bmp_to_buffer+0x9e>
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    123a <bmp_to_buffer+0x10a>
mov    -0x14(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%eax
imul   -0x24(%rbp),%eax
add    $0x3,%eax
and    $0xfffffffc,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
imul   -0x10(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1223 <bmp_to_buffer+0xf3>
mov    -0x2c(%rbp),%esi
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    123a <bmp_to_buffer+0x10a>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
mov    -0x14(%rbp),%r8d
call   1030 <_to_buffer@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bmp_to_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     11a7 <bmp_to_buffer+0x67>
mov    %esi,%r12d
test   %esi,%esi
jle    11b0 <bmp_to_buffer+0x70>
mov    %edx,%r15d
test   %edx,%edx
jle    11b0 <bmp_to_buffer+0x70>
mov    %ecx,%r14d
mov    %ecx,%eax
and    $0xfffffff7,%eax
cmp    $0x10,%eax
jne    11d0 <bmp_to_buffer+0x90>
mov    %rdi,%r13
mov    %r14d,%ebp
shr    $0x3,%ebp
imul   %r12d,%ebp
add    $0x3,%ebp
and    $0xfffffffc,%ebp
imul   %r15d,%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11e2 <bmp_to_buffer+0xa2>
mov    %rax,%rbx
mov    %r13,%rdi
mov    %rax,%rsi
mov    %r12d,%edx
mov    %r15d,%ecx
mov    %r14d,%r8d
call   1040 <_to_buffer@plt>
jmp    11be <bmp_to_buffer+0x7e>
lea    0xec0(%rip),%rdi
jmp    11b7 <bmp_to_buffer+0x77>
lea    0xed4(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe29(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11be <bmp_to_buffer+0x7e>
lea    0xe59(%rip),%rdi
xor    %ebx,%ebx
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11be <bmp_to_buffer+0x7e>

LLVM IR (-O0):
@.str = [30 x i8] c"bmp_to_buffer : path is null\0A\00"
@.str.1 = [52 x i8] c"bmp_to_buffer : buffer's width <= 0 or height <= 0\0A\00"
@.str.2 = [66 x i8] c"bmp_to_buffer : buffer bits (%d) only support 16 bits or 24 bits\0A\00"
@.str.3 = [44 x i8] c"bmp_to_buffer : buffer = malloc(%) failed!\0A\00"
define i8* @bmp_to_buffer(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i8* null, i8** %9
store i32 0, i32* %10
store i32 0, i32* %11
store i32 0, i32* %12
br label %13
13: ; preds = %4
%14 = load i8*, i8** %5
%15 = icmp ne i8* %14, null
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %59
18: ; preds = %13
%19 = load i32, i32* %6
%20 = icmp sle i32 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = load i32, i32* %7
%23 = icmp sle i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21, %18
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
br label %59
26: ; preds = %21
%27 = load i32, i32* %8
%28 = icmp ne i32 %27, 16
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = load i32, i32* %8
%31 = icmp ne i32 %30, 24
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.2, i64 0, i64 0))
br label %59
34: ; preds = %29, %26
%35 = load i32, i32* %8
%36 = sdiv i32 %35, 8
store i32 %36, i32* %10
%37 = load i32, i32* %6
%38 = load i32, i32* %10
%39 = mul nsw i32 %37, %38
%40 = add nsw i32 %39, 3
%41 = and i32 %40, -4
store i32 %41, i32* %11
%42 = load i32, i32* %11
%43 = load i32, i32* %7
%44 = mul nsw i32 %42, %43
store i32 %44, i32* %12
%45 = load i32, i32* %12
%46 = call i8* @malloc(i32 %45)
store i8* %46, i8** %9
%47 = load i8*, i8** %9
%48 = icmp ne i8* %47, null
br i1 %48, label %52, label %49
49: ; preds = %34
%50 = load i32, i32* %12
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i32 %50)
br label %59
52: ; preds = %34
%53 = load i8*, i8** %5
%54 = load i8*, i8** %9
%55 = load i32, i32* %6
%56 = load i32, i32* %7
%57 = load i32, i32* %8
%58 = call i32 @_to_buffer(i8* %53, i8* %54, i32 %55, i32 %56, i32 %57)
br label %59
59: ; preds = %52, %49, %32, %24, %16
%60 = load i8*, i8** %9
ret i8* %60
}

LLVM IR (-O2):
@.str.2 = [66 x i8] c"bmp_to_buffer : buffer bits (%d) only support 16 bits or 24 bits\0A\00"
@.str.3 = [44 x i8] c"bmp_to_buffer : buffer = malloc(%) failed!\0A\00"
@str = [29 x i8] c"bmp_to_buffer : path is null\00"
@str.4 = [51 x i8] c"bmp_to_buffer : buffer's width <= 0 or height <= 0\00"
define i8* @bmp_to_buffer(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = icmp eq i8* %0, null
br i1 %5, label %6, label %8
6: ; preds = %4
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %31
8: ; preds = %4
%9 = icmp slt i32 %1, 1
%10 = icmp slt i32 %2, 1
%11 = or i1 %9, %10
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str.4, i64 0, i64 0))
br label %31
14: ; preds = %8
%15 = and i32 %3, -9
%16 = icmp eq i32 %15, 16
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([66 x i8], [66 x i8]* @.str.2, i64 0, i64 0))
br label %31
19: ; preds = %14
%20 = lshr i32 %3, 3
%21 = mul nsw i32 %20, %1
%22 = add nsw i32 %21, 3
%23 = and i32 %22, -4
%24 = mul nsw i32 %23, %2
%25 = tail call i8* @malloc(i32 %24)
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %29
27: ; preds = %19
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i32 %24)
br label %31
29: ; preds = %19
%30 = tail call i32 @_to_buffer(i8* %0, i8* %25, i32 %1, i32 %2, i32 %3)
br label %31
31: ; preds = %29, %27, %17, %12, %6
%32 = phi i8* [ null, %12 ], [ null, %17 ], [ %25, %29 ], [ null, %27 ], [ null, %6 ]
ret i8* %32
}