#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*/ FILE;

/* Variables and functions */
int fprintf(int /*<<< orphan*/ *, char *, char const *);
char *get_log_prefix_envvar();
int vfprintf(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/);
int det_vfprintf(FILE *stream, const char *format, va_list arg) {
  int err;
  int ret = 0;
  const char *pref = get_log_prefix_envvar();
  if (pref) {
    err = fprintf(stream, "%s", pref);
    if (err < 0)
      return err;
    ret += err;
  }
  err = vfprintf(stream, format, arg);
  if (err < 0)
    return err;
  ret += err;
  return ret;
}


x86 assembly (clang, -O0):
<det_vfprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    $0x0,%al
call   1050 <get_log_prefix_envvar@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1195 <det_vfprintf+0x65>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rdx
lea    0xe91(%rip),%rsi
call   1030 <fprintf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    118c <det_vfprintf+0x5c>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11cc <det_vfprintf+0x9c>
mov    -0x20(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <vfprintf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11bd <det_vfprintf+0x8d>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11cc <det_vfprintf+0x9c>
mov    -0x20(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<det_vfprintf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbp
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <get_log_prefix_envvar@plt>
test   %rax,%rax
je     1166 <det_vfprintf+0x36>
lea    0xeab(%rip),%rsi
mov    %rbp,%rdi
mov    %rax,%rdx
call   1030 <fprintf@plt>
mov    %eax,%ebx
test   %eax,%eax
js     117e <det_vfprintf+0x4e>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1040 <vfprintf@plt>
test   %eax,%eax
js     117c <det_vfprintf+0x4c>
add    %eax,%ebx
jmp    117e <det_vfprintf+0x4e>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
define i32 @det_vfprintf(i32* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = tail call i8* (...) @get_log_prefix_envvar()
%5 = icmp eq i8* %4, null
br i1 %5, label %9, label %6
6: ; preds = %3
%7 = tail call i32 @fprintf(i32* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %4)
%8 = icmp slt i32 %7, 0
br i1 %8, label %15, label %9
9: ; preds = %6, %3
%10 = phi i32 [ 0, %3 ], [ %7, %6 ]
%11 = tail call i32 @vfprintf(i32* %0, i8* %1, i32 %2)
%12 = icmp slt i32 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = add nuw nsw i32 %11, %10
br label %15
15: ; preds = %9, %6, %13
%16 = phi i32 [ %14, %13 ], [ %7, %6 ], [ %11, %9 ]
ret i32 %16
}