// File: /workspace/github_c_makefile_star2_O3/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 0x396b(%rip), %rdi      # 0x824e
callq 0x3050
movq %rax, %r14
leaq 0x3964(%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
movl $0x1, %esi
callq 0x3340
movq %rax, 0x7f8a(%rip)      # 0xc8b0
leaq 0x392e(%rip), %rsi      # 0x825b
movq %rax, %rdi
movq %r15, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x3620
movq 0x7f6c(%rip), %rcx      # 0xc8b0
leaq 0x3a9f(%rip), %rdx      # 0x83ea
movl $0x6c, %esi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3250
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x36e0
movl %eax, 0x7f47(%rip)      # 0xc8b8
testl %eax, %eax
je 0x49dc
movw $0x1, %bx
movl 0x7f3d(%rip), %ecx      # 0xc8bc
cmpl $0x1, %ecx
je 0x4a12
testl %ecx, %ecx
jne 0x4a04
leaq 0xa(%rsp), %rsi
movl %eax, %edi
movl $0x6e, %edx
callq 0x34e0
testl %eax, %eax
js 0x4a4a
movl 0x7f0d(%rip), %edi      # 0xc8b8
movl $0x1, %esi
callq 0x3460
testl %eax, %eax
jns 0x4a04
leaq 0x38b3(%rip), %rsi      # 0x8273
leaq 0x38bd(%rip), %rdx      # 0x8284
leaq 0x38f4(%rip), %r8       # 0x82c2
xorl %ebx, %ebx
movl $0x1, %edi
movl $0x2d, %ecx
jmp 0x49fd
leaq 0x3890(%rip), %rsi      # 0x8273
leaq 0x389a(%rip), %rdx      # 0x8284
leaq 0x38a5(%rip), %r8       # 0x8296
xorl %ebx, %ebx
movl $0x1, %edi
movl $0x21, %ecx
xorl %eax, %eax
callq 0x51d6
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq 0xa(%rsp), %rsi
movl %eax, %edi
movl $0x6e, %edx
callq 0x3640
testl %eax, %eax
jns 0x4a04
leaq 0x3845(%rip), %rsi      # 0x8273
leaq 0x384f(%rip), %rdx      # 0x8284
leaq 0x38a1(%rip), %r8       # 0x82dd
xorl %ebx, %ebx
movl $0x1, %edi
movl $0x33, %ecx
jmp 0x49fd
leaq 0x3822(%rip), %rsi      # 0x8273
leaq 0x382c(%rip), %rdx      # 0x8284
leaq 0x384d(%rip), %r8       # 0x82ac
xorl %ebx, %ebx
movl $0x1, %edi
movl $0x28, %ecx
jmp 0x49fd