// File: /workspace/github_c_makefile_star2_O1/comfies[P]custard/src/wm/custard.c
// Function: custard
int custard(int argc, char **argv) {
    rc_path = (char *)calloc(512, sizeof(char));

    /* Process arguments */

    char *argument;
    int index = 1;

    while (index < argc) {
        argument = argv[index++];
        log_message("arg = %s", argument);

        if (!argv[index] || !strlen(argv[index])) {
            log_fatal("No argument provided with %s, exiting.", argument);
            return EXIT_FAILURE;
        }

        if (!strcmp(argument, "--rc")) {
            strcpy(rc_path, argv[index]);
        } else if (!strcmp(argument, "--loglevel")) {
            loglevel = (short)string_to_integer(argv[index]);
        }

        index++;
    }

    /* initialize window manager */

    log_debug("Initializing window manager connections");
    if (!initialize()) {
        log_fatal("Initialization of window manager failed");
        finalize();
        return EXIT_FAILURE;
    }


    if (!strlen(rc_path)) {
        char *xdg_config_home = getenv("XDG_CONFIG_HOME");
        char *home_directory = getenv("HOME");

        if (xdg_config_home && strlen(xdg_config_home))
            sprintf(rc_path, "%s/custard/rc", xdg_config_home);
        else if (home_directory && strlen(home_directory))
            sprintf(rc_path, "%s/.config/custard/rc", home_directory);
        else
            log_debug("%s %s",
                "Unable to determine default rc path.",
                "Are $HOME or $XDG_CONFIG_HOME set?");
    }

    if (strlen(rc_path) && access(rc_path, X_OK | F_OK | R_OK) > -1) {
        log_debug("Attempting to execute file at %s", rc_path);
        if (fork() == 0)
            execl(rc_path, rc_path, NULL);
    }

    custard_is_running = 1;
    manage_pre_existing_windows();

    xcb_generic_event_t *xcb_event;
    unsigned int xcb_event_type;

    struct pollfd descriptors[2] = {
        { xcb_file_descriptor,    .events = POLLIN },
        { socket_file_descriptor, .events = POLLIN }
    };

    log_debug("Starting event loop");
    while (custard_is_running) {

        if (poll(descriptors, 2, -1)) {
            if (descriptors[0].revents & POLLIN) {
                while ((xcb_event = xcb_poll_for_event(xcb_connection))) {
                    xcb_event_type = xcb_event->response_type & ~0x80;

                    if (xcb_events[xcb_event_type])
                        xcb_events[xcb_event_type](xcb_event);

                }
            }

            if (descriptors[1].revents & POLLIN)
                ipc_process_input(read_from_socket());
        }
    }

    finalize();

    return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rbx
movl %edi, %ebp
movl $0x1, %r13d
movl $0x200, %edi            # imm = 0x200
movl $0x1, %esi
callq 0x3340
movq %rax, 0x79e9(%rip)      # 0xc8c8
cmpl $0x2, %ebp
jl 0x4f83
leaq 0x34f9(%rip), %r12      # 0x83e8
movq (%rbx,%r13,8), %r14
movl $0x2, %edi
leaq 0x34d0(%rip), %rsi      # 0x83cf
leaq 0x34da(%rip), %rdx      # 0x83e0
movl $0x28, %ecx
movq %r12, %r8
movq %r14, %r9
xorl %eax, %eax
callq 0x5206
movq 0x8(%rbx,%r13,8), %r15
testq %r15, %r15
je 0x4ff8
cmpb $0x0, (%r15)
je 0x4ff8
movq %r14, %rdi
leaq 0x34de(%rip), %rsi      # 0x8418
callq 0x3370
testl %eax, %eax
je 0x4f67
movq %r14, %rdi
leaq 0x34d0(%rip), %rsi      # 0x841d
callq 0x3370
testl %eax, %eax
jne 0x4f76
movq %r15, %rdi
callq 0x479c
movw %ax, 0x742b(%rip)       # 0xc390
jmp 0x4f76
movq 0x795a(%rip), %rdi      # 0xc8c8
movq %r15, %rsi
callq 0x30e0
addq $0x2, %r13
cmpl %ebp, %r13d
jl 0x4eef
leaq 0x3445(%rip), %rsi      # 0x83cf
leaq 0x344f(%rip), %rdx      # 0x83e0
leaq 0x3490(%rip), %r8       # 0x8428
movl $0x3, %edi
movl $0x3a, %ecx
xorl %eax, %eax
callq 0x5206
callq 0x52c9
testw %ax, %ax
je 0x502b
movq 0x790e(%rip), %rbx      # 0xc8c8
cmpb $0x0, (%rbx)
jne 0x50b3
leaq 0x34ae(%rip), %rdi      # 0x8478
callq 0x3050
movq %rax, %r14
leaq 0x34aa(%rip), %rdi      # 0x8483
callq 0x3050
testq %r14, %r14
je 0x505b
cmpb $0x0, (%r14)
je 0x505b
leaq 0x3498(%rip), %rsi      # 0x8488
movq %rbx, %rdi
movq %r14, %rdx
jmp 0x5072
leaq 0x33d0(%rip), %rsi      # 0x83cf
leaq 0x33da(%rip), %rdx      # 0x83e0
leaq 0x33e4(%rip), %r8       # 0x83f1
movl $0x1, %ebx
movl $0x1, %edi
movl $0x2b, %ecx
movq %r14, %r9
xorl %eax, %eax
callq 0x5206
jmp 0x51f5
leaq 0x339d(%rip), %rsi      # 0x83cf
leaq 0x33a7(%rip), %rdx      # 0x83e0
leaq 0x3410(%rip), %r8       # 0x8450
movl $0x1, %ebx
movl $0x1, %edi
movl $0x3c, %ecx
xorl %eax, %eax
callq 0x5206
jmp 0x51f0
testq %rax, %rax
je 0x507b
cmpb $0x0, (%rax)
je 0x507b
leaq 0x342a(%rip), %rsi      # 0x8496
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x3620
jmp 0x50b3
leaq 0x3455(%rip), %rax      # 0x84d7
movq %rax, (%rsp)
leaq 0x3342(%rip), %rsi      # 0x83cf
leaq 0x334c(%rip), %rdx      # 0x83e0
leaq 0x3411(%rip), %r8       # 0x84ac
leaq 0x3410(%rip), %r9       # 0x84b2
movl $0x3, %edi
movl $0x4d, %ecx
xorl %eax, %eax
callq 0x5206
movq 0x780e(%rip), %rdi      # 0xc8c8
cmpb $0x0, (%rdi)
je 0x5116
movl $0x5, %esi
callq 0x3550
testl %eax, %eax
js 0x5116
movq 0x77f4(%rip), %r9       # 0xc8c8
leaq 0x32f4(%rip), %rsi      # 0x83cf
leaq 0x32fe(%rip), %rdx      # 0x83e0
leaq 0x3411(%rip), %r8       # 0x84fa
movl $0x3, %edi
movl $0x51, %ecx
xorl %eax, %eax
callq 0x5206
callq 0x36c0
testl %eax, %eax
jne 0x5116
movq 0x77be(%rip), %rdi      # 0xc8c8
movq %rdi, %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x36a0
movw $0x1, 0x77b1(%rip)      # 0xc8d0
callq 0x5662
leaq 0x77e9(%rip), %rax      # 0xc914
movl (%rax), %eax
movl %eax, 0x10(%rsp)
movl $0x1, %eax
movl %eax, 0x14(%rsp)
leaq 0x7777(%rip), %rcx      # 0xc8b8
movl (%rcx), %ecx
movl %ecx, 0x18(%rsp)
movl %eax, 0x1c(%rsp)
leaq 0x327d(%rip), %rsi      # 0x83cf
leaq 0x3287(%rip), %rdx      # 0x83e0
leaq 0x33bb(%rip), %r8       # 0x851b
xorl %ebx, %ebx
movl $0x3, %edi
movl $0x61, %ecx
xorl %eax, %eax
callq 0x5206
cmpw $0x0, 0x7755(%rip)      # 0xc8d0
je 0x51f0
leaq 0x10(%rsp), %r14
leaq 0x776f(%rip), %r15      # 0xc8f8
leaq 0x7210(%rip), %r12      # 0xc3a0
movl $0x2, %esi
movq %r14, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x34d0
testl %eax, %eax
je 0x51c1
testb $0x1, 0x16(%rsp)
jne 0x51cd
testb $0x1, 0x1e(%rsp)
je 0x51c1
callq 0x4b1a
movq %rax, %rdi
callq 0x3a30
cmpw $0x0, 0x7707(%rip)      # 0xc8d0
jne 0x5190
jmp 0x51f0
movq (%r15), %rdi
callq 0x3470
testq %rax, %rax
je 0x51ad
movzbl (%rax), %ecx
andl $0x7f, %ecx
movq (%r12,%rcx,8), %rcx
testq %rcx, %rcx
je 0x51cd
movq %rax, %rdi
callq *%rcx
jmp 0x51cd
callq 0x53c0
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq