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

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int /*<<< orphan*/);
int vasprintf(char **, char const *, int /*<<< orphan*/);
int safe_vasprintf(char **strp, const char *fmt, va_list ap) {
  int retval;
  retval = vasprintf(strp, fmt, ap);
  if (retval == -1) {
    do {
      printf("Failed to vasprintf: %s.  Bailing out", strerror(errno));
      printf("\n");
    } while (0);
    exit(16);
  }
  return (retval);
}


x86 assembly (clang, -O0):
<safe_vasprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <vasprintf@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11ac <safe_vasprintf+0x6c>
jmp    1175 <safe_vasprintf+0x35>
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %rax,%rsi
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x10,%edi
call   1050 <exit@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_vasprintf>:
push   %rax
call   1040 <vasprintf@plt>
cmp    $0xffffffff,%eax
je     115d <safe_vasprintf+0xd>
pop    %rcx
ret
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe8e(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    $0x10,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [38 x i8] c"Failed to vasprintf: %s. Bailing out\00"
@errno = global i32 0
@.str.1 = [2 x i8] c"\0A\00"
define i32 @safe_vasprintf(i8** %0, i8* %1, i32 %2) {
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8** %0, i8*** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i8**, i8*** %4
%9 = load i8*, i8** %5
%10 = load i32, i32* %6
%11 = call i32 @vasprintf(i8** %8, i8* %9, i32 %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %22
14: ; preds = %3
br label %15
15: ; preds = %14
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %15
%21 = call i32 @exit(i32 16)
unreachable
22: ; preds = %3
%23 = load i32, i32* %7
ret i32 %23
}

LLVM IR (-O2):
@.str = [38 x i8] c"Failed to vasprintf: %s. Bailing out\00"
@errno = global i32 0
define i32 @safe_vasprintf(i8** %0, i8* %1, i32 %2) {
%4 = tail call i32 @vasprintf(i8** %0, i8* %1, i32 %2)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %12
6: ; preds = %3
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @putchar(i32 10)
%11 = tail call i32 @exit(i32 16)
unreachable
12: ; preds = %3
ret i32 %4
}