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

/* Variables and functions */
int /*<<< orphan*/ BUF_MEM_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ BUF_MEM_grow(int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ *BUF_MEM_new();
BUF_MEM *BUF_MEM_create(size_t len) {
  BUF_MEM *out = BUF_MEM_new();
  if (!out)
    return NULL;
  if (len == 0)
    return out;
  if (!BUF_MEM_grow(out, len)) {
    BUF_MEM_free(out);
    return NULL;
  }
  return out;
}


x86 assembly (clang, -O0):
<BUF_MEM_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <BUF_MEM_new@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    115f <BUF_MEM_create+0x2f>
movq   $0x0,-0x8(%rbp)
jmp    11ab <BUF_MEM_create+0x7b>
cmpq   $0x0,-0x10(%rbp)
jne    1177 <BUF_MEM_create+0x47>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ab <BUF_MEM_create+0x7b>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <BUF_MEM_grow@plt>
cmp    $0x0,%eax
jne    11a3 <BUF_MEM_create+0x73>
mov    -0x18(%rbp),%rdi
call   1050 <BUF_MEM_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11ab <BUF_MEM_create+0x7b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BUF_MEM_create>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %eax,%eax
call   1040 <BUF_MEM_new@plt>
test   %rax,%rax
je     1162 <BUF_MEM_create+0x32>
mov    %rax,%rbx
test   %r14,%r14
je     1164 <BUF_MEM_create+0x34>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <BUF_MEM_grow@plt>
test   %eax,%eax
jne    1164 <BUF_MEM_create+0x34>
mov    %rbx,%rdi
call   1050 <BUF_MEM_free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32* @BUF_MEM_create(i64 %0) {
%2 = alloca i32*
%3 = alloca i64
%4 = alloca i32*
store i64 %0, i64* %3
%5 = call i32* (...) @BUF_MEM_new()
store i32* %5, i32** %4
%6 = load i32*, i32** %4
%7 = icmp ne i32* %6, null
br i1 %7, label %9, label %8
8: ; preds = %1
store i32* null, i32** %2
br label %24
9: ; preds = %1
%10 = load i64, i64* %3
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = load i32*, i32** %4
store i32* %13, i32** %2
br label %24
14: ; preds = %9
%15 = load i32*, i32** %4
%16 = load i64, i64* %3
%17 = call i32 @BUF_MEM_grow(i32* %15, i64 %16)
%18 = icmp ne i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %14
%20 = load i32*, i32** %4
%21 = call i32 @BUF_MEM_free(i32* %20)
store i32* null, i32** %2
br label %24
22: ; preds = %14
%23 = load i32*, i32** %4
store i32* %23, i32** %2
br label %24
24: ; preds = %22, %19, %12, %8
%25 = load i32*, i32** %2
ret i32* %25
}

LLVM IR (-O2):
define i32* @BUF_MEM_create(i64 %0) {
%2 = tail call i32* (...) @BUF_MEM_new()
%3 = icmp eq i32* %2, null
br i1 %3, label %11, label %4
4: ; preds = %1
%5 = icmp eq i64 %0, 0
br i1 %5, label %11, label %6
6: ; preds = %4
%7 = tail call i32 @BUF_MEM_grow(i32* %2, i64 %0)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @BUF_MEM_free(i32* %2)
br label %11
11: ; preds = %6, %4, %1, %9
%12 = phi i32* [ null, %9 ], [ null, %1 ], [ %2, %4 ], [ %2, %6 ]
ret i32* %12
}