#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 terminal_print(int, int, char *);
int vsnprintf(char *, int, char const *, int /*<<< orphan*/);
int terminal_vprintf(int x, int y, const char *s, va_list args) {
  static char buffer[1024] = {0};
  int rc = 0;
  if (s == NULL)
    return 0;
  rc = vsnprintf(buffer, 1024 - 1, s, args);
  if (rc > 0)
    rc = terminal_print(x, y, buffer);
  return rc;
}


x86 assembly (clang, -O0):
<terminal_vprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1153 <terminal_vprintf+0x33>
movl   $0x0,-0x4(%rbp)
jmp    1193 <terminal_vprintf+0x73>
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
lea    0x2edf(%rip),%rdi
mov    $0x3ff,%esi
call   1030 <vsnprintf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    118d <terminal_vprintf+0x6d>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
lea    0x2ebb(%rip),%rdx
call   1040 <terminal_print@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<terminal_vprintf>:
push   %rbp
push   %rbx
push   %rax
test   %rdx,%rdx
je     1157 <terminal_vprintf+0x37>
mov    %esi,%ebx
mov    %edi,%ebp
lea    0x2f0d(%rip),%rdi
mov    $0x3ff,%esi
call   1030 <vsnprintf@plt>
test   %eax,%eax
jle    1159 <terminal_vprintf+0x39>
lea    0x2ef8(%rip),%rdx
mov    %ebp,%edi
mov    %ebx,%esi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <terminal_print@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@terminal_vprintf.buffer = internal global [1024 x i8] zeroinitializer
define i32 @terminal_vprintf(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 0, i32* %10
%11 = load i8*, i8** %8
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %4
store i32 0, i32* %5
br label %26
14: ; preds = %4
%15 = load i8*, i8** %8
%16 = load i32, i32* %9
%17 = call i32 @vsnprintf(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @terminal_vprintf.buffer, i64 0, i64 0), i32 1023, i8* %15, i32 %16)
store i32 %17, i32* %10
%18 = load i32, i32* %10
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = load i32, i32* %6
%22 = load i32, i32* %7
%23 = call i32 @terminal_print(i32 %21, i32 %22, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @terminal_vprintf.buffer, i64 0, i64 0))
store i32 %23, i32* %10
br label %24
24: ; preds = %20, %14
%25 = load i32, i32* %10
store i32 %25, i32* %5
br label %26
26: ; preds = %24, %13
%27 = load i32, i32* %5
ret i32 %27
}

LLVM IR (-O2):
@terminal_vprintf.buffer = internal global [1024 x i8] zeroinitializer
define i32 @terminal_vprintf(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = icmp eq i8* %2, null
br i1 %5, label %11, label %6
6: ; preds = %4
%7 = tail call i32 @vsnprintf(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @terminal_vprintf.buffer, i64 0, i64 0), i32 1023, i8* %2, i32 %3)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @terminal_print(i32 %0, i32 %1, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @terminal_vprintf.buffer, i64 0, i64 0))
br label %11
11: ; preds = %6, %9, %4
%12 = phi i32 [ 0, %4 ], [ %10, %9 ], [ %7, %6 ]
ret i32 %12
}