// File: /workspace/github_c_makefile_star2_O1/tkln[P]mcwm/mcwm.c
// Function: fitonscreen
void fitonscreen(struct client *client)
{
    int16_t mon_x;
    int16_t mon_y;
    uint16_t mon_width;
    uint16_t mon_height;
    bool willmove = false;
    bool willresize = false;

    client->vertmaxed = false;

    if (client->maxed)
    {
        client->maxed = false;
        setborders(client, conf.borderwidth);
    }
        
    if (NULL == client->monitor)
    {
        /*
         * This window isn't attached to any physical monitor. This
         * probably means there is no RANDR, so we use the root window
         * size.
         */
        mon_x = 0;
        mon_y = 0;
        mon_width = screen->width_in_pixels;
        mon_height = screen->height_in_pixels;
    }
    else
    {
        mon_x = client->monitor->x;
        mon_y = client->monitor->y;
        mon_width = client->monitor->width;
        mon_height = client->monitor->height;        
    }

    PDEBUG("Is window outside monitor?\n");
    PDEBUG("x: %d between %d and %d?\n", client->x, mon_x, mon_x + mon_width);
    PDEBUG("y: %d between %d and %d?\n", client->y, mon_y, mon_y + mon_height);
    
    /* Is it outside the physical monitor? */
    if (client->x > mon_x + mon_width)
    {
        client->x = mon_x + mon_width - client->width;
        willmove = true;
    }
    if (client->y > mon_y + mon_height)
    {
        client->y = mon_y + mon_height - client->height;
        willmove = true;        
    }
    
    if (client->x < mon_x)
    {
        client->x = mon_x;
        willmove = true;
    }
    if (client->y < mon_y)
    {
        client->y = mon_y;
        willmove = true;
    }
    
    /* Is it smaller than it wants to  be? */
    if (0 != client->min_height && client->height < client->min_height)
    {
        client->height = client->min_height;
        willresize = true;
    }

    if (0 != client->min_width && client->width < client->min_width)
    {
        client->width = client->min_width;
        willresize = true;        
    }

    /*
     * If the window is larger than our screen, just place it in the
     * corner and resize.
     */
    if (client->width + conf.borderwidth * 2 > mon_width)
    {
        client->x = mon_x;
        client->width = mon_width - conf.borderwidth * 2;;
        willmove = true;
        willresize = true;
    }
    else if (client->x + client->width + conf.borderwidth * 2
             > mon_x + mon_width)
    {
        client->x = mon_x + mon_width - (client->width + conf.borderwidth * 2);
        willmove = true;
    }

    if (client->height + conf.borderwidth * 2 > mon_height)
    {
        client->y = mon_y;
        client->height = mon_height - conf.borderwidth * 2;
        willmove = true;
        willresize = true;
    }
    else if (client->y + client->height + conf.borderwidth * 2
             > mon_y + mon_height)
    {
        client->y = mon_y + mon_height - (client->height + conf.borderwidth
                                          * 2);
        willmove = true;
    }

    if (willmove)
    {
        PDEBUG("Moving to %d,%d.\n", client->x, client->y);
        movewindow(client->id, client->x, client->y);
    }

    if (willresize)
    {
        PDEBUG("Resizing to %d x %d.\n", client->width, client->height);
        resize(client->id, client->width, client->height);
    }
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0x10, %rsp
movq %rdi, %rbx
movb $0x0, 0x30(%rdi)
cmpb $0x1, 0x31(%rdi)
jne 0x432d
movb $0x0, 0x31(%rbx)
movl 0x6649(%rip), %eax      # 0xa950
leaq 0xc(%rsp), %rcx
movl %eax, (%rcx)
movq 0x6663(%rip), %rdi      # 0xa978
movl (%rbx), %esi
movl $0x10, %edx
callq 0x3200
movq 0x6650(%rip), %rdi      # 0xa978
callq 0x3150
movq 0x38(%rbx), %rdx
testq %rdx, %rdx
je 0x4340
movzwl 0x10(%rdx), %edi
movzwl 0x12(%rdx), %eax
jmp 0x434b
movq 0x6601(%rip), %rdx      # 0xa948
xorl %edi, %edi
xorl %eax, %eax
movzwl 0x16(%rdx), %ecx
movzwl 0x14(%rdx), %r9d
movswl 0x6(%rbx), %edx
movswl %di, %r8d
addl %r9d, %r8d
cmpl %edx, %r8d
setl %dl
jge 0x4377
movzwl 0xa(%rbx), %esi
movl %r8d, %r10d
subw %si, %r10w
movw %r10w, 0x6(%rbx)
movswl 0x8(%rbx), %r10d
movswl %ax, %esi
addl %ecx, %esi
cmpl %r10d, %esi
jge 0x4398
movzwl 0xc(%rbx), %edx
movl %esi, %r10d
subw %dx, %r10w
movw %r10w, 0x8(%rbx)
movb $0x1, %dl
cmpw %di, 0x6(%rbx)
jge 0x43a4
movw %di, 0x6(%rbx)
movb $0x1, %dl
cmpw %ax, 0x8(%rbx)
jge 0x43b0
movw %ax, 0x8(%rbx)
movb $0x1, %dl
movzwl 0x18(%rbx), %r10d
testw %r10w, %r10w
je 0x43cc
cmpw %r10w, 0xc(%rbx)
jae 0x43cc
movw %r10w, 0xc(%rbx)
movb $0x1, %bpl
jmp 0x43ce
xorl %ebp, %ebp
movzwl 0x16(%rbx), %r10d
testw %r10w, %r10w
je 0x43e8
cmpw %r10w, 0xa(%rbx)
jae 0x43e8
movw %r10w, 0xa(%rbx)
movb $0x1, %bpl
movzwl 0xa(%rbx), %r11d
movl 0x655c(%rip), %r10d     # 0xa950
leal (%r11,%r10,2), %r14d
addl %r10d, %r10d
cmpl %r9d, %r14d
jle 0x4413
movw %di, 0x6(%rbx)
subl %r10d, %r9d
movw %r9w, 0xa(%rbx)
movb $0x1, %dl
movb $0x1, %bpl
jmp 0x442c
movswl 0x6(%rbx), %edi
addl %r10d, %r11d
addl %edi, %r11d
cmpl %r8d, %r11d
jle 0x442c
subl %r14d, %r8d
movw %r8w, 0x6(%rbx)
movb $0x1, %dl
movzwl 0xc(%rbx), %edi
addl %r10d, %edi
cmpl %ecx, %edi
jle 0x4449
movw %ax, 0x8(%rbx)
subl %r10d, %ecx
movw %cx, 0xc(%rbx)
movb $0x1, %dl
movb $0x1, %bpl
jmp 0x445b
movswl 0x8(%rbx), %eax
addl %edi, %eax
cmpl %esi, %eax
jle 0x445b
subl %edi, %esi
movw %si, 0x8(%rbx)
movb $0x1, %dl
testb %dl, %dl
je 0x446e
movl (%rbx), %edi
movzwl 0x6(%rbx), %esi
movzwl 0x8(%rbx), %edx
callq 0x623d
testb %bpl, %bpl
je 0x4482
movl (%rbx), %edi
movzwl 0xa(%rbx), %esi
movzwl 0xc(%rbx), %edx
callq 0x6281
addq $0x10, %rsp
popq %rbx
popq %r14
popq %rbp
retq