#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;
};
typedef int /*<<< orphan*/ mem;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int mg_avprintf(char **, int, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ mg_out(struct mg_connection *, char *, int);
int mg_vprintf(struct mg_connection *nc, const char *fmt, va_list ap) {
  char mem[100], *buf = mem;
  int len;
  if ((len = mg_avprintf(&buf, sizeof(mem), fmt, ap)) > 0) {
    mg_out(nc, buf, len);
  }
  if (buf != mem && buf != NULL) {
    free(buf);
  }
  return len;
}


x86 assembly (clang, -O0):
<mg_vprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x80(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    -0x88(%rbp),%rdi
mov    $0x64,%esi
call   1040 <mg_avprintf@plt>
mov    %eax,-0x8c(%rbp)
cmp    $0x0,%eax
jle    118e <mg_vprintf+0x5e>
mov    -0x8(%rbp),%rdi
mov    -0x88(%rbp),%rsi
mov    -0x8c(%rbp),%edx
call   1050 <mg_out@plt>
mov    -0x88(%rbp),%rax
lea    -0x80(%rbp),%rcx
cmp    %rcx,%rax
je     11bc <mg_vprintf+0x8c>
cmpq   $0x0,-0x88(%rbp)
je     11bc <mg_vprintf+0x8c>
mov    -0x88(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8c(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mg_vprintf>:
push   %r15
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    %edx,%ecx
mov    %rsi,%rdx
mov    %rdi,%r14
lea    0x10(%rsp),%r15
mov    %r15,0x8(%rsp)
lea    0x8(%rsp),%rdi
mov    $0x64,%esi
call   1040 <mg_avprintf@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    1172 <mg_vprintf+0x42>
mov    0x8(%rsp),%rsi
mov    %r14,%rdi
mov    %ebx,%edx
call   1050 <mg_out@plt>
mov    0x8(%rsp),%rdi
cmp    %r15,%rdi
je     1186 <mg_vprintf+0x56>
test   %rdi,%rdi
je     1186 <mg_vprintf+0x56>
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.mg_connection = type { i32 }
define i32 @mg_vprintf(%struct.mg_connection* %0, i8* %1, i32 %2) {
%4 = alloca %struct.mg_connection*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [100 x i8]
%8 = alloca i8*
%9 = alloca i32
store %struct.mg_connection* %0, %struct.mg_connection** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
store i8* %10, i8** %8
%11 = load i8*, i8** %5
%12 = load i32, i32* %6
%13 = call i32 @mg_avprintf(i8** %8, i32 100, i8* %11, i32 %12)
store i32 %13, i32* %9
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %3
%16 = load %struct.mg_connection*, %struct.mg_connection** %4
%17 = load i8*, i8** %8
%18 = load i32, i32* %9
%19 = call i32 @mg_out(%struct.mg_connection* %16, i8* %17, i32 %18)
br label %20
20: ; preds = %15, %3
%21 = load i8*, i8** %8
%22 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%23 = icmp ne i8* %21, %22
br i1 %23, label %24, label %30
24: ; preds = %20
%25 = load i8*, i8** %8
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i8*, i8** %8
%29 = call i32 @free(i8* %28)
br label %30
30: ; preds = %27, %24, %20
%31 = load i32, i32* %9
ret i32 %31
}

LLVM IR (-O2):
%struct.mg_connection = type { i32 }
define i32 @mg_vprintf(%struct.mg_connection* %0, i8* %1, i32 %2) {
%4 = alloca [100 x i8]
%5 = alloca i8*
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%7 = bitcast i8** %5 to i8*
store i8* %6, i8** %5
%8 = call i32 @mg_avprintf(i8** %5, i32 100, i8* %1, i32 %2)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = load i8*, i8** %5
%12 = call i32 @mg_out(%struct.mg_connection* %0, i8* %11, i32 %8)
br label %13
13: ; preds = %10, %3
%14 = load i8*, i8** %5
%15 = icmp ne i8* %14, %6
%16 = icmp ne i8* %14, null
%17 = and i1 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 @free(i8* %14)
br label %20
20: ; preds = %18, %13
ret i32 %8
}