// File: /workspace/github_c_makefile_star2_O1/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
xorps %xmm0, %xmm0
leaq 0x2(%rsp), %rbx
movaps %xmm0, -0x2(%rbx)
movups %xmm0, 0x5c(%rbx)
movaps %xmm0, 0x4e(%rbx)
movaps %xmm0, 0x3e(%rbx)
movaps %xmm0, 0x2e(%rbx)
movaps %xmm0, 0x1e(%rbx)
movaps %xmm0, 0xe(%rbx)
movw $0x1, -0x2(%rbx)
leaq 0x393e(%rip), %rdi      # 0x8252
callq 0x3050
movq %rax, %r14
leaq 0x3937(%rip), %rdi      # 0x825a
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, 0x7f59(%rip)      # 0xc8b0
leaq 0x3901(%rip), %rsi      # 0x825f
movq %rax, %rdi
movq %r15, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x3620
movq 0x7f3b(%rip), %rcx      # 0xc8b0
leaq 0x3a72(%rip), %rdx      # 0x83ee
movl $0x6c, %esi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3250
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x36e0
movl %eax, 0x7f16(%rip)      # 0xc8b8
testl %eax, %eax
je 0x4a0b
movw $0x1, %bx
movl 0x7f0c(%rip), %ecx      # 0xc8bc
cmpl $0x1, %ecx
je 0x4a41
testl %ecx, %ecx
jne 0x4a33
movq %rsp, %rsi
movl %eax, %edi
movl $0x6e, %edx
callq 0x34e0
testl %eax, %eax
js 0x4a77
movl 0x7ede(%rip), %edi      # 0xc8b8
movl $0x1, %esi
callq 0x3460
testl %eax, %eax
jns 0x4a33
leaq 0x3888(%rip), %rsi      # 0x8277
leaq 0x3892(%rip), %rdx      # 0x8288
leaq 0x38c9(%rip), %r8       # 0x82c6
xorl %ebx, %ebx
movl $0x1, %edi
movl $0x2d, %ecx
jmp 0x4a2c
leaq 0x3865(%rip), %rsi      # 0x8277
leaq 0x386f(%rip), %rdx      # 0x8288
leaq 0x387a(%rip), %r8       # 0x829a
xorl %ebx, %ebx
movl $0x1, %edi
movl $0x21, %ecx
xorl %eax, %eax
callq 0x5206
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq %rsp, %rsi
movl %eax, %edi
movl $0x6e, %edx
callq 0x3640
testl %eax, %eax
jns 0x4a33
leaq 0x381c(%rip), %rsi      # 0x8277
leaq 0x3826(%rip), %rdx      # 0x8288
leaq 0x3878(%rip), %r8       # 0x82e1
xorl %ebx, %ebx
movl $0x1, %edi
movl $0x33, %ecx
jmp 0x4a2c
leaq 0x37f9(%rip), %rsi      # 0x8277
leaq 0x3803(%rip), %rdx      # 0x8288
leaq 0x3824(%rip), %r8       # 0x82b0
xorl %ebx, %ebx
movl $0x1, %edi
movl $0x28, %ecx
jmp 0x4a2c