// File: /workspace/github_c_makefile_star2_O2/OnionIoT[P]lv_example/lvgl/src/lv_objx/lv_label.c
// Function: lv_label_get_letter_pos
void lv_label_get_letter_pos(const lv_obj_t * label, uint16_t char_id, 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;

    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  the set the max length to very big */
    if(ext->long_mode == LV_LABEL_LONG_EXPAND) {
        max_w = LV_COORD_MAX;
    }

    uint16_t byte_id = lv_txt_encoded_get_byte_id(txt, char_id);

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

        y += letter_height + style->text.line_space;
        line_start = new_line_start;
    }

    /*If the last character is line break then go to the next line*/
    if(byte_id > 0) {
        if((txt[byte_id - 1] == '\n' || txt[byte_id - 1] == '\r') && txt[byte_id] == '\0') {
            y += letter_height + style->text.line_space;
            line_start = byte_id;
        }
    }

    const char *bidi_txt;
    uint16_t visual_byte_pos;
#if LV_USE_BIDI
    /*Handle Bidi*/
    if(new_line_start == byte_id) {
        visual_byte_pos = byte_id - line_start;
        bidi_txt =  &txt[line_start];
    }
    else {
        uint16_t line_char_id = lv_txt_encoded_get_char_id(&txt[line_start], byte_id - line_start);

        bool is_rtl;
        char *mutable_bidi_txt;
        uint16_t visual_char_pos = lv_bidi_get_visual_pos(&txt[line_start], &mutable_bidi_txt, new_line_start - line_start, lv_obj_get_base_dir(label), line_char_id, &is_rtl);
        bidi_txt = mutable_bidi_txt;
        if (is_rtl) visual_char_pos++;
        visual_byte_pos = lv_txt_encoded_get_byte_id(bidi_txt, visual_char_pos);
    }
#else
    bidi_txt = &txt[line_start];
    visual_byte_pos = byte_id - line_start;
#endif


    /*Calculate the x coordinate*/
    lv_coord_t x = lv_txt_get_width(bidi_txt, visual_byte_pos, font, style->text.letter_space, flag);

    if(char_id != line_start) x += style->text.letter_space;

    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;
    }
    pos->x = x;
    pos->y = y;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, %r14
movl %esi, %ebx
movq %rdi, %r15
callq 0x7994
testb %al, %al
je 0x19953
movq %r14, %rdi
callq 0x7994
testb %al, %al
je 0x19964
movq %r14, 0x30(%rsp)
movq %r15, %rdi
callq 0x18db9
movq %rax, %r14
movq %r15, %rdi
callq 0x6747
movq %rax, %rbp
movq %r15, %rdi
callq 0x5308
movl %eax, 0xc(%rsp)
movq %r15, %rdi
callq 0x6316
movq %rax, %r12
movq 0x28(%rax), %rax
movq %rax, 0x10(%rsp)
movzbl 0x10(%rax), %eax
movl %eax, 0x8(%rsp)
movb 0x18(%rbp), %al
movl %eax, %ecx
shrb $0x6, %cl
movl %ecx, %edx
andb $0x1, %dl
orb $0x2, %cl
testb %al, %al
movzbl %dl, %eax
movzbl %cl, %r13d
cmovnsl %eax, %r13d
movq %r15, 0x28(%rsp)
movq %r15, %rdi
callq 0x18d7f
leal 0x4(%r13), %ecx
cmpb $0x1, %al
movzbl %cl, %ecx
movl %ecx, 0x1c(%rsp)
cmovel %ecx, %r13d
movl %r13d, %ecx
orb $0x8, %cl
movb %al, 0x3(%rsp)
cmpb $0x2, %al
movzbl %cl, %eax
movl %eax, 0x18(%rsp)
cmovel %eax, %r13d
movzwl 0x18(%rbp), %eax
testb $0x7, %al
movl $0x7c18, %ebp           # imm = 0x7C18
cmovnel 0xc(%rsp), %ebp
leaq 0x37d81(%rip), %rax     # 0x51640
movzwl %bx, %esi
movq %r14, %rdi
movl %esi, 0x20(%rsp)
callq *(%rax)
movq %r12, %rcx
movq %r14, %rdi
movl %eax, 0x4(%rsp)
movzwl %ax, %esi
cmpb $0x0, (%r14)
sete %al
xorl %r14d, %r14d
movswl %bp, %edx
movl %edx, 0x24(%rsp)
movzbl %r13b, %edx
movl %edx, 0xc(%rsp)
movzwl 0x8(%rsp), %edx
movl %edx, 0x8(%rsp)
xorl %ebx, %ebx
movl %ebx, %r13d
testb $0x1, %al
jne 0x19975
movq %rdi, %r15
addq %r13, %rdi
movq %rcx, %rbp
movswl 0x30(%rcx), %edx
movq %rsi, %r12
movq 0x10(%rsp), %rsi
movl 0x24(%rsp), %ecx
movl 0xc(%rsp), %r8d
callq 0x28866
movq %r12, %rsi
movzwl %ax, %r12d
addq %r13, %r12
cmpl %r12d, %esi
jb 0x1997a
cmpb $0x0, (%r15,%r12)
movq %r15, %rdi
je 0x1997d
addl 0x8(%rsp), %r14d
movq %rbp, %rcx
addw 0x32(%rbp), %r14w
xorl %eax, %eax
movl %r12d, %ebx
jmp 0x198fc
leaq 0x217ea(%rip), %rdi     # 0x3b144
movq %r15, %rsi
callq 0x7b01
jmp 0x19962
leaq 0x217d9(%rip), %rdi     # 0x3b144
movq %r14, %rsi
callq 0x7b01
jmp 0x19973
movl %ebx, %r12d
jmp 0x19980
movq %r15, %rdi
movq %rbp, %rcx
testl %esi, %esi
je 0x1999d
movzbl -0x1(%rdi,%rsi), %eax
cmpl $0xd, %eax
je 0x19993
cmpl $0xa, %eax
jne 0x1999d
cmpb $0x0, (%rdi,%rsi)
je 0x19a70
movq %r13, %rsi
movl 0x4(%rsp), %eax
addq %rsi, %rdi
subl %ebx, %eax
movq %rcx, %r15
movswl 0x30(%rcx), %ecx
movzwl %ax, %esi
movq %rdi, %r13
movq 0x10(%rsp), %rdx
movl 0xc(%rsp), %r8d
callq 0x28bb3
movl %eax, %ebp
cmpl 0x20(%rsp), %ebx
je 0x199d2
addw 0x30(%r15), %bp
movq 0x28(%rsp), %rcx
movb 0x3(%rsp), %al
cmpb $0x1, %al
movq %r13, %rdi
jne 0x19a24
subl %ebx, %r12d
movq %rcx, %r13
movswl 0x30(%r15), %ecx
movzwl %r12w, %esi
movq 0x10(%rsp), %rdx
movl 0x1c(%rsp), %r8d
callq 0x28bb3
movl %eax, %ebx
movq %r13, %rdi
callq 0x5308
movw $0x2, %cx
cwtd
idivw %cx
movl %eax, %ecx
movw $0xfffe, %si            # imm = 0xFFFE
movl %ebx, %eax
cwtd
idivw %si
addl %ecx, %ebp
jmp 0x19a52
cmpb $0x2, %al
jne 0x19a54
subl %ebx, %r12d
movq %rcx, %r13
movswl 0x30(%r15), %ecx
movzwl %r12w, %esi
movq 0x10(%rsp), %rdx
movl 0x18(%rsp), %r8d
callq 0x28bb3
movl %eax, %ebx
movq %r13, %rdi
callq 0x5308
subl %ebx, %ebp
addl %eax, %ebp
movq 0x30(%rsp), %rax
movw %bp, (%rax)
movw %r14w, 0x2(%rax)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addl 0x8(%rsp), %r14d
addw 0x32(%rcx), %r14w
movl %esi, %ebx
jmp 0x199a0