#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 */
scalar_t__ likely(int);
int vsnprintf(char *, size_t, char const *, int /*<<< orphan*/);
int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) {
  int i;
  i = vsnprintf(buf, size, fmt, args);
  if (likely((size_t)i < size))
    return i;
  if (size != 0)
    return size - 1;
  return 0;
}


x86 assembly (clang, -O0):
<vscnprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
call   1030 <vsnprintf@plt>
mov    %eax,-0x28(%rbp)
movslq -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
setb   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <likely@plt>
cmp    $0x0,%rax
je     1178 <vscnprintf+0x58>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    119a <vscnprintf+0x7a>
cmpq   $0x0,-0x18(%rbp)
je     1193 <vscnprintf+0x73>
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
mov    %eax,-0x4(%rbp)
jmp    119a <vscnprintf+0x7a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vscnprintf>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
call   1030 <vsnprintf@plt>
mov    %eax,%ebp
cltq
xor    %edi,%edi
cmp    %rbx,%rax
setb   %dil
call   1040 <likely@plt>
test   %rax,%rax
jne    1150 <vscnprintf+0x30>
test   %rbx,%rbx
je     114e <vscnprintf+0x2e>
add    $0xffffffff,%ebx
mov    %ebx,%ebp
jmp    1150 <vscnprintf+0x30>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @vscnprintf(i8* %0, i64 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%11 = load i8*, i8** %6
%12 = load i64, i64* %7
%13 = load i8*, i8** %8
%14 = load i32, i32* %9
%15 = call i32 @vsnprintf(i8* %11, i64 %12, i8* %13, i32 %14)
store i32 %15, i32* %10
%16 = load i32, i32* %10
%17 = sext i32 %16 to i64
%18 = load i64, i64* %7
%19 = icmp ult i64 %17, %18
%20 = zext i1 %19 to i32
%21 = call i64 @likely(i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %4
%24 = load i32, i32* %10
store i32 %24, i32* %5
br label %33
25: ; preds = %4
%26 = load i64, i64* %7
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = load i64, i64* %7
%30 = sub i64 %29, 1
%31 = trunc i64 %30 to i32
store i32 %31, i32* %5
br label %33
32: ; preds = %25
store i32 0, i32* %5
br label %33
33: ; preds = %32, %28, %23
%34 = load i32, i32* %5
ret i32 %34
}

LLVM IR (-O2):
define i32 @vscnprintf(i8* nocapture %0, i64 %1, i8* nocapture %2, i32 %3) {
%5 = tail call i32 @vsnprintf(i8* %0, i64 %1, i8* %2, i32 %3)
%6 = sext i32 %5 to i64
%7 = icmp ult i64 %6, %1
%8 = zext i1 %7 to i32
%9 = tail call i64 @likely(i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %4
%12 = icmp eq i64 %1, 0
br i1 %12, label %16, label %13
13: ; preds = %11
%14 = trunc i64 %1 to i32
%15 = add i32 %14, -1
br label %16
16: ; preds = %11, %4, %13
%17 = phi i32 [ %15, %13 ], [ %5, %4 ], [ 0, %11 ]
ret i32 %17
}