#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;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int BUFSIZ;
char *strdup(char *);
int /*<<< orphan*/ vsnprintf(char *, int, char const *, int /*<<< orphan*/);
char *vstrdupf(const char *fmt, va_list vargs) {
  char buf[BUFSIZ];
  char *p;
  if (!fmt) {
    return (NULL);
  }
  vsnprintf(buf, sizeof(buf), fmt, vargs);
  buf[sizeof(buf) - 1] = '\0';
  if (!(p = strdup(buf))) {
    return (NULL);
  }
  return (p);
}


x86 assembly (clang, -O0):
<vstrdupf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2eaa(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117e <vstrdupf+0x5e>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    11da <vstrdupf+0xba>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1030 <vsnprintf@plt>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rax
sub    $0x1,%rax
movb   $0x0,(%rdi,%rax,1)
call   1040 <strdup@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11cb <vstrdupf+0xab>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    11da <vstrdupf+0xba>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vstrdupf>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    0x2eb2(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
test   %rdi,%rdi
je     1167 <vstrdupf+0x47>
mov    %esi,%ecx
mov    %rdi,%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
call   1030 <vsnprintf@plt>
movb   $0x0,-0x1(%r14,%rbx,1)
mov    %rbx,%rdi
call   1040 <strdup@plt>
jmp    1169 <vstrdupf+0x49>
xor    %eax,%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
define i8* @vstrdupf(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = load i32, i32* @BUFSIZ
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %6
%13 = alloca i8, i64 %11
store i64 %11, i64* %7
%14 = load i8*, i8** %4
%15 = icmp ne i8* %14, null
br i1 %15, label %17, label %16
16: ; preds = %2
store i8* null, i8** %3
store i32 1, i32* %9
br label %29
17: ; preds = %2
%18 = trunc i64 %11 to i32
%19 = load i8*, i8** %4
%20 = load i32, i32* %5
%21 = call i32 @vsnprintf(i8* %13, i32 %18, i8* %19, i32 %20)
%22 = sub i64 %11, 1
%23 = getelementptr inbounds i8, i8* %13, i64 %22
store i8 0, i8* %23
%24 = call i8* @strdup(i8* %13)
store i8* %24, i8** %8
%25 = icmp ne i8* %24, null
br i1 %25, label %27, label %26
26: ; preds = %17
store i8* null, i8** %3
store i32 1, i32* %9
br label %29
27: ; preds = %17
%28 = load i8*, i8** %8
store i8* %28, i8** %3
store i32 1, i32* %9
br label %29
29: ; preds = %27, %26, %16
%30 = load i8*, i8** %6
%31 = load i8*, i8** %3
ret i8* %31
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
define noalias i8* @vstrdupf(i8* %0, i32 %1) {
%3 = load i32, i32* @BUFSIZ
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = icmp eq i8* %0, null
br i1 %6, label %12, label %7
7: ; preds = %2
%8 = call i32 @vsnprintf(i8* %5, i32 %3, i8* %0, i32 %1)
%9 = add nsw i64 %4, -1
%10 = getelementptr inbounds i8, i8* %5, i64 %9
store i8 0, i8* %10
%11 = call i8* @strdup(i8* %5)
br label %12
12: ; preds = %7, %2
%13 = phi i8* [ null, %2 ], [ %11, %7 ]
ret i8* %13
}