#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 scalar_t__ int32_t;

/* Variables and functions */
scalar_t__ vsnprintf(char *, scalar_t__, char const *, int /*<<< orphan*/);
int32_t ds_vsnzprintf(char *pBuffer, int32_t iLength, const char *pFormat,
                      va_list Args) {
  int32_t iResult;
  if (iLength <= 0)
    return (0);
  iResult = vsnprintf(pBuffer, iLength, pFormat, Args);
  if (iResult >= iLength) {
    iResult = 0;
  } else if (iResult < 0) {
    iResult = 0;
  }
  pBuffer[iResult] = '\0';
  return (iResult);
}


x86 assembly (clang, -O0):
<ds_vsnzprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
cmpq   $0x0,-0x18(%rbp)
jg     113f <ds_vsnzprintf+0x2f>
movq   $0x0,-0x8(%rbp)
jmp    119e <ds_vsnzprintf+0x8e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
call   1030 <vsnprintf@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jl     1172 <ds_vsnzprintf+0x62>
movq   $0x0,-0x30(%rbp)
jmp    118a <ds_vsnzprintf+0x7a>
cmpq   $0x0,-0x30(%rbp)
jge    1185 <ds_vsnzprintf+0x75>
movq   $0x0,-0x30(%rbp)
jmp    118a <ds_vsnzprintf+0x7a>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ds_vsnzprintf>:
test   %rsi,%rsi
jle    1149 <ds_vsnzprintf+0x39>
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
call   1030 <vsnprintf@plt>
mov    %rax,%rcx
xor    %edx,%edx
test   %rax,%rax
mov    $0x0,%eax
cmovns %rcx,%rax
cmp    %rbx,%rcx
cmovge %rdx,%rax
movb   $0x0,(%r14,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
ret

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

LLVM IR (-O2):
define i64 @ds_vsnzprintf(i8* nocapture %0, i64 %1, i8* nocapture %2, i32 %3) {
%5 = icmp slt i64 %1, 1
br i1 %5, label %13, label %6
6: ; preds = %4
%7 = tail call i64 @vsnprintf(i8* %0, i64 %1, i8* %2, i32 %3)
%8 = icmp slt i64 %7, %1
%9 = icmp sgt i64 %7, 0
%10 = and i1 %8, %9
%11 = select i1 %10, i64 %7, i64 0
%12 = getelementptr inbounds i8, i8* %0, i64 %11
store i8 0, i8* %12
br label %13
13: ; preds = %4, %6
%14 = phi i64 [ %11, %6 ], [ 0, %4 ]
ret i64 %14
}