#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*/ u_int8_t;
struct kore_buf {
  int dummy;
};
typedef int /*<<< orphan*/ sb;

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ fatal(char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ kore_buf_append(struct kore_buf *, int /*<<< orphan*/ *,
                                   int);
int vasprintf(char **, char const *, int /*<<< orphan*/);
int vsnprintf(char *, int, char const *, int /*<<< orphan*/);
void kore_buf_appendv(struct kore_buf *buf, const char *fmt, va_list args) {
  int l;
  char *b, sb[BUFSIZ];
  l = vsnprintf(sb, sizeof(sb), fmt, args);
  if (l == -1)
    fatal("kore_buf_appendv(): vsnprintf error");
  if ((size_t)l >= sizeof(sb)) {
    l = vasprintf(&b, fmt, args);
    if (l == -1)
      fatal("kore_buf_appendv(): error or truncation");
  } else {
    b = sb;
  }
  kore_buf_append(buf, (u_int8_t *)b, l);
  if (b != sb)
    free(b);
}


x86 assembly (clang, -O0):
<kore_buf_appendv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    %eax,%esi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1040 <vsnprintf@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11ba <kore_buf_appendv+0x6a>
lea    0xe4b(%rip),%rdi
call   1070 <fatal@plt>
mov    -0x40(%rbp),%rcx
movslq -0x18(%rbp),%rax
cmp    %rcx,%rax
jb     11f9 <kore_buf_appendv+0xa9>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    -0x20(%rbp),%rdi
call   1050 <vasprintf@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11f4 <kore_buf_appendv+0xa4>
lea    0xe35(%rip),%rdi
call   1070 <fatal@plt>
jmp    1201 <kore_buf_appendv+0xb1>
mov    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1060 <kore_buf_append@plt>
mov    -0x38(%rbp),%rax
cmp    %rax,-0x20(%rbp)
je     1228 <kore_buf_appendv+0xd8>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kore_buf_appendv>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r15d
mov    %rsi,%r12
mov    %rdi,-0x38(%rbp)
mov    %rsp,-0x40(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%r13
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    %r13,%rdi
mov    %r14d,%esi
mov    %r12,%rdx
mov    %r15d,%ecx
call   1040 <vsnprintf@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11ae <kore_buf_appendv+0x5e>
lea    0xe57(%rip),%rdi
call   1070 <fatal@plt>
movslq %ebx,%rax
cmp    %r14,%rax
jae    11bc <kore_buf_appendv+0x6c>
mov    %r13,-0x30(%rbp)
jmp    11e3 <kore_buf_appendv+0x93>
lea    -0x30(%rbp),%rdi
mov    %r12,%rsi
mov    %r15d,%edx
call   1050 <vasprintf@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11e3 <kore_buf_appendv+0x93>
lea    0xe4b(%rip),%rdi
call   1070 <fatal@plt>
mov    $0xffffffff,%ebx
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdi
mov    %ebx,%edx
call   1060 <kore_buf_append@plt>
mov    -0x30(%rbp),%rdi
cmp    %r13,%rdi
je     1200 <kore_buf_appendv+0xb0>
call   1030 <free@plt>
mov    -0x40(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.kore_buf = type { i32 }
@BUFSIZ = global i32 0
@.str = [36 x i8] c"kore_buf_appendv(): vsnprintf error\00"
@.str.1 = [40 x i8] c"kore_buf_appendv(): error or truncation\00"
define void @kore_buf_appendv(%struct.kore_buf* %0, i8* %1, i32 %2) {
%4 = alloca %struct.kore_buf*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
store %struct.kore_buf* %0, %struct.kore_buf** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%11 = load i32, i32* @BUFSIZ
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %9
%14 = alloca i8, i64 %12
store i64 %12, i64* %10
%15 = trunc i64 %12 to i32
%16 = load i8*, i8** %5
%17 = load i32, i32* %6
%18 = call i32 @vsnprintf(i8* %14, i32 %15, i8* %16, i32 %17)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %3
%22 = call i32 @fatal(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %3
%24 = load i32, i32* %7
%25 = sext i32 %24 to i64
%26 = icmp uge i64 %25, %12
br i1 %26, label %27, label %36
27: ; preds = %23
%28 = load i8*, i8** %5
%29 = load i32, i32* %6
%30 = call i32 @vasprintf(i8** %8, i8* %28, i32 %29)
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = call i32 @fatal(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %27
br label %37
36: ; preds = %23
store i8* %14, i8** %8
br label %37
37: ; preds = %36, %35
%38 = load %struct.kore_buf*, %struct.kore_buf** %4
%39 = load i8*, i8** %8
%40 = bitcast i8* %39 to i32*
%41 = load i32, i32* %7
%42 = call i32 @kore_buf_append(%struct.kore_buf* %38, i32* %40, i32 %41)
%43 = load i8*, i8** %8
%44 = icmp ne i8* %43, %14
br i1 %44, label %45, label %48
45: ; preds = %37
%46 = load i8*, i8** %8
%47 = call i32 @free(i8* %46)
br label %48
48: ; preds = %45, %37
%49 = load i8*, i8** %9
ret void
}

LLVM IR (-O2):
%struct.kore_buf = type { i32 }
@BUFSIZ = global i32 0
@.str = [36 x i8] c"kore_buf_appendv(): vsnprintf error\00"
@.str.1 = [40 x i8] c"kore_buf_appendv(): error or truncation\00"
define void @kore_buf_appendv(%struct.kore_buf* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = load i32, i32* @BUFSIZ
%7 = zext i32 %6 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = call i32 @vsnprintf(i8* %9, i32 %6, i8* %1, i32 %2)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = tail call i32 @fatal(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %3
%15 = sext i32 %10 to i64
%16 = icmp ult i64 %15, %7
br i1 %16, label %22, label %17
17: ; preds = %14
%18 = call i32 @vasprintf(i8** %4, i8* %1, i32 %2)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = call i32 @fatal(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %23
22: ; preds = %14
store i8* %9, i8** %4
br label %23
23: ; preds = %17, %20, %22
%24 = phi i32 [ -1, %20 ], [ %18, %17 ], [ %10, %22 ]
%25 = bitcast i8** %4 to i32**
%26 = load i32*, i32** %25
%27 = call i32 @kore_buf_append(%struct.kore_buf* %0, i32* %26, i32 %24)
%28 = load i8*, i8** %4
%29 = icmp eq i8* %28, %9
br i1 %29, label %32, label %30
30: ; preds = %23
%31 = call i32 @free(i8* %28)
br label %32
32: ; preds = %30, %23
ret void
}