#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 */
scalar_t__ bgra8888_to_rgb565(void const *, int, int, void *);
int /*<<< orphan*/ free(void *);
void *malloc(int);
void *bgra8888_to_rgb565_buffer(const void *psrc, int w, int h) {
  int size = h * (((w * 2) + 3) & ~3);
  void *pdst = NULL;
  if (psrc && w > 0 && h > 0) {
    pdst = malloc(size);
    if (pdst) {
      if (bgra8888_to_rgb565(psrc, w, h, pdst)) {
        free(pdst);
        pdst = NULL;
      }
    }
  }
  return pdst;
}


x86 assembly (clang, -O0):
<bgra8888_to_rgb565_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
shl    $0x1,%ecx
add    $0x3,%ecx
and    $0xfffffffc,%ecx
imul   %ecx,%eax
mov    %eax,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11cd <bgra8888_to_rgb565_buffer+0x9d>
cmpl   $0x0,-0xc(%rbp)
jle    11cd <bgra8888_to_rgb565_buffer+0x9d>
cmpl   $0x0,-0x10(%rbp)
jle    11cd <bgra8888_to_rgb565_buffer+0x9d>
mov    -0x14(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11c8 <bgra8888_to_rgb565_buffer+0x98>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x20(%rbp),%rcx
call   1040 <bgra8888_to_rgb565@plt>
cmp    $0x0,%rax
je     11c3 <bgra8888_to_rgb565_buffer+0x93>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
jmp    11c8 <bgra8888_to_rgb565_buffer+0x98>
jmp    11cd <bgra8888_to_rgb565_buffer+0x9d>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bgra8888_to_rgb565_buffer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %ebx,%ebx
test   %rdi,%rdi
je     1185 <bgra8888_to_rgb565_buffer+0x55>
mov    %esi,%ebp
test   %esi,%esi
jle    1185 <bgra8888_to_rgb565_buffer+0x55>
mov    %edx,%r14d
test   %edx,%edx
jle    1185 <bgra8888_to_rgb565_buffer+0x55>
mov    %rdi,%r15
lea    0x3(%rbp,%rbp,1),%edi
and    $0xfffffffc,%edi
imul   %r14d,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1183 <bgra8888_to_rgb565_buffer+0x53>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %ebp,%esi
mov    %r14d,%edx
mov    %rax,%rcx
call   1040 <bgra8888_to_rgb565@plt>
test   %rax,%rax
je     1185 <bgra8888_to_rgb565_buffer+0x55>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @bgra8888_to_rgb565_buffer(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = load i32, i32* %6
%10 = load i32, i32* %5
%11 = mul nsw i32 %10, 2
%12 = add nsw i32 %11, 3
%13 = and i32 %12, -4
%14 = mul nsw i32 %9, %13
store i32 %14, i32* %7
store i8* null, i8** %8
%15 = load i8*, i8** %4
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %40
17: ; preds = %3
%18 = load i32, i32* %5
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %40
20: ; preds = %17
%21 = load i32, i32* %6
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %40
23: ; preds = %20
%24 = load i32, i32* %7
%25 = call i8* @malloc(i32 %24)
store i8* %25, i8** %8
%26 = load i8*, i8** %8
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %39
28: ; preds = %23
%29 = load i8*, i8** %4
%30 = load i32, i32* %5
%31 = load i32, i32* %6
%32 = load i8*, i8** %8
%33 = call i64 @bgra8888_to_rgb565(i8* %29, i32 %30, i32 %31, i8* %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %28
%36 = load i8*, i8** %8
%37 = call i32 @free(i8* %36)
store i8* null, i8** %8
br label %38
38: ; preds = %35, %28
br label %39
39: ; preds = %38, %23
br label %40
40: ; preds = %39, %20, %17, %3
%41 = load i8*, i8** %8
ret i8* %41
}

LLVM IR (-O2):
define i8* @bgra8888_to_rgb565_buffer(i8* %0, i32 %1, i32 %2) {
%4 = icmp ne i8* %0, null
%5 = icmp sgt i32 %1, 0
%6 = and i1 %4, %5
%7 = icmp sgt i32 %2, 0
%8 = and i1 %6, %7
br i1 %8, label %9, label %21
9: ; preds = %3
%10 = shl nuw nsw i32 %1, 1
%11 = add nsw i32 %10, 3
%12 = and i32 %11, -4
%13 = mul nsw i32 %12, %2
%14 = tail call i8* @malloc(i32 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %21, label %16
16: ; preds = %9
%17 = tail call i64 @bgra8888_to_rgb565(i8* %0, i32 %1, i32 %2, i8* %14)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = tail call i32 @free(i8* %14)
br label %21
21: ; preds = %9, %19, %16, %3
%22 = phi i8* [ null, %19 ], [ %14, %16 ], [ null, %9 ], [ null, %3 ]
ret i8* %22
}