// File: /workspace/github_c_makefile_star2_O3/OnionIoT[P]lv_example/lv_drivers/display/fbdev.c
// Function: fbdev_flush
void fbdev_flush(lv_disp_drv_t * drv, const lv_area_t * area, lv_color_t * color_p)
{
    if(fbp == NULL ||
            area->x2 < 0 ||
            area->y2 < 0 ||
            area->x1 > (int32_t)vinfo.xres - 1 ||
            area->y1 > (int32_t)vinfo.yres - 1) {
        lv_disp_flush_ready(drv);
        return;
    }

    /*Truncate the area to the screen*/
    int32_t act_x1 = area->x1 < 0 ? 0 : area->x1;
    int32_t act_y1 = area->y1 < 0 ? 0 : area->y1;
    int32_t act_x2 = area->x2 > (int32_t)vinfo.xres - 1 ? (int32_t)vinfo.xres - 1 : area->x2;
    int32_t act_y2 = area->y2 > (int32_t)vinfo.yres - 1 ? (int32_t)vinfo.yres - 1 : area->y2;


    lv_coord_t w = lv_area_get_width(area);
    long int location = 0;
    long int byte_location = 0;
    unsigned char bit_location = 0;

    /*32 or 24 bit per pixel*/
    if(vinfo.bits_per_pixel == 32 || vinfo.bits_per_pixel == 24) {
        uint32_t * fbp32 = (uint32_t *)fbp;
        int32_t y;
        for(y = act_y1; y <= act_y2; y++) {
            location = (act_x1 + vinfo.xoffset) + (y + vinfo.yoffset) * finfo.line_length / 4;
            memcpy(&fbp32[location], (uint32_t *)color_p, (act_x2 - act_x1 + 1) * 4);
            color_p += w;
        }
    }
    /*16 bit per pixel*/
    else if(vinfo.bits_per_pixel == 16) {
        uint16_t * fbp16 = (uint16_t *)fbp;
        int32_t y;
        for(y = act_y1; y <= act_y2; y++) {
            location = (act_x1 + vinfo.xoffset) + (y + vinfo.yoffset) * finfo.line_length / 2;
            memcpy(&fbp16[location], (uint32_t *)color_p, (act_x2 - act_x1 + 1) * 2);
            color_p += w;
        }
    }
    /*8 bit per pixel*/
    else if(vinfo.bits_per_pixel == 8) {
        uint8_t * fbp8 = (uint8_t *)fbp;
        int32_t y;
        for(y = act_y1; y <= act_y2; y++) {
            location = (act_x1 + vinfo.xoffset) + (y + vinfo.yoffset) * finfo.line_length;
            memcpy(&fbp8[location], (uint32_t *)color_p, (act_x2 - act_x1 + 1));
            color_p += w;
        }
    }
    /*1 bit per pixel*/
    else if(vinfo.bits_per_pixel == 1) {
        uint8_t * fbp8 = (uint8_t *)fbp;
        int32_t x;
        int32_t y;
        for(y = act_y1; y <= act_y2; y++) {
            for(x = act_x1; x <= act_x2; x++) {
                location = (x + vinfo.xoffset) + (y + vinfo.yoffset) * vinfo.xres;
                byte_location = location / 8; /* find the byte we need to change */
                bit_location = location % 8; /* inside the byte found, find the bit we need to change */
                fbp8[byte_location] &= ~(((uint8_t)(1)) << bit_location);
                fbp8[byte_location] |= ((uint8_t)(color_p->full)) << bit_location;
                color_p++;
            }

            color_p += area->x2 - act_x2;
        }
    } else {
        /*Not supported bit per pixel*/
    }

    //May be some direct update command is required
    //ret = ioctl(state->fd, FBIO_UPDATE, (unsigned long)((uintptr_t)rect));

    lv_disp_flush_ready(drv);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r14
movq 0x331a3(%rip), %r12     # 0x71bf8
testq %r12, %r12
je 0x3ecdc
movswl 0x4(%rsi), %ecx
testl %ecx, %ecx
js 0x3ecdc
movq %rdx, %rbx
movswl 0x6(%rsi), %edx
testl %edx, %edx
js 0x3ecdc
movswl (%rsi), %edi
movl 0x330cd(%rip), %r8d     # 0x71b50
cmpl %edi, %r8d
jle 0x3ecdc
movswl 0x2(%rsi), %r13d
movl 0x330bc(%rip), %r9d     # 0x71b54
cmpl %r13d, %r9d
jle 0x3ecdc
xorl %eax, %eax
testw %di, %di
movl $0x0, %r11d
cmovgl %edi, %r11d
movl $0xffff, %r10d          # imm = 0xFFFF
andl %r10d, %r11d
testw %r13w, %r13w
cmovlel %eax, %r13d
andl %r10d, %r13d
leal -0x1(%r8), %eax
cmpl %ecx, %r8d
cmovgl %ecx, %eax
leal -0x1(%r9), %r8d
cmpl %edx, %r9d
cmovgl %edx, %r8d
movl %ecx, %edx
subl %edi, %edx
incl %edx
movl 0x33083(%rip), %edi     # 0x71b68
cmpl $0xf, %edi
movl %r11d, 0xc(%rsp)
jle 0x3eb78
cmpl $0x10, %edi
je 0x3ebef
cmpl $0x18, %edi
je 0x3eb0a
cmpl $0x20, %edi
jne 0x3ecdc
cmpl %r13d, %r8d
jl 0x3ecdc
subl %r11d, %eax
leal 0x4(,%rax,4), %eax
cltq
movq %rax, 0x10(%rsp)
movswq %dx, %rbp
addq %rbp, %rbp
incl %r8d
movl 0x3302c(%rip), %eax     # 0x71b60
movl 0x3302a(%rip), %ecx     # 0x71b64
addl %r13d, %ecx
imull 0x32fec(%rip), %ecx     # 0x71b30
addl %r11d, %eax
shrl $0x2, %ecx
addl %eax, %ecx
leaq (%r12,%rcx,4), %rdi
movq %rbx, %rsi
movq 0x10(%rsp), %rdx
movl %r8d, %r15d
callq 0x20f0
movl %r15d, %r8d
movl 0xc(%rsp), %r11d
incl %r13d
addq %rbp, %rbx
cmpl %r13d, %r15d
jne 0x3eb2e
jmp 0x3ecdc
cmpl $0x1, %edi
je 0x3ec5c
cmpl $0x8, %edi
jne 0x3ecdc
cmpl %r13d, %r8d
jl 0x3ecdc
subl %r11d, %eax
incl %eax
cltq
movq %rax, 0x10(%rsp)
movswq %dx, %rbp
addq %rbp, %rbp
incl %r8d
movl 0x32fb1(%rip), %eax     # 0x71b60
addl %r11d, %eax
movl 0x32fac(%rip), %edi     # 0x71b64
addl %r13d, %edi
imull 0x32f6e(%rip), %edi     # 0x71b30
addl %eax, %edi
addq %r12, %rdi
movq %rbx, %rsi
movq 0x10(%rsp), %rdx
movl %r8d, %r15d
callq 0x20f0
movl %r15d, %r8d
movl 0xc(%rsp), %r11d
incl %r13d
addq %rbp, %rbx
cmpl %r13d, %r15d
jne 0x3eba9
jmp 0x3ecdc
cmpl %r13d, %r8d
jl 0x3ecdc
subl %r11d, %eax
leal 0x2(,%rax,2), %eax
cltq
movq %rax, 0x10(%rsp)
movswq %dx, %r15
addq %r15, %r15
incl %r8d
movl 0x32f47(%rip), %eax     # 0x71b60
movl 0x32f45(%rip), %ecx     # 0x71b64
addl %r13d, %ecx
imull 0x32f07(%rip), %ecx     # 0x71b30
addl %r11d, %eax
shrl %ecx
addl %eax, %ecx
leaq (%r12,%rcx,2), %rdi
movq %rbx, %rsi
movq 0x10(%rsp), %rdx
movl %r8d, %ebp
callq 0x20f0
movl %ebp, %r8d
movl 0xc(%rsp), %r11d
incl %r13d
addq %r15, %rbx
cmpl %r13d, %ebp
jne 0x3ec13
jmp 0x3ecdc
cmpl %r13d, %r8d
jl 0x3ecdc
leal 0x1(%rax), %edx
cmpl %r11d, %eax
jl 0x3ecc4
movl %r8d, %ebp
movl %r11d, %edi
movl 0x32eef(%rip), %ecx     # 0x71b64
addl %r13d, %ecx
imull 0x32ed1(%rip), %ecx     # 0x71b50
addl 0x32edb(%rip), %ecx     # 0x71b60
addl %edi, %ecx
movl %ecx, %r8d
andb $0x7, %cl
movl $0x1, %r9d
shll %cl, %r9d
shrl $0x3, %r8d
notb %r9b
andb (%r12,%r8), %r9b
movb %r9b, (%r12,%r8)
movzwl (%rbx), %r10d
shll %cl, %r10d
orb %r9b, %r10b
movb %r10b, (%r12,%r8)
addq $0x2, %rbx
incl %edi
cmpl %edi, %edx
jne 0x3ec6f
movzwl 0x4(%rsi), %ecx
movl %ebp, %r8d
movswl %cx, %edi
subl %eax, %edi
movslq %edi, %rdi
leaq (%rbx,%rdi,2), %rbx
leal 0x1(%r13), %edi
cmpl %r8d, %r13d
movl %edi, %r13d
jne 0x3ec64
movq %r14, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x85af
nop