// File: /workspace/github_c_makefile_star2_O2/kaugm[P]mmwm/mmwm.c
// Function: maprequest
void maprequest(xcb_generic_event_t *e)
{
    xcb_map_request_event_t            *ev = (xcb_map_request_event_t *)e;
    xcb_window_t                       transient = 0;
    xcb_get_property_reply_t           *prop_reply;
    xcb_get_property_cookie_t          cookie;
    xcb_ewmh_get_utf8_strings_reply_t  wtitle;
    xcb_atom_t                         wtype = ewmh->_NET_WM_WINDOW_TYPE_NORMAL;
    client *c;
    bool isFloating = False;

    DEBUG("xcb: map request");

    if ((c = wintoclient(ev->window))) {
        if (!find_client(c->win)) {     /* client is on different display */
            rem_node(&c->link);
            add_tail(&current_display->clients, &c->link);
        }
        xcb_map_window(dis, c->win);
        update_current(c);
        return;
    }

    if (check_if_window_is_alien(ev->window, &isFloating, &wtype))
        return;

    xcb_remove_property(dis, ev->window, ewmh->_NET_WM_STATE, ewmh->_NET_WM_STATE_HIDDEN);

    DEBUG("event is valid");


    int cd = current_desktop_number, border_width = -1, n = 0;
    client *t = NULL;

    cookie = xcb_ewmh_get_wm_name_unchecked(ewmh, ev->window);

    if (xcb_ewmh_get_wm_name_reply(ewmh, cookie, &wtitle, (void *)0)) {
        DEBUGP("EWMH window title: %s\n", wtitle.strings);
		
        xcb_ewmh_get_utf8_strings_reply_wipe(&wtitle);
    }

    /* This section allows for moving to the next desktop if the current one is full */ 
    int maxwin = WIN_LIMIT - 2;
	/* count stack windows and grab first non-floating, non-maximize window */
    for (t = M_HEAD; t; t = M_GETNEXT(t)) {
        if (!ISFMTM(t)) {
            if (c)
                ++n;
            else
                c = t;
        }
    }
	while (n > maxwin) {
		if (current_desktop_number == DESKTOPS-1)
			break;
		change_desktop(&(Arg)
			{.i = (DESKTOPS + current_desktop_number + 1) % DESKTOPS});
		n = 0;
		/* count stack windows and grab first non-floating, non-maximize window */
		for (t = M_HEAD; t; t = M_GETNEXT(t)) {
			if (!ISFMTM(t)) {
				if (c)
					++n;
				else
					c = t;
			}
		}
	}
	/* original add window code is the line below */
    c = addwindow(ev->window, wtype);

    xcb_icccm_get_wm_transient_for_reply(dis,
                    xcb_icccm_get_wm_transient_for_unchecked(dis, ev->window),
                    &transient, NULL); /* TODO: error handling */
    c->istransient = transient ? true : false;
    c->isfloating  = isFloating || c->istransient;
    c->borderwidth = border_width;

    prop_reply = xcb_get_property_reply(dis, xcb_get_property_unchecked(
                                    dis, 0, ev->window, ewmh->_NET_WM_STATE,
                                    XCB_ATOM_ATOM, 0, 1), NULL);
                                    /* TODO: error handling */
    if (prop_reply) {
        if (prop_reply->format == 32) {
            xcb_atom_t *v = xcb_get_property_value(prop_reply);
            for (unsigned int i = 0; i < prop_reply->value_len; i++) {
                DEBUGP("%d : %d\n", i, v[i]);
                if (v[i] == ewmh->_NET_WM_STATE_FULLSCREEN)
                    setfullscreen(c, True);
            }
        }
        free(prop_reply);
    }

    DEBUGP("transient: %d\n", c->istransient);
    DEBUGP("floating:  %d\n", c->isfloating);

    int wmdsk = cd;
    bool visible = True;
    xcb_move(dis, c->win, -2 * M_WW, 0, &c->position_info);
    xcb_map_window(dis, c->win);
    
    /* code edit. if new window is transient, go to current desktop (variable was set before window was mapped)
    * if the desktop was full, the transient window was put on the next available desktop, but is moved back to
    * the original desktop*/
    if (c->istransient) {
        select_desktop(cd);
		change_desktop(&(Arg){.i = cd});
    }
    /* end code addition */
    
    if (visible && show) {
        xcb_move(dis, c->win, c->position_info.previous_x,
                              c->position_info.previous_y, NULL);
        c->position_info.previous_x = c->position_info.current_x;
        c->position_info.previous_y = c->position_info.current_y;
        update_current(c);
    }
    xcb_ewmh_set_wm_desktop(ewmh, c->win, wmdsk);
    grabbuttons(c);
    desktopinfo();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %r14
andl $0x0, 0x1c(%rsp)
movq 0x5034(%rip), %rax      # 0xa768
movl 0x104(%rax), %eax
movl %eax, 0x18(%rsp)
movb $0x0, 0x17(%rsp)
movl 0x8(%rdi), %ebp
movl %ebp, %edi
callq 0x65c7
testq %rax, %rax
je 0x5780
movq %rax, %rbx
movl 0x20(%rax), %esi
movq 0x5021(%rip), %r14      # 0xa780
addq $0x18, %r14
movq %r14, %rax
movq (%rax), %rcx
testq %rcx, %rcx
je 0x584d
leaq 0x8(%rcx), %rax
cmpl %esi, 0x20(%rcx)
jne 0x5766
jmp 0x5863
leaq 0x17(%rsp), %rsi
leaq 0x18(%rsp), %rdx
movl %ebp, %edi
callq 0x5b5c
testb %al, %al
jne 0x5a88
movq 0x4fb0(%rip), %rdi      # 0xa750
movl 0x8(%r14), %esi
movq 0x4fbd(%rip), %rax      # 0xa768
movl 0x84(%rax), %edx
movl 0x124(%rax), %ecx
callq 0x37b3
movl 0x4fe6(%rip), %ebx      # 0xa7a8
movq 0x4f9f(%rip), %rdi      # 0xa768
movl 0x8(%r14), %esi
callq 0x3460
movq 0x4f8f(%rip), %rdi      # 0xa768
xorl %r12d, %r12d
leaq 0x30(%rsp), %rdx
movl %eax, %esi
xorl %ecx, %ecx
callq 0x31c0
testb %al, %al
je 0x57f8
leaq 0x30(%rsp), %rdi
callq 0x35b0
movq 0x4f81(%rip), %rax      # 0xa780
addq $0x18, %rax
movq 0x4f5e(%rip), %rcx      # 0xa768
xorl %r13d, %r13d
movq (%rax), %rax
testq %rax, %rax
je 0x5885
cmpb $0x0, 0x1b(%rax)
jne 0x5847
cmpb $0x0, 0x1c(%rax)
jne 0x5847
cmpb $0x0, 0x19(%rax)
jne 0x5847
cmpb $0x0, 0x1d(%rax)
jne 0x5847
movl 0x24(%rax), %edx
cmpl 0x104(%rcx), %edx
jne 0x5847
xorl %edx, %edx
testq %r13, %r13
setne %dl
cmoveq %rax, %r13
addl %edx, %r12d
addq $0x8, %rax
jmp 0x580d
movq %rbx, %rdi
callq 0x4c14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4d07
movl 0x20(%rbx), %esi
movq 0x4ee6(%rip), %rdi      # 0xa750
callq 0x34b0
movq %rbx, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3cfa
pushq $0xa
popq %rbp
leaq 0x20(%rsp), %r15
cmpl $0x2, %r12d
jl 0x5905
movl 0x4f0f(%rip), %eax      # 0xa7a8
cmpl $0x9, %eax
je 0x5905
addl $0xb, %eax
cltd
idivl %ebp
movl %edx, 0x20(%rsp)
movq %r15, %rdi
callq 0x5dc5
movq 0x4ec9(%rip), %rax      # 0xa780
addq $0x18, %rax
xorl %r12d, %r12d
movq 0x4ea3(%rip), %rcx      # 0xa768
movq (%rax), %rax
testq %rax, %rax
je 0x588d
cmpb $0x0, 0x1b(%rax)
jne 0x58ff
cmpb $0x0, 0x1c(%rax)
jne 0x58ff
cmpb $0x0, 0x19(%rax)
jne 0x58ff
cmpb $0x0, 0x1d(%rax)
jne 0x58ff
movl 0x24(%rax), %edx
cmpl 0x104(%rcx), %edx
jne 0x58ff
xorl %edx, %edx
testq %r13, %r13
setne %dl
cmoveq %rax, %r13
addl %edx, %r12d
addq $0x8, %rax
jmp 0x58c5
movl 0x8(%r14), %edi
movl 0x18(%rsp), %esi
callq 0x5c86
movq %rax, %r15
movq 0x4e34(%rip), %r12      # 0xa750
movl 0x8(%r14), %esi
movq %r12, %rdi
callq 0x3260
leaq 0x1c(%rsp), %r13
movq %r12, %rdi
movl %eax, %esi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x3070
xorl %eax, %eax
cmpl $0x0, (%r13)
setne 0x19(%r15)
setne %al
cmpb $0x0, 0x17(%rsp)
pushq $0x1
popq %rcx
cmovel %eax, %ecx
movb %cl, 0x1a(%r15)
orl $-0x1, 0x40(%r15)
movq 0x4dea(%rip), %r12      # 0xa750
movl 0x8(%r14), %edx
movq 0x4df7(%rip), %rax      # 0xa768
movl 0x84(%rax), %ecx
movl $0x1, (%rsp)
pushq $0x4
popq %r8
movq %r12, %rdi
xorl %esi, %esi
xorl %r9d, %r9d
callq 0x31e0
movq %r12, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x34f0
testq %rax, %rax
je 0x59ed
movq %rax, %r14
cmpb $0x20, 0x1(%rax)
jne 0x59e5
movq %r14, %rdi
callq 0x3100
movq %rax, %r12
xorl %r13d, %r13d
pushq $0x1
popq %rbp
movl 0x10(%r14), %eax
cmpq %rax, %r13
jae 0x59e5
movl (%r12,%r13,4), %eax
movq 0x4d9a(%rip), %rcx      # 0xa768
cmpl 0x128(%rcx), %eax
jne 0x59e0
movq %r15, %rdi
movl %ebp, %esi
callq 0x39b9
incq %r13
jmp 0x59ba
movq %r14, %rdi
callq 0x3050
movq 0x4d5c(%rip), %rdi      # 0xa750
movl 0x20(%r15), %esi
movq 0x4d89(%rip), %rax      # 0xa788
movl 0x34(%rax), %edx
addl %edx, %edx
negl %edx
movq %r15, %r8
addq $0x30, %r8
xorl %ecx, %ecx
callq 0x4c77
movq 0x4d35(%rip), %rdi      # 0xa750
movl 0x20(%r15), %esi
callq 0x34b0
cmpb $0x1, 0x19(%r15)
jne 0x5a3e
movl %ebx, %edi
callq 0x4ca0
leaq 0x28(%rsp), %rdi
movl %ebx, (%rdi)
callq 0x5dc5
movq 0x4d0b(%rip), %rdi      # 0xa750
movl 0x20(%r15), %esi
movl 0x30(%r15), %edx
movl 0x34(%r15), %ecx
xorl %r8d, %r8d
callq 0x4c77
movq 0x38(%r15), %rax
movq %rax, 0x30(%r15)
movq %r15, %rdi
callq 0x3cfa
movq 0x4cf8(%rip), %rdi      # 0xa768
movl 0x20(%r15), %esi
movl %ebx, %edx
callq 0x3640
movq %r15, %rdi
callq 0x5d55
callq 0x4b1f
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq