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

/* Variables and functions */
int cb_sprint(char *, char const *, int /*<<< orphan*/, int /*<<< orphan*/,
              int /*<<< orphan*/);
scalar_t__ malloc(size_t);
int cb_asprint(char **ret, const char *buf, ssize_t length, ssize_t max_width,
               ssize_t max_lines) {
  size_t n = cb_sprint(NULL, buf, length, max_width, max_lines);
  *ret = (char *)malloc((n + 1) * sizeof(char));
  if (!*ret) {
    return -1;
  }
  return cb_sprint(*ret, buf, length, max_width, max_lines);
}


x86 assembly (clang, -O0):
<cb_asprint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
call   1030 <cb_sprint@plt>
cltq
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
shl    $0x0,%rdi
call   1040 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    118c <cb_asprint+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a9 <cb_asprint+0x89>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    -0x24(%rbp),%r8d
call   1030 <cb_sprint@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cb_asprint>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8d,%r14d
mov    %ecx,%r15d
mov    %edx,%ebx
mov    %rsi,%rbp
mov    %rdi,%r12
xor    %edi,%edi
call   1030 <cb_sprint@plt>
movslq %eax,%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
mov    %rax,(%r12)
test   %rax,%rax
je     116d <cb_asprint+0x4d>
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %ebx,%edx
mov    %r15d,%ecx
mov    %r14d,%r8d
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <cb_sprint@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @cb_asprint(i8** nocapture writeonly %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = tail call i32 @cb_sprint(i8* null, i8* %1, i32 %2, i32 %3, i32 %4)
%7 = sext i32 %6 to i64
%8 = add nsw i64 %7, 1
%9 = tail call i64 @malloc(i64 %8)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %0
%11 = icmp eq i64 %9, 0
br i1 %11, label %14, label %12
12: ; preds = %5
%13 = tail call i32 @cb_sprint(i8* %10, i8* %1, i32 %2, i32 %3, i32 %4)
br label %14
14: ; preds = %5, %12
%15 = phi i32 [ %13, %12 ], [ -1, %5 ]
ret i32 %15
}