// File: /workspace/github_c_makefile_star2_O1/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 $0x68, %rsp
movq %rsi, %r13
movq %rdi, %r15
callq 0x80d4
testb %al, %al
je 0x217a0
movq %r13, %rdi
callq 0x80d4
testb %al, %al
je 0x217b1
movq %r15, %rdi
callq 0x6ad8
movq %rax, 0x30(%rsp)
movq (%rax), %rbx
cmpq %r13, %rbx
je 0x218bc
testq %rbx, %rbx
sete %al
testq %r13, %r13
sete %cl
orb %al, %cl
jne 0x217da
movq (%rbx), %rdi
cmpb $0x0, (%rdi)
sete %cl
je 0x217c2
movq (%r13), %rsi
cmpb $0x0, (%rsi)
je 0x217c6
movw $0x1, %bp
callq 0x20e0
testl %eax, %eax
jne 0x217da
movzwl %bp, %eax
movq (%rbx,%rax,8), %rdi
cmpb $0x0, (%rdi)
sete %cl
je 0x217cc
movq (%r13,%rax,8), %rsi
incl %ebp
cmpb $0x0, (%rsi)
jne 0x2177a
jmp 0x217c8
leaq 0x1d98d(%rip), %rdi     # 0x3f134
movq %r15, %rsi
callq 0x829e
jmp 0x217af
leaq 0x1d97c(%rip), %rdi     # 0x3f134
movq %r13, %rsi
callq 0x829e
jmp 0x217c0
xorl %eax, %eax
jmp 0x217cc
xorl %eax, %eax
testb %cl, %cl
je 0x217da
movq (%r13,%rax,8), %rax
cmpb $0x0, (%rax)
je 0x218bc
movq (%r13), %rdi
cmpb $0x0, (%rdi)
je 0x21813
movw $0x1, %r14w
xorl %ebp, %ebp
leaq 0x21bdb(%rip), %rbx     # 0x433cc
movq %rbx, %rsi
callq 0x20e0
cmpl $0x1, %eax
sbbw $-0x1, %bp
movzwl %r14w, %eax
movq (%r13,%rax,8), %rdi
incl %r14d
cmpb $0x0, (%rdi)
jne 0x217f1
jmp 0x21815
xorl %ebp, %ebp
movq %r15, %rdi
callq 0x6ad8
movq %rax, %r12
movq 0x8(%rax), %rdi
testq %rdi, %rdi
je 0x21837
callq 0x29984
movq $0x0, 0x8(%r12)
movq 0x10(%r12), %rdi
testq %rdi, %rdi
je 0x2184f
callq 0x29984
movq $0x0, 0x10(%r12)
movzwl %bp, %ebx
leal (,%rbx,8), %edi
callq 0x298d9
movq %rax, 0x8(%r12)
movq %rax, %rdi
callq 0x80d4
testb %al, %al
je 0x2194d
addl %ebx, %ebx
movq %rbx, %rdi
callq 0x298d9
movq %rax, 0x10(%r12)
movq %rax, %rdi
callq 0x80d4
testb %al, %al
je 0x21c0d
cmpq $0x0, 0x8(%r12)
je 0x218a2
cmpq $0x0, 0x10(%r12)
jne 0x218a4
xorl %ebp, %ebp
movq 0x10(%r12), %rdi
movzwl %bp, %ebx
leal (%rbx,%rbx), %edx
xorl %esi, %esi
callq 0x20a0
movw %bx, 0x40(%r12)
movq 0x30(%rsp), %rax
movq %r13, (%rax)
movq %r15, %rdi
xorl %esi, %esi
callq 0x21ccf
movq %rax, %r14
movq %r15, %rdi
callq 0x5651
movl %eax, 0x24(%rsp)
movzwl 0x1a(%r14), %eax
movl %eax, 0x38(%rsp)
movzwl 0x1c(%r14), %eax
movl %eax, 0x8(%rsp)
movq %r15, 0x48(%rsp)
movq %r15, %rdi
callq 0x567b
movl %eax, %ebx
movzwl 0x16(%r14), %eax
movq %rax, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movzwl 0x18(%r14), %r15d
movq %r13, %rbp
movq (%r13), %rdi
cmpb $0x0, (%rdi)
je 0x21960
movb $0x1, %r14b
leaq 0x21aa4(%rip), %r13     # 0x433cc
movb $0x1, %r12b
movq %r13, %rsi
callq 0x20e0
cmpl $0x1, %eax
adcb $0x0, %r12b
movzbl %r14b, %r14d
movq (%rbp,%r14,8), %rdi
incb %r14b
cmpb $0x0, (%rdi)
jne 0x2192b
jmp 0x21963
movq 0x8(%r12), %rsi
leaq 0x1d6e7(%rip), %rdi     # 0x3f040
callq 0x829e
jmp 0x2195e
movb $0x1, %r12b
movzbl %r12b, %ecx
movl $0x1, %eax
subl %ecx, %eax
movq 0x18(%rsp), %rdx
imulw 0x1e(%rdx), %ax
addl 0x10(%rsp), %r15d
subl %r15d, %ebx
addl %eax, %ebx
movswl %bx, %eax
cltd
idivl %ecx
decl %eax
movq %rax, 0x40(%rsp)
movl 0x38(%rsp), %eax
addl 0x8(%rsp), %eax
subl %eax, 0x24(%rsp)
xorl %r13d, %r13d
movq 0x48(%rsp), %r15
movq %rbp, %r12
movq (%r12), %rbx
movq %rbx, %rdi
leaq 0x21a18(%rip), %rsi     # 0x433cc
callq 0x20e0
movzwl %r13w, %ecx
movq %rcx, 0x38(%rsp)
testl %eax, %eax
je 0x21a28
movq $0x0, 0x8(%rsp)
xorl %ebp, %ebp
xorl %r14d, %r14d
cmpb $0x0, (%rbx)
je 0x21a1e
movq 0x38(%rsp), %rax
addq %rax, %rax
movq 0x30(%rsp), %rcx
addq 0x10(%rcx), %rax
movq 0x8(%rsp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x7, %eax
cmpw $0x1, %ax
adcl %eax, %r14d
incl %ebp
movzwl %bp, %eax
movq %rax, 0x8(%rsp)
movq (%r12,%rax,8), %rbx
movq %rbx, %rdi
leaq 0x219b7(%rip), %rsi     # 0x433cc
callq 0x20e0
testl %eax, %eax
jne 0x219d4
movzwl %r14w, %eax
movl %eax, 0x2c(%rsp)
jmp 0x21a3b
movq $0x0, 0x8(%rsp)
movl $0x0, 0x2c(%rsp)
xorl %ebp, %ebp
cmpb $0x0, (%rbx)
jne 0x21a5f
movq %r15, %rdi
callq 0x567b
movq 0x18(%rsp), %rcx
movzwl 0x18(%rcx), %ecx
addw 0x10(%rsp), %cx
notl %ecx
addl %eax, %ecx
movq %rcx, 0x40(%rsp)
movq %r15, %rdi
callq 0x4abc
movb %al, 0x7(%rsp)
testw %bp, %bp
je 0x21bb9
movq %r13, 0x58(%rsp)
movq %r12, 0x60(%rsp)
movl $0x1, %eax
subl %ebp, %eax
movq 0x18(%rsp), %rsi
imulw 0x1e(%rsi), %ax
addl 0x24(%rsp), %eax
movswl %ax, %r13d
movq 0x10(%rsp), %rax
movq 0x40(%rsp), %rcx
addl %ecx, %eax
movzwl %bp, %ecx
movl %ecx, 0x54(%rsp)
xorl %ebp, %ebp
cwtl
movl %eax, 0x28(%rsp)
xorl %r12d, %r12d
movq 0x30(%rsp), %r15
movq 0x38(%rsp), %rax
addl %ebp, %eax
movq 0x10(%r15), %rcx
movzwl %ax, %r14d
movzwl (%rcx,%r14,2), %eax
andl $0x7, %eax
cmpw $0x1, %ax
adcl $0x0, %eax
movzbl %al, %eax
imull %r13d, %eax
cltd
movl 0x2c(%rsp), %ecx
idivl %ecx
movl %eax, %ebx
decl %ebx
movzwl %r12w, %eax
imull %r13d, %eax
cltd
idivl %ecx
movzwl 0x1e(%rsi), %r15d
imull %ebp, %r15d
addl %eax, %r15d
cmpb $0x1, 0x7(%rsp)
jne 0x21b2a
movq 0x48(%rsp), %rdi
callq 0x5651
movq 0x18(%rsp), %rsi
movzwl 0x1c(%rsi), %r8d
addw %bx, %r8w
addl %r15d, %r8d
notl %r8d
addl %eax, %r8d
jmp 0x21b32
addw 0x1a(%rsi), %r15w
movl %r15d, %r8d
cmpw $0x0, 0x1e(%rsi)
movswl 0x10(%rsp), %edx
movq 0x30(%rsp), %r15
jne 0x21b4a
cmpw 0x1a(%rsi), %r8w
jne 0x21b90
leal (,%r14,8), %edi
addq 0x8(%r15), %rdi
addl %r8d, %ebx
movswl %bx, %ecx
movswl %r8w, %esi
movl 0x28(%rsp), %r8d
callq 0x285f8
movq 0x10(%r15), %rax
movzwl (%rax,%r14,2), %eax
andl $0x7, %eax
cmpw $0x1, %ax
adcl %eax, %r12d
incl %ebp
cmpl %ebp, 0x54(%rsp)
movq 0x18(%rsp), %rsi
jne 0x21ab9
jmp 0x21ba5
movq 0x8(%r15), %rax
leaq (%rax,%r14,8), %rdi
movswl -0x4(%rdi), %esi
addl %ebx, %r8d
movswl %r8w, %ecx
jmp 0x21b60
movq 0x58(%rsp), %r13
addl %ebp, %r13d
movq 0x48(%rsp), %r15
movq 0x60(%rsp), %r12
jmp 0x21bbe
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rax
movq 0x40(%rsp), %rcx
addl %ecx, %eax
addw 0x1e(%rsi), %ax
incl %eax
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rcx
movq (%r12,%rcx,8), %rax
incq %rcx
cmpb $0x0, (%rax)
movl $0x0, %eax
cmovneq %rcx, %rax
leaq (%r12,%rax,8), %r12
jne 0x219a6
movq %r15, %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4c36
movq 0x10(%r12), %rsi
leaq 0x1d427(%rip), %rdi     # 0x3f040
callq 0x829e
jmp 0x21c1e