#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 /*<<< orphan*/ free(char *);
char *libintl_vasnprintf(char *, size_t *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ *strchr(char const *, char);
int vsprintf(char *, char const *, int /*<<< orphan*/);
int libintl_vsprintf(char *resultbuf, const char *format, va_list args) {
  if (strchr(format, '$') == NULL)
    return vsprintf(resultbuf, format, args);
  else {
    size_t length = (size_t)~0 / (4 * sizeof(char));
    char *result = libintl_vasnprintf(resultbuf, &length, format, args);
    if (result != resultbuf) {
      free(result);
      return -1;
    } else
      return length;
  }
}


x86 assembly (clang, -O0):
<libintl_vsprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x24,%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
jne    1183 <libintl_vsprintf+0x43>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1060 <vsprintf@plt>
mov    %eax,-0x4(%rbp)
jmp    11d3 <libintl_vsprintf+0x93>
movabs $0x3fffffffffffffff,%rax
ff ff 3f
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
lea    -0x28(%rbp),%rsi
call   1050 <libintl_vasnprintf@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     11cc <libintl_vsprintf+0x8c>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d3 <libintl_vsprintf+0x93>
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<libintl_vsprintf>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    %rsi,%rdi
mov    $0x24,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1199 <libintl_vsprintf+0x59>
movabs $0x3fffffffffffffff,%rax
ff ff 3f
mov    %rax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    %rbp,%rdx
mov    %r14d,%ecx
call   1050 <libintl_vasnprintf@plt>
cmp    %rbx,%rax
je     11af <libintl_vsprintf+0x6f>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%eax
jmp    11b3 <libintl_vsprintf+0x73>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <vsprintf@plt>
mov    0x8(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @libintl_vsprintf(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %6
%11 = call i32* @strchr(i8* %10, i8 signext 36)
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %18
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = load i8*, i8** %6
%16 = load i32, i32* %7
%17 = call i32 @vsprintf(i8* %14, i8* %15, i32 %16)
store i32 %17, i32* %4
br label %32
18: ; preds = %3
store i64 4611686018427387903, i64* %8
%19 = load i8*, i8** %5
%20 = load i8*, i8** %6
%21 = load i32, i32* %7
%22 = call i8* @libintl_vasnprintf(i8* %19, i64* %8, i8* %20, i32 %21)
store i8* %22, i8** %9
%23 = load i8*, i8** %9
%24 = load i8*, i8** %5
%25 = icmp ne i8* %23, %24
br i1 %25, label %26, label %29
26: ; preds = %18
%27 = load i8*, i8** %9
%28 = call i32 @free(i8* %27)
store i32 -1, i32* %4
br label %32
29: ; preds = %18
%30 = load i64, i64* %8
%31 = trunc i64 %30 to i32
store i32 %31, i32* %4
br label %32
32: ; preds = %29, %26, %13
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
define i32 @libintl_vsprintf(i8* %0, i8* %1, i32 %2) {
%4 = alloca i64
%5 = tail call i32* @strchr(i8* %1, i8 signext 36)
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @vsprintf(i8* %0, i8* %1, i32 %2)
br label %20
9: ; preds = %3
%10 = bitcast i64* %4 to i8*
store i64 4611686018427387903, i64* %4
%11 = call i8* @libintl_vasnprintf(i8* %0, i64* %4, i8* %1, i32 %2)
%12 = icmp eq i8* %11, %0
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = call i32 @free(i8* %11)
br label %18
15: ; preds = %9
%16 = load i64, i64* %4
%17 = trunc i64 %16 to i32
br label %18
18: ; preds = %15, %13
%19 = phi i32 [ -1, %13 ], [ %17, %15 ]
br label %20
20: ; preds = %18, %7
%21 = phi i32 [ %8, %7 ], [ %19, %18 ]
ret i32 %21
}