// File: /workspace/github_c_makefile_star2_O3/OnionIoT[P]lv_example/lvgl/src/lv_objx/lv_btnm.c
// Function: lv_btnm_set_map
void lv_btnm_set_map(const lv_obj_t * btnm, const char * map[])
{
    LV_ASSERT_OBJ(btnm, LV_OBJX_NAME);
    LV_ASSERT_NULL(map);

    /*
     * lv_btnm_set_map is called on receipt of signals such as
     * LV_SIGNAL_CORD_CHG regardless of whether the map has changed (e.g.
     * calling lv_obj_align on the map will trigger this).
     *
     * We check if the map has changed here to avoid overwriting changes made
     * to hidden/longpress/disabled states after the map was originally set.
     *
     * TODO: separate all map set/allocation from layout code below and skip
     * set/allocation when map hasn't changed.
     */
    lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
    if(!maps_are_identical(ext->map_p, map)) {

        /*Analyze the map and create the required number of buttons*/
        allocate_btn_areas_and_controls(btnm, map);
    }
    ext->map_p = map;

    /*Set size and positions of the buttons*/
    const lv_style_t * style_bg = lv_btnm_get_style(btnm, LV_BTNM_STYLE_BG);
    lv_coord_t max_w            = lv_obj_get_width(btnm) - style_bg->body.padding.left - style_bg->body.padding.right;
    lv_coord_t max_h            = lv_obj_get_height(btnm) - style_bg->body.padding.top - style_bg->body.padding.bottom;
    lv_coord_t act_y            = style_bg->body.padding.top;

    /*Count the lines to calculate button height*/
    uint8_t line_cnt = 1;
    uint8_t li;
    for(li = 0; strlen(map[li]) != 0; li++) {
        if(strcmp(map[li], "\n") == 0) line_cnt++;
    }

    lv_coord_t btn_h = max_h - ((line_cnt - 1) * style_bg->body.padding.inner);
    btn_h            = btn_h / line_cnt;
    btn_h--; /*-1 because e.g. height = 100 means 101 pixels (0..100)*/

    /* Count the units and the buttons in a line
     * (A button can be 1,2,3... unit wide)*/
    uint16_t unit_cnt;           /*Number of units in a row*/
    uint16_t unit_act_cnt;       /*Number of units currently put in a row*/
    uint16_t btn_cnt;            /*Number of buttons in a row*/
    uint16_t i_tot          = 0; /*Act. index in the str map*/
    uint16_t btn_i          = 0; /*Act. index of button areas*/
    const char ** map_p_tmp = map;

    /*Count the units and the buttons in a line*/
    while(1) {
        unit_cnt = 0;
        btn_cnt  = 0;
        /*Count the buttons in a line*/
        while(strcmp(map_p_tmp[btn_cnt], "\n") != 0 && strlen(map_p_tmp[btn_cnt]) != 0) { /*Check a line*/
            unit_cnt += get_button_width(ext->ctrl_bits[btn_i + btn_cnt]);
            btn_cnt++;
        }

        /*Make sure the last row is at the bottom of 'btnm'*/
        if(map_p_tmp[btn_cnt][0] == '\0') { /*Last row?*/
            btn_h = lv_obj_get_height(btnm)- act_y - style_bg->body.padding.bottom - 1;
        }

        lv_bidi_dir_t base_dir = lv_obj_get_base_dir(btnm);

        /*Only deal with the non empty lines*/
        if(btn_cnt != 0) {
            /*Calculate the width of all units*/
            lv_coord_t all_unit_w = max_w - ((btn_cnt - 1) * style_bg->body.padding.inner);

            /*Set the button size and positions and set the texts*/
            uint16_t i;
            lv_coord_t act_x;

            lv_coord_t act_unit_w;
            unit_act_cnt = 0;
            for(i = 0; i < btn_cnt; i++) {
                /* one_unit_w = all_unit_w / unit_cnt
                 * act_unit_w = one_unit_w * button_width
                 * do this two operations but the multiply first to divide a greater number */
                act_unit_w = (all_unit_w * get_button_width(ext->ctrl_bits[btn_i])) / unit_cnt;
                act_unit_w--; /*-1 because e.g. width = 100 means 101 pixels (0..100)*/

                /*Always recalculate act_x because of rounding errors */
                if(base_dir == LV_BIDI_DIR_RTL)  {
                    act_x = (unit_act_cnt * all_unit_w) / unit_cnt + i * style_bg->body.padding.inner;
                    act_x = lv_obj_get_width(btnm) - style_bg->body.padding.right - act_x - act_unit_w - 1;
                } else {
                    act_x = (unit_act_cnt * all_unit_w) / unit_cnt + i * style_bg->body.padding.inner +
                            style_bg->body.padding.left;
                }
                /* Set the button's area.
                 * If inner padding is zero then use the prev. button x2 as x1 to avoid rounding
                 * errors*/
                if(style_bg->body.padding.inner == 0 && act_x != style_bg->body.padding.left) {
                    lv_area_set(&ext->button_areas[btn_i], ext->button_areas[btn_i - 1].x2, act_y, act_x + act_unit_w,
                                act_y + btn_h);
                } else {
                    lv_area_set(&ext->button_areas[btn_i], act_x, act_y, act_x + act_unit_w, act_y + btn_h);
                }

                unit_act_cnt += get_button_width(ext->ctrl_bits[btn_i]);

                i_tot++;
                btn_i++;
            }
        }
        act_y += btn_h + style_bg->body.padding.inner + 1;

        if(strlen(map_p_tmp[btn_cnt]) == 0) break; /*Break on end of map*/
        map_p_tmp = &map_p_tmp[btn_cnt + 1];       /*Set the map to the next line*/
        i_tot++;                                   /*Skip the '\n'*/
    }

    lv_obj_invalidate(btnm);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r12
movq %rdi, %r15
callq 0x80e4
testb %al, %al
je 0x21fb8
movq %r12, %rdi
callq 0x80e4
testb %al, %al
je 0x21fc9
movq %r15, %rdi
callq 0x6af7
movq %rax, 0x10(%rsp)
movq (%rax), %rbx
cmpq %r12, %rbx
je 0x220da
testq %rbx, %rbx
sete %al
testq %r12, %r12
sete %cl
orb %al, %cl
jne 0x21fe9
movq (%rbx), %rdi
cmpb $0x0, (%rdi)
je 0x21fda
movw $0x1, %r14w
xorl %eax, %eax
movq (%r12,%rax,8), %rsi
cmpb $0x0, (%rsi)
je 0x21fe9
callq 0x20e0
testl %eax, %eax
jne 0x21fe9
movzwl %r14w, %eax
movq (%rbx,%rax,8), %rdi
incl %r14d
cmpb $0x0, (%rdi)
jne 0x21f94
jmp 0x21fdc
leaq 0x1d225(%rip), %rdi     # 0x3f1e4
movq %r15, %rsi
callq 0x8294
jmp 0x21fc7
leaq 0x1d214(%rip), %rdi     # 0x3f1e4
movq %r12, %rsi
callq 0x8294
jmp 0x21fd8
xorl %eax, %eax
movq (%r12,%rax,8), %rax
cmpb $0x0, (%rax)
je 0x220da
movq (%r12), %rcx
movb (%rcx), %sil
testb %sil, %sil
je 0x22029
movw $0x1, %ax
xorl %edx, %edx
movw $0x1, %di
cmpb $0xa, %sil
jne 0x2200f
xorl %edi, %edi
cmpb $0x0, 0x1(%rcx)
setne %dil
movzwl %di, %ebx
addl %edx, %ebx
movzwl %ax, %ecx
movq (%r12,%rcx,8), %rcx
movb (%rcx), %sil
incl %eax
movl %ebx, %edx
testb %sil, %sil
jne 0x21ffb
jmp 0x2202b
xorl %ebx, %ebx
movq %r12, %r14
movq %r15, %rdi
callq 0x6af7
movq %rax, %r12
movq 0x8(%rax), %rdi
testq %rdi, %rdi
je 0x22050
callq 0x2a006
movq $0x0, 0x8(%r12)
movq 0x10(%r12), %rdi
testq %rdi, %rdi
je 0x22068
callq 0x2a006
movq $0x0, 0x10(%r12)
movzwl %bx, %r13d
leal (,%r13,8), %edi
callq 0x29f85
movq %rax, 0x8(%r12)
movq %rax, %rdi
callq 0x80e4
testb %al, %al
je 0x2215c
addl %r13d, %r13d
movq %r13, %rdi
callq 0x29f85
movq %rax, 0x10(%r12)
movq %rax, %rdi
callq 0x80e4
testb %al, %al
je 0x223cc
movq 0x10(%r12), %rdi
xorl %eax, %eax
testq %rdi, %rdi
cmovel %eax, %ebx
cmpq $0x0, 0x8(%r12)
cmovel %eax, %ebx
movzwl %bx, %ebx
leal (%rbx,%rbx), %edx
xorl %esi, %esi
callq 0x20a0
movw %bx, 0x40(%r12)
movq %r14, %r12
movq 0x10(%rsp), %rax
movq %r12, (%rax)
movq %r15, %rdi
xorl %esi, %esi
callq 0x2248e
movq %rax, %r14
movq %r15, %rdi
callq 0x566d
movl %eax, 0x1c(%rsp)
movzwl 0x1a(%r14), %ebx
movzwl 0x1c(%r14), %ebp
movq %r15, %rdi
callq 0x5697
movq %r14, %r9
movzwl 0x16(%r14), %ecx
movq %rcx, 0x8(%rsp)
movzwl 0x18(%r14), %ecx
movq (%r12), %rdi
movb (%rdi), %r8b
testb %r8b, %r8b
je 0x2216f
movb $0x1, %dl
movb $0x1, %sil
cmpb $0xa, %r8b
jne 0x22140
cmpb $0x0, 0x1(%rdi)
sete %dil
jmp 0x22142
xorl %edi, %edi
addb %dil, %sil
movzbl %dl, %edx
movq (%r12,%rdx,8), %rdi
movb (%rdi), %r8b
incb %dl
testb %r8b, %r8b
jne 0x22130
movzbl %sil, %edx
jmp 0x22173
movq 0x8(%r12), %rsi
leaq 0x1cf68(%rip), %rdi     # 0x3f0d0
callq 0x8294
jmp 0x2216d
movw $0x1, %dx
movzwl %dx, %esi
movl $0x1, %edx
subl %esi, %edx
imulw 0x1e(%r9), %dx
addl 0x8(%rsp), %ecx
subl %ecx, %eax
addl %edx, %eax
cwtd
idivw %si
movl %eax, %r13d
decl %r13d
addl %ebp, %ebx
subl %ebx, 0x1c(%rsp)
xorl %ecx, %ecx
movq 0x10(%rsp), %rsi
movq %r9, 0x28(%rsp)
movq %r15, 0x38(%rsp)
movl %ecx, 0x20(%rsp)
movzwl %cx, %ebp
xorl %ebx, %ebx
xorl %eax, %eax
movzwl %ax, %r14d
movq (%r12,%r14,8), %rax
movzbl (%rax), %ecx
cmpl $0xa, %ecx
je 0x221ce
testl %ecx, %ecx
jne 0x221d4
jmp 0x221f4
cmpb $0x0, 0x1(%rax)
je 0x22210
leaq (,%rbp,2), %rax
addq 0x10(%rsi), %rax
movzwl (%rax,%r14,2), %eax
andl $0x7, %eax
cmpw $0x1, %ax
adcl %eax, %ebx
leal 0x1(%r14), %eax
jmp 0x221b8
movq %r15, %rdi
movq %r9, %r13
callq 0x5697
movzwl 0x18(%r13), %r13d
addw 0x8(%rsp), %r13w
notl %r13d
addl %eax, %r13d
movq %r15, %rdi
callq 0x4ab2
movb %al, 0x7(%rsp)
testq %r14, %r14
je 0x2237e
movq %r12, 0x48(%rsp)
movl $0x1, %eax
subl %r14d, %eax
movq 0x28(%rsp), %r9
imulw 0x1e(%r9), %ax
addl 0x1c(%rsp), %eax
cwtl
movl %eax, 0x34(%rsp)
movzwl %bx, %eax
movl %eax, 0x30(%rsp)
movq 0x8(%rsp), %rax
movq %r13, 0x40(%rsp)
addl %r13d, %eax
movl %eax, 0x24(%rsp)
movq 0x10(%rsp), %rsi
movq 0x10(%rsi), %rax
xorl %r12d, %r12d
xorl %ebx, %ebx
movq %rbp, 0x50(%rsp)
leal (%r12,%rbp), %ecx
movzwl %cx, %ebp
movzwl (%rax,%rbp,2), %eax
andl $0x7, %eax
cmpw $0x1, %ax
adcl $0x0, %eax
movzbl %al, %eax
movl 0x34(%rsp), %ecx
imull %ecx, %eax
cltd
movl 0x30(%rsp), %edi
idivl %edi
movl %eax, %r13d
decl %r13d
movzwl %bx, %eax
imull %ecx, %eax
cltd
idivl %edi
movzwl 0x1e(%r9), %ecx
movl %r12d, %r15d
imull %ecx, %r15d
addl %eax, %r15d
cmpb $0x1, 0x7(%rsp)
jne 0x222e7
movq 0x38(%rsp), %rdi
callq 0x566d
movq 0x28(%rsp), %r9
movq 0x10(%rsp), %rsi
movzwl 0x1c(%r9), %edx
addw %r13w, %dx
addl %r15d, %edx
notl %edx
addl %eax, %edx
movzwl 0x1e(%r9), %ecx
jmp 0x222ef
addw 0x1a(%r9), %r15w
movl %r15d, %edx
testw %cx, %cx
jne 0x222fb
cmpw 0x1a(%r9), %dx
jne 0x2234f
leal (,%rbp,8), %edi
addq 0x8(%rsi), %rdi
addl %edx, %r13d
movswl %r13w, %ecx
movq %rsi, %r15
movswl %dx, %esi
movswl 0x8(%rsp), %edx
movswl 0x24(%rsp), %r8d
callq 0x28cbc
movq 0x10(%r15), %rax
movzwl (%rax,%rbp,2), %ecx
andl $0x7, %ecx
cmpw $0x1, %cx
adcl %ecx, %ebx
incl %r12d
cmpl %r12d, %r14d
movq %r15, %rsi
movq 0x28(%rsp), %r9
movq 0x50(%rsp), %rbp
jne 0x22271
jmp 0x22366
movq 0x8(%rsi), %rax
leaq (%rax,%rbp,8), %rdi
movq %rsi, %r15
movswl -0x4(%rdi), %esi
addl %r13d, %edx
movswl %dx, %ecx
jmp 0x22313
movl 0x20(%rsp), %ecx
addl %r12d, %ecx
movq 0x38(%rsp), %r15
movq 0x48(%rsp), %r12
movq 0x40(%rsp), %r13
jmp 0x2238c
movq 0x10(%rsp), %rsi
movq 0x28(%rsp), %r9
movl 0x20(%rsp), %ecx
movq 0x8(%rsp), %rax
addl %r13d, %eax
addw 0x1e(%r9), %ax
incl %eax
movq %rax, 0x8(%rsp)
leaq (%r12,%r14,8), %r12
addq $0x8, %r12
movq -0x8(%r12), %rax
cmpb $0x0, (%rax)
jne 0x221ad
movq %r15, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4c4d
movq 0x10(%r12), %rsi
leaq 0x1ccf8(%rip), %rdi     # 0x3f0d0
callq 0x8294
jmp 0x223dd