// File: /workspace/github_cpp_makefile_star2_O1/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, 0x3bbb0(%rip)     # 0x46b98
jne 0xb026
movq 0x3b5ff(%rip), %r15     # 0x465f0
cmpb $0x0, 0x3bd1a(%rip)     # 0x46d12
je 0xb02f
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 0xb0c8
movl %ebx, %edi
callq 0x2e485
movb $0x1, 0x3bb72(%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 0xb0af
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 0xb0f8
movl %eax, %ebp
movl %eax, %edi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x84f0
testl %eax, %eax
je 0xb09f
callq 0x8050
cmpl $0x73, (%rax)
jne 0xb122
movl %ebp, %edi
callq 0x2e485
movb $0x1, 0x3baeb(%rip)     # 0x46b98
jmp 0xb129
leaq 0x28144(%rip), %rsi     # 0x331fa
movl $0x3, %edi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x2e562
callq 0x8050
movl (%rax), %edi
callq 0x81d0
movq %rax, %rbx
leaq 0x28178(%rip), %rsi     # 0x33256
movl $0x3, %edi
callq 0x2e63a
movq %rbx, %rdi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x2e759
callq 0x8050
movl (%rax), %edi
callq 0x81d0
movq %rax, %r14
leaq 0x28114(%rip), %rsi     # 0x33222
movl $0x3, %edi
callq 0x2e63a
movq %r14, %rdi
callq 0x2e759
jmp 0xb129
movl %ebp, %edi
callq 0x8b60
movq %rbx, %rdi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x8720
cmpl $0x2, %edx
jne 0xb1bc
movq %rax, %rdi
callq 0x8210
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
movq %rax, %r14
leaq 0x280e5(%rip), %rsi     # 0x3323e
movl $0x3, %edi
callq 0x2e63a
movq %r14, %rdi
callq 0x2e759
movl %ebp, %edi
callq 0x8b60
callq 0x8a50
jmp 0xb129
cmpl $0x2, %edx
jne 0xb1bc
movq %rax, %rdi
callq 0x8210
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
movq %rax, %r14
leaq 0x280a5(%rip), %rsi     # 0x3323e
movl $0x3, %edi
callq 0x2e63a
movq %r14, %rdi
callq 0x2e759
movl %ebx, %edi
callq 0x8b60
callq 0x8a50
jmp 0xb026
movq %rax, %rdi
callq 0xb8be