#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*/ set_val(char *, int);
int strlen(char *);
int /*<<< orphan*/ vsnprintf(char *, int, char const *, int /*<<< orphan*/);
int vform_message(char *d, int dsize, int tag, const char *fmt, va_list ap) {
  int len;
  vsnprintf(d + 4, dsize - 4, fmt, ap);
  len = strlen(d + 4);
  set_val(d + 2, tag);
  set_val(d, len);
  return (len + 4);
}


x86 assembly (clang, -O0):
<vform_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
add    $0x4,%rdi
mov    -0xc(%rbp),%esi
sub    $0x4,%esi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <vsnprintf@plt>
mov    -0x8(%rbp),%rdi
add    $0x4,%rdi
call   1040 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
add    $0x2,%rdi
mov    -0x10(%rbp),%esi
call   1050 <set_val@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1050 <set_val@plt>
mov    -0x20(%rbp),%eax
add    $0x4,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vform_message>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rdi,%rbx
lea    0x4(%rdi),%rbp
add    $0xfffffffc,%esi
mov    %rbp,%rdi
mov    %rcx,%rdx
mov    %r8d,%ecx
call   1030 <vsnprintf@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
lea    0x2(%rbx),%rdi
mov    %r14d,%esi
call   1050 <set_val@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <set_val@plt>
add    $0x4,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @vform_message(i8* %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 %4, i32* %10
%12 = load i8*, i8** %6
%13 = getelementptr inbounds i8, i8* %12, i64 4
%14 = load i32, i32* %7
%15 = sub nsw i32 %14, 4
%16 = load i8*, i8** %9
%17 = load i32, i32* %10
%18 = call i32 @vsnprintf(i8* %13, i32 %15, i8* %16, i32 %17)
%19 = load i8*, i8** %6
%20 = getelementptr inbounds i8, i8* %19, i64 4
%21 = call i32 @strlen(i8* %20)
store i32 %21, i32* %11
%22 = load i8*, i8** %6
%23 = getelementptr inbounds i8, i8* %22, i64 2
%24 = load i32, i32* %8
%25 = call i32 @set_val(i8* %23, i32 %24)
%26 = load i8*, i8** %6
%27 = load i32, i32* %11
%28 = call i32 @set_val(i8* %26, i32 %27)
%29 = load i32, i32* %11
%30 = add nsw i32 %29, 4
ret i32 %30
}

LLVM IR (-O2):
define i32 @vform_message(i8* %0, i32 %1, i32 %2, i8* nocapture %3, i32 %4) {
%6 = getelementptr inbounds i8, i8* %0, i64 4
%7 = add nsw i32 %1, -4
%8 = tail call i32 @vsnprintf(i8* %6, i32 %7, i8* %3, i32 %4)
%9 = tail call i32 @strlen(i8* %6)
%10 = getelementptr inbounds i8, i8* %0, i64 2
%11 = tail call i32 @set_val(i8* %10, i32 %2)
%12 = tail call i32 @set_val(i8* %0, i32 %9)
%13 = add nsw i32 %9, 4
ret i32 %13
}