// File: davmac314[P]dinit/src/includes/dinit-client.h
// Function: connect_to_daemon(char const*)
inline int connect_to_daemon(const char *control_socket_path)
{
    int socknum = socket(AF_UNIX, SOCK_STREAM, 0);
    if (socknum == -1) {
        throw general_error(errno, "opening socket");
    }

    struct sockaddr_un * name;
    uint sockaddr_size = offsetof(struct sockaddr_un, sun_path) + strlen(control_socket_path) + 1;
    name = (struct sockaddr_un *) malloc(sockaddr_size);
    if (name == nullptr) {
        throw general_error(ENOMEM);
    }

    name->sun_family = AF_UNIX;
    strcpy(name->sun_path, control_socket_path);

    int connr = connect(socknum, (struct sockaddr *) name, sockaddr_size);
    free(name);

    if (connr == -1) {
        throw control_sock_conn_err(errno, control_socket_path);
    }

    return socknum;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movl $0x1, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x3070
movl %eax, -0xc(%rbp)
cmpl $-0x1, -0xc(%rbp)
jne 0x5e30
movb $0x1, -0x3d(%rbp)
movl $0x30, %edi
callq 0x3130
movq %rax, -0x90(%rbp)
movq %rax, -0x98(%rbp)
callq 0x3040
movl (%rax), %eax
movl %eax, -0x84(%rbp)
leaq -0x30(%rbp), %rdi
movq %rdi, -0x80(%rbp)
callq 0x3420
movq -0x90(%rbp), %rdi
movl -0x84(%rbp), %esi
movq -0x80(%rbp), %rcx
leaq 0x8b3c(%rip), %rdx      # 0xe914
callq 0x69d0
jmp 0x5ddf
movq -0x98(%rbp), %rdi
movb $0x0, -0x3d(%rbp)
leaq 0xff97(%rip), %rsi      # 0x15d88
leaq 0xc28(%rip), %rdx       # 0x6a20
callq 0x34e0
jmp 0x5fd7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x3190
testb $0x1, -0x3d(%rbp)
jne 0x5e1f
jmp 0x5e2b
movq -0x98(%rbp), %rdi
callq 0x31d0
jmp 0x5fce
movq -0x8(%rbp), %rdi
callq 0x3100
addq $0x2, %rax
addq $0x1, %rax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, %edi
callq 0x33e0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x5eb3
movl $0x30, %edi
callq 0x3130
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xa0(%rbp)
movl $0xc, %esi
callq 0x6a40
jmp 0x5e7c
movq -0xa0(%rbp), %rdi
leaq 0xfefe(%rip), %rsi      # 0x15d88
leaq 0xb8f(%rip), %rdx       # 0x6a20
callq 0x34e0
movq -0xa0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
callq 0x31d0
jmp 0x5fce
movq -0x48(%rbp), %rax
movw $0x1, (%rax)
movq -0x48(%rbp), %rdi
addq $0x2, %rdi
movq -0x8(%rbp), %rsi
callq 0x3280
movl -0xc(%rbp), %edi
movq -0x48(%rbp), %rsi
movl -0x4c(%rbp), %edx
callq 0x3220
movl %eax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
callq 0x3350
cmpl $-0x1, -0x50(%rbp)
jne 0x5fc2
movb $0x1, -0x72(%rbp)
movl $0x30, %edi
callq 0x3130
movq %rax, -0xc0(%rbp)
callq 0x3040
movl (%rax), %eax
movl %eax, -0xb4(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xb0(%rbp)
leaq -0x71(%rbp), %rdi
movq %rdi, -0xa8(%rbp)
callq 0x3510
movq -0xb0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
leaq -0x70(%rbp), %rdi
callq 0x6a70
jmp 0x5f48
movl -0xb4(%rbp), %esi
movq -0xc0(%rbp), %rdi
leaq -0x70(%rbp), %rdx
callq 0x6b20
jmp 0x5f60
movq -0xc0(%rbp), %rdi
movb $0x0, -0x72(%rbp)
leaq 0xfe26(%rip), %rsi      # 0x15d98
leaq 0xc07(%rip), %rdx       # 0x6b80
callq 0x34e0
jmp 0x5fd7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x5fa3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x3190
leaq -0x71(%rbp), %rdi
callq 0x32f0
testb $0x1, -0x72(%rbp)
jne 0x5fb4
jmp 0x5fc0
movq -0xc0(%rbp), %rdi
callq 0x31d0
jmp 0x5fce
movl -0xc(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x3500
nopw (%rax,%rax)