// File: /workspace/github_cpp_makefile_star2_O3/davmac314[P]dinit/src/dinit.cc
// Function: setup_external_log()
void setup_external_log() noexcept
{
    if (!external_log_open) {
        if (log_is_syslog) {
            const char * saddrname = log_path;
            size_t saddrname_len = strlen(saddrname);
            uint sockaddr_size = offsetof(struct sockaddr_un, sun_path) + saddrname_len + 1;

            struct sockaddr_un * name = static_cast<sockaddr_un *>(malloc(sockaddr_size));
            if (name == nullptr) {
                log(loglevel_t::ERROR, "Connecting to log socket: out of memory");
                return;
            }

            name->sun_family = AF_UNIX;
            memcpy(name->sun_path, saddrname, saddrname_len + 1);

            int sockfd = dinit_socket(AF_UNIX, SOCK_DGRAM, 0, SOCK_NONBLOCK | SOCK_CLOEXEC);
            if (sockfd == -1) {
                log(loglevel_t::ERROR, "Error creating log socket: ", strerror(errno));
                free(name);
                return;
            }

            if (connect(sockfd, (struct sockaddr *) name, sockaddr_size) == 0 || errno == EINPROGRESS) {
                // For EINPROGRESS, connection is still being established; however, we can select on
                // the file descriptor so we will be notified when it's ready. In other words we can
                // basically use it anyway.
                try {
                    setup_main_log(sockfd);
                    external_log_open = true;
                }
                catch (std::exception &e) {
                    log(loglevel_t::ERROR, "Setting up log failed: ", e.what());
                    close(sockfd);
                }
            }
            else {
                // Note if connect fails, we haven't warned at all, because the syslog server might not
                // have started yet.
                close(sockfd);
            }

            free(name);
        }
        else {
            // log to file:
            int log_fd = open(log_path, O_WRONLY | O_CREAT | O_APPEND | O_NONBLOCK | O_CLOEXEC, 0644);
            if (log_fd >= 0) {
                try {
                    setup_main_log(log_fd);
                    external_log_open = true;
                }
                catch (std::exception &e) {
                    log(loglevel_t::ERROR, "Setting up log failed: ", e.what());
                    close(log_fd);
                }
            }
            else {
                // log failure to log? It makes more sense than first appears, because we also log
                // to console:
                log(loglevel_t::ERROR, "Opening log file failed: ", strerror(errno));
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
cmpb $0x0, 0x3bd27(%rip)     # 0x46b98
jne 0xaeaf
movq 0x3b776(%rip), %r15     # 0x465f0
cmpb $0x0, 0x3be91(%rip)     # 0x46d12
je 0xaeb8
movq %r15, %rdi
movl $0x80c41, %esi          # imm = 0x80C41
movl $0x1a4, %edx            # imm = 0x1A4
xorl %eax, %eax
callq 0x8450
movl %eax, %ebx
testl %eax, %eax
js 0xaf51
movl %ebx, %edi
callq 0x2e25d
movb $0x1, 0x3bce9(%rip)     # 0x46b98
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x8230
movq %rax, %r12
leal 0x3(%rax), %r14d
movq %r14, %rdi
callq 0x8820
testq %rax, %rax
je 0xaf38
movq %rax, %rbx
movw $0x1, (%rax)
movq %rax, %rdi
addq $0x2, %rdi
incq %r12
movq %r15, %rsi
movq %r12, %rdx
callq 0x84d0
movl $0x1, %edi
movl $0x80802, %esi          # imm = 0x80802
xorl %edx, %edx
callq 0x8110
cmpl $-0x1, %eax
je 0xaf81
movl %eax, %ebp
movl %eax, %edi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x84f0
testl %eax, %eax
je 0xaf28
callq 0x8050
cmpl $0x73, (%rax)
jne 0xafab
movl %ebp, %edi
callq 0x2e25d
movb $0x1, 0x3bc62(%rip)     # 0x46b98
jmp 0xafb2
leaq 0x2829f(%rip), %rsi     # 0x331de
movl $0x3, %edi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x2e33a
callq 0x8050
movl (%rax), %edi
callq 0x81d0
movq %rax, %rbx
leaq 0x282d3(%rip), %rsi     # 0x3323a
movl $0x3, %edi
callq 0x2e412
movq %rbx, %rdi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x2e53c
callq 0x8050
movl (%rax), %edi
callq 0x81d0
movq %rax, %r14
leaq 0x2826f(%rip), %rsi     # 0x33206
movl $0x3, %edi
callq 0x2e412
movq %r14, %rdi
callq 0x2e53c
jmp 0xafb2
movl %ebp, %edi
callq 0x8b60
movq %rbx, %rdi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x8720
cmpl $0x2, %edx
jne 0xb045
movq %rax, %rdi
callq 0x8210
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
movq %rax, %r14
leaq 0x28240(%rip), %rsi     # 0x33222
movl $0x3, %edi
callq 0x2e412
movq %r14, %rdi
callq 0x2e53c
movl %ebp, %edi
callq 0x8b60
callq 0x8a50
jmp 0xafb2
cmpl $0x2, %edx
jne 0xb045
movq %rax, %rdi
callq 0x8210
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
movq %rax, %r14
leaq 0x28200(%rip), %rsi     # 0x33222
movl $0x3, %edi
callq 0x2e412
movq %r14, %rdi
callq 0x2e53c
movl %ebx, %edi
callq 0x8b60
callq 0x8a50
jmp 0xaeaf
movq %rax, %rdi
callq 0xb74e