// File: /workspace/github_c_makefile_star2_O1/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 0x3d2e6
cmpw %cx, 0x4(%rdi)
jge 0x3d2e6
cmpw %cx, 0x8(%rdi)
jl 0x3d32c
movzwl 0x4(%rsi), %ecx
cmpw %cx, %ax
jle 0x3d2fb
cmpw %cx, 0x4(%rdi)
jle 0x3d2fb
cmpw %cx, 0x8(%rdi)
jg 0x3d32c
movzwl 0x2(%rdi), %r10d
movzwl 0x2(%rsi), %ecx
cmpw %cx, %r10w
jge 0x3d316
cmpw %cx, 0x6(%rdi)
jge 0x3d316
cmpw %cx, 0xa(%rdi)
jl 0x3d32c
movzwl 0x6(%rsi), %ecx
cmpw %cx, %r10w
jle 0x3d33b
cmpw %cx, 0x6(%rdi)
jle 0x3d33b
cmpw %cx, 0xa(%rdi)
jle 0x3d33b
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movzwl 0x4(%rdi), %ecx
movzwl 0x6(%rdi), %edx
movzwl 0xa(%rdi), %r13d
cmpw %r10w, %dx
jge 0x3d356
movl %r10d, %r8d
movl %eax, %r9d
jmp 0x3d361
movl %edx, %r8d
movl %ecx, %r9d
movl %r10d, %edx
movl %eax, %ecx
movzwl 0x8(%rdi), %eax
cmpw %r8w, %r13w
jge 0x3d376
movw %r8w, 0x8(%rsp)
movl %r9d, %edi
jmp 0x3d384
movw %r13w, 0x8(%rsp)
movl %eax, %edi
movl %r8d, %r13d
movl %r9d, %eax
movw %di, 0xa(%rsp)
cmpw %dx, %r13w
movq %rsi, 0x58(%rsp)
jge 0x3d39a
movl %edx, %esi
movl %ecx, %edi
jmp 0x3d3a4
movl %r13d, %esi
movl %eax, %edi
movl %edx, %r13d
movl %ecx, %eax
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, 0x28(%rsp)
movw %si, 0x1a(%rsp)
movswl %si, %esi
movswl %r13w, %r11d
movl %r11d, %ecx
subl %esi, %ecx
movl %ecx, %r12d
negl %r12d
cmovsl %ecx, %r12d
xorl %ecx, %ecx
cmpw %si, %r11w
setl %cl
movl %edi, 0xc(%rsp)
movswl %di, %ebx
movswl %r12w, %edi
movl %edi, %r8d
negl %r8d
cmpl %edi, %ebx
cmovgl %ebx, %r8d
movl %r8d, %ebp
shrl $0x1f, %ebp
addl %r8d, %ebp
shrl %ebp
xorl %edi, %edi
movzwl 0xa(%rsp), %r15d
cmpw %r15w, %r9w
setl %dil
xorl %r8d, %r8d
movzwl 0x8(%rsp), %r10d
cmpw %r10w, %r11w
setl %r8b
movswl %r10w, %r10d
subl %r10d, %r11d
movl %r11d, %r14d
negl %r14d
cmovsl %r11d, %r14d
movswl %r14w, %eax
movl %eax, %r11d
negl %r11d
movswl %r15w, %r14d
subl %r14d, %r9d
movl %r9d, %r15d
negl %r15d
cmovsl %r9d, %r15d
movl %eax, 0x44(%rsp)
cmpl %eax, %ebx
movswl %r15w, %eax
cmovgl %eax, %r11d
movl %r11d, %r15d
shrl $0x1f, %r15d
addl %r11d, %r15d
shrl %r15d
movl %edx, %r9d
subl %r14d, %r9d
movl %r9d, %r11d
negl %r11d
cmovsl %r9d, %r11d
xorl %r9d, %r9d
cmpw %r14w, %dx
setl %r9b
movl %esi, %edx
subl %r10d, %edx
movl %edx, %ebx
negl %ebx
cmovsl %edx, %ebx
xorl %edx, %edx
cmpw %r10w, %si
setl %dl
movl %ebx, 0x50(%rsp)
movswl %bx, %esi
movl %esi, %r10d
negl %r10d
movl %r11d, 0x54(%rsp)
movswl %r11w, %r11d
cmpl %esi, %r11d
cmovgl %r11d, %r10d
movl %r13d, %esi
movl %r10d, %r11d
shrl $0x1f, %r11d
addl %r10d, %r11d
movl 0x14(%rsp), %r10d
movq 0x28(%rsp), %rbx
leal -0x1(,%rbx,2), %r13d
leal -0x1(,%rcx,2), %ebx
leal -0x1(%rdi,%rdi), %ecx
movl %ecx, 0x3c(%rsp)
leal -0x1(%r8,%r8), %ecx
movl %ecx, 0x38(%rsp)
leal -0x1(%r9,%r9), %ecx
movl %ecx, 0x4c(%rsp)
leal -0x1(,%rdx,2), %ecx
movl %ecx, 0x48(%rsp)
shrl %r11d
movl %r11d, 0x28(%rsp)
movl %eax, 0x40(%rsp)
negl %eax
movl %eax, 0x34(%rsp)
leaq 0x20(%rsp), %rdi
movl %r10d, %r14d
movl %esi, %r8d
movw %r8w, 0x1c(%rsp)
movswl %r14w, %eax
movl %r10d, 0x14(%rsp)
movswl %r10w, %ecx
cmpl %ecx, %eax
movl %ecx, %edx
cmovll %eax, %edx
cmovgl %eax, %ecx
incl %edx
movw %dx, 0x20(%rsp)
movw %cx, 0x24(%rsp)
movswl %r8w, %eax
movw %si, 0x12(%rsp)
movswl %si, %ecx
cmpl %ecx, %eax
movl %ecx, %edx
cmovll %eax, %edx
cmovgl %eax, %ecx
decl %edx
movw %dx, 0x22(%rsp)
decl %ecx
movw %cx, 0x26(%rsp)
movq 0x60(%rsp), %rax
movzwl 0x2(%rax), %edx
movzbl 0x30(%rsp), %ecx
movq 0x58(%rsp), %rsi
callq 0x35310
movzwl 0x1c(%rsp), %eax
movl %eax, %r11d
movzwl 0x1a(%rsp), %r10d
movl 0xc(%rsp), %r8d
movl %r8d, %edx
movl %r13d, %esi
movl %r12d, %edi
movl %ebx, %r8d
cmpw 0x1e(%rsp), %r14w
jne 0x3d5df
movl 0x28(%rsp), %ecx
movl 0x48(%rsp), %ebx
movl 0x50(%rsp), %r12d
movl 0x4c(%rsp), %r13d
movl 0x54(%rsp), %r9d
movl %r9d, 0xc(%rsp)
cmpw %r10w, %ax
je 0x3d611
cmpw 0xa(%rsp), %r14w
jne 0x3d602
movl %ebp, %ecx
movl %r8d, %ebx
movl %edi, %r12d
movl %esi, %r13d
movl %edx, 0xc(%rsp)
cmpw 0x8(%rsp), %ax
jne 0x3d611
jmp 0x3d32c
movl %ebp, %ecx
movl %r8d, %ebx
movl %edi, %r12d
movl %esi, %r13d
movl %edx, 0xc(%rsp)
movswl %cx, %edx
movl 0xc(%rsp), %r8d
movswl %r8w, %ebp
movl %ebp, %esi
negl %esi
cmpl %esi, %edx
movl $0x0, %esi
cmovgl %r12d, %esi
movl %r13d, %edi
movl $0x0, %r9d
cmovlel %r9d, %edi
subl %esi, %ecx
addl %edi, %r14d
cmpw %r12w, %dx
cmovgel %r9d, %ebp
movl %ebx, %edx
cmovgel %r9d, %edx
addl %ecx, %ebp
addl %edx, %eax
cmpw %r11w, %ax
je 0x3d5a9
movzwl 0x12(%rsp), %ecx
movl 0x14(%rsp), %r10d
movl %r10d, %r8d
cmpw 0xa(%rsp), %r10w
jne 0x3d67a
cmpw 0x8(%rsp), %cx
je 0x3d32c
movswl %r15w, %edx
cmpl 0x34(%rsp), %edx
movl $0x0, %esi
cmovgl 0x3c(%rsp), %esi
movl $0x0, %edi
movl 0x44(%rsp), %r9d
cmovgl %r9d, %edi
movl %r8d, %r10d
addl %esi, %r10d
subl %edi, %r15d
cmpl %edx, %r9d
movl $0x0, %edx
cmovgl 0x38(%rsp), %edx
movl $0x0, %esi
cmovgl 0x40(%rsp), %esi
addl %edx, %ecx
addl %r15d, %esi
movl %esi, %r15d
cmpw 0x12(%rsp), %cx
je 0x3d664
movl %ecx, %esi
movl %eax, %r8d
leaq 0x20(%rsp), %rdi
jmp 0x3d531