// File: /workspace/github_c_makefile_star2_O3/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
movl $0x0, 0x14(%rsp)
movq 0x4d75(%rip), %rax      # 0xa768
movl 0x104(%rax), %eax
movl %eax, 0x10(%rsp)
movb $0x0, 0xf(%rsp)
movl 0x8(%rdi), %ebp
movl %ebp, %edi
callq 0x6aca
testq %rax, %rax
je 0x5a68
movq %rax, %rbx
movq 0x4d65(%rip), %r14      # 0xa780
movq 0x18(%r14), %rax
testq %rax, %rax
je 0x5a39
movl 0x20(%rbx), %esi
cmpl %esi, 0x20(%rax)
je 0x5d22
movq 0x8(%rax), %rax
testq %rax, %rax
jne 0x5a27
leaq 0x18(%r14), %r15
movq %rbx, %rdi
callq 0x4e73
cmpq $0x0, 0x18(%r14)
je 0x5d14
movq 0x20(%r14), %rax
movq %rbx, 0x8(%rax)
movq %rax, (%rbx)
movq $0x0, 0x8(%rbx)
jmp 0x5d17
leaq 0xf(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movl %ebp, %edi
callq 0x5eaf
testb %al, %al
jne 0x5d05
movq 0x4cc8(%rip), %rdi      # 0xa750
movl 0x8(%r14), %esi
movq 0x4cd5(%rip), %rax      # 0xa768
movl 0x84(%rax), %edx
movl 0x124(%rax), %ecx
callq 0x37cb
movl 0x4cfe(%rip), %ebx      # 0xa7a8
movq 0x4cb7(%rip), %rdi      # 0xa768
movl 0x8(%r14), %esi
callq 0x3470
movq 0x4ca7(%rip), %rdi      # 0xa768
leaq 0x30(%rsp), %rdx
movl %eax, %esi
xorl %ecx, %ecx
callq 0x31c0
testb %al, %al
je 0x5add
leaq 0x30(%rsp), %rdi
callq 0x35c0
movq 0x4c9c(%rip), %rax      # 0xa780
movq 0x18(%rax), %rax
testq %rax, %rax
je 0x5b4e
xorl %r12d, %r12d
movq 0x4c71(%rip), %rcx      # 0xa768
xorl %edx, %edx
cmpb $0x0, 0x1b(%rax)
jne 0x5b2b
cmpb $0x0, 0x1c(%rax)
jne 0x5b2b
cmpb $0x0, 0x19(%rax)
jne 0x5b2b
cmpb $0x0, 0x1d(%rax)
jne 0x5b2b
movl 0x24(%rax), %esi
cmpl 0x104(%rcx), %esi
jne 0x5b2b
xorl %esi, %esi
testq %r12, %r12
setne %sil
cmoveq %rax, %r12
addl %esi, %edx
movq 0x8(%rax), %rax
testq %rax, %rax
jne 0x5af9
cmpl $0x2, %edx
setl %cl
movl 0x4c68(%rip), %eax      # 0xa7a8
cmpl $0x9, %eax
sete %dl
orb %cl, %dl
je 0x5d44
movl 0x8(%r14), %edi
movl 0x10(%rsp), %esi
callq 0x5fe5
movq %rax, %r15
movq 0x4beb(%rip), %r12      # 0xa750
movl 0x8(%r14), %esi
movq %r12, %rdi
callq 0x3260
xorl %ebp, %ebp
leaq 0x14(%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, 0xf(%rsp)
movl $0x1, %ecx
cmovel %eax, %ecx
movb %cl, 0x1a(%r15)
movl $0xffffffff, 0x40(%r15) # imm = 0xFFFFFFFF
movq 0x4b9a(%rip), %r12      # 0xa750
movl 0x8(%r14), %edx
movq 0x4ba7(%rip), %rax      # 0xa768
movl 0x84(%rax), %ecx
movl $0x1, (%rsp)
movq %r12, %rdi
xorl %esi, %esi
movl $0x4, %r8d
xorl %r9d, %r9d
callq 0x31e0
movq %r12, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x3500
testq %rax, %rax
je 0x5c4e
movq %rax, %r14
cmpb $0x20, 0x1(%rax)
jne 0x5c46
movq %r14, %rdi
callq 0x3100
movq %rax, %r12
movl 0x10(%r14), %eax
testl %eax, %eax
je 0x5c46
movq 0x4b53(%rip), %rcx      # 0xa768
xorl %r13d, %r13d
movl (%r12,%r13,4), %edx
cmpl 0x128(%rcx), %edx
jne 0x5c3c
movq %r15, %rdi
movl $0x1, %esi
callq 0x39e5
movq 0x4b30(%rip), %rcx      # 0xa768
movl 0x10(%r14), %eax
incq %r13
movl %eax, %edx
cmpq %rdx, %r13
jb 0x5c18
movq %r14, %rdi
callq 0x3050
movq 0x4afb(%rip), %rdi      # 0xa750
movl 0x20(%r15), %esi
movq 0x4b28(%rip), %rax      # 0xa788
movl 0x34(%rax), %eax
addl %eax, %eax
negl %eax
leaq 0x20(%rsp), %rcx
movl %eax, (%rcx)
movl %ebp, 0x4(%rcx)
movq 0x38(%r15), %rdx
movq %rdx, 0x30(%r15)
movl %eax, 0x38(%r15)
movl %ebp, 0x3c(%r15)
movl $0x3, %edx
callq 0x32c0
movq 0x4abe(%rip), %rdi      # 0xa750
movl 0x20(%r15), %esi
callq 0x34c0
cmpb $0x1, 0x19(%r15)
jne 0x5cb5
movl %ebx, %edi
callq 0x4ee2
leaq 0x28(%rsp), %rdi
movl %ebx, (%rdi)
callq 0x616c
movq 0x4a94(%rip), %rdi      # 0xa750
movl 0x20(%r15), %esi
movq 0x30(%r15), %rax
leaq 0x20(%rsp), %rcx
movq %rax, (%rcx)
movl $0x3, %edx
callq 0x32c0
movq 0x38(%r15), %rax
movq %rax, 0x30(%r15)
movq %r15, %rdi
callq 0x3dc0
movq 0x4a7b(%rip), %rdi      # 0xa768
movl 0x20(%r15), %esi
movl %ebx, %edx
callq 0x3650
movq %r15, %rdi
callq 0x60fc
callq 0x4d6d
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, (%r15)
movq %rbx, 0x20(%r14)
movq %r15, 0x10(%rbx)
movl 0x20(%rbx), %esi
movq 0x4a27(%rip), %rdi      # 0xa750
callq 0x34c0
movq %rbx, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3dc0
leaq 0x18(%rsp), %r15
addl $0xb, %eax
cltq
imulq $0x66666667, %rax, %rcx # imm = 0x66666667
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x22, %rcx
addl %edx, %ecx
addl %ecx, %ecx
leal (%rcx,%rcx,4), %ecx
subl %ecx, %eax
movl %eax, 0x18(%rsp)
movq %r15, %rdi
callq 0x616c
movq 0x4a04(%rip), %rax      # 0xa780
movq 0x18(%rax), %rax
testq %rax, %rax
je 0x5b4e
xorl %ecx, %ecx
movq 0x49d6(%rip), %rdx      # 0xa768
cmpb $0x0, 0x1b(%rax)
jne 0x5dc4
cmpb $0x0, 0x1c(%rax)
jne 0x5dc4
cmpb $0x0, 0x19(%rax)
jne 0x5dc4
cmpb $0x0, 0x1d(%rax)
jne 0x5dc4
movl 0x24(%rax), %esi
cmpl 0x104(%rdx), %esi
jne 0x5dc4
xorl %esi, %esi
testq %r12, %r12
setne %sil
cmoveq %rax, %r12
addl %esi, %ecx
movq 0x8(%rax), %rax
testq %rax, %rax
jne 0x5d92
cmpl $0x2, %ecx
jl 0x5b4e
movl 0x49cc(%rip), %eax      # 0xa7a8
cmpl $0x9, %eax
jne 0x5d49
jmp 0x5b4e