#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;
typedef int /*<<< orphan*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EINVAL;
int errno;
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int fprintf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
int vsnprintf(char *, int, char const *, int /*<<< orphan*/);
int dbg_msg_fp_va(FILE *fp, const char *format, va_list ap) {
  char buffer[1024], *bptr = buffer;
  int bsize = sizeof(buffer);
  int ret = -EINVAL;
  ret = vsnprintf(bptr, bsize, format, ap);
  if (ret < 0)
    return -errno;
  strcat(buffer, "\n");
  ret = fprintf(fp, "%s", buffer);
  if (ret < 0)
    return -errno;
  fflush(fp);
  return 0;
}


x86 assembly (clang, -O0):
<dbg_msg_fp_va>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x428(%rbp)
movl   $0x400,-0x42c(%rbp)
xor    %eax,%eax
mov    0x2e61(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x430(%rbp)
mov    -0x428(%rbp),%rdi
mov    -0x42c(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <vsnprintf@plt>
mov    %eax,-0x430(%rbp)
cmpl   $0x0,-0x430(%rbp)
jge    11be <dbg_msg_fp_va+0x7e>
xor    %eax,%eax
mov    0x2e2c(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    121e <dbg_msg_fp_va+0xde>
lea    -0x420(%rbp),%rdi
lea    0xe34(%rip),%rsi
call   1060 <strcat@plt>
mov    -0x10(%rbp),%rdi
lea    -0x420(%rbp),%rdx
lea    0xe1f(%rip),%rsi
call   1040 <fprintf@plt>
mov    %eax,-0x430(%rbp)
cmpl   $0x0,-0x430(%rbp)
jge    120e <dbg_msg_fp_va+0xce>
xor    %eax,%eax
mov    0x2ddc(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    121e <dbg_msg_fp_va+0xde>
mov    -0x10(%rbp),%rdi
call   1050 <fflush@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dbg_msg_fp_va>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %edx,%ecx
mov    %rsi,%rdx
mov    %rdi,%rbx
mov    %rsp,%rdi
mov    $0x400,%esi
call   1030 <vsnprintf@plt>
test   %eax,%eax
js     1197 <dbg_msg_fp_va+0x57>
lea    0xe96(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
call   1060 <strcat@plt>
lea    0xe86(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1040 <fprintf@plt>
test   %eax,%eax
js     1197 <dbg_msg_fp_va+0x57>
mov    %rbx,%rdi
call   1050 <fflush@plt>
xor    %eax,%eax
jmp    11a2 <dbg_msg_fp_va+0x62>
mov    0x2e42(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [2 x i8] c"\0A\00"
@.str.1 = [3 x i8] c"%s\00"
define i32 @dbg_msg_fp_va(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [1024 x i8]
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
store i8* %12, i8** %9
store i32 1024, i32* %10
%13 = load i32, i32* @EINVAL
%14 = sub nsw i32 0, %13
store i32 %14, i32* %11
%15 = load i8*, i8** %9
%16 = load i32, i32* %10
%17 = load i8*, i8** %6
%18 = load i32, i32* %7
%19 = call i32 @vsnprintf(i8* %15, i32 %16, i8* %17, i32 %18)
store i32 %19, i32* %11
%20 = load i32, i32* %11
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %3
%23 = load i32, i32* @errno
%24 = sub nsw i32 0, %23
store i32 %24, i32* %4
br label %39
25: ; preds = %3
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%27 = call i32 @strcat(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%28 = load i32*, i32** %5
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%30 = call i32 @fprintf(i32* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %29)
store i32 %30, i32* %11
%31 = load i32, i32* %11
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %25
%34 = load i32, i32* @errno
%35 = sub nsw i32 0, %34
store i32 %35, i32* %4
br label %39
36: ; preds = %25
%37 = load i32*, i32** %5
%38 = call i32 @fflush(i32* %37)
store i32 0, i32* %4
br label %39
39: ; preds = %36, %33, %22
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [2 x i8] c"\0A\00"
@.str.1 = [3 x i8] c"%s\00"
define i32 @dbg_msg_fp_va(i32* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = call i32 @vsnprintf(i8* %5, i32 1024, i8* %1, i32 %2)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = load i32, i32* @errno
%10 = sub nsw i32 0, %9
br label %20
11: ; preds = %3
%12 = call i32 @strcat(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @fprintf(i32* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* @errno
%17 = sub nsw i32 0, %16
br label %20
18: ; preds = %11
%19 = call i32 @fflush(i32* %0)
br label %20
20: ; preds = %18, %15, %8
%21 = phi i32 [ %10, %8 ], [ %17, %15 ], [ 0, %18 ]
ret i32 %21
}