// File: /workspace/github_c_makefile_star2_O1/OnionIoT[P]lv_example/lvgl/src/lv_objx/lv_label.c
// Function: lv_label_get_letter_on
uint16_t lv_label_get_letter_on(const lv_obj_t * label, lv_point_t * pos)
{
    LV_ASSERT_OBJ(label, LV_OBJX_NAME);
    LV_ASSERT_NULL(pos);

    const char * txt         = lv_label_get_text(label);
    lv_label_ext_t * ext     = lv_obj_get_ext_attr(label);
    uint32_t line_start      = 0;
    uint32_t new_line_start  = 0;
    lv_coord_t max_w         = lv_obj_get_width(label);
    const lv_style_t * style = lv_obj_get_style(label);
    const lv_font_t * font   = style->text.font;
    uint8_t letter_height    = lv_font_get_line_height(font);
    lv_coord_t y             = 0;
    lv_txt_flag_t flag       = LV_TXT_FLAG_NONE;
    uint16_t logical_pos;
    char *bidi_txt;

    if(ext->recolor != 0) flag |= LV_TXT_FLAG_RECOLOR;
    if(ext->expand != 0) flag |= LV_TXT_FLAG_EXPAND;

    lv_label_align_t align = lv_label_get_align(label);
    if(align == LV_LABEL_ALIGN_CENTER) flag |= LV_TXT_FLAG_CENTER;
    if(align == LV_LABEL_ALIGN_RIGHT) flag |= LV_TXT_FLAG_RIGHT;

    /*If the width will be expanded set the max length to very big */
    if(ext->long_mode == LV_LABEL_LONG_EXPAND) {
        max_w = LV_COORD_MAX;
    }

    /*Search the line of the index letter */;
    while(txt[line_start] != '\0') {
        new_line_start += lv_txt_get_next_line(&txt[line_start], font, style->text.letter_space, max_w, flag);

        if(pos->y <= y + letter_height) {
            /*The line is found (stored in 'line_start')*/
            /* Include the NULL terminator in the last line */
            uint32_t tmp = new_line_start;
            uint32_t letter;
            letter = lv_txt_encoded_prev(txt, &tmp);
            if(letter != '\n' && txt[new_line_start] == '\0' ) new_line_start++;
            break;
        }
        y += letter_height + style->text.line_space;

        line_start = new_line_start;
    }

#if LV_USE_BIDI
    bidi_txt = lv_draw_get_buf(new_line_start - line_start + 1);
    uint16_t txt_len = new_line_start - line_start;
    if(bidi_txt[new_line_start] == '\0') txt_len--;
    lv_bidi_process_paragraph(txt + line_start, bidi_txt, txt_len, lv_obj_get_base_dir(label), NULL, 0);
#else
    bidi_txt = (char*)txt + line_start;
#endif

    /*Calculate the x coordinate*/
    lv_coord_t x = 0;
    if(align == LV_LABEL_ALIGN_CENTER) {
        lv_coord_t line_w;
        line_w = lv_txt_get_width(bidi_txt, new_line_start - line_start, font, style->text.letter_space, flag);
        x += lv_obj_get_width(label) / 2 - line_w / 2;
    }
    else if(align == LV_LABEL_ALIGN_RIGHT) {
        lv_coord_t line_w;
        line_w = lv_txt_get_width(bidi_txt, new_line_start - line_start, font, style->text.letter_space, flag);
        x += lv_obj_get_width(label) - line_w;
    }

    lv_txt_cmd_state_t cmd_state = LV_TXT_CMD_STATE_WAIT;

    uint32_t i = 0;
    uint32_t i_act = i;
    uint32_t letter;
    uint32_t letter_next;

    if(new_line_start > 0) {
        while(i + line_start < new_line_start) {
            /* Get the current letter.*/
            letter = lv_txt_encoded_next(bidi_txt, &i);

            /*Get the next letter too for kerning*/
            letter_next = lv_txt_encoded_next(&bidi_txt[i], NULL);

            /*Handle the recolor command*/
            if((flag & LV_TXT_FLAG_RECOLOR) != 0) {
                if(lv_txt_is_cmd(&cmd_state, bidi_txt[i]) != false) {
                    continue; /*Skip the letter is it is part of a command*/
                }
            }

            x += lv_font_get_glyph_width(font, letter, letter_next);

            /*Finish if the x position or the last char of the line is reached*/
            if(pos->x < x || i + line_start == new_line_start) {
                i = i_act;
                break;
            }
            x += style->text.letter_space;
            i_act = i;
        }
    }

#if LV_USE_BIDI
    /*Handle Bidi*/
    bool is_rtl;
    logical_pos = lv_bidi_get_logical_pos(&txt[line_start], NULL, txt_len, lv_obj_get_base_dir(label), lv_txt_encoded_get_char_id(bidi_txt, i), &is_rtl);
    if (is_rtl) logical_pos++;
#else
    logical_pos = lv_txt_encoded_get_char_id(bidi_txt, i);
#endif

    return  logical_pos + lv_txt_encoded_get_char_id(txt, line_start);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %rbx
movq %rdi, %r15
callq 0x80d4
testb %al, %al
je 0x1b0da
movq %rbx, %rdi
callq 0x80d4
testb %al, %al
je 0x1b0eb
movq %rbx, 0x48(%rsp)
movq %r15, %rdi
callq 0x1a239
movq %rax, %r13
movq %r15, %rdi
callq 0x6ad8
movq %rax, %rbx
movq %r15, %rdi
callq 0x5651
movl %eax, %ebp
movq %r15, %rdi
callq 0x6697
movq %rax, 0x18(%rsp)
movq 0x28(%rax), %rax
movq %rax, 0x28(%rsp)
movzbl 0x10(%rax), %eax
movl %eax, 0x20(%rsp)
movb 0x18(%rbx), %al
movl %eax, %ecx
shrb $0x6, %cl
movl %ecx, %edx
andb $0x1, %dl
orb $0x2, %cl
testb %al, %al
movzbl %dl, %eax
movzbl %cl, %r14d
cmovnsl %eax, %r14d
movq %r15, 0x40(%rsp)
movq %r15, %rdi
callq 0x1a1ff
leal 0x4(%r14), %ecx
cmpb $0x1, %al
movzbl %cl, %edx
cmovnel %r14d, %edx
movl %edx, %ecx
orb $0x8, %cl
movb %al, 0x16(%rsp)
cmpb $0x2, %al
movzbl %cl, %ecx
cmovnel %edx, %ecx
testb $0x7, 0x18(%rbx)
movl $0x7c18, %eax           # imm = 0x7C18
cmovnel %ebp, %eax
cmpb $0x0, (%r13)
movl %ecx, 0x3c(%rsp)
movzbl %cl, %ecx
movl %ecx, 0xc(%rsp)
movq %r13, 0x30(%rsp)
je 0x1b0fc
xorl %ebx, %ebx
cwtl
movl %eax, 0x38(%rsp)
xorl %r15d, %r15d
movq 0x18(%rsp), %r14
movswl 0x30(%r14), %edx
movq %r13, %rdi
movq 0x28(%rsp), %rsi
movl 0x38(%rsp), %ecx
movl 0xc(%rsp), %r8d
callq 0x2a787
movzwl %ax, %ebp
addl %ebx, %ebp
movq 0x48(%rsp), %rax
movswl 0x2(%rax), %eax
movswl %r15w, %r15d
addl 0x20(%rsp), %r15d
cmpl %eax, %r15d
jge 0x1b103
movzwl 0x32(%r14), %eax
addl %eax, %r15d
movl %ebp, %eax
movq 0x30(%rsp), %rcx
leaq (%rcx,%rax), %r13
cmpb $0x0, (%rcx,%rax)
movl %ebp, %ebx
movl %ebp, %r12d
jne 0x1b083
jmp 0x1b12f
leaq 0x24053(%rip), %rdi     # 0x3f134
movq %r15, %rsi
callq 0x829e
jmp 0x1b0e9
leaq 0x24042(%rip), %rdi     # 0x3f134
movq %rbx, %rsi
callq 0x829e
jmp 0x1b0fa
xorl %ebp, %ebp
xorl %r12d, %r12d
jmp 0x1b12f
leaq 0x54(%rsp), %rsi
movl %ebp, (%rsi)
leaq 0x38527(%rip), %rax     # 0x53638
movq 0x30(%rsp), %r14
movq %r14, %rdi
callq *(%rax)
cmpl $0xa, %eax
je 0x1b12a
movl %ebp, %eax
cmpb $0x1, (%r14,%rax)
adcl $0x0, %ebp
movl %ebp, %r12d
movl %ebx, %ebp
movb 0x16(%rsp), %al
cmpb $0x1, %al
jne 0x1b18a
movl %r12d, %eax
subl %ebp, %eax
movq 0x18(%rsp), %rcx
movswl 0x30(%rcx), %ecx
movzwl %ax, %esi
movq %r13, %rdi
movq 0x28(%rsp), %rdx
movl 0xc(%rsp), %r8d
callq 0x2ab49
movl %eax, %r14d
movzwl %ax, %ebx
movq 0x40(%rsp), %rdi
callq 0x5651
movzwl %ax, %ecx
shrl $0xf, %ecx
addl %eax, %ecx
movswl %cx, %ecx
sarl %ecx
shrl $0xf, %ebx
addl %r14d, %ebx
movswl %bx, %eax
sarl %eax
subl %eax, %ecx
movl %ecx, 0xc(%rsp)
jmp 0x1b1cf
cmpb $0x2, %al
jne 0x1b1c7
movl %r12d, %eax
subl %ebp, %eax
movq 0x18(%rsp), %rcx
movswl 0x30(%rcx), %ecx
movzwl %ax, %esi
movq %r13, %rdi
movq 0x28(%rsp), %rdx
movl 0xc(%rsp), %r8d
callq 0x2ab49
movl %eax, %r14d
movq 0x40(%rsp), %rdi
callq 0x5651
subl %r14d, %eax
movl %eax, 0xc(%rsp)
jmp 0x1b1cf
movl $0x0, 0xc(%rsp)
movb $0x0, 0x17(%rsp)
movl $0x0, 0x10(%rsp)
testl %r12d, %r12d
je 0x1b28a
movq $0x0, 0x20(%rsp)
leaq 0x3843b(%rip), %rbx     # 0x53630
movl 0x10(%rsp), %eax
addl %ebp, %eax
cmpl %r12d, %eax
jae 0x1b28a
movq %r13, %rdi
leaq 0x10(%rsp), %rsi
callq *(%rbx)
movl %eax, %r15d
movl 0x10(%rsp), %edi
addq %r13, %rdi
xorl %esi, %esi
callq *(%rbx)
movl %eax, %r14d
testb $0x1, 0x3c(%rsp)
je 0x1b23e
movl 0x10(%rsp), %eax
movsbl (%r13,%rax), %esi
leaq 0x17(%rsp), %rdi
callq 0x2ac84
testb %al, %al
jne 0x1b1f5
movq 0x28(%rsp), %rdi
movl %r15d, %esi
movl %r14d, %edx
callq 0x27d87
movl 0xc(%rsp), %edx
addl %eax, %edx
movq 0x48(%rsp), %rax
cmpw %dx, (%rax)
jl 0x1b281
movl 0x10(%rsp), %eax
leal (%rax,%rbp), %ecx
cmpl %r12d, %ecx
je 0x1b281
movq 0x18(%rsp), %rcx
addw 0x30(%rcx), %dx
movl %edx, 0xc(%rsp)
movq %rax, 0x20(%rsp)
jmp 0x1b1f5
movq 0x20(%rsp), %rax
movl %eax, 0x10(%rsp)
leaq 0x383b7(%rip), %r14     # 0x53648
movl 0x10(%rsp), %esi
movq %r13, %rdi
callq *(%r14)
movl %eax, %ebx
movq 0x30(%rsp), %rdi
movl %ebp, %esi
callq *(%r14)
addl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq