// File: /workspace/github_c_makefile_star2_O1/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 0x5c15(%rip), %rax      # 0xb768
movl 0x104(%rax), %eax
movl %eax, 0x10(%rsp)
movb $0x0, 0xf(%rsp)
movl 0x8(%rdi), %ebp
movl %ebp, %edi
callq 0x6c8f
testq %rax, %rax
je 0x5bca
movq %rax, %rbx
movq 0x5c05(%rip), %r14      # 0xb780
movq 0x18(%r14), %rax
testq %rax, %rax
je 0x5b99
movl 0x20(%rbx), %ecx
cmpl %ecx, 0x20(%rax)
je 0x5cba
movq 0x8(%rax), %rax
testq %rax, %rax
jne 0x5b87
leaq 0x18(%r14), %r15
movq %rbx, %rdi
callq 0x4f4a
cmpq $0x0, (%r15)
je 0x5ca2
movq 0x20(%r14), %rax
movq %rbx, 0x20(%r14)
movq %rbx, 0x8(%rax)
movq %rax, (%rbx)
movl $0x8, %eax
xorl %ecx, %ecx
jmp 0x5cb2
leaq 0xf(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movl %ebp, %edi
callq 0x601b
testb %al, %al
jne 0x5f47
movq 0x5b66(%rip), %rdi      # 0xb750
movl 0x8(%r14), %esi
movq 0x5b73(%rip), %rax      # 0xb768
movl 0x84(%rax), %edx
movl 0x124(%rax), %ecx
callq 0x37cb
movl 0x5b9c(%rip), %ebx      # 0xb7a8
movq 0x5b55(%rip), %rdi      # 0xb768
movl 0x8(%r14), %esi
callq 0x3470
movq 0x5b45(%rip), %rdi      # 0xb768
leaq 0x30(%rsp), %rdx
movl %eax, %esi
xorl %ecx, %ecx
callq 0x31c0
testb %al, %al
je 0x5c3f
leaq 0x30(%rsp), %rdi
callq 0x35c0
movq 0x5b3a(%rip), %rax      # 0xb780
movq 0x18(%rax), %rcx
xorl %eax, %eax
testq %rcx, %rcx
je 0x5cdf
movq 0x5b0c(%rip), %rdx      # 0xb768
xorl %r12d, %r12d
cmpb $0x0, 0x1b(%rcx)
jne 0x5c91
cmpb $0x0, 0x1c(%rcx)
jne 0x5c91
cmpb $0x0, 0x19(%rcx)
jne 0x5c91
cmpb $0x0, 0x1d(%rcx)
jne 0x5c91
movl 0x24(%rcx), %esi
cmpl 0x104(%rdx), %esi
jne 0x5c91
xorl %esi, %esi
testq %r12, %r12
setne %sil
cmoveq %rcx, %r12
addl %esi, %eax
movq 0x8(%rcx), %rcx
testq %rcx, %rcx
jne 0x5c5f
cmpl $0x2, %eax
setl %cl
jmp 0x5ce4
movq %rbx, 0x18(%r14)
movq %rbx, 0x20(%r14)
movl $0x10, %eax
movq %r15, %rcx
movq %rcx, (%rbx,%rax)
movq %r15, 0x10(%rbx)
movq 0x5a8f(%rip), %rdi      # 0xb750
movl 0x20(%rbx), %esi
callq 0x34c0
movq %rbx, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3dcb
movb $0x1, %cl
xorl %r12d, %r12d
movl 0x5abe(%rip), %eax      # 0xb7a8
cmpl $0x9, %eax
sete %dl
orb %cl, %dl
jne 0x5d9a
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 0x62e4
movq 0x5a50(%rip), %rax      # 0xb780
movq 0x18(%rax), %rax
testq %rax, %rax
je 0x5d85
xorl %ecx, %ecx
movq 0x5a26(%rip), %rdx      # 0xb768
cmpb $0x0, 0x1b(%rax)
jne 0x5d74
cmpb $0x0, 0x1c(%rax)
jne 0x5d74
cmpb $0x0, 0x19(%rax)
jne 0x5d74
cmpb $0x0, 0x1d(%rax)
jne 0x5d74
movl 0x24(%rax), %esi
cmpl 0x104(%rdx), %esi
jne 0x5d74
xorl %esi, %esi
testq %r12, %r12
setne %sil
cmoveq %rax, %r12
addl %esi, %ecx
movq 0x8(%rax), %rax
testq %rax, %rax
jne 0x5d42
cmpl $0x2, %ecx
setl %al
jmp 0x5d87
movb $0x1, %al
testb %al, %al
jne 0x5d9a
movl 0x5a17(%rip), %eax      # 0xb7a8
cmpl $0x9, %eax
jne 0x5cfd
movl 0x8(%r14), %edi
movl 0x10(%rsp), %esi
callq 0x614c
movq %rax, %r15
movq 0x599f(%rip), %r12      # 0xb750
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 0x594e(%rip), %r12      # 0xb750
movl 0x8(%r14), %edx
movq 0x595b(%rip), %rax      # 0xb768
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 0x5e90
movq %rax, %r14
cmpb $0x20, 0x1(%rax)
jne 0x5e88
movq %r14, %rdi
callq 0x3100
cmpl $0x0, 0x10(%r14)
je 0x5e88
movq %rax, %r12
xorl %r13d, %r13d
movl (%r12,%r13,4), %eax
movq 0x5901(%rip), %rcx      # 0xb768
cmpl 0x128(%rcx), %eax
jne 0x5e7c
movq %r15, %rdi
movl $0x1, %esi
callq 0x39df
incq %r13
movl 0x10(%r14), %eax
cmpq %rax, %r13
jb 0x5e5c
movq %r14, %rdi
callq 0x3050
movq 0x58b9(%rip), %rdi      # 0xb750
movl 0x20(%r15), %esi
movq 0x58e6(%rip), %rax      # 0xb788
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 0x587c(%rip), %rdi      # 0xb750
movl 0x20(%r15), %esi
callq 0x34c0
cmpb $0x1, 0x19(%r15)
jne 0x5ef7
movl %ebx, %edi
callq 0x4fc0
leaq 0x28(%rsp), %rdi
movl %ebx, (%rdi)
callq 0x62e4
movq 0x5852(%rip), %rdi      # 0xb750
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 0x3dcb
movq 0x5839(%rip), %rdi      # 0xb768
movl 0x20(%r15), %esi
movl %ebx, %edx
callq 0x3650
movq %r15, %rdi
callq 0x6274
callq 0x4e44
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq