// File: /workspace/github_c_makefile_star2_O2/comfies[P]custard/src/ipc/socket.c
// Function: initialize_socket
unsigned short initialize_socket() {
    struct sockaddr_un address;

    memset(&address, 0, sizeof(address));
    address.sun_family = AF_UNIX;

    char *display = getenv("DISPLAY");
    char *user = getenv("USER");

    socket_path = (char*)calloc((20 + strlen(user) + strlen(display)),
        sizeof(char));
    sprintf(socket_path, "/tmp/custard.%s_%s.sock", user, display);
    snprintf(address.sun_path, sizeof(address.sun_path), "%s", socket_path);

    socket_file_descriptor = socket(AF_UNIX, SOCK_STREAM, 0);

    if (!socket_file_descriptor) {
        log_fatal("Unable to open socket");
        return 0;
    }

    if (socket_mode == WINDOW_MANAGER) {
        if (bind(socket_file_descriptor, (struct sockaddr *)&address,
            sizeof(address)) < 0) {
            log_fatal("Unable to bind socket");
            return 0;
        }

        if (listen(socket_file_descriptor, 1) < 0) {
            log_fatal("Unable to listen to socket");
            return 0;
        }
    } else if (socket_mode == CONTROLLER) {
        if (connect(socket_file_descriptor, (struct sockaddr*)&address,
            sizeof(address)) < 0) {
            log_fatal("Unable to connect to socket");
            return 0;
        }
    }

    return 1;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x78, %rsp
leaq 0xc(%rsp), %rbx
xorps %xmm0, %xmm0
movups %xmm0, 0x5c(%rbx)
movups %xmm0, 0x50(%rbx)
movups %xmm0, 0x40(%rbx)
movups %xmm0, 0x30(%rbx)
movups %xmm0, 0x20(%rbx)
movups %xmm0, 0x10(%rbx)
movups %xmm0, (%rbx)
movw $0x1, -0x2(%rbx)
leaq 0x39f7(%rip), %rdi      # 0x824e
callq 0x3050
movq %rax, %r14
leaq 0x39f0(%rip), %rdi      # 0x8256
callq 0x3050
movq %rax, %r15
movq %rax, %rdi
callq 0x31f0
movq %rax, %r12
movq %r14, %rdi
callq 0x31f0
leaq (%r12,%rax), %rdi
addq $0x14, %rdi
pushq $0x1
popq %rsi
callq 0x3340
movq %rax, 0x8008(%rip)      # 0xc8a0
leaq 0x39bc(%rip), %rsi      # 0x825b
movq %rax, %rdi
movq %r15, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x3610
movq 0x7fea(%rip), %rcx      # 0xc8a0
leaq 0x3b2d(%rip), %rdx      # 0x83ea
pushq $0x6c
popq %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3250
pushq $0x1
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x36d0
movl %eax, 0x7fcc(%rip)      # 0xc8a8
testl %eax, %eax
je 0x493a
movw $0x1, %bx
movl 0x7fc2(%rip), %ecx      # 0xc8ac
cmpl $0x1, %ecx
je 0x496c
testl %ecx, %ecx
jne 0x495e
leaq 0xa(%rsp), %rsi
pushq $0x6e
popq %rdx
movl %eax, %edi
callq 0x34d0
testl %eax, %eax
js 0x499d
movl 0x7f98(%rip), %edi      # 0xc8a8
pushq $0x1
popq %rsi
callq 0x3450
testl %eax, %eax
jns 0x495e
leaq 0x3950(%rip), %rsi      # 0x8273
leaq 0x395a(%rip), %rdx      # 0x8284
leaq 0x3991(%rip), %r8       # 0x82c2
xorl %ebx, %ebx
pushq $0x1
popq %rdi
pushq $0x2d
jmp 0x4956
leaq 0x3932(%rip), %rsi      # 0x8273
leaq 0x393c(%rip), %rdx      # 0x8284
leaq 0x3947(%rip), %r8       # 0x8296
xorl %ebx, %ebx
pushq $0x1
popq %rdi
pushq $0x21
popq %rcx
xorl %eax, %eax
callq 0x50dd
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq 0xa(%rsp), %rsi
pushq $0x6e
popq %rdx
movl %eax, %edi
callq 0x3630
testl %eax, %eax
jns 0x495e
leaq 0x38ed(%rip), %rsi      # 0x8273
leaq 0x38f7(%rip), %rdx      # 0x8284
leaq 0x3949(%rip), %r8       # 0x82dd
xorl %ebx, %ebx
pushq $0x1
popq %rdi
pushq $0x33
jmp 0x4956
leaq 0x38cf(%rip), %rsi      # 0x8273
leaq 0x38d9(%rip), %rdx      # 0x8284
leaq 0x38fa(%rip), %r8       # 0x82ac
xorl %ebx, %ebx
pushq $0x1
popq %rdi
pushq $0x28
jmp 0x4956