#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*/ assert(int);
int /*<<< orphan*/ coco_error(char *, ...);
char *coco_strdup(char *);
int strlen(char const *);
long vsprintf(char *, char const *, int /*<<< orphan*/);
char *coco_vstrdupf(const char *str, va_list args) {
  static char buf[444];
  long written;
  assert(strlen(str) < 444 / 2 - 2);
  if (strlen(str) >= 444 / 2 - 2)
    coco_error("coco_vstrdupf(): string is too long");
  written = vsprintf(buf, str, args);
  if (written < 0)
    coco_error("coco_vstrdupf(): vsprintf failed on '%s'", str);
  if (written > 444 - 3)
    coco_error("coco_vstrdupf(): A suspiciously long string is tried to being "
               "duplicated '%s'",
               buf);
  return coco_strdup(buf);
}


x86 assembly (clang, -O0):
<coco_vstrdupf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0xdc,%eax
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0xdc,%eax
jl     119c <coco_vstrdupf+0x4c>
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1050 <coco_error@plt>
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    0x2eb6(%rip),%rdi
call   1060 <vsprintf@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11d0 <coco_vstrdupf+0x80>
mov    -0x8(%rbp),%rsi
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1050 <coco_error@plt>
cmpq   $0x1b9,-0x18(%rbp)
jle    11f3 <coco_vstrdupf+0xa3>
lea    0xe68(%rip),%rdi
lea    0x2e74(%rip),%rsi
mov    $0x0,%al
call   1050 <coco_error@plt>
lea    0x2e66(%rip),%rdi
call   1070 <coco_strdup@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<coco_vstrdupf>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
call   1030 <strlen@plt>
xor    %edi,%edi
cmp    $0xdc,%eax
setl   %dil
call   1040 <assert@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cmp    $0xdc,%eax
jl     118a <coco_vstrdupf+0x3a>
lea    0xe7d(%rip),%rdi
xor    %eax,%eax
call   1050 <coco_error@plt>
lea    0x2ecf(%rip),%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1060 <vsprintf@plt>
test   %rax,%rax
js     11b8 <coco_vstrdupf+0x68>
cmp    $0x1ba,%rax
jb     11c9 <coco_vstrdupf+0x79>
lea    0xe9e(%rip),%rdi
lea    0x2eaa(%rip),%rsi
jmp    11c2 <coco_vstrdupf+0x72>
lea    0xe65(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <coco_error@plt>
lea    0x2e90(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1070 <coco_strdup@plt>

LLVM IR (-O0):
@coco_vstrdupf.buf = internal global [444 x i8] zeroinitializer
@.str = [36 x i8] c"coco_vstrdupf(): string is too long\00"
@.str.1 = [41 x i8] c"coco_vstrdupf(): vsprintf failed on '%s'\00"
@.str.2 = [78 x i8] c"coco_vstrdupf(): A suspiciously long string is tried to being duplicated '%s'\00"
define i8* @coco_vstrdupf(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i8*, i8** %3
%7 = call i32 @strlen(i8* %6)
%8 = icmp slt i32 %7, 220
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = load i8*, i8** %3
%12 = call i32 @strlen(i8* %11)
%13 = icmp sge i32 %12, 220
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (i8*, ...) @coco_error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = load i8*, i8** %3
%18 = load i32, i32* %4
%19 = call i64 @vsprintf(i8* getelementptr inbounds ([444 x i8], [444 x i8]* @coco_vstrdupf.buf, i64 0, i64 0), i8* %17, i32 %18)
store i64 %19, i64* %5
%20 = load i64, i64* %5
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = load i8*, i8** %3
%24 = call i32 (i8*, ...) @coco_error(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %22, %16
%26 = load i64, i64* %5
%27 = icmp sgt i64 %26, 441
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 (i8*, ...) @coco_error(i8* getelementptr inbounds ([78 x i8], [78 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([444 x i8], [444 x i8]* @coco_vstrdupf.buf, i64 0, i64 0))
br label %30
30: ; preds = %28, %25
%31 = call i8* @coco_strdup(i8* getelementptr inbounds ([444 x i8], [444 x i8]* @coco_vstrdupf.buf, i64 0, i64 0))
ret i8* %31
}

LLVM IR (-O2):
@coco_vstrdupf.buf = internal global [444 x i8] zeroinitializer
@.str = [36 x i8] c"coco_vstrdupf(): string is too long\00"
@.str.1 = [41 x i8] c"coco_vstrdupf(): vsprintf failed on '%s'\00"
@.str.2 = [78 x i8] c"coco_vstrdupf(): A suspiciously long string is tried to being duplicated '%s'\00"
define i8* @coco_vstrdupf(i8* %0, i32 %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = icmp slt i32 %3, 220
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = tail call i32 @strlen(i8* %0)
%8 = icmp sgt i32 %7, 219
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 (i8*, ...) @coco_error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = tail call i64 @vsprintf(i8* getelementptr inbounds ([444 x i8], [444 x i8]* @coco_vstrdupf.buf, i64 0, i64 0), i8* %0, i32 %1)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 (i8*, ...) @coco_error(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %20
16: ; preds = %11
%17 = icmp ugt i64 %12, 441
br i1 %17, label %18, label %20
18: ; preds = %16
%19 = tail call i32 (i8*, ...) @coco_error(i8* getelementptr inbounds ([78 x i8], [78 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([444 x i8], [444 x i8]* @coco_vstrdupf.buf, i64 0, i64 0))
br label %20
20: ; preds = %14, %18, %16
%21 = tail call i8* @coco_strdup(i8* getelementptr inbounds ([444 x i8], [444 x i8]* @coco_vstrdupf.buf, i64 0, i64 0))
ret i8* %21
}