#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 */
scalar_t__ qemu_log_line(char const *, int);
char *strchr(char const *, char);
int strlen(char const *);
int vsnprintf(char *, int, char const *, int /*<<< orphan*/);
int __qemu_log_vprintf(const char *fmt, va_list args) {
  char logbuf[1024];
  const char *next, *nl;
  int size;
  size = vsnprintf(logbuf, 1024, fmt, args);
  if (size < 0)
    return size;
  if (size >= 1024)
    logbuf[1024 - 1] = '\0';
  next = logbuf;
  while ((nl = strchr(next, '\n'))) {
    if (qemu_log_line(next, nl - next + 1) < 0)
      return -1;
    next = nl + 1;
  }
  if (*next)
    if (qemu_log_line(next, strlen(next)) < 0)
      return -1;
  if (size >= 1024)
    if (qemu_log_line("...[truncated]", 14))
      return -1;
  return 0;
}


x86 assembly (clang, -O0):
<__qemu_log_vprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
mov    $0x400,%esi
call   1030 <vsnprintf@plt>
mov    %eax,-0x434(%rbp)
cmpl   $0x0,-0x434(%rbp)
jge    118b <__qemu_log_vprintf+0x4b>
mov    -0x434(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b5 <__qemu_log_vprintf+0x175>
cmpl   $0x400,-0x434(%rbp)
jl     119f <__qemu_log_vprintf+0x5f>
movb   $0x0,-0x21(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x428(%rbp)
mov    -0x428(%rbp),%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
mov    %rax,-0x430(%rbp)
cmp    $0x0,%rax
je     121f <__qemu_log_vprintf+0xdf>
mov    -0x428(%rbp),%rdi
mov    -0x430(%rbp),%rax
mov    -0x428(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,%esi
call   1060 <qemu_log_line@plt>
cmp    $0x0,%rax
jge    1208 <__qemu_log_vprintf+0xc8>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b5 <__qemu_log_vprintf+0x175>
mov    -0x430(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x428(%rbp)
jmp    11ad <__qemu_log_vprintf+0x6d>
mov    -0x428(%rbp),%rax
cmpb   $0x0,(%rax)
je     1272 <__qemu_log_vprintf+0x132>
mov    -0x428(%rbp),%rax
mov    %rax,-0x440(%rbp)
mov    -0x428(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x440(%rbp),%rdi
mov    %eax,%esi
call   1060 <qemu_log_line@plt>
cmp    $0x0,%rax
jge    126d <__qemu_log_vprintf+0x12d>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b5 <__qemu_log_vprintf+0x175>
jmp    1272 <__qemu_log_vprintf+0x132>
cmpl   $0x400,-0x434(%rbp)
jl     12ae <__qemu_log_vprintf+0x16e>
lea    0xd77(%rip),%rdi
mov    $0xe,%esi
call   1060 <qemu_log_line@plt>
cmp    $0x0,%rax
je     12a9 <__qemu_log_vprintf+0x169>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b5 <__qemu_log_vprintf+0x175>
jmp    12ae <__qemu_log_vprintf+0x16e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__qemu_log_vprintf>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %esi,%ecx
mov    %rdi,%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1030 <vsnprintf@plt>
mov    %eax,%r14d
test   %eax,%eax
js     121c <__qemu_log_vprintf+0xdc>
cmp    $0x400,%r14d
jb     117c <__qemu_log_vprintf+0x3c>
movb   $0x0,0x3ff(%rsp)
mov    %rbx,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11d0 <__qemu_log_vprintf+0x90>
mov    %rax,%rbp
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%esi
sub    %ebx,%esi
add    $0x1,%esi
mov    %rbx,%rdi
call   1060 <qemu_log_line@plt>
test   %rax,%rax
js     1216 <__qemu_log_vprintf+0xd6>
mov    %rbp,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11a0 <__qemu_log_vprintf+0x60>
cmpb   $0x0,(%rbx)
je     11ec <__qemu_log_vprintf+0xac>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1060 <qemu_log_line@plt>
test   %rax,%rax
js     1216 <__qemu_log_vprintf+0xd6>
cmp    $0x400,%r14d
jb     1211 <__qemu_log_vprintf+0xd1>
lea    0xe04(%rip),%rdi
mov    $0xe,%esi
call   1060 <qemu_log_line@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
jne    121c <__qemu_log_vprintf+0xdc>
xor    %r14d,%r14d
jmp    121c <__qemu_log_vprintf+0xdc>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"...[truncated]\00"
define i32 @__qemu_log_vprintf(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%11 = load i8*, i8** %4
%12 = load i32, i32* %5
%13 = call i32 @vsnprintf(i8* %10, i32 1024, i8* %11, i32 %12)
store i32 %13, i32* %9
%14 = load i32, i32* %9
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = load i32, i32* %9
store i32 %17, i32* %3
br label %65
18: ; preds = %2
%19 = load i32, i32* %9
%20 = icmp sge i32 %19, 1024
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 1023
store i8 0, i8* %22
br label %23
23: ; preds = %21, %18
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
store i8* %24, i8** %7
br label %25
25: ; preds = %41, %23
%26 = load i8*, i8** %7
%27 = call i8* @strchr(i8* %26, i8 signext 10)
store i8* %27, i8** %8
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %44
29: ; preds = %25
%30 = load i8*, i8** %7
%31 = load i8*, i8** %8
%32 = load i8*, i8** %7
%33 = ptrtoint i8* %31 to i64
%34 = ptrtoint i8* %32 to i64
%35 = sub i64 %33, %34
%36 = add nsw i64 %35, 1
%37 = trunc i64 %36 to i32
%38 = call i64 @qemu_log_line(i8* %30, i32 %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %29
store i32 -1, i32* %3
br label %65
41: ; preds = %29
%42 = load i8*, i8** %8
%43 = getelementptr inbounds i8, i8* %42, i64 1
store i8* %43, i8** %7
br label %25
44: ; preds = %25
%45 = load i8*, i8** %7
%46 = load i8, i8* %45
%47 = icmp ne i8 %46, 0
br i1 %47, label %48, label %56
48: ; preds = %44
%49 = load i8*, i8** %7
%50 = load i8*, i8** %7
%51 = call i32 @strlen(i8* %50)
%52 = call i64 @qemu_log_line(i8* %49, i32 %51)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %48
store i32 -1, i32* %3
br label %65
55: ; preds = %48
br label %56
56: ; preds = %55, %44
%57 = load i32, i32* %9
%58 = icmp sge i32 %57, 1024
br i1 %58, label %59, label %64
59: ; preds = %56
%60 = call i64 @qemu_log_line(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 14)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %59
store i32 -1, i32* %3
br label %65
63: ; preds = %59
br label %64
64: ; preds = %63, %56
store i32 0, i32* %3
br label %65
65: ; preds = %64, %62, %54, %40, %16
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
@.str = [15 x i8] c"...[truncated]\00"
define i32 @__qemu_log_vprintf(i8* nocapture %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = call i32 @vsnprintf(i8* %4, i32 1024, i8* %0, i32 %1)
%6 = icmp slt i32 %5, 0
br i1 %6, label %41, label %7
7: ; preds = %2
%8 = icmp ugt i32 %5, 1023
br i1 %8, label %9, label %11
9: ; preds = %7
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 1023
store i8 0, i8* %10
br label %11
11: ; preds = %9, %7
%12 = call i8* @strchr(i8* dereferenceable(1) %4, i8 signext 10)
%13 = icmp eq i8* %12, null
br i1 %13, label %28, label %17
14: ; preds = %17
%15 = call i8* @strchr(i8* dereferenceable(1) %27, i8 signext 10)
%16 = icmp eq i8* %15, null
br i1 %16, label %28, label %17
17: ; preds = %11, %14
%18 = phi i8* [ %15, %14 ], [ %12, %11 ]
%19 = phi i8* [ %27, %14 ], [ %4, %11 ]
%20 = ptrtoint i8* %18 to i64
%21 = ptrtoint i8* %19 to i64
%22 = sub i64 %20, %21
%23 = trunc i64 %22 to i32
%24 = add i32 %23, 1
%25 = call i64 @qemu_log_line(i8* %19, i32 %24)
%26 = icmp slt i64 %25, 0
%27 = getelementptr inbounds i8, i8* %18, i64 1
br i1 %26, label %41, label %14
28: ; preds = %14, %11
%29 = phi i8* [ %4, %11 ], [ %27, %14 ]
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 0
br i1 %31, label %36, label %32
32: ; preds = %28
%33 = call i32 @strlen(i8* %29)
%34 = call i64 @qemu_log_line(i8* %29, i32 %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %41, label %36
36: ; preds = %32, %28
br i1 %8, label %37, label %40
37: ; preds = %36
%38 = call i64 @qemu_log_line(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 14)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %37, %36
br label %41
41: ; preds = %17, %37, %32, %2, %40
%42 = phi i32 [ 0, %40 ], [ %5, %2 ], [ -1, %32 ], [ -1, %37 ], [ -1, %17 ]
ret i32 %42
}