// File: /workspace/github_c_makefile_star2_O2/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 $0x68, %rsp
movl %ecx, 0x30(%rsp)
movq %rdx, 0x60(%rsp)
movzwl (%rdi), %eax
movzwl (%rsi), %ecx
cmpw %cx, %ax
jge 0x39788
cmpw %cx, 0x4(%rdi)
jge 0x39788
cmpw %cx, 0x8(%rdi)
jl 0x397de
movzwl 0x4(%rsi), %ecx
cmpw %cx, %ax
jle 0x3979d
cmpw %cx, 0x4(%rdi)
jle 0x3979d
cmpw %cx, 0x8(%rdi)
jg 0x397de
movzwl 0x2(%rdi), %r10d
movzwl 0x2(%rsi), %ecx
cmpw %cx, %r10w
jge 0x397b8
cmpw %cx, 0x6(%rdi)
jge 0x397b8
cmpw %cx, 0xa(%rdi)
jl 0x397de
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 0x397ed
cmpw %dx, 0xa(%rdi)
jle 0x397ed
addq $0x68, %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 0x39804
movl %r10d, %r8d
movl %eax, %r9d
jmp 0x3980f
movl %ecx, %r8d
movl %edx, %r9d
movl %r10d, %ecx
movl %eax, %edx
movzwl 0x8(%rdi), %eax
cmpw %r8w, %r11w
jge 0x39824
movl %r8d, %edi
movw %r9w, 0xc(%rsp)
jmp 0x39832
movl %r11d, %edi
movw %ax, 0xc(%rsp)
movl %r8d, %r11d
movl %r9d, %eax
movw %di, 0xe(%rsp)
cmpw %cx, %r11w
movq %rsi, 0x58(%rsp)
jge 0x3984b
movl %ecx, %esi
movl %edx, %edi
movl %r11d, %r10d
jmp 0x39855
movl %r11d, %esi
movl %eax, %edi
movl %ecx, %r10d
movl %edx, %eax
movl %r10d, 0x10(%rsp)
movl %eax, 0x18(%rsp)
movw %di, 0x1e(%rsp)
movswl %di, %edi
movswl %ax, %r11d
movl %r11d, %eax
subl %edi, %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
xorl %r9d, %r9d
cmpw %di, %r11w
setl %r9b
movw %si, 0x1c(%rsp)
movswl %si, %r8d
movswl %r10w, %r12d
movl %r12d, %eax
subl %r8d, %eax
movl %eax, %r15d
negl %r15d
cmovsl %eax, %r15d
xorl %esi, %esi
cmpw %r8w, %r12w
setl %sil
movl %ecx, 0x20(%rsp)
movswl %cx, %r13d
movswl %r15w, %edx
movl %edx, %eax
negl %eax
cmpl %edx, %r13d
cmovgl %r13d, %eax
pushq $0x2
popq %rcx
cltd
idivl %ecx
movl %eax, %ebp
xorl %r10d, %r10d
movzwl 0xc(%rsp), %ecx
cmpw %cx, %r11w
setl %r10b
xorl %ebx, %ebx
movzwl 0xe(%rsp), %eax
cmpw %ax, %r12w
setl %bl
movswl %ax, %r14d
subl %r14d, %r12d
movl %r12d, %eax
negl %eax
cmovsl %r12d, %eax
movswl %ax, %r12d
movl %r12d, %eax
negl %eax
movswl %cx, %ecx
subl %ecx, %r11d
movl %r11d, %edx
negl %edx
cmovsl %r11d, %edx
movl %r12d, 0x40(%rsp)
cmpl %r12d, %r13d
movswl %dx, %r11d
cmovgl %r11d, %eax
cltd
pushq $0x2
popq %r12
idivl %r12d
movl %eax, %r12d
movl %edi, %eax
subl %ecx, %eax
movl %eax, %r13d
negl %r13d
cmovsl %eax, %r13d
xorl %edx, %edx
cmpw %cx, %di
setl %dl
movl %r8d, %eax
subl %r14d, %eax
movl %eax, %edi
negl %edi
cmovsl %eax, %edi
xorl %ecx, %ecx
cmpw %r14w, %r8w
setl %cl
movl %edi, 0x50(%rsp)
movswl %di, %edi
movl %edi, %eax
negl %eax
movl %r13d, 0x54(%rsp)
movswl %r13w, %r8d
cmpl %edi, %r8d
cmovgl %r8d, %eax
leal -0x1(%r9,%r9), %r14d
leal -0x1(%rsi,%rsi), %r13d
leal -0x1(%r10,%r10), %esi
movl %esi, 0x38(%rsp)
movl 0x18(%rsp), %esi
leal -0x1(%rbx,%rbx), %edi
leal -0x1(%rdx,%rdx), %edx
movl %edx, 0x4c(%rsp)
leal -0x1(%rcx,%rcx), %ecx
movl %ecx, 0x48(%rsp)
cltd
pushq $0x2
popq %rcx
idivl %ecx
movl %eax, 0x44(%rsp)
movl %r11d, 0x3c(%rsp)
negl %r11d
movl %r11d, 0x34(%rsp)
movswl %di, %eax
movl %eax, 0x2c(%rsp)
leaq 0x24(%rsp), %rdi
movl %esi, %ebx
movl 0x10(%rsp), %r10d
movl %r10d, %r8d
movl %r10d, 0x10(%rsp)
movswl %bx, %eax
movl %esi, 0x18(%rsp)
movswl %si, %ecx
cmpl %ecx, %eax
movl %ecx, %edx
cmovll %eax, %edx
cmovgl %eax, %ecx
incl %edx
movw %dx, 0x24(%rsp)
movw %cx, 0x28(%rsp)
movl %r8d, 0x14(%rsp)
movswl %r8w, %eax
movswl %r10w, %ecx
cmpl %ecx, %eax
movl %ecx, %edx
cmovll %eax, %edx
cmovgl %eax, %ecx
decl %edx
movw %dx, 0x26(%rsp)
decl %ecx
movw %cx, 0x2a(%rsp)
movq 0x60(%rsp), %rax
movzwl 0x2(%rax), %edx
movzbl 0x30(%rsp), %ecx
movq 0x58(%rsp), %rsi
callq 0x32757
movl %r13d, %ecx
movl %r15d, %edx
movl %r14d, %esi
movl 0x20(%rsp), %eax
movl %eax, %edi
movzwl 0xe(%rsp), %r10d
movzwl 0xc(%rsp), %r11d
cmpw 0x1e(%rsp), %bx
jne 0x39a70
movl 0x44(%rsp), %eax
movl 0x48(%rsp), %r13d
movl 0x50(%rsp), %r15d
movl 0x4c(%rsp), %r14d
movl 0x54(%rsp), %r9d
movl 0x14(%rsp), %r8d
cmpw 0x1c(%rsp), %r8w
je 0x39a90
movl %ebp, %eax
movl %ecx, %r13d
movl %edx, %r15d
movl %esi, %r14d
movl %edi, %r9d
cmpw %r11w, %bx
jne 0x39a90
cmpw %r10w, 0x14(%rsp)
je 0x397de
movswl %ax, %r8d
movl %r9d, %esi
movswl %si, %ebp
movl %ebp, %ecx
negl %ecx
cmpl %ecx, %r8d
movl $0x0, %ecx
cmovgl %r15d, %ecx
movl %r14d, %edx
movl $0x0, %edi
cmovlel %edi, %edx
subl %ecx, %eax
addl %edx, %ebx
cmpw %r15w, %r8w
cmovgel %edi, %ebp
addl %eax, %ebp
movl %r9d, %eax
movl %r13d, %ecx
movl %r15d, %edx
movl %r14d, %esi
movl %eax, %edi
cmpw %r15w, %r8w
jge 0x39a44
movl %r13d, %ecx
movl %r15d, %edx
movl %r14d, %esi
movl %eax, %edi
testw %r13w, %r13w
je 0x39a44
addl %r13d, 0x14(%rsp)
movl 0x18(%rsp), %esi
movl %esi, %edi
cmpw %r11w, %si
jne 0x39b0c
cmpw %r10w, 0x10(%rsp)
je 0x397de
movswl %r12w, %eax
cmpl 0x34(%rsp), %eax
movl $0x0, %ecx
cmovgl 0x38(%rsp), %ecx
movl $0x0, %edx
movl 0x40(%rsp), %r8d
cmovgl %r8d, %edx
movl %edi, %esi
addl %ecx, %esi
subl %edx, %r12d
cmpl %eax, %r8d
movl $0x0, %ecx
cmovgl 0x3c(%rsp), %ecx
addl %r12d, %ecx
movl %ecx, %r12d
cmpl %eax, %r8d
jle 0x39af8
movl 0x10(%rsp), %r10d
addl 0x2c(%rsp), %r10d
movl %r9d, 0x20(%rsp)
leaq 0x24(%rsp), %rdi
movl 0x14(%rsp), %r8d
jmp 0x399c7