#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*/ printf(char *, int);
int vsnprintf(char *, int, char const *, int /*<<< orphan*/);
int mysprintf_1(char *buffer, int len, const char *fmt, va_list ap) {
  int ret = 0;
  ret = vsnprintf(buffer, len, fmt, ap);
  printf("ret: %d\n", ret);
  if (ret > len - 1)
    ret = len - 1;
  else if (ret == -1)
    ret = len - 1;
  buffer[ret] = '\0';
  return ret;
}


x86 assembly (clang, -O0):
<mysprintf_1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <vsnprintf@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xea3(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    1181 <mysprintf_1+0x61>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1199 <mysprintf_1+0x79>
cmpl   $0xffffffff,-0x20(%rbp)
jne    1194 <mysprintf_1+0x74>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1199 <mysprintf_1+0x79>
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mysprintf_1>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %rdi,%r14
call   1030 <vsnprintf@plt>
mov    %eax,%ebp
lea    0xec9(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
lea    -0x1(%rbx),%eax
cmp    $0xffffffff,%ebp
mov    %ebp,%ecx
cmove  %eax,%ecx
cmp    %ebx,%ebp
cmovge %eax,%ecx
movslq %ecx,%rax
movb   $0x0,(%r14,%rax,1)
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"ret: %d\0A\00"
define i32 @mysprintf_1(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i32 0, i32* %9
%10 = load i8*, i8** %5
%11 = load i32, i32* %6
%12 = load i8*, i8** %7
%13 = load i32, i32* %8
%14 = call i32 @vsnprintf(i8* %10, i32 %11, i8* %12, i32 %13)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = load i32, i32* %9
%18 = load i32, i32* %6
%19 = sub nsw i32 %18, 1
%20 = icmp sgt i32 %17, %19
br i1 %20, label %21, label %24
21: ; preds = %4
%22 = load i32, i32* %6
%23 = sub nsw i32 %22, 1
store i32 %23, i32* %9
br label %31
24: ; preds = %4
%25 = load i32, i32* %9
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* %6
%29 = sub nsw i32 %28, 1
store i32 %29, i32* %9
br label %30
30: ; preds = %27, %24
br label %31
31: ; preds = %30, %21
%32 = load i8*, i8** %5
%33 = load i32, i32* %9
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 0, i8* %35
%36 = load i32, i32* %9
ret i32 %36
}

LLVM IR (-O2):
@.str = [9 x i8] c"ret: %d\0A\00"
define i32 @mysprintf_1(i8* nocapture %0, i32 %1, i8* nocapture %2, i32 %3) {
%5 = tail call i32 @vsnprintf(i8* %0, i32 %1, i8* %2, i32 %3)
%6 = tail call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = add nsw i32 %1, -1
%8 = icmp slt i32 %5, %1
%9 = icmp eq i32 %5, -1
%10 = select i1 %9, i32 %7, i32 %5
%11 = select i1 %8, i32 %10, i32 %7
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %0, i64 %12
store i8 0, i8* %13
ret i32 %11
}