#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 usputc(char, int);
int vsprintf(char *, char const *, int /*<<< orphan*/);
int usvprintf(int fd, const char *fmt, va_list args) {
  static char buf[1500];
  int len, i;
  len = vsprintf(buf, fmt, args);
  for (i = 0; i < len; i++)
    if (usputc(buf[i], fd) == -1)
      return -1;
  return len;
}


x86 assembly (clang, -O0):
<usvprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0x2f00(%rip),%rdi
call   1040 <vsprintf@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    119c <usvprintf+0x7c>
movslq -0x1c(%rbp),%rcx
lea    0x2eda(%rip),%rax
mov    (%rax,%rcx,1),%al
mov    -0x8(%rbp),%esi
movsbl %al,%edi
call   1030 <usputc@plt>
cmp    $0xffffffff,%eax
jne    1189 <usvprintf+0x69>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a2 <usvprintf+0x82>
jmp    118e <usvprintf+0x6e>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    114f <usvprintf+0x2f>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<usvprintf>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r12d
lea    0x2f0e(%rip),%r15
mov    %r15,%rdi
call   1040 <vsprintf@plt>
mov    %eax,%r14d
test   %eax,%eax
jle    1174 <usvprintf+0x54>
mov    %r14d,%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
movsbl 0x0(%rbp,%r15,1),%edi
mov    %r12d,%esi
call   1030 <usputc@plt>
cmp    $0xffffffff,%eax
je     116e <usvprintf+0x4e>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1150 <usvprintf+0x30>
jmp    1174 <usvprintf+0x54>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@usvprintf.buf = internal global [1500 x i8] zeroinitializer
define i32 @usvprintf(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %6
%11 = load i32, i32* %7
%12 = call i32 @vsprintf(i8* getelementptr inbounds ([1500 x i8], [1500 x i8]* @usvprintf.buf, i64 0, i64 0), i8* %10, i32 %11)
store i32 %12, i32* %8
store i32 0, i32* %9
br label %13
13: ; preds = %27, %3
%14 = load i32, i32* %9
%15 = load i32, i32* %8
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %30
17: ; preds = %13
%18 = load i32, i32* %9
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [1500 x i8], [1500 x i8]* @usvprintf.buf, i64 0, i64 %19
%21 = load i8, i8* %20
%22 = load i32, i32* %5
%23 = call i32 @usputc(i8 signext %21, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %26
25: ; preds = %17
store i32 -1, i32* %4
br label %32
26: ; preds = %17
br label %27
27: ; preds = %26
%28 = load i32, i32* %9
%29 = add nsw i32 %28, 1
store i32 %29, i32* %9
br label %13
30: ; preds = %13
%31 = load i32, i32* %8
store i32 %31, i32* %4
br label %32
32: ; preds = %30, %25
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
@usvprintf.buf = internal global [1500 x i8] zeroinitializer
define i32 @usvprintf(i32 %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32 @vsprintf(i8* getelementptr inbounds ([1500 x i8], [1500 x i8]* @usvprintf.buf, i64 0, i64 0), i8* %1, i32 %2)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %17
6: ; preds = %3
%7 = zext i32 %4 to i64
br label %10
8: ; preds = %10
%9 = icmp eq i64 %16, %7
br i1 %9, label %17, label %10
10: ; preds = %6, %8
%11 = phi i64 [ 0, %6 ], [ %16, %8 ]
%12 = getelementptr inbounds [1500 x i8], [1500 x i8]* @usvprintf.buf, i64 0, i64 %11
%13 = load i8, i8* %12
%14 = tail call i32 @usputc(i8 signext %13, i32 %0)
%15 = icmp eq i32 %14, -1
%16 = add nuw nsw i64 %11, 1
br i1 %15, label %17, label %8
17: ; preds = %10, %8, %3
%18 = phi i32 [ %4, %3 ], [ %4, %8 ], [ -1, %10 ]
ret i32 %18
}