// File: /workspace/github_c_makefile_star2_O3/OnionIoT[P]lv_example/lvgl/src/lv_draw/lv_draw_triangle.c
// Function: tri_draw_flat
void tri_draw_flat(const lv_point_t * points, const lv_area_t * mask, const lv_style_t * style, lv_opa_t opa)
{
    /*Return if the points are out of the mask*/
    if(points[0].x < mask->x1 && points[1].x < mask->x1 && points[2].x < mask->x1) {
        return;
    }

    if(points[0].x > mask->x2 && points[1].x > mask->x2 && points[2].x > mask->x2) {
        return;
    }

    if(points[0].y < mask->y1 && points[1].y < mask->y1 && points[2].y < mask->y1) {
        return;
    }

    if(points[0].y > mask->y2 && points[1].y > mask->y2 && points[2].y > mask->y2) {
        return;
    }

    lv_point_t tri[3];

    memcpy(tri, points, sizeof(tri));

    /*Sort the vertices according to their y coordinate (0: y max, 1: y mid, 2:y min)*/
    if(tri[1].y < tri[0].y) point_swap(&tri[1], &tri[0]);
    if(tri[2].y < tri[1].y) point_swap(&tri[2], &tri[1]);
    if(tri[1].y < tri[0].y) point_swap(&tri[1], &tri[0]);

    /*Draw the triangle*/
    lv_point_t edge1;
    lv_coord_t dx1  = LV_MATH_ABS(tri[0].x - tri[1].x);
    lv_coord_t sx1  = tri[0].x < tri[1].x ? 1 : -1;
    lv_coord_t dy1  = LV_MATH_ABS(tri[0].y - tri[1].y);
    lv_coord_t sy1  = tri[0].y < tri[1].y ? 1 : -1;
    lv_coord_t err1 = (dx1 > dy1 ? dx1 : -dy1) / 2;
    lv_coord_t err_tmp1;

    lv_point_t edge2;
    lv_coord_t dx2  = LV_MATH_ABS(tri[0].x - tri[2].x);
    lv_coord_t sx2  = tri[0].x < tri[2].x ? 1 : -1;
    lv_coord_t dy2  = LV_MATH_ABS(tri[0].y - tri[2].y);
    lv_coord_t sy2  = tri[0].y < tri[2].y ? 1 : -1;
    lv_coord_t err2 = (dx1 > dy2 ? dx2 : -dy2) / 2;
    lv_coord_t err_tmp2;

    lv_coord_t y1_tmp;
    lv_coord_t y2_tmp;

    edge1.x = tri[0].x;
    edge1.y = tri[0].y;
    edge2.x = tri[0].x;
    edge2.y = tri[0].y;
    lv_area_t act_area;
    lv_area_t draw_area;

    while(1) {
        act_area.x1 = edge1.x;
        act_area.x2 = edge2.x;
        act_area.y1 = edge1.y;
        act_area.y2 = edge2.y;

        /* Get the area of a line.
         * Adjust it a little bit to perfectly match (no redrawn pixels) with the adjacent triangles*/
        draw_area.x1 = LV_MATH_MIN(act_area.x1, act_area.x2) + 1;
        draw_area.x2 = LV_MATH_MAX(act_area.x1, act_area.x2);
        draw_area.y1 = LV_MATH_MIN(act_area.y1, act_area.y2) - 1;
        draw_area.y2 = LV_MATH_MAX(act_area.y1, act_area.y2) - 1;

        lv_draw_fill(&draw_area, mask, style->body.main_color, opa);

        /*Calc. the next point of edge1*/
        y1_tmp = edge1.y;
        do {
            if(edge1.x == tri[1].x && edge1.y == tri[1].y) {

                dx1  = LV_MATH_ABS(tri[1].x - tri[2].x);
                sx1  = tri[1].x < tri[2].x ? 1 : -1;
                dy1  = LV_MATH_ABS(tri[1].y - tri[2].y);
                sy1  = tri[1].y < tri[2].y ? 1 : -1;
                err1 = (dx1 > dy1 ? dx1 : -dy1) / 2;
            } else if(edge1.x == tri[2].x && edge1.y == tri[2].y) {
                return;
            }
            err_tmp1 = err1;
            if(err_tmp1 > -dx1) {
                err1 -= dy1;
                edge1.x += sx1;
            }
            if(err_tmp1 < dy1) {
                err1 += dx1;
                edge1.y += sy1;
            }
        } while(edge1.y == y1_tmp);

        /*Calc. the next point of edge2*/
        y2_tmp = edge2.y;
        do {
            if(edge2.x == tri[2].x && edge2.y == tri[2].y) return;
            err_tmp2 = err2;
            if(err_tmp2 > -dx2) {
                err2 -= dy2;
                edge2.x += sx2;
            }
            if(err_tmp2 < dy2) {
                err2 += dx2;
                edge2.y += sy2;
            }
        } while(edge2.y == y2_tmp);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %ecx, 0x44(%rsp)
movq %rdx, 0x70(%rsp)
movzwl (%rdi), %eax
movzwl (%rsi), %ecx
cmpw %cx, %ax
jge 0x3d6a7
cmpw %cx, 0x4(%rdi)
jge 0x3d6a7
cmpw %cx, 0x8(%rdi)
jl 0x3d6fd
movzwl 0x4(%rsi), %ecx
cmpw %cx, %ax
jle 0x3d6bc
cmpw %cx, 0x4(%rdi)
jle 0x3d6bc
cmpw %cx, 0x8(%rdi)
jg 0x3d6fd
movzwl 0x2(%rdi), %r10d
movzwl 0x2(%rsi), %ecx
cmpw %cx, %r10w
jge 0x3d6d7
cmpw %cx, 0x6(%rdi)
jge 0x3d6d7
cmpw %cx, 0xa(%rdi)
jl 0x3d6fd
movzwl 0x6(%rsi), %edx
cmpw %dx, %r10w
setg %r8b
movzwl 0x6(%rdi), %ecx
cmpw %dx, %cx
setg %r9b
andb %r8b, %r9b
cmpb $0x1, %r9b
jne 0x3d70c
cmpw %dx, 0xa(%rdi)
jle 0x3d70c
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movzwl 0x4(%rdi), %edx
movzwl 0xa(%rdi), %r11d
cmpw %r10w, %cx
jge 0x3d723
movl %r10d, %r8d
movl %eax, %r9d
jmp 0x3d72e
movl %ecx, %r8d
movl %edx, %r9d
movl %r10d, %ecx
movl %eax, %edx
movzwl 0x8(%rdi), %eax
cmpw %r8w, %r11w
jge 0x3d743
movl %r8d, %r10d
movw %r9w, 0xe(%rsp)
jmp 0x3d751
movl %r11d, %r10d
movw %ax, 0xe(%rsp)
movl %r8d, %r11d
movl %r9d, %eax
cmpw %cx, %r11w
movq %rsi, 0x68(%rsp)
jge 0x3d762
movl %ecx, %esi
movl %edx, %edi
jmp 0x3d76c
movl %r11d, %esi
movl %eax, %edi
movl %ecx, %r11d
movl %edx, %eax
movl %r11d, 0x10(%rsp)
movl %eax, 0x14(%rsp)
movw %di, 0x1e(%rsp)
movswl %di, %edx
movswl %ax, %r9d
movl %r9d, %eax
subl %edx, %eax
movl %eax, %edi
negl %edi
cmovsl %eax, %edi
xorl %eax, %eax
cmpw %dx, %r9w
setl %al
movq %rax, 0x20(%rsp)
movw %si, 0x1c(%rsp)
movswl %si, %esi
movswl 0x10(%rsp), %r11d
movl %r11d, %ecx
subl %esi, %ecx
movl %ecx, %r12d
negl %r12d
cmovsl %ecx, %r12d
xorl %eax, %eax
cmpw %si, %r11w
setl %al
movq %rax, 0x38(%rsp)
movl %edi, 0x2c(%rsp)
movswl %di, %ebx
movswl %r12w, %edi
movl %edi, %r8d
negl %r8d
cmpl %edi, %ebx
cmovgl %ebx, %r8d
movl %r8d, %r14d
shrl $0x1f, %r14d
addl %r8d, %r14d
shrl %r14d
xorl %r13d, %r13d
movzwl 0xe(%rsp), %r15d
cmpw %r15w, %r9w
setl %r13b
xorl %edi, %edi
cmpw %r10w, %r11w
setl %dil
movw %r10w, 0x1a(%rsp)
movswl %r10w, %r10d
subl %r10d, %r11d
movl %r11d, %ebp
negl %ebp
cmovsl %r11d, %ebp
movswl %bp, %eax
movl %eax, %r11d
negl %r11d
movswl %r15w, %ebp
subl %ebp, %r9d
movl %r9d, %r15d
negl %r15d
cmovsl %r9d, %r15d
movl %eax, 0x54(%rsp)
cmpl %eax, %ebx
movswl %r15w, %r8d
cmovgl %r8d, %r11d
movl %r11d, %r15d
shrl $0x1f, %r15d
addl %r11d, %r15d
shrl %r15d
movl %edx, %r9d
subl %ebp, %r9d
movl %r9d, %eax
negl %eax
cmovsl %r9d, %eax
xorl %r9d, %r9d
cmpw %bp, %dx
setl %r9b
movl %esi, %edx
subl %r10d, %edx
movl %edx, %r11d
negl %r11d
cmovsl %edx, %r11d
xorl %edx, %edx
cmpw %r10w, %si
setl %dl
movl %r11d, 0x60(%rsp)
movswl %r11w, %esi
movl %esi, %r10d
negl %r10d
movl %eax, 0x64(%rsp)
movswl %ax, %r11d
cmpl %esi, %r11d
cmovgl %r11d, %r10d
movl 0x10(%rsp), %ecx
movl %r10d, %r11d
shrl $0x1f, %r11d
addl %r10d, %r11d
movq 0x20(%rsp), %rax
leal -0x1(,%rax,2), %ebp
movq 0x38(%rsp), %rax
leal -0x1(,%rax,2), %ebx
leal -0x1(%r13,%r13), %eax
movl %eax, 0x4c(%rsp)
movl 0x14(%rsp), %esi
leal -0x1(%rdi,%rdi), %eax
leal -0x1(%r9,%r9), %edi
movl %edi, 0x5c(%rsp)
movl %ecx, %r9d
leal -0x1(,%rdx,2), %edx
movl %edx, 0x58(%rsp)
shrl %r11d
movl %r11d, 0x38(%rsp)
movl %r8d, 0x50(%rsp)
negl %r8d
movl %r8d, 0x48(%rsp)
cwtl
movl %eax, 0x40(%rsp)
leaq 0x30(%rsp), %rdi
movl %esi, %r13d
movl %r9d, %r8d
movl %r9d, 0x10(%rsp)
movswl %r13w, %eax
movl %esi, 0x14(%rsp)
movswl %si, %ecx
cmpl %ecx, %eax
movl %ecx, %edx
cmovll %eax, %edx
cmovgl %eax, %ecx
incl %edx
movw %dx, 0x30(%rsp)
movw %cx, 0x34(%rsp)
movl %r8d, 0x20(%rsp)
movswl %r8w, %eax
movswl %r9w, %ecx
cmpl %ecx, %eax
movl %ecx, %edx
cmovll %eax, %edx
cmovgl %eax, %ecx
decl %edx
movw %dx, 0x32(%rsp)
decl %ecx
movw %cx, 0x36(%rsp)
movq 0x70(%rsp), %rax
movzwl 0x2(%rax), %edx
movzbl 0x44(%rsp), %ecx
movq 0x68(%rsp), %rsi
callq 0x35793
movl %ebx, %ecx
movl %r12d, %edx
movl %ebp, %esi
movl 0x2c(%rsp), %eax
movl %eax, %edi
movzwl 0x1a(%rsp), %r9d
movzwl 0xe(%rsp), %r10d
cmpw 0x1e(%rsp), %r13w
jne 0x3d9b9
movl 0x38(%rsp), %eax
movl 0x58(%rsp), %ebx
movl 0x60(%rsp), %r12d
movl 0x5c(%rsp), %ebp
movl 0x64(%rsp), %r11d
movl 0x20(%rsp), %r8d
cmpw 0x1c(%rsp), %r8w
je 0x3d9d8
movl %r14d, %eax
movl %ecx, %ebx
movl %edx, %r12d
movl %esi, %ebp
movl %edi, %r11d
cmpw %r10w, %r13w
jne 0x3d9d8
cmpw %r9w, 0x20(%rsp)
je 0x3d6fd
movswl %ax, %r8d
movl %r11d, %esi
movswl %si, %r14d
movl %r14d, %ecx
negl %ecx
cmpl %ecx, %r8d
movl $0x0, %ecx
cmovgl %r12d, %ecx
movl %ebp, %edx
movl $0x0, %edi
cmovlel %edi, %edx
subl %ecx, %eax
addl %edx, %r13d
cmpw %r12w, %r8w
cmovgel %edi, %r14d
addl %eax, %r14d
movl %r11d, %eax
movl %ebx, %ecx
movl %r12d, %edx
movl %ebp, %esi
movl %eax, %edi
cmpw %r12w, %r8w
jge 0x3d98e
movl %ebx, %ecx
movl %r12d, %edx
movl %ebp, %esi
movl %eax, %edi
testw %bx, %bx
je 0x3d98e
addl %ebx, 0x20(%rsp)
movl 0x14(%rsp), %esi
movl %esi, %edi
cmpw %r10w, %si
jne 0x3da52
cmpw %r9w, 0x10(%rsp)
je 0x3d6fd
movswl %r15w, %eax
cmpl 0x48(%rsp), %eax
movl $0x0, %ecx
cmovgl 0x4c(%rsp), %ecx
movl $0x0, %edx
movl 0x54(%rsp), %r8d
cmovgl %r8d, %edx
movl %edi, %esi
addl %ecx, %esi
subl %edx, %r15d
cmpl %eax, %r8d
movl $0x0, %ecx
cmovgl 0x50(%rsp), %ecx
addl %r15d, %ecx
movl %ecx, %r15d
cmpl %eax, %r8d
jle 0x3da3e
movl 0x10(%rsp), %r9d
addl 0x40(%rsp), %r9d
movl %r11d, 0x2c(%rsp)
leaq 0x30(%rsp), %rdi
movl 0x20(%rsp), %r8d
jmp 0x3d912