// File: /workspace/github_c_makefile_star2_O3/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, 0x7a19(%rip)      # 0xc8c8
cmpl $0x2, %ebp
jl 0x4f53
leaq 0x3525(%rip), %r12      # 0x83e4
movq (%rbx,%r13,8), %r14
movl $0x2, %edi
leaq 0x34fc(%rip), %rsi      # 0x83cb
leaq 0x3506(%rip), %rdx      # 0x83dc
movl $0x28, %ecx
movq %r12, %r8
movq %r14, %r9
xorl %eax, %eax
callq 0x51d6
movq 0x8(%rbx,%r13,8), %r15
testq %r15, %r15
je 0x4fc8
cmpb $0x0, (%r15)
je 0x4fc8
movq %r14, %rdi
leaq 0x350a(%rip), %rsi      # 0x8414
callq 0x3370
testl %eax, %eax
je 0x4f37
movq %r14, %rdi
leaq 0x34fc(%rip), %rsi      # 0x8419
callq 0x3370
testl %eax, %eax
jne 0x4f46
movq %r15, %rdi
callq 0x4770
movw %ax, 0x745b(%rip)       # 0xc390
jmp 0x4f46
movq 0x798a(%rip), %rdi      # 0xc8c8
movq %r15, %rsi
callq 0x30e0
addq $0x2, %r13
cmpl %ebp, %r13d
jl 0x4ebf
leaq 0x3471(%rip), %rsi      # 0x83cb
leaq 0x347b(%rip), %rdx      # 0x83dc
leaq 0x34bc(%rip), %r8       # 0x8424
movl $0x3, %edi
movl $0x3a, %ecx
xorl %eax, %eax
callq 0x51d6
callq 0x5299
testw %ax, %ax
je 0x4ffb
movq 0x793e(%rip), %rbx      # 0xc8c8
cmpb $0x0, (%rbx)
jne 0x5083
leaq 0x34da(%rip), %rdi      # 0x8474
callq 0x3050
movq %rax, %r14
leaq 0x34d6(%rip), %rdi      # 0x847f
callq 0x3050
testq %r14, %r14
je 0x502b
cmpb $0x0, (%r14)
je 0x502b
leaq 0x34c4(%rip), %rsi      # 0x8484
movq %rbx, %rdi
movq %r14, %rdx
jmp 0x5042
leaq 0x33fc(%rip), %rsi      # 0x83cb
leaq 0x3406(%rip), %rdx      # 0x83dc
leaq 0x3410(%rip), %r8       # 0x83ed
movl $0x1, %ebx
movl $0x1, %edi
movl $0x2b, %ecx
movq %r14, %r9
xorl %eax, %eax
callq 0x51d6
jmp 0x51c5
leaq 0x33c9(%rip), %rsi      # 0x83cb
leaq 0x33d3(%rip), %rdx      # 0x83dc
leaq 0x343c(%rip), %r8       # 0x844c
movl $0x1, %ebx
movl $0x1, %edi
movl $0x3c, %ecx
xorl %eax, %eax
callq 0x51d6
jmp 0x51c0
testq %rax, %rax
je 0x504b
cmpb $0x0, (%rax)
je 0x504b
leaq 0x3456(%rip), %rsi      # 0x8492
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x3620
jmp 0x5083
leaq 0x3481(%rip), %rax      # 0x84d3
movq %rax, (%rsp)
leaq 0x336e(%rip), %rsi      # 0x83cb
leaq 0x3378(%rip), %rdx      # 0x83dc
leaq 0x343d(%rip), %r8       # 0x84a8
leaq 0x343c(%rip), %r9       # 0x84ae
movl $0x3, %edi
movl $0x4d, %ecx
xorl %eax, %eax
callq 0x51d6
movq 0x783e(%rip), %rdi      # 0xc8c8
cmpb $0x0, (%rdi)
je 0x50e6
movl $0x5, %esi
callq 0x3550
testl %eax, %eax
js 0x50e6
movq 0x7824(%rip), %r9       # 0xc8c8
leaq 0x3320(%rip), %rsi      # 0x83cb
leaq 0x332a(%rip), %rdx      # 0x83dc
leaq 0x343d(%rip), %r8       # 0x84f6
movl $0x3, %edi
movl $0x51, %ecx
xorl %eax, %eax
callq 0x51d6
callq 0x36c0
testl %eax, %eax
jne 0x50e6
movq 0x77ee(%rip), %rdi      # 0xc8c8
movq %rdi, %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x36a0
movw $0x1, 0x77e1(%rip)      # 0xc8d0
callq 0x562c
leaq 0x7819(%rip), %rax      # 0xc914
movl (%rax), %eax
movl %eax, 0x10(%rsp)
movl $0x1, %eax
movl %eax, 0x14(%rsp)
leaq 0x77a7(%rip), %rcx      # 0xc8b8
movl (%rcx), %ecx
movl %ecx, 0x18(%rsp)
movl %eax, 0x1c(%rsp)
leaq 0x32a9(%rip), %rsi      # 0x83cb
leaq 0x32b3(%rip), %rdx      # 0x83dc
leaq 0x33e7(%rip), %r8       # 0x8517
xorl %ebx, %ebx
movl $0x3, %edi
movl $0x61, %ecx
xorl %eax, %eax
callq 0x51d6
cmpw $0x0, 0x7785(%rip)      # 0xc8d0
je 0x51c0
leaq 0x10(%rsp), %r14
leaq 0x779f(%rip), %r15      # 0xc8f8
leaq 0x7240(%rip), %r12      # 0xc3a0
movl $0x2, %esi
movq %r14, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x34d0
testl %eax, %eax
je 0x5191
testb $0x1, 0x16(%rsp)
jne 0x519d
testb $0x1, 0x1e(%rsp)
je 0x5191
callq 0x4aed
movq %rax, %rdi
callq 0x3a20
cmpw $0x0, 0x7737(%rip)      # 0xc8d0
jne 0x5160
jmp 0x51c0
movq (%r15), %rdi
callq 0x3470
testq %rax, %rax
je 0x517d
movzbl (%rax), %ecx
andl $0x7f, %ecx
movq (%r12,%rcx,8), %rcx
testq %rcx, %rcx
je 0x519d
movq %rax, %rdi
callq *%rcx
jmp 0x519d
callq 0x5390
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq