// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x1
popq %r14
movl $0x200, %edi            # imm = 0x200
movq %r14, %rsi
callq 0x3340
movq %rax, 0x7acd(%rip)      # 0xc8b8
movslq %ebp, %rbp
pushq $0x28
popq %r15
cmpq %rbp, %r14
jge 0x4eb0
movq (%rbx,%r14,8), %r12
pushq $0x2
popq %rdi
leaq 0x35c2(%rip), %rsi      # 0x83cb
leaq 0x35cc(%rip), %rdx      # 0x83dc
movl %r15d, %ecx
leaq 0x35ca(%rip), %r8       # 0x83e4
movq %r12, %r9
xorl %eax, %eax
callq 0x50dd
movq 0x8(%rbx,%r14,8), %r13
testq %r13, %r13
je 0x4e84
cmpb $0x0, (%r13)
je 0x4e84
movq %r12, %rdi
leaq 0x35d5(%rip), %rsi      # 0x8414
callq 0x3370
testl %eax, %eax
je 0x4e6c
movq %r12, %rdi
leaq 0x35c7(%rip), %rsi      # 0x8419
callq 0x3370
testl %eax, %eax
jne 0x4e7b
movq %r13, %rdi
callq 0x46ec
movw %ax, 0x7516(%rip)       # 0xc380
jmp 0x4e7b
movq 0x7a45(%rip), %rdi      # 0xc8b8
movq %r13, %rsi
callq 0x30e0
addq $0x2, %r14
jmp 0x4df2
leaq 0x3540(%rip), %rsi      # 0x83cb
leaq 0x354a(%rip), %rdx      # 0x83dc
leaq 0x3554(%rip), %r8       # 0x83ed
pushq $0x1
popq %rbx
pushq $0x2b
popq %rcx
movl %ebx, %edi
movq %r12, %r9
xorl %eax, %eax
callq 0x50dd
jmp 0x50cc
leaq 0x3514(%rip), %rsi      # 0x83cb
leaq 0x351e(%rip), %rdx      # 0x83dc
leaq 0x355f(%rip), %r8       # 0x8424
pushq $0x3
popq %rdi
pushq $0x3a
popq %rcx
xorl %eax, %eax
callq 0x50dd
callq 0x51a0
testw %ax, %ax
je 0x4f21
movq 0x79d5(%rip), %rbx      # 0xc8b8
cmpb $0x0, (%rbx)
jne 0x4fa3
leaq 0x3581(%rip), %rdi      # 0x8474
callq 0x3050
movq %rax, %r14
leaq 0x357d(%rip), %rdi      # 0x847f
callq 0x3050
testq %r14, %r14
je 0x4f4f
cmpb $0x0, (%r14)
je 0x4f4f
leaq 0x356b(%rip), %rsi      # 0x8484
movq %rbx, %rdi
movq %r14, %rdx
jmp 0x4f66
leaq 0x34a3(%rip), %rsi      # 0x83cb
leaq 0x34ad(%rip), %rdx      # 0x83dc
leaq 0x3516(%rip), %r8       # 0x844c
pushq $0x1
popq %rbx
pushq $0x3c
popq %rcx
movl %ebx, %edi
xorl %eax, %eax
callq 0x50dd
callq 0x5294
jmp 0x50cc
testq %rax, %rax
je 0x4f6f
cmpb $0x0, (%rax)
je 0x4f6f
leaq 0x3532(%rip), %rsi      # 0x8492
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x3610
jmp 0x4fa3
leaq 0x355d(%rip), %rax      # 0x84d3
movq %rax, (%rsp)
leaq 0x344a(%rip), %rsi      # 0x83cb
leaq 0x3454(%rip), %rdx      # 0x83dc
leaq 0x3519(%rip), %r8       # 0x84a8
leaq 0x3518(%rip), %r9       # 0x84ae
pushq $0x3
popq %rdi
pushq $0x4d
popq %rcx
xorl %eax, %eax
callq 0x50dd
movq 0x790e(%rip), %rdi      # 0xc8b8
cmpb $0x0, (%rdi)
je 0x5000
pushq $0x5
popq %rsi
callq 0x3540
testl %eax, %eax
js 0x5000
movq 0x78f6(%rip), %r9       # 0xc8b8
leaq 0x3402(%rip), %rsi      # 0x83cb
leaq 0x340c(%rip), %rdx      # 0x83dc
leaq 0x351f(%rip), %r8       # 0x84f6
pushq $0x3
popq %rdi
pushq $0x51
popq %rcx
xorl %eax, %eax
callq 0x50dd
callq 0x36b0
testl %eax, %eax
jne 0x5000
movq 0x78c4(%rip), %rdi      # 0xc8b8
movq %rdi, %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x3690
movw $0x1, 0x78b7(%rip)      # 0xc8c0
callq 0x54dc
leaq 0x78ef(%rip), %rax      # 0xc904
movl (%rax), %eax
leaq 0x10(%rsp), %rbx
movl %eax, (%rbx)
pushq $0x1
popq %rax
movl %eax, 0x4(%rbx)
leaq 0x787d(%rip), %rcx      # 0xc8a8
movl (%rcx), %ecx
movl %ecx, 0x8(%rbx)
movl %eax, 0xc(%rbx)
leaq 0x3391(%rip), %rsi      # 0x83cb
leaq 0x339b(%rip), %rdx      # 0x83dc
leaq 0x34cf(%rip), %r8       # 0x8517
pushq $0x3
popq %rdi
pushq $0x61
popq %rcx
xorl %eax, %eax
callq 0x50dd
pushq $0x2
popq %r14
pushq $-0x1
popq %rbp
leaq 0x7885(%rip), %r15      # 0xc8e8
leaq 0x7326(%rip), %r12      # 0xc390
cmpw $0x0, 0x784e(%rip)      # 0xc8c0
je 0x50c5
movq %rbx, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x34c0
testl %eax, %eax
je 0x506a
testb $0x1, 0x16(%rsp)
jne 0x50a2
testb $0x1, 0x1e(%rsp)
je 0x506a
callq 0x4a37
movq %rax, %rdi
callq 0x3a00
jmp 0x506a
movq (%r15), %rdi
callq 0x3460
testq %rax, %rax
je 0x508c
movzbl (%rax), %ecx
andl $0x7f, %ecx
movq (%r12,%rcx,8), %rcx
testq %rcx, %rcx
je 0x50a2
movq %rax, %rdi
callq *%rcx
jmp 0x50a2
callq 0x5294
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq