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

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int ns_avprintf(char **, int, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ ns_out(struct ns_connection *, char *, int);
int ns_vprintf(struct ns_connection *nc, const char *fmt, va_list ap) {
  char mem[500], *buf = mem;
  int len;
  if ((len = ns_avprintf(&buf, sizeof(mem), fmt, ap)) > 0) {
    ns_out(nc, buf, len);
  }
  if (buf != mem && buf != NULL) {
    free(buf);
  }
  return len;
}


x86 assembly (clang, -O0):
<ns_vprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x210(%rbp),%rax
mov    %rax,-0x218(%rbp)
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    -0x218(%rbp),%rdi
mov    $0x1f4,%esi
call   1050 <ns_avprintf@plt>
mov    %eax,-0x21c(%rbp)
cmp    $0x0,%eax
jle    1191 <ns_vprintf+0x61>
mov    -0x8(%rbp),%rdi
mov    -0x218(%rbp),%rsi
mov    -0x21c(%rbp),%edx
call   1040 <ns_out@plt>
mov    -0x218(%rbp),%rax
lea    -0x210(%rbp),%rcx
cmp    %rcx,%rax
je     11c2 <ns_vprintf+0x92>
cmpq   $0x0,-0x218(%rbp)
je     11c2 <ns_vprintf+0x92>
mov    -0x218(%rbp),%rdi
call   1030 <free@plt>
mov    -0x21c(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

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

LLVM IR (-O0):
%struct.ns_connection = type { i32 }
define i32 @ns_vprintf(%struct.ns_connection* %0, i8* %1, i32 %2) {
%4 = alloca %struct.ns_connection*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [500 x i8]
%8 = alloca i8*
%9 = alloca i32
store %struct.ns_connection* %0, %struct.ns_connection** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = getelementptr inbounds [500 x i8], [500 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 @ns_avprintf(i8** %8, i32 500, 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.ns_connection*, %struct.ns_connection** %4
%17 = load i8*, i8** %8
%18 = load i32, i32* %9
%19 = call i32 @ns_out(%struct.ns_connection* %16, i8* %17, i32 %18)
br label %20
20: ; preds = %15, %3
%21 = load i8*, i8** %8
%22 = getelementptr inbounds [500 x i8], [500 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.ns_connection = type { i32 }
define i32 @ns_vprintf(%struct.ns_connection* %0, i8* %1, i32 %2) {
%4 = alloca [500 x i8]
%5 = alloca i8*
%6 = getelementptr inbounds [500 x i8], [500 x i8]* %4, i64 0, i64 0
%7 = bitcast i8** %5 to i8*
store i8* %6, i8** %5
%8 = call i32 @ns_avprintf(i8** %5, i32 500, 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 @ns_out(%struct.ns_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
}