// File: /workspace/github_cpp_makefile_star2_O2/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
subq $0x10, %rsp
cmpb $0x0, 0x3b52e(%rip)     # 0x45b98
jne 0xa6a8
movq 0x3af7d(%rip), %r15     # 0x455f0
cmpb $0x0, 0x3b698(%rip)     # 0x45d12
je 0xa6b5
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 0xa74e
movl %ebx, %edi
callq 0x28caf
movb $0x1, 0x3b4f0(%rip)     # 0x45b98
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x8220
movq %rax, %r12
leal 0x3(%rax), %r14d
movq %r14, %rdi
callq 0x8820
testq %rax, %rax
je 0xa733
movq %rax, %rbx
movw $0x1, (%rax)
movq %rax, %rdi
addq $0x2, %rdi
incq %r12
movq %r15, %rsi
movq %r12, %rdx
callq 0x84d0
pushq $0x1
popq %rdi
movl $0x80802, %esi          # imm = 0x80802
xorl %edx, %edx
callq 0x8100
cmpl $-0x1, %eax
je 0xa780
movl %eax, %ebp
movl %eax, %edi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x84f0
testl %eax, %eax
je 0xa723
callq 0x8040
cmpl $0x73, (%rax)
jne 0xa7a8
movl %ebp, %edi
callq 0x28caf
movb $0x1, 0x3b467(%rip)     # 0x45b98
jmp 0xa7af
leaq 0x22aa4(%rip), %rsi     # 0x2d1de
pushq $0x3
popq %rdi
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x28d42
callq 0x8040
movl (%rax), %edi
callq 0x81c0
movq %rax, %rbx
leaq 0x22ad6(%rip), %rsi     # 0x2d23a
pushq $0x3
popq %rdi
callq 0x28e0a
movq %rbx, %rdi
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x28f31
callq 0x8040
movl (%rax), %edi
callq 0x81c0
movq %rax, %r14
leaq 0x22a70(%rip), %rsi     # 0x2d206
pushq $0x3
popq %rdi
callq 0x28e0a
movq %r14, %rdi
callq 0x28f31
jmp 0xa7af
movl %ebp, %edi
callq 0x8b50
movq %rbx, %rdi
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x8710
cmpl $0x2, %edx
jne 0xa828
movq %rax, %rdi
callq 0x8200
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
leaq 0x8(%rsp), %rdi
movq %rax, (%rdi)
callq 0xa933
movl %ebp, %edi
callq 0x8b50
callq 0x8a40
jmp 0xa7af
cmpl $0x2, %edx
jne 0xa828
movq %rax, %rdi
callq 0x8200
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
leaq 0x8(%rsp), %rdi
movq %rax, (%rdi)
callq 0xa933
movl %ebx, %edi
callq 0x8b50
callq 0x8a40
jmp 0xa6a8
movq %rax, %rdi
callq 0xaebb