// File: /workspace/github_c_makefile_star2_O1/OnionIoT[P]lv_example/lvgl/src/lv_draw/lv_draw_basic.c
// Function: lv_draw_map
void lv_draw_map(const lv_area_t * cords_p, const lv_area_t * mask_p, const uint8_t * map_p, lv_opa_t opa,
                 bool chroma_key, bool alpha_byte, lv_color_t recolor, lv_opa_t recolor_opa)
{

    if(opa < LV_OPA_MIN) return;
    if(opa > LV_OPA_MAX) opa = LV_OPA_COVER;

    lv_area_t masked_a;
    bool union_ok;

    /*Get the union of map size and mask*/
    /* The mask is already truncated to the vdb size
     * in 'lv_refr_area_with_vdb' function */
    union_ok = lv_area_intersect(&masked_a, cords_p, mask_p);

    /*If there are common part of the three area then draw to the vdb*/
    if(union_ok == false) return;

    /*The pixel size in byte is different if an alpha byte is added too*/
    uint8_t px_size_byte = alpha_byte ? LV_IMG_PX_SIZE_ALPHA_BYTE : sizeof(lv_color_t);

    /*If the map starts OUT of the masked area then calc. the first pixel*/
    lv_coord_t map_width = lv_area_get_width(cords_p);
    if(cords_p->y1 < masked_a.y1) {
        map_p += (uint32_t)map_width * ((masked_a.y1 - cords_p->y1)) * px_size_byte;
    }
    if(cords_p->x1 < masked_a.x1) {
        map_p += (masked_a.x1 - cords_p->x1) * px_size_byte;
    }

    lv_disp_t * disp    = lv_refr_get_disp_refreshing();
    lv_disp_buf_t * vdb = lv_disp_get_buf(disp);

    /*Stores coordinates relative to the current VDB*/
    masked_a.x1 = masked_a.x1 - vdb->area.x1;
    masked_a.y1 = masked_a.y1 - vdb->area.y1;
    masked_a.x2 = masked_a.x2 - vdb->area.x1;
    masked_a.y2 = masked_a.y2 - vdb->area.y1;

    lv_coord_t vdb_width     = lv_area_get_width(&vdb->area);
    lv_color_t * vdb_buf_tmp = vdb->buf_act;
    vdb_buf_tmp += (uint32_t)vdb_width * masked_a.y1; /*Move to the first row*/
    vdb_buf_tmp += (uint32_t)masked_a.x1;             /*Move to the first col*/

    lv_coord_t row;
    lv_coord_t map_useful_w = lv_area_get_width(&masked_a);

    bool scr_transp = false;
#if LV_COLOR_DEPTH == 32 && LV_COLOR_SCREEN_TRANSP
    scr_transp = disp->driver.screen_transp;
#endif

    /*The simplest case just copy the pixels into the VDB*/
    if(chroma_key == false && alpha_byte == false && opa == LV_OPA_COVER && recolor_opa == LV_OPA_TRANSP) {

        /*Use the custom VDB write function is exists*/
        if(disp->driver.set_px_cb) {
            lv_coord_t col;
            for(row = masked_a.y1; row <= masked_a.y2; row++) {
                for(col = 0; col < map_useful_w; col++) {
                    lv_color_t px_color = *((lv_color_t *)&map_p[(uint32_t)col * px_size_byte]);
                    disp->driver.set_px_cb(&disp->driver, (uint8_t *)vdb->buf_act, vdb_width, col + masked_a.x1, row,
                                           px_color, opa);
                }
                map_p += map_width * px_size_byte; /*Next row on the map*/
            }
        }
        /*Normal native VDB*/
        else {
            for(row = masked_a.y1; row <= masked_a.y2; row++) {
#if LV_USE_GPU
                if(disp->driver.gpu_blend_cb == false) {
                    sw_mem_blend(vdb_buf_tmp, (lv_color_t *)map_p, map_useful_w, opa);
                } else {
                    disp->driver.gpu_blend_cb(&disp->driver, vdb_buf_tmp, (lv_color_t *)map_p, map_useful_w, opa);
                }
#else
                sw_mem_blend(vdb_buf_tmp, (lv_color_t *)map_p, map_useful_w, opa);
#endif
                map_p += map_width * px_size_byte; /*Next row on the map*/
                vdb_buf_tmp += vdb_width;          /*Next row on the VDB*/
            }
        }
    }

    /*In the other cases every pixel need to be checked one-by-one*/
    else {

        lv_coord_t col;
        lv_color_t last_img_px  = LV_COLOR_BLACK;
        lv_color_t recolored_px = lv_color_mix(recolor, last_img_px, recolor_opa);
        for(row = masked_a.y1; row <= masked_a.y2; row++) {
            for(col = 0; col < map_useful_w; col++) {
                lv_opa_t opa_result  = opa;
                uint8_t * px_color_p = (uint8_t *)&map_p[(uint32_t)col * px_size_byte];
                lv_color_t px_color;

                /*Calculate with the pixel level alpha*/
                if(alpha_byte) {
#if LV_COLOR_DEPTH == 8 || LV_COLOR_DEPTH == 1
                    px_color.full = px_color_p[0];
#elif LV_COLOR_DEPTH == 16
                    /*Because of Alpha byte 16 bit color can start on odd address which can cause
                     * crash*/
                    px_color.full = px_color_p[0] + (px_color_p[1] << 8);
#elif LV_COLOR_DEPTH == 32
                    px_color = *((lv_color_t *)px_color_p);
#endif
                    lv_opa_t px_opa = *(px_color_p + LV_IMG_PX_SIZE_ALPHA_BYTE - 1);
                    if(px_opa == LV_OPA_TRANSP)
                        continue;
                    else if(px_opa != LV_OPA_COVER)
                        opa_result = (uint32_t)((uint32_t)px_opa * opa_result) >> 8;
                } else {
                    px_color = *((lv_color_t *)px_color_p);
                }

                /*Handle chroma key*/
                if(chroma_key && px_color.full == disp->driver.color_chroma_key.full) continue;

                /*Re-color the pixel if required*/
                if(recolor_opa != LV_OPA_TRANSP) {
                    if(last_img_px.full != px_color.full) { /*Minor acceleration: calculate only for
                                                               new colors (save the last)*/
                        last_img_px  = px_color;
                        recolored_px = lv_color_mix(recolor, last_img_px, recolor_opa);
                    }
                    /*Handle custom VDB write is present*/
                    if(disp->driver.set_px_cb) {
                        disp->driver.set_px_cb(&disp->driver, (uint8_t *)vdb->buf_act, vdb_width, col + masked_a.x1,
                                               row, recolored_px, opa_result);
                    }
                    /*Normal native VDB write*/
                    else {
                        if(opa_result == LV_OPA_COVER)
                            vdb_buf_tmp[col].full = recolored_px.full;
                        else
                            vdb_buf_tmp[col] = lv_color_mix(recolored_px, vdb_buf_tmp[col], opa_result);
                    }
                } else {
                    /*Handle custom VDB write is present*/
                    if(disp->driver.set_px_cb) {
                        disp->driver.set_px_cb(&disp->driver, (uint8_t *)vdb->buf_act, vdb_width, col + masked_a.x1,
                                               row, px_color, opa_result);
                    }
                    /*Normal native VDB write*/
                    else {

                        if(opa_result == LV_OPA_COVER)
                            vdb_buf_tmp[col] = px_color;
                        else {
                            if(scr_transp == false) {
                                vdb_buf_tmp[col] = lv_color_mix(px_color, vdb_buf_tmp[col], opa_result);
                            } else {
#if LV_COLOR_DEPTH == 32 && LV_COLOR_SCREEN_TRANSP
                                vdb_buf_tmp[col] = color_mix_2_alpha(vdb_buf_tmp[col], vdb_buf_tmp[col].ch.alpha,
                                                                     px_color, opa_result);
#endif
                            }
                        }
                    }
                }
            }

            map_p += map_width * px_size_byte; /*Next row on the map*/
            vdb_buf_tmp += vdb_width;          /*Next row on the VDB*/
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r9, 0x30(%rsp)
movl %r8d, 0x20(%rsp)
movl %ecx, %r14d
cmpb $0x10, %r14b
jb 0x36165
movq %rdx, %r13
movq %rsi, %rdx
movq %rdi, %r15
cmpb $-0x4, %r14b
movzbl %r14b, %eax
movl $0xff, %ecx
cmovbl %eax, %ecx
movl %ecx, 0x40(%rsp)
leaq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x2865e
testb %al, %al
je 0x36165
movq 0x30(%rsp), %rax
leal 0x2(%rax), %esi
movzwl 0x4(%r15), %ebp
movswq (%r15), %rax
subl %eax, %ebp
incl %ebp
movswq 0x2(%r15), %rdx
movswq 0x1a(%rsp), %rcx
movzbl %sil, %esi
cmpw %cx, %dx
jge 0x35d4f
subl %edx, %ecx
movswl %bp, %edx
imull %esi, %ecx
imull %edx, %ecx
addq %rcx, %r13
movswq 0x18(%rsp), %rcx
movq %rcx, %rdx
subq %rax, %rdx
movq %rsi, 0x28(%rsp)
imulq %rsi, %rdx
xorl %ebx, %ebx
cmpw %cx, %ax
cmovlq %rdx, %rbx
addq %r13, %rbx
callq 0x72ad
movq %rax, %r13
movq %rax, %rdi
callq 0x8660
movq %rax, 0x60(%rsp)
movzwl 0x18(%rsp), %r10d
movl 0x1c(%rax), %edx
movl %r10d, %r12d
subl %edx, %r12d
movw %r12w, 0x18(%rsp)
movzwl 0x1a(%rsp), %edi
movzwl 0x1e(%rax), %esi
subl %esi, %edi
movw %di, 0x1a(%rsp)
movzwl 0x1c(%rsp), %ecx
movl %ecx, %r8d
subl %edx, %r8d
movw %r8w, 0x1c(%rsp)
subw %si, 0x1e(%rsp)
movzwl 0x20(%rax), %esi
subl %edx, %esi
incl %esi
movq 0x10(%rax), %r9
movswq %si, %r11
movl %edi, 0x14(%rsp)
movswl %di, %edi
movswl %si, %edx
imull %edi, %edx
movswq %r12w, %rsi
movl %esi, %esi
incl %r8d
movl %r8d, %eax
subl %r12d, %eax
movl %eax, 0x3c(%rsp)
cmpb $0x0, 0x20(%rsp)
movq %r11, 0x58(%rsp)
je 0x36177
movl 0x14(%rsp), %edi
cmpw 0x1e(%rsp), %di
jg 0x36165
movzwl 0xd0(%rsp), %eax
movzwl %ax, %r14d
movl %r14d, %edi
shrl $0xb, %edi
movl %ebp, %r15d
movzbl 0xd8(%rsp), %ebp
imull %ebp, %edi
shrl $0x5, %r14d
andl $0x3f, %r14d
imull %ebp, %r14d
andl $0x1f, %eax
imull %ebp, %eax
movl %edi, 0x78(%rsp)
andl $0x1f00, %edi           # imm = 0x1F00
movl %eax, 0x80(%rsp)
movl %eax, %r8d
shrl $0x8, %r8d
leal (%r8,%rdi,8), %edi
movq 0x28(%rsp), %r8
movl %r14d, 0x7c(%rsp)
shrl $0x3, %r14d
andl $-0x20, %r14d
orl %edi, %r14d
movzbl 0x40(%rsp), %edi
xorl $0xff, %ebp
movl %ebp, 0x74(%rsp)
movswq %r15w, %rax
imulq %r8, %rax
movq %rax, 0x90(%rsp)
subl %r10d, %ecx
incl %ecx
movzwl %cx, %r15d
addq %rsi, %rsi
leaq (%rsi,%rdx,2), %rax
addq %rax, %r9
leaq (%r11,%r11), %rax
movq %rax, 0x88(%rsp)
movq $0x0, 0x48(%rsp)
movl %edi, 0x68(%rsp)
movzwl %di, %eax
movl %eax, 0x84(%rsp)
movq %r9, 0x50(%rsp)
cmpw $0x0, 0x3c(%rsp)
jle 0x3613b
movq %rbx, %rbp
xorl %r12d, %r12d
cmpb $0x0, 0x30(%rsp)
je 0x35f11
movzbl 0x2(%rbp), %ecx
movl %ecx, %eax
imull 0x84(%rsp), %eax
shrl $0x8, %eax
leal 0x1(%rcx), %edx
cmpb $0x2, %dl
cmovbl 0x68(%rsp), %eax
testw %cx, %cx
je 0x3600b
movzwl (%rbp), %r9d
jmp 0x35f1a
movzwl (%rbp), %r9d
movl 0x40(%rsp), %eax
cmpb $0x0, 0x20(%rsp)
je 0x35f2c
cmpw 0x38(%r13), %r9w
je 0x3600b
cmpb $0x0, 0xd8(%rsp)
je 0x35fcb
cmpw %r9w, 0x48(%rsp)
je 0x35f97
movzwl %r9w, %ecx
movl %ecx, %edx
shrl $0xb, %edx
movl 0x74(%rsp), %edi
imull %edi, %edx
addl 0x78(%rsp), %edx
andl $0xffffff00, %edx       # imm = 0xFFFFFF00
shrl $0x5, %ecx
andl $0x3f, %ecx
imull %edi, %ecx
addl 0x7c(%rsp), %ecx
shrl $0x3, %ecx
andl $0x7e0, %ecx            # imm = 0x7E0
movl %r9d, %esi
andl $0x1f, %esi
imull %edi, %esi
addl 0x80(%rsp), %esi
shrl $0x8, %esi
andl $0x1f, %esi
leal (%rsi,%rdx,8), %r14d
orl %ecx, %r14d
movl %r9d, %ecx
movq %rcx, 0x48(%rsp)
movq 0x28(%r13), %r10
testq %r10, %r10
je 0x3601f
movq 0x60(%rsp), %rcx
movq 0x10(%rcx), %rsi
movzwl 0x18(%rsp), %ecx
addl %r12d, %ecx
movzbl %al, %eax
movl %eax, (%rsp)
movswl %cx, %ecx
movswl 0x14(%rsp), %r8d
movq %r13, %rdi
movl %r11d, %edx
movl %r14d, %r9d
jmp 0x35ffe
movq 0x28(%r13), %r10
testq %r10, %r10
je 0x360a2
movq 0x60(%rsp), %rcx
movq 0x10(%rcx), %rsi
movzwl 0x18(%rsp), %ecx
addl %r12d, %ecx
movzbl %al, %eax
movl %eax, (%rsp)
movswl %cx, %ecx
movswl 0x14(%rsp), %r8d
movq %r13, %rdi
movl %r11d, %edx
callq *%r10
movq 0x28(%rsp), %r8
movq 0x58(%rsp), %r11
incq %r12
addq %r8, %rbp
cmpq %r12, %r15
jne 0x35ede
jmp 0x3613b
cmpb $-0x1, %al
je 0x3611d
movq 0x50(%rsp), %r9
movzwl (%r9,%r12,2), %ecx
movzwl %r14w, %edx
movl %edx, %esi
shrl $0xb, %esi
movzbl %al, %edi
imull %edi, %esi
movl %ecx, %eax
shrl $0xb, %eax
shrl $0x5, %edx
andl $0x3f, %edx
imull %edi, %edx
movl %r14d, %r8d
andl $0x1f, %r8d
imull %edi, %r8d
xorl $0xff, %edi
imull %edi, %eax
addl %esi, %eax
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
movl %ecx, %esi
shrl $0x5, %esi
andl $0x3f, %esi
imull %edi, %esi
addl %edx, %esi
shrl $0x3, %esi
andl $0x7e0, %esi            # imm = 0x7E0
andl $0x1f, %ecx
imull %edi, %ecx
addl %r8d, %ecx
movq 0x28(%rsp), %r8
shrl $0x8, %ecx
andl $0x1f, %ecx
leal (%rcx,%rax,8), %eax
orl %esi, %eax
movw %ax, (%r9,%r12,2)
jmp 0x3600b
cmpb $-0x1, %al
je 0x3612c
movq 0x50(%rsp), %r10
movzwl (%r10,%r12,2), %ecx
movzwl %r9w, %edx
movl %edx, %esi
shrl $0xb, %esi
movzbl %al, %edi
imull %edi, %esi
movl %ecx, %eax
shrl $0xb, %eax
shrl $0x5, %edx
andl $0x3f, %edx
imull %edi, %edx
andl $0x1f, %r9d
imull %edi, %r9d
xorl $0xff, %edi
imull %edi, %eax
addl %esi, %eax
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
movl %ecx, %esi
shrl $0x5, %esi
andl $0x3f, %esi
imull %edi, %esi
addl %edx, %esi
shrl $0x3, %esi
andl $0x7e0, %esi            # imm = 0x7E0
andl $0x1f, %ecx
imull %edi, %ecx
addl %r9d, %ecx
shrl $0x8, %ecx
andl $0x1f, %ecx
leal (%rcx,%rax,8), %eax
orl %esi, %eax
movw %ax, (%r10,%r12,2)
jmp 0x3600b
movq 0x50(%rsp), %rax
movw %r14w, (%rax,%r12,2)
jmp 0x3600b
movq 0x50(%rsp), %rax
movw %r9w, (%rax,%r12,2)
jmp 0x3600b
addq 0x90(%rsp), %rbx
movl 0x14(%rsp), %eax
incl %eax
movq 0x50(%rsp), %r9
addq 0x88(%rsp), %r9
movl %eax, 0x14(%rsp)
cmpw 0x1e(%rsp), %ax
jle 0x35ec7
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $-0x4, %r14b
jb 0x35e00
cmpb $0x0, 0xd8(%rsp)
jne 0x35e00
cmpb $0x0, 0x30(%rsp)
jne 0x35e00
movq %rbx, 0x20(%rsp)
cmpq $0x0, 0x28(%r13)
movzwl 0x1e(%rsp), %edi
je 0x3624b
cmpw %di, 0x14(%rsp)
movq 0x28(%rsp), %rdx
jg 0x36165
movswq %bp, %rax
imulq %rdx, %rax
movq %rax, 0x48(%rsp)
subl %r10d, %ecx
incl %ecx
movzwl %cx, %r12d
movzbl 0x40(%rsp), %r15d
movq 0x60(%rsp), %rbp
cmpw $0x0, 0x3c(%rsp)
jle 0x3622b
movswl 0x14(%rsp), %esi
movl %esi, 0x30(%rsp)
movq 0x20(%rsp), %r14
xorl %ebx, %ebx
movq 0x10(%rbp), %rsi
movzwl 0x18(%rsp), %eax
addl %ebx, %eax
movzwl (%r14), %r9d
movl %r15d, (%rsp)
movswl %ax, %ecx
movq %r13, %rdi
movl %r11d, %edx
movl 0x30(%rsp), %r8d
callq *0x28(%r13)
movq 0x58(%rsp), %r11
incq %rbx
addq 0x28(%rsp), %r14
cmpq %rbx, %r12
jne 0x361f4
movq 0x48(%rsp), %rax
addq %rax, 0x20(%rsp)
movl 0x14(%rsp), %eax
incl %eax
movl %eax, 0x14(%rsp)
cmpw 0x1e(%rsp), %ax
jle 0x361dc
jmp 0x36165
movl %r8d, 0x58(%rsp)
cmpw %di, 0x14(%rsp)
movq 0x28(%rsp), %rdi
jg 0x36165
movswl 0x3c(%rsp), %r8d
movl %ebp, %eax
movzbl 0x40(%rsp), %ebp
movl %ebp, %r13d
xorl $0xff, %r13d
addq %r8, %r8
movq %r8, 0x40(%rsp)
movswq %ax, %rax
imulq %rdi, %rax
movq %rax, 0x30(%rsp)
subl %r10d, %ecx
incl %ecx
movswl %cx, %ebx
addq %rsi, %rsi
leaq (%rsi,%rdx,2), %rax
addq %rax, %r9
addq %r11, %r11
movl %r14d, 0x48(%rsp)
movq %r12, 0x68(%rsp)
cmpb $-0x5, %r14b
jbe 0x362e1
movq %r9, %rdi
movq 0x20(%rsp), %r15
movq %r15, %rsi
movq 0x40(%rsp), %rdx
movq %r9, %r14
movq %r11, %r12
callq 0x20f0
movq %r12, %r11
movq 0x68(%rsp), %r12
movq %r14, %r9
movl 0x48(%rsp), %r14d
jmp 0x36363
cmpw %r12w, 0x58(%rsp)
movq 0x20(%rsp), %r15
je 0x36363
xorl %eax, %eax
movzwl (%r15,%rax,2), %esi
movzwl (%r9,%rax,2), %ecx
movl %esi, %edi
shrl $0xb, %edi
imull %ebp, %edi
movl %ecx, %edx
shrl $0xb, %edx
imull %r13d, %edx
addl %edi, %edx
andl $0x3f00, %edx           # imm = 0x3F00
movl %esi, %edi
shrl $0x5, %edi
andl $0x3f, %edi
imull %ebp, %edi
movl %ecx, %r8d
shrl $0x5, %r8d
andl $0x3f, %r8d
imull %r13d, %r8d
addl %edi, %r8d
shrl $0x3, %r8d
andl $0x7e0, %r8d            # imm = 0x7E0
andl $0x1f, %esi
imull %ebp, %esi
andl $0x1f, %ecx
imull %r13d, %ecx
addl %esi, %ecx
shrl $0x8, %ecx
andl $0x1f, %ecx
leal (%rcx,%rdx,8), %ecx
orl %r8d, %ecx
movw %cx, (%r9,%rax,2)
incq %rax
cmpq %rax, %rbx
jne 0x362f0
addq 0x30(%rsp), %r15
movq %r15, 0x20(%rsp)
addq %r11, %r9
movl 0x14(%rsp), %eax
incl %eax
movl %eax, 0x14(%rsp)
cmpw 0x1e(%rsp), %ax
jle 0x362ab
jmp 0x36165
nop