// File: /workspace/github_c_makefile_star2_O2/aremmell[P]libsir/src/sirinternal.c
// Function: sir_logv
bool _sir_logv(sir_level level, PRINTF_FORMAT const char* format, va_list args) {
    if (!_sir_sanity() || !_sir_validlevel(level) || !_sir_validstr(format))
        return false;

    (void)_sir_seterror(_SIR_E_NOERROR);

    _SIR_LOCK_SECTION(sirconfig, _cfg, SIRMI_CONFIG, false);

    sirbuf buf = {0};

    /* from time to time, update the host name in the config, just in case. */
    time_t now_sec = -1;
    if (-1 != time(&now_sec) &&
        (now_sec - _cfg->state.last_hname_chk) > SIR_HNAME_CHK_INTERVAL) {
        _sir_selflog("updating hostname...");
        if (!_sir_gethostname(_cfg->state.hostname)) {
            _sir_selflog("error: failed to get hostname!");
        } else {
            _cfg->state.last_hname_chk = now_sec;
            _sir_selflog("hostname: '%s'", _cfg->state.hostname);
        }
    }

    /* format timestamp (h/m/s only if the integer time has changed). */
    long now_msec = 0L;
    bool gettime = _sir_clock_gettime(SIR_WALLCLOCK, &now_sec, &now_msec);
    SIR_ASSERT_UNUSED(gettime, gettime);

    /* milliseconds. */
    _sir_snprintf_trunc(buf.msec, SIR_MAXMSEC, SIR_MSECFORMAT, now_msec);

    /* hours/minutes/seconds. */
    if (now_sec > _sir_last_timestamp || !*_cfg->state.timestamp) {
        _sir_last_timestamp = now_sec;
        bool fmt = _sir_formattime(now_sec, _cfg->state.timestamp, SIR_TIMEFORMAT);
        SIR_ASSERT_UNUSED(fmt, fmt);
    }

    /* check elapsed time since updating thread identifier/name. */
    sir_time thrd_chk;
    double msec_since_thrd_chk = _sir_msec_since(&_sir_last_thrd_chk, &thrd_chk);

    /* update the thread identifier/name if enough time has elapsed. */
    if (msec_since_thrd_chk > SIR_THRD_CHK_INTERVAL) {
        _sir_last_thrd_chk = thrd_chk;

        pid_t tid         = _sir_gettid();
        bool resolved_tid = false;

        /* prefer thread names. */
        resolved_tid = _sir_getthreadname(_sir_tid);

        /* if tid is identical to pid... */
        if (!resolved_tid && tid == _cfg->state.pid) {
            /* don't use anything to identify the thread. */
            _sir_resetstr(_sir_tid);
            resolved_tid = true;
        }

        /* fall back on tid. */
        if (!resolved_tid)
            _sir_snprintf_trunc(_sir_tid, SIR_MAXPID, SIR_TIDFORMAT,
                PID_CAST tid);
    }

    sirconfig cfg;
    (void)memcpy(&cfg, _cfg, sizeof(sirconfig));
    _SIR_UNLOCK_SECTION(SIRMI_CONFIG);

    buf.timestamp = cfg.state.timestamp;
    buf.hostname  = cfg.state.hostname;
    buf.pid       = cfg.state.pidbuf;
    buf.name      = cfg.si.name;

#if !defined(SIR_NO_TEXT_STYLING)
    const char* style_str = _sir_gettextstyle(level);

    SIR_ASSERT(NULL != style_str);
    if (NULL != style_str)
        (void)_sir_strncpy(buf.style, SIR_MAXSTYLE, style_str,
            strnlen(style_str, SIR_MAXSTYLE));
#endif

    buf.level = _sir_formattedlevelstr(level);

    if (_sir_validstrnofail(_sir_tid))
        (void)_sir_strncpy(buf.tid, SIR_MAXPID, _sir_tid,
            strnlen(_sir_tid, SIR_MAXPID));

    (void)vsnprintf(buf.message, SIR_MAXMESSAGE, format, args);

    if (!_sir_validstrnofail(buf.message))
        return _sir_seterror(_SIR_E_INTERNAL);

    bool match             = false;
    bool exit_early        = false;
    bool update_last_props = true;
    uint64_t hash          = 0ULL;

    if (cfg.state.last.level == level &&
        cfg.state.last.prefix[0] == buf.message[0]  &&
        cfg.state.last.prefix[1] == buf.message[1]) {
        hash  = FNV64_1a(buf.message);
        match = cfg.state.last.hash == hash;
    }

    if (match) {
        cfg.state.last.counter++;

        if (cfg.state.last.counter >= cfg.state.last.threshold - 2) {
            size_t old_threshold = cfg.state.last.threshold;

            update_last_props = false;
            cfg.state.last.threshold *= SIR_SQUELCH_BACKOFF_FACTOR;
            cfg.state.last.squelch = true;

            _sir_selflog("hit squelch threshold of %zu; setting new threshold"
                         " to %zu (factor: %d)", old_threshold,
                         cfg.state.last.threshold, SIR_SQUELCH_BACKOFF_FACTOR);

            (void)snprintf(buf.message, SIR_MAXMESSAGE, SIR_SQUELCH_MSG_FORMAT, old_threshold);
        } else if (cfg.state.last.squelch) {
            exit_early = true;
        }
    } else {
        cfg.state.last.squelch   = false;
        cfg.state.last.counter   = 0;
        cfg.state.last.threshold = SIR_SQUELCH_THRESHOLD;
        /* _sir_selflog("message '%s' does not match last; resetting", buf.message); */
    }

    _cfg = _sir_locksection(SIRMI_CONFIG);
    if (!_cfg)
        return _sir_seterror(_SIR_E_INTERNAL);

    _cfg->state.last.squelch = cfg.state.last.squelch;

    if (update_last_props) {
        _cfg->state.last.level     = level;
        _cfg->state.last.hash      = hash;
        _cfg->state.last.prefix[0] = buf.message[0];
        _cfg->state.last.prefix[1] = buf.message[1];
    }

    _cfg->state.last.counter   = cfg.state.last.counter;
    _cfg->state.last.threshold = cfg.state.last.threshold;

    _SIR_UNLOCK_SECTION(SIRMI_CONFIG);

    if (exit_early)
        return false;

    bool dispatched = _sir_dispatch(&cfg.si, level, &buf);
    return update_last_props ? dispatched : false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2428, %rsp           # imm = 0x2428
movq %rdx, %r14
movq %rsi, %r15
movl %edi, %ebp
callq 0x5f4b
testb %al, %al
je 0x66c9
movzwl %bp, %ebx
leaq 0x370b(%rip), %rsi      # 0x9c36
leaq 0x3674(%rip), %rdx      # 0x9ba6
movl %ebx, %edi
movl $0x22c, %ecx            # imm = 0x22C
callq 0x532b
testb %al, %al
je 0x66c9
leaq 0x36e9(%rip), %rdx      # 0x9c36
pushq $0x1
popq %rsi
movq %r15, %rdi
movl $0x22c, %ecx            # imm = 0x22C
callq 0x62aa
testb %al, %al
je 0x66c9
leaq 0x36ca(%rip), %rsi      # 0x9c36
leaq 0x3633(%rip), %rdx      # 0x9ba6
movl $0x80010000, %edi       # imm = 0x80010000
movl $0x22f, %ecx            # imm = 0x22F
callq 0x3138
xorl %edi, %edi
callq 0x5c53
testq %rax, %rax
je 0x66ac
movq %rax, %r12
leaq 0x2c8(%rsp), %rdi
movl $0x2160, %edx           # imm = 0x2160
xorl %esi, %esi
callq 0x21b0
leaq 0x8(%rsp), %rdi
orq $-0x1, (%rdi)
callq 0x22b0
cmpq $-0x1, %rax
je 0x65ee
movq 0x8(%rsp), %rax
subq 0x208(%r12), %rax
cmpq $0x3d, %rax
jl 0x65ee
leaq 0x108(%r12), %rdi
callq 0x6a5f
testb %al, %al
je 0x65ee
movq 0x8(%rsp), %rax
movq %rax, 0x208(%r12)
leaq 0x30(%rsp), %r13
andq $0x0, (%r13)
pushq $0x5
popq %rdi
leaq 0x8(%rsp), %rsi
movq %r13, %rdx
callq 0x6a92
movq $0x5, 0x38(%rsp)
leaq 0x300(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq (%r13), %r9
leaq 0x3617(%rip), %r8       # 0x9c40
pushq $0x1
popq %rdx
pushq $0x5
popq %rcx
xorl %eax, %eax
callq 0x2040
testl %eax, %eax
jne 0x663f
movq 0x38(%rsp), %rax
movq 0x8(%rsp), %r13
movq %fs:0x0, %rax
movq %rax, 0x10(%rsp)
cmpq -0x10(%rax), %r13
jg 0x666d
cmpb $0x0, 0x224(%r12)
jne 0x66fe
leaq -0x10(%rax), %rax
movq %r13, (%rax)
leaq 0x20(%rsp), %rdi
movq %r13, (%rdi)
leaq 0x38(%rsp), %rsi
callq 0x20b0
testq %rax, %rax
je 0x66df
movq %r12, %rdi
addq $0x224, %rdi            # imm = 0x224
leaq 0x35a8(%rip), %rdx      # 0x9c47
pushq $0x40
popq %rsi
movq %rax, %rcx
callq 0x2320
jmp 0x66fe
leaq 0x3583(%rip), %rsi      # 0x9c36
leaq 0x34ec(%rip), %rdx      # 0x9ba6
movl $0x800f0000, %edi       # imm = 0x800F0000
movl $0x231, %ecx            # imm = 0x231
callq 0x3138
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x2428, %rsp           # imm = 0x2428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2080
movl (%rax), %edi
leaq 0x32db(%rip), %rsi      # 0x99c8
leaq 0x32e3(%rip), %rdx      # 0x99d7
movl $0x196, %ecx            # imm = 0x196
callq 0x3231
movq 0x10(%rsp), %rax
leaq -0x20(%rax), %r13
leaq 0x20(%rsp), %rsi
movq %r13, %rdi
callq 0x6b10
ucomisd 0x3469(%rip), %xmm0     # 0x9b88
jbe 0x6792
movupd 0x20(%rsp), %xmm0
movupd %xmm0, (%r13)
callq 0x20c0
movl %eax, 0x1c(%rsp)
movq 0x10(%rsp), %rax
leaq -0x30(%rax), %r13
movq %r13, %rdi
callq 0x6bc9
testb %al, %al
jne 0x6792
movl 0x1c(%rsp), %r9d
cmpl 0x220(%r12), %r9d
jne 0x6764
movb $0x0, (%r13)
jmp 0x6792
movq $0x10, 0x38(%rsp)
movq 0x38(%rsp), %rsi
leaq 0x3445(%rip), %r8       # 0x9bbe
pushq $0x1
popq %rdx
pushq $0x10
popq %rcx
movq %r13, %rdi
xorl %eax, %eax
callq 0x2040
testl %eax, %eax
jne 0x6792
movq 0x38(%rsp), %rax
leaq 0x38(%rsp), %rdi
movl $0x290, %edx            # imm = 0x290
movq %r12, %rsi
callq 0x2280
xorl %edi, %edi
callq 0x5df7
leaq 0x140(%rsp), %rax
leaq 0x25c(%rsp), %rcx
movq %rcx, 0x2f8(%rsp)
movq %rax, 0x308(%rsp)
leaq 0x248(%rsp), %rax
movq %rax, 0x310(%rsp)
leaq 0x120(%rsp), %rax
movq %rax, 0x320(%rsp)
movl %ebx, %edi
callq 0x7e5c
testq %rax, %rax
je 0x681f
movq %rax, %r12
pushq $0x2b
popq %r13
movq %rax, %rdi
movq %r13, %rsi
callq 0x21c0
leaq 0x2c8(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %rax, %rcx
callq 0x5401
movl %ebx, %edi
callq 0x6c18
movq %rax, 0x318(%rsp)
movq 0x10(%rsp), %rax
cmpb $0x0, -0x30(%rax)
je 0x6868
leaq -0x30(%rax), %r12
pushq $0x10
popq %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x21c0
leaq 0x328(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %rax, %rcx
callq 0x5401
leaq 0x338(%rsp), %r12
pushq $0x1
popq %rdx
movl $0x1000, %esi           # imm = 0x1000
movl $0x20f0, %ecx           # imm = 0x20F0
movq %r12, %rdi
movq %r15, %r8
movq %r14, %r9
callq 0x2310
movb (%r12), %al
testb %al, %al
je 0x68f2
xorl %r14d, %r14d
cmpw %bp, 0x2b2(%rsp)
jne 0x6953
cmpb %al, 0x2b0(%rsp)
jne 0x6953
movb 0x2b1(%rsp), %cl
cmpb 0x339(%rsp), %cl
jne 0x690f
movabsq $-0x340d631b7bdddcdb, %r14 # imm = 0xCBF29CE484222325
leaq 0x339(%rsp), %rcx
movabsq $0x100000001b3, %rdx    # imm = 0x100000001B3
testb %al, %al
je 0x6914
movzbl %al, %eax
xorq %rax, %r14
imulq %rdx, %r14
movb (%rcx), %al
incq %rcx
jmp 0x68dd
leaq 0x333d(%rip), %rsi      # 0x9c36
leaq 0x32a6(%rip), %rdx      # 0x9ba6
movl $0x800f0000, %edi       # imm = 0x800F0000
movl $0x287, %ecx            # imm = 0x287
jmp 0x6a16
xorl %r14d, %r14d
jmp 0x6953
cmpq %r14, 0x2a8(%rsp)
jne 0x6953
movq 0x2b8(%rsp), %rax
movq 0x2c0(%rsp), %r9
incq %rax
movq %rax, 0x2b8(%rsp)
leaq -0x2(%r9), %rcx
cmpq %rcx, %rax
jae 0x6a22
movb 0x2a0(%rsp), %r15b
movb $0x1, %r12b
jmp 0x6976
movb $0x0, 0x2a0(%rsp)
andq $0x0, 0x2b8(%rsp)
movq $0x5, 0x2c0(%rsp)
movb $0x1, %r12b
xorl %r15d, %r15d
xorl %edi, %edi
callq 0x5c53
testq %rax, %rax
je 0x69fe
movb 0x2a0(%rsp), %cl
movb %cl, 0x268(%rax)
testb %r12b, %r12b
je 0x69bc
movw %bp, 0x27a(%rax)
movq %r14, 0x270(%rax)
movb 0x338(%rsp), %cl
movb %cl, 0x278(%rax)
movb 0x339(%rsp), %cl
movb %cl, 0x279(%rax)
movupd 0x2b8(%rsp), %xmm0
movupd %xmm0, 0x280(%rax)
xorl %ebp, %ebp
xorl %edi, %edi
callq 0x5df7
testb %r15b, %r15b
jne 0x66cb
leaq 0x38(%rsp), %rdi
leaq 0x2c8(%rsp), %rdx
movl %ebx, %esi
callq 0x6c84
andb %al, %r12b
movl %r12d, %ebp
jmp 0x66cb
leaq 0x3231(%rip), %rsi      # 0x9c36
leaq 0x319a(%rip), %rdx      # 0x9ba6
movl $0x800f0000, %edi       # imm = 0x800F0000
movl $0x2b0, %ecx            # imm = 0x2B0
callq 0x3138
movl %eax, %ebp
jmp 0x66cb
leaq (%r9,%r9), %rax
movq %rax, 0x2c0(%rsp)
movb $0x1, 0x2a0(%rsp)
leaq 0x3213(%rip), %r8       # 0x9c50
xorl %r15d, %r15d
pushq $0x1
popq %rdx
movl $0x1000, %esi           # imm = 0x1000
movl $0x1000, %ecx           # imm = 0x1000
movq %r12, %rdi
xorl %eax, %eax
callq 0x2040
xorl %r12d, %r12d
jmp 0x6976