// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movq 0x67a3(%rip), %rax      # 0xd7b8
movl 0x104(%rax), %eax
movl %eax, -0x44(%rbp)
movb $0x0, -0x51(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %edi
callq 0x88d0
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
je 0x708b
movq -0x50(%rbp), %rax
movl 0x20(%rax), %edi
callq 0x91f0
cmpq $0x0, %rax
jne 0x7067
movq -0x50(%rbp), %rdi
callq 0x5300
movq 0x6776(%rip), %rdi      # 0xd7d0
addq $0x18, %rdi
movq -0x50(%rbp), %rsi
callq 0x56a0
movq 0x6732(%rip), %rdi      # 0xd7a0
movq -0x50(%rbp), %rax
movl 0x20(%rax), %esi
callq 0x34c0
movl %eax, -0x58(%rbp)
movq -0x50(%rbp), %rdi
callq 0x4010
jmp 0x7522
movq -0x10(%rbp), %rax
movl 0x8(%rax), %edi
leaq -0x51(%rbp), %rsi
leaq -0x44(%rbp), %rdx
callq 0x7650
testb $0x1, %al
jne 0x70a5
jmp 0x70aa
jmp 0x7522
movq 0x66ef(%rip), %rdi      # 0xd7a0
movq -0x10(%rbp), %rax
movl 0x8(%rax), %esi
movq 0x66f9(%rip), %rax      # 0xd7b8
movl 0x84(%rax), %edx
movq 0x66ec(%rip), %rax      # 0xd7b8
movl 0x124(%rax), %ecx
callq 0x3800
movl 0x671b(%rip), %eax      # 0xd7f8
movl %eax, -0x5c(%rbp)
movl $0xffffffff, -0x60(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x64(%rbp)
movq $0x0, -0x70(%rbp)
movq 0x66bb(%rip), %rdi      # 0xd7b8
movq -0x10(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x3470
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
movl %eax, -0x24(%rbp)
movq 0x669f(%rip), %rdi      # 0xd7b8
movl -0x24(%rbp), %esi
leaq -0x40(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x9250
cmpb $0x0, %al
je 0x7136
leaq -0x40(%rbp), %rdi
callq 0x35c0
movl $0x1, -0x78(%rbp)
movq 0x668c(%rip), %rax      # 0xd7d0
movq 0x18(%rax), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
je 0x71be
movq -0x70(%rbp), %rax
testb $0x1, 0x1b(%rax)
jne 0x71ad
movq -0x70(%rbp), %rax
testb $0x1, 0x1c(%rax)
jne 0x71ad
movq -0x70(%rbp), %rax
testb $0x1, 0x19(%rax)
jne 0x71ad
movq -0x70(%rbp), %rax
testb $0x1, 0x1d(%rax)
jne 0x71ad
movq -0x70(%rbp), %rax
movl 0x24(%rax), %eax
movq 0x662f(%rip), %rcx      # 0xd7b8
cmpl 0x104(%rcx), %eax
jne 0x71ad
cmpq $0x0, -0x50(%rbp)
je 0x71a3
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x71ab
movq -0x70(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x71ad
jmp 0x71af
movq -0x70(%rbp), %rdi
callq 0x5490
movq %rax, -0x70(%rbp)
jmp 0x714c
jmp 0x71c0
movl -0x64(%rbp), %eax
cmpl -0x78(%rbp), %eax
jle 0x7287
cmpl $0x9, 0x6625(%rip)      # 0xd7f8
jne 0x71da
jmp 0x7287
movl 0x6618(%rip), %eax      # 0xd7f8
addl $0xa, %eax
addl $0x1, %eax
movl $0xa, %ecx
cltd
idivl %ecx
movl %edx, -0x80(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x79a0
movl $0x0, -0x64(%rbp)
movq 0x65c8(%rip), %rax      # 0xd7d0
movq 0x18(%rax), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
je 0x7282
movq -0x70(%rbp), %rax
testb $0x1, 0x1b(%rax)
jne 0x7271
movq -0x70(%rbp), %rax
testb $0x1, 0x1c(%rax)
jne 0x7271
movq -0x70(%rbp), %rax
testb $0x1, 0x19(%rax)
jne 0x7271
movq -0x70(%rbp), %rax
testb $0x1, 0x1d(%rax)
jne 0x7271
movq -0x70(%rbp), %rax
movl 0x24(%rax), %eax
movq 0x656b(%rip), %rcx      # 0xd7b8
cmpl 0x104(%rcx), %eax
jne 0x7271
cmpq $0x0, -0x50(%rbp)
je 0x7267
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x726f
movq -0x70(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x7271
jmp 0x7273
movq -0x70(%rbp), %rdi
callq 0x5490
movq %rax, -0x70(%rbp)
jmp 0x7210
jmp 0x71c0
movq -0x10(%rbp), %rax
movl 0x8(%rax), %edi
movl -0x44(%rbp), %esi
callq 0x7850
movq %rax, -0x50(%rbp)
movq 0x64ff(%rip), %rax      # 0xd7a0
movq %rax, -0xb8(%rbp)
movq 0x64f1(%rip), %rdi      # 0xd7a0
movq -0x10(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x3260
movq -0xb8(%rbp), %rdi
movl %eax, -0x84(%rbp)
movl -0x84(%rbp), %esi
leaq -0x14(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x3070
movl -0x14(%rbp), %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
cmpl $0x0, %eax
setne %cl
movq -0x50(%rbp), %rax
andb $0x1, %cl
movb %cl, 0x19(%rax)
movb $0x1, %al
testb $0x1, -0x51(%rbp)
movb %al, -0xad(%rbp)
jne 0x7316
movq -0x50(%rbp), %rax
movb 0x19(%rax), %al
movb %al, -0xad(%rbp)
movb -0xad(%rbp), %cl
movq -0x50(%rbp), %rax
andb $0x1, %cl
movb %cl, 0x1a(%rax)
movl -0x60(%rbp), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x40(%rax)
movq 0x6469(%rip), %rax      # 0xd7a0
movq %rax, -0xc0(%rbp)
movq 0x645b(%rip), %rdi      # 0xd7a0
movq -0x10(%rbp), %rax
movl 0x8(%rax), %edx
movq 0x6465(%rip), %rax      # 0xd7b8
movl 0x84(%rax), %ecx
xorl %r9d, %r9d
movl $0x4, %r8d
movl %r9d, %esi
movl $0x1, (%rsp)
callq 0x31e0
movq -0xc0(%rbp), %rdi
movl %eax, -0x88(%rbp)
movl -0x88(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x3500
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x741d
movq -0x20(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x20, %eax
jne 0x7414
movq -0x20(%rbp), %rdi
callq 0x3100
movq %rax, -0x90(%rbp)
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0x7412
movq -0x90(%rbp), %rax
movl -0x94(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movq 0x63cf(%rip), %rcx      # 0xd7b8
cmpl 0x128(%rcx), %eax
jne 0x73ff
movq -0x50(%rbp), %rdi
movl $0x1, %esi
callq 0x3b50
jmp 0x7401
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x73c3
jmp 0x7414
movq -0x20(%rbp), %rdi
callq 0x3050
movl -0x5c(%rbp), %eax
movl %eax, -0x98(%rbp)
movb $0x1, -0x99(%rbp)
movq 0x636c(%rip), %rdi      # 0xd7a0
movq -0x50(%rbp), %rax
movl 0x20(%rax), %esi
movq 0x6396(%rip), %rax      # 0xd7d8
imull $0xfffffffe, 0x34(%rax), %edx # imm = 0xFFFFFFFE
movq -0x50(%rbp), %r8
addq $0x30, %r8
xorl %ecx, %ecx
callq 0x5410
movq 0x6341(%rip), %rdi      # 0xd7a0
movq -0x50(%rbp), %rax
movl 0x20(%rax), %esi
callq 0x34c0
movl %eax, -0xa0(%rbp)
movq -0x50(%rbp), %rax
testb $0x1, 0x19(%rax)
je 0x7498
movl -0x5c(%rbp), %edi
callq 0x5570
movl -0x5c(%rbp), %eax
movl %eax, -0xa8(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x79a0
testb $0x1, -0x99(%rbp)
je 0x74f5
testb $0x1, 0x62d8(%rip)      # 0xd780
je 0x74f5
movq 0x62ef(%rip), %rdi      # 0xd7a0
movq -0x50(%rbp), %rax
movl 0x20(%rax), %esi
movq -0x50(%rbp), %rax
movl 0x30(%rax), %edx
movq -0x50(%rbp), %rax
movl 0x34(%rax), %ecx
xorl %eax, %eax
movl %eax, %r8d
callq 0x5410
movq -0x50(%rbp), %rax
movl 0x38(%rax), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x30(%rax)
movq -0x50(%rbp), %rax
movl 0x3c(%rax), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x34(%rax)
movq -0x50(%rbp), %rdi
callq 0x4010
movq 0x62bc(%rip), %rdi      # 0xd7b8
movq -0x50(%rbp), %rax
movl 0x20(%rax), %esi
movl -0x98(%rbp), %edx
callq 0x3650
movl %eax, -0xac(%rbp)
movq -0x50(%rbp), %rdi
callq 0x78c0
callq 0x4f80
addq $0xd0, %rsp
popq %rbp
retq
nopl (%rax,%rax)