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

/* Variables and functions */
int /*<<< orphan*/ do_write(int, char *, int);
int getpid();
int sprintf(char *, char *, char const *, char const *, char const *,
            char const *, int, int);
int vsnprintf(char *, int, char const *, int /*<<< orphan*/);
void logger_write_v(const char *prog, const char *tm, const char *level,
                    const char *func, const char *file, int line,
                    const char *fmt, va_list argptr) {
  char buff[1024] = {'\0'};
  int len;
  int l;
  len = sprintf(buff, "%s %s [func:%s()][%s:%d][pid:%d] ", tm, level, func,
                file, line, getpid());
  l = vsnprintf(buff + len, sizeof(buff) - len - 1, fmt, argptr);
  if (l < sizeof(buff) - len - 1) {
    len += l;
  } else {
    len = sizeof(buff) - 1;
  }
  buff[len++] = '\n';
  do_write(1, buff, len);
  return;
}


x86 assembly (clang, -O0):
<logger_write_v>:
push   %rbp
mov    %rsp,%rbp
sub    $0x480,%rsp
mov    0x18(%rbp),%eax
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    -0x430(%rbp),%rax
mov    %rax,-0x468(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x460(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x458(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x450(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x448(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x43c(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x468(%rbp),%rdi
mov    -0x460(%rbp),%rdx
mov    -0x458(%rbp),%rcx
mov    -0x450(%rbp),%r8
mov    -0x448(%rbp),%r9
mov    -0x43c(%rbp),%r10d
lea    0xdf8(%rip),%rsi
mov    %r10d,(%rsp)
mov    %eax,0x8(%rsp)
call   1070 <sprintf@plt>
mov    %eax,-0x434(%rbp)
lea    -0x430(%rbp),%rdi
movslq -0x434(%rbp),%rax
add    %rax,%rdi
movslq -0x434(%rbp),%rcx
mov    $0x400,%eax
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%esi
mov    0x10(%rbp),%rdx
mov    0x18(%rbp),%ecx
call   1040 <vsnprintf@plt>
mov    %eax,-0x438(%rbp)
movslq -0x438(%rbp),%rax
movslq -0x434(%rbp),%rdx
mov    $0x400,%ecx
sub    %rdx,%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jae    128d <logger_write_v+0x13d>
mov    -0x438(%rbp),%eax
add    -0x434(%rbp),%eax
mov    %eax,-0x434(%rbp)
jmp    1297 <logger_write_v+0x147>
movl   $0x3ff,-0x434(%rbp)
mov    -0x434(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x434(%rbp)
cltq
movb   $0xa,-0x430(%rbp,%rax,1)
0a
lea    -0x430(%rbp),%rsi
mov    -0x434(%rbp),%edx
mov    $0x1,%edi
call   1030 <do_write@plt>
add    $0x480,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<logger_write_v>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %r9d,%r13d
mov    %r8,%rbx
mov    %rcx,%rbp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rsp,%r12
mov    $0x400,%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe70(%rip),%rsi
mov    %r12,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %rbp,%r8
mov    %rbx,%r9
push   %rax
push   %r13
call   1070 <sprintf@plt>
add    $0x10,%rsp
movslq %eax,%rbp
lea    (%rsp,%rbp,1),%rdi
mov    $0x3ff,%ebx
sub    %rbp,%rbx
mov    %ebx,%esi
mov    0x440(%rsp),%rdx
mov    0x448(%rsp),%ecx
call   1040 <vsnprintf@plt>
cltq
add    %eax,%ebp
cmp    %rax,%rbx
mov    $0x3ff,%edx
cmova  %ebp,%edx
movslq %edx,%rax
add    $0x1,%edx
movb   $0xa,(%rsp,%rax,1)
mov    $0x1,%edi
mov    %r12,%rsi
call   1030 <do_write@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [34 x i8] c"%s %s [func:%s()][%s:%d][pid:%d] \00"
define void @logger_write_v(i8* %0, i8* %1, i8* %2, i8* %3, i8* %4, i32 %5, i8* %6, i32 %7) {
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i8*
%16 = alloca i32
%17 = alloca [1024 x i8]
%18 = alloca i32
%19 = alloca i32
store i8* %0, i8** %9
store i8* %1, i8** %10
store i8* %2, i8** %11
store i8* %3, i8** %12
store i8* %4, i8** %13
store i32 %5, i32* %14
store i8* %6, i8** %15
store i32 %7, i32* %16
%20 = bitcast [1024 x i8]* %17 to i8*
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %17, i64 0, i64 0
%22 = load i8*, i8** %10
%23 = load i8*, i8** %11
%24 = load i8*, i8** %12
%25 = load i8*, i8** %13
%26 = load i32, i32* %14
%27 = call i32 (...) @getpid()
%28 = call i32 @sprintf(i8* %21, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %22, i8* %23, i8* %24, i8* %25, i32 %26, i32 %27)
store i32 %28, i32* %18
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %17, i64 0, i64 0
%30 = load i32, i32* %18
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = load i32, i32* %18
%34 = sext i32 %33 to i64
%35 = sub i64 1024, %34
%36 = sub i64 %35, 1
%37 = trunc i64 %36 to i32
%38 = load i8*, i8** %15
%39 = load i32, i32* %16
%40 = call i32 @vsnprintf(i8* %32, i32 %37, i8* %38, i32 %39)
store i32 %40, i32* %19
%41 = load i32, i32* %19
%42 = sext i32 %41 to i64
%43 = load i32, i32* %18
%44 = sext i32 %43 to i64
%45 = sub i64 1024, %44
%46 = sub i64 %45, 1
%47 = icmp ult i64 %42, %46
br i1 %47, label %48, label %52
48: ; preds = %8
%49 = load i32, i32* %19
%50 = load i32, i32* %18
%51 = add nsw i32 %50, %49
store i32 %51, i32* %18
br label %53
52: ; preds = %8
store i32 1023, i32* %18
br label %53
53: ; preds = %52, %48
%54 = load i32, i32* %18
%55 = add nsw i32 %54, 1
store i32 %55, i32* %18
%56 = sext i32 %54 to i64
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %17, i64 0, i64 %56
store i8 10, i8* %57
%58 = getelementptr inbounds [1024 x i8], [1024 x i8]* %17, i64 0, i64 0
%59 = load i32, i32* %18
%60 = call i32 @do_write(i32 1, i8* %58, i32 %59)
ret void
}

LLVM IR (-O2):
@.str = [34 x i8] c"%s %s [func:%s()][%s:%d][pid:%d] \00"
define void @logger_write_v(i8* nocapture readnone %0, i8* %1, i8* %2, i8* %3, i8* %4, i32 %5, i8* nocapture %6, i32 %7) {
%9 = alloca [1024 x i8]
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%11 = tail call i32 (...) @getpid()
%12 = call i32 @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %2, i8* %3, i8* %4, i32 %5, i32 %11)
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 %13
%15 = sub nsw i64 1023, %13
%16 = trunc i64 %15 to i32
%17 = call i32 @vsnprintf(i8* %14, i32 %16, i8* %6, i32 %7)
%18 = sext i32 %17 to i64
%19 = icmp ugt i64 %15, %18
%20 = add nsw i32 %17, %12
%21 = select i1 %19, i32 %20, i32 1023
%22 = add nsw i32 %21, 1
%23 = sext i32 %21 to i64
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 %23
store i8 10, i8* %24
%25 = call i32 @do_write(i32 1, i8* %10, i32 %22)
ret void
}