#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;
struct mg_connection {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ cry(struct mg_connection *, char *, ...);
int vsnprintf(char *, size_t, char const *, int /*<<< orphan*/);
int mg_vsnprintf(struct mg_connection *conn, char *buf, size_t buflen,
                 const char *fmt, va_list ap) {
  int n;
  if (buflen == 0)
    return 0;
  n = vsnprintf(buf, buflen, fmt, ap);
  if (n < 0) {
    cry(conn, "vsnprintf error");
    n = 0;
  } else if (n >= (int)buflen) {
    cry(conn, "truncating vsnprintf buffer: [%.*s]", n > 200 ? 200 : n, buf);
    n = (int)buflen - 1;
  }
  buf[n] = '\0';
  return n;
}


x86 assembly (clang, -O0):
<mg_vsnprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1153 <mg_vsnprintf+0x33>
movl   $0x0,-0x4(%rbp)
jmp    1203 <mg_vsnprintf+0xe3>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
call   1030 <vsnprintf@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    1192 <mg_vsnprintf+0x72>
mov    -0x10(%rbp),%rdi
lea    0xe81(%rip),%rsi
mov    $0x0,%al
call   1040 <cry@plt>
movl   $0x0,-0x30(%rbp)
jmp    11f1 <mg_vsnprintf+0xd1>
mov    -0x30(%rbp),%eax
mov    -0x20(%rbp),%rcx
cmp    %ecx,%eax
jl     11ec <mg_vsnprintf+0xcc>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpl   $0xc8,-0x30(%rbp)
jle    11c3 <mg_vsnprintf+0xa3>
mov    $0xc8,%eax
mov    %eax,-0x3c(%rbp)
jmp    11c9 <mg_vsnprintf+0xa9>
mov    -0x30(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x3c(%rbp),%edx
mov    -0x18(%rbp),%rcx
lea    0xe35(%rip),%rsi
mov    $0x0,%al
call   1040 <cry@plt>
mov    -0x20(%rbp),%rax
sub    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11f1 <mg_vsnprintf+0xd1>
mov    -0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mg_vsnprintf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdx,%rdx
je     1179 <mg_vsnprintf+0x59>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rdi
mov    %rdx,%rsi
mov    %rcx,%rdx
mov    %r8d,%ecx
call   1030 <vsnprintf@plt>
test   %eax,%eax
js     117d <mg_vsnprintf+0x5d>
mov    %eax,%ebp
cmp    %ebx,%eax
jl     1190 <mg_vsnprintf+0x70>
cmp    $0xc8,%ebp
mov    $0xc8,%edx
cmovb  %ebp,%edx
lea    0xeab(%rip),%rsi
mov    %r15,%rdi
mov    %r14,%rcx
xor    %eax,%eax
call   1040 <cry@plt>
add    $0xffffffff,%ebx
mov    %ebx,%ebp
jmp    1190 <mg_vsnprintf+0x70>
xor    %ebp,%ebp
jmp    1198 <mg_vsnprintf+0x78>
lea    0xe7c(%rip),%rsi
xor    %ebp,%ebp
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <cry@plt>
movslq %ebp,%rax
movb   $0x0,(%r14,%rax,1)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.mg_connection = type { i32 }
@.str = [16 x i8] c"vsnprintf error\00"
@.str.1 = [36 x i8] c"truncating vsnprintf buffer: [%.*s]\00"
define i32 @mg_vsnprintf(%struct.mg_connection* %0, i8* %1, i64 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca %struct.mg_connection*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store %struct.mg_connection* %0, %struct.mg_connection** %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%13 = load i64, i64* %9
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %5
store i32 0, i32* %6
br label %53
16: ; preds = %5
%17 = load i8*, i8** %8
%18 = load i64, i64* %9
%19 = load i8*, i8** %10
%20 = load i32, i32* %11
%21 = call i32 @vsnprintf(i8* %17, i64 %18, i8* %19, i32 %20)
store i32 %21, i32* %12
%22 = load i32, i32* %12
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %16
%25 = load %struct.mg_connection*, %struct.mg_connection** %7
%26 = call i32 (%struct.mg_connection*, i8*, ...) @cry(%struct.mg_connection* %25, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %12
br label %47
27: ; preds = %16
%28 = load i32, i32* %12
%29 = load i64, i64* %9
%30 = trunc i64 %29 to i32
%31 = icmp sge i32 %28, %30
br i1 %31, label %32, label %46
32: ; preds = %27
%33 = load %struct.mg_connection*, %struct.mg_connection** %7
%34 = load i32, i32* %12
%35 = icmp sgt i32 %34, 200
br i1 %35, label %36, label %37
36: ; preds = %32
br label %39
37: ; preds = %32
%38 = load i32, i32* %12
br label %39
39: ; preds = %37, %36
%40 = phi i32 [ 200, %36 ], [ %38, %37 ]
%41 = load i8*, i8** %8
%42 = call i32 (%struct.mg_connection*, i8*, ...) @cry(%struct.mg_connection* %33, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %40, i8* %41)
%43 = load i64, i64* %9
%44 = trunc i64 %43 to i32
%45 = sub nsw i32 %44, 1
store i32 %45, i32* %12
br label %46
46: ; preds = %39, %27
br label %47
47: ; preds = %46, %24
%48 = load i8*, i8** %8
%49 = load i32, i32* %12
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
store i8 0, i8* %51
%52 = load i32, i32* %12
store i32 %52, i32* %6
br label %53
53: ; preds = %47, %15
%54 = load i32, i32* %6
ret i32 %54
}

LLVM IR (-O2):
%struct.mg_connection = type { i32 }
@.str = [16 x i8] c"vsnprintf error\00"
@.str.1 = [36 x i8] c"truncating vsnprintf buffer: [%.*s]\00"
define i32 @mg_vsnprintf(%struct.mg_connection* %0, i8* %1, i64 %2, i8* nocapture %3, i32 %4) {
%6 = icmp eq i64 %2, 0
br i1 %6, label %24, label %7
7: ; preds = %5
%8 = tail call i32 @vsnprintf(i8* %1, i64 %2, i8* %3, i32 %4)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 (%struct.mg_connection*, i8*, ...) @cry(%struct.mg_connection* %0, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %20
12: ; preds = %7
%13 = trunc i64 %2 to i32
%14 = icmp slt i32 %8, %13
br i1 %14, label %20, label %15
15: ; preds = %12
%16 = icmp ult i32 %8, 200
%17 = select i1 %16, i32 %8, i32 200
%18 = tail call i32 (%struct.mg_connection*, i8*, ...) @cry(%struct.mg_connection* %0, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %17, i8* %1)
%19 = add nsw i32 %13, -1
br label %20
20: ; preds = %12, %15, %10
%21 = phi i32 [ 0, %10 ], [ %19, %15 ], [ %8, %12 ]
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %1, i64 %22
store i8 0, i8* %23
br label %24
24: ; preds = %5, %20
%25 = phi i32 [ %21, %20 ], [ 0, %5 ]
ret i32 %25
}