// File: /workspace/github_c_makefile_star2_O2/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movb $0x0, 0x30(%rdi)
cmpb $0x1, 0x31(%rdi)
jne 0x415f
movb $0x0, 0x31(%rbx)
movl 0x67f9(%rip), %esi      # 0xa950
movq %rbx, %rdi
callq 0x6020
movq 0x38(%rbx), %rsi
testq %rsi, %rsi
je 0x4172
movzwl 0x10(%rsi), %eax
movzwl 0x12(%rsi), %ecx
jmp 0x417d
movq 0x67cf(%rip), %rsi      # 0xa948
xorl %eax, %eax
xorl %ecx, %ecx
movzwl 0x16(%rsi), %edx
movzwl 0x14(%rsi), %r10d
movswl 0x6(%rbx), %r11d
movswl %ax, %r9d
addl %r10d, %r9d
cmpl %r11d, %r9d
setl %dil
jge 0x41ab
movzwl 0xa(%rbx), %esi
movl %r9d, %r11d
subw %si, %r11w
movw %r11w, 0x6(%rbx)
movswl 0x8(%rbx), %r8d
movswl %cx, %esi
addl %edx, %esi
cmpl %r8d, %esi
jge 0x41cd
movzwl 0xc(%rbx), %edi
movl %esi, %r8d
subw %di, %r8w
movw %r8w, 0x8(%rbx)
movb $0x1, %dil
cmpw %ax, %r11w
jge 0x41dd
movw %ax, 0x6(%rbx)
movb $0x1, %dil
movl %eax, %r11d
cmpw %cx, %r8w
jge 0x41ed
movw %cx, 0x8(%rbx)
movb $0x1, %dil
movl %ecx, %r8d
movzwl 0x18(%rbx), %ebp
testw %bp, %bp
je 0x4205
cmpw %bp, 0xc(%rbx)
jae 0x4205
movw %bp, 0xc(%rbx)
movb $0x1, %bpl
jmp 0x4207
xorl %ebp, %ebp
movzwl 0xa(%rbx), %r14d
movzwl 0x16(%rbx), %r15d
testw %r15w, %r15w
setne %r12b
cmpw %r15w, %r14w
setb %r13b
andb %r12b, %r13b
cmpb $0x1, %r13b
jne 0x4235
movw %r15w, 0xa(%rbx)
movb $0x1, %bpl
movl %r15d, %r14d
movzwl %r14w, %r14d
movl 0x6710(%rip), %r12d     # 0xa950
leal (%r14,%r12,2), %r15d
movl %r12d, %r14d
addl %r12d, %r14d
cmpl %r10d, %r15d
jle 0x4263
movw %ax, 0x6(%rbx)
subl %r14d, %r10d
movw %r10w, 0xa(%rbx)
movb $0x1, %dil
movb $0x1, %bpl
jmp 0x4282
movswl %r11w, %eax
addl %r15d, %eax
cmpl %r9d, %eax
jle 0x427f
subl %r15d, %r9d
movw %r9w, 0x6(%rbx)
movb $0x1, %dil
movl %r9d, %eax
jmp 0x4282
movl %r11d, %eax
movzwl 0xc(%rbx), %r9d
addl %r14d, %r9d
cmpl %edx, %r9d
jle 0x42a9
movw %cx, 0x8(%rbx)
subl %r14d, %edx
movw %dx, 0xc(%rbx)
movl (%rbx), %edi
movzwl %ax, %esi
movzwl %cx, %edx
callq 0x604a
jmp 0x42d8
movswl %r8w, %ecx
addl %r9d, %ecx
cmpl %esi, %ecx
jle 0x42c0
subl %r9d, %esi
movw %si, 0x8(%rbx)
movl %esi, %r8d
jmp 0x42c5
testb %dil, %dil
je 0x42d3
movl (%rbx), %edi
movzwl %ax, %esi
movzwl %r8w, %edx
callq 0x604a
testb %bpl, %bpl
je 0x42f5
movl (%rbx), %edi
movzwl 0xa(%rbx), %esi
movzwl 0xc(%rbx), %edx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x608c
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq