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


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

x86 assembly (clang, -O2):
<abgr8888_to_bgra8888_buffer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %ebx,%ebx
test   %rdi,%rdi
je     1183 <abgr8888_to_bgra8888_buffer+0x53>
mov    %esi,%ebp
test   %esi,%esi
jle    1183 <abgr8888_to_bgra8888_buffer+0x53>
mov    %edx,%r14d
test   %edx,%edx
jle    1183 <abgr8888_to_bgra8888_buffer+0x53>
mov    %rdi,%r15
mov    %ebp,%edi
imul   %r14d,%edi
shl    $0x2,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     1181 <abgr8888_to_bgra8888_buffer+0x51>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %ebp,%esi
mov    %r14d,%edx
mov    %rax,%rcx
call   1050 <abgr8888_to_bgra8888@plt>
test   %rax,%rax
je     1183 <abgr8888_to_bgra8888_buffer+0x53>
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* @abgr8888_to_bgra8888_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, 4
%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 @abgr8888_to_bgra8888(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* @abgr8888_to_bgra8888_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 %19
9: ; preds = %3
%10 = shl nsw i32 %1, 2
%11 = mul nsw i32 %10, %2
%12 = tail call i8* @malloc(i32 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %19, label %14
14: ; preds = %9
%15 = tail call i64 @abgr8888_to_bgra8888(i8* %0, i32 %1, i32 %2, i8* %12)
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = tail call i32 @free(i8* %12)
br label %19
19: ; preds = %9, %17, %14, %3
%20 = phi i8* [ null, %17 ], [ %12, %14 ], [ null, %9 ], [ null, %3 ]
ret i8* %20
}