// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x90, %rsp
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
movl $0x6e, %edx
callq 0x32e0
movw $0x1, -0x70(%rbp)
leaq 0x51fc(%rip), %rdi      # 0xa234
callq 0x3050
movq %rax, -0x78(%rbp)
leaq 0x51f4(%rip), %rdi      # 0xa23c
callq 0x3050
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
callq 0x31f0
addq $0x14, %rax
movq %rax, -0x88(%rbp)
movq -0x78(%rbp), %rdi
callq 0x31f0
movq -0x88(%rbp), %rdi
addq %rax, %rdi
movl $0x1, %esi
callq 0x3350
movq %rax, 0x8827(%rip)      # 0xd8b0
movq 0x8820(%rip), %rdi      # 0xd8b0
movq -0x80(%rbp), %rdx
movq -0x78(%rbp), %rcx
leaq 0x51a2(%rip), %rsi      # 0xa241
movb $0x0, %al
callq 0x3630
leaq -0x70(%rbp), %rdi
addq $0x2, %rdi
movq 0x87fb(%rip), %rcx      # 0xd8b0
movl $0x6c, %esi
leaq 0x530f(%rip), %rdx      # 0xa3d0
movb $0x0, %al
callq 0x3250
movl $0x1, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x36f0
movl %eax, 0x87dc(%rip)      # 0xd8b8
cmpl $0x0, 0x87d5(%rip)      # 0xd8b8
jne 0x5116
movl $0x1, %edi
leaq 0x5168(%rip), %rsi      # 0xa259
leaq 0x5172(%rip), %rdx      # 0xa26a
movl $0x21, %ecx
leaq 0x5178(%rip), %r8       # 0xa27c
movb $0x0, %al
callq 0x5bf0
movw $0x0, -0x2(%rbp)
jmp 0x520c
cmpl $0x0, 0x879f(%rip)      # 0xd8bc
jne 0x51b2
movl 0x878f(%rip), %edi      # 0xd8b8
leaq -0x70(%rbp), %rsi
movl $0x6e, %edx
callq 0x34f0
cmpl $0x0, %eax
jge 0x516d
movl $0x1, %edi
leaq 0x5111(%rip), %rsi      # 0xa259
leaq 0x511b(%rip), %rdx      # 0xa26a
movl $0x28, %ecx
leaq 0x5137(%rip), %r8       # 0xa292
movb $0x0, %al
callq 0x5bf0
movw $0x0, -0x2(%rbp)
jmp 0x520c
movl 0x8745(%rip), %edi      # 0xd8b8
movl $0x1, %esi
callq 0x3470
cmpl $0x0, %eax
jge 0x51b0
movl $0x1, %edi
leaq 0x50cb(%rip), %rsi      # 0xa259
leaq 0x50d5(%rip), %rdx      # 0xa26a
movl $0x2d, %ecx
leaq 0x5107(%rip), %r8       # 0xa2a8
movb $0x0, %al
callq 0x5bf0
movw $0x0, -0x2(%rbp)
jmp 0x520c
jmp 0x5206
cmpl $0x1, 0x8703(%rip)      # 0xd8bc
jne 0x5204
movl 0x86f7(%rip), %edi      # 0xd8b8
leaq -0x70(%rbp), %rsi
movl $0x6e, %edx
callq 0x3650
cmpl $0x0, %eax
jge 0x5202
movl $0x1, %edi
leaq 0x5079(%rip), %rsi      # 0xa259
leaq 0x5083(%rip), %rdx      # 0xa26a
movl $0x33, %ecx
leaq 0x50d0(%rip), %r8       # 0xa2c3
movb $0x0, %al
callq 0x5bf0
movw $0x0, -0x2(%rbp)
jmp 0x520c
jmp 0x5204
jmp 0x5206
movw $0x1, -0x2(%rbp)
movw -0x2(%rbp), %ax
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)