// File: /workspace/github_c_makefile_star2_O3/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 0x431b
movb $0x0, 0x31(%rbx)
movl 0x665b(%rip), %eax      # 0xa950
movl (%rbx), %esi
leaq 0x4(%rsp), %rcx
movl %eax, (%rcx)
movq 0x6673(%rip), %rdi      # 0xa978
movl $0x10, %edx
callq 0x3200
movq 0x6662(%rip), %rdi      # 0xa978
callq 0x3150
movq 0x38(%rbx), %rsi
testq %rsi, %rsi
je 0x432e
movzwl 0x10(%rsi), %eax
movzwl 0x12(%rsi), %ecx
jmp 0x4339
movq 0x6613(%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 0x4367
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 0x4389
movzwl 0xc(%rbx), %edi
movl %esi, %r8d
subw %di, %r8w
movw %r8w, 0x8(%rbx)
movb $0x1, %dil
cmpw %ax, %r11w
jge 0x4399
movw %ax, 0x6(%rbx)
movb $0x1, %dil
movl %eax, %r11d
cmpw %cx, %r8w
jge 0x43a9
movw %cx, 0x8(%rbx)
movb $0x1, %dil
movl %ecx, %r8d
movzwl 0x18(%rbx), %ebp
testw %bp, %bp
je 0x43c1
cmpw %bp, 0xc(%rbx)
jae 0x43c1
movw %bp, 0xc(%rbx)
movb $0x1, %bpl
jmp 0x43c3
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 0x43f1
movw %r15w, 0xa(%rbx)
movb $0x1, %bpl
movl %r15d, %r14d
movzwl %r14w, %r14d
movl 0x6554(%rip), %r12d     # 0xa950
leal (%r14,%r12,2), %r15d
movl %r12d, %r14d
addl %r12d, %r14d
cmpl %r10d, %r15d
jle 0x441f
movw %ax, 0x6(%rbx)
subl %r14d, %r10d
movw %r10w, 0xa(%rbx)
movb $0x1, %dil
movb $0x1, %bpl
jmp 0x443e
movswl %r11w, %eax
addl %r15d, %eax
cmpl %r9d, %eax
jle 0x443b
subl %r15d, %r9d
movw %r9w, 0x6(%rbx)
movb $0x1, %dil
movl %r9d, %eax
jmp 0x443e
movl %r11d, %eax
movzwl 0xc(%rbx), %r9d
addl %r14d, %r9d
cmpl %edx, %r9d
jle 0x4465
movw %cx, 0x8(%rbx)
subl %r14d, %edx
movw %dx, 0xc(%rbx)
movl (%rbx), %edi
movzwl %ax, %esi
movzwl %cx, %edx
callq 0x629c
jmp 0x4499
movswl %r8w, %ecx
addl %r9d, %ecx
cmpl %esi, %ecx
jle 0x4481
subl %r9d, %esi
movw %si, 0x8(%rbx)
movl (%rbx), %edi
movzwl %si, %edx
movzwl %ax, %esi
jmp 0x448f
testb %dil, %dil
je 0x4494
movl (%rbx), %edi
movzwl %ax, %esi
movzwl %r8w, %edx
callq 0x629c
testb %bpl, %bpl
je 0x44a8
movl (%rbx), %edi
movzwl 0xa(%rbx), %esi
movzwl 0xc(%rbx), %edx
callq 0x62e0
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq