// File: /workspace/github_c_makefile_star2_O1/OnionIoT[P]lv_example/lvgl/src/lv_objx/lv_tabview.c
// Function: lv_tabview_add_tab
lv_obj_t * lv_tabview_add_tab(lv_obj_t * tabview, const char * name)
{
    LV_ASSERT_OBJ(tabview, LV_OBJX_NAME);
    LV_ASSERT_STR(name);

    lv_tabview_ext_t * ext = lv_obj_get_ext_attr(tabview);

    /*Create the container page*/
    lv_obj_t * h = lv_page_create(ext->content, NULL);
    lv_obj_set_size(h, lv_obj_get_width(tabview), lv_obj_get_height(ext->content));
    lv_page_set_sb_mode(h, LV_SB_MODE_AUTO);
    lv_page_set_style(h, LV_PAGE_STYLE_BG, &lv_style_transp_tight);
    lv_page_set_style(h, LV_PAGE_STYLE_SCRL, &lv_style_transp);//plain_color);

    if(page_signal == NULL) page_signal = lv_obj_get_signal_cb(h);
    if(page_scrl_signal == NULL) page_scrl_signal = lv_obj_get_signal_cb(lv_page_get_scrl(h));
    lv_obj_set_signal_cb(h, tabpage_signal);
    lv_obj_set_signal_cb(lv_page_get_scrl(h), tabpage_scrl_signal);

    /*Extend the button matrix map with the new name*/
    char * name_dm;
    name_dm = lv_mem_alloc(strlen(name) + 1); /*+1 for the the closing '\0' */
    LV_ASSERT_MEM(name_dm);
    if(name_dm == NULL) return NULL;
    strcpy(name_dm, name);

    ext->tab_cnt++;

    switch(ext->btns_pos) {
        case LV_TABVIEW_BTNS_POS_TOP:
        case LV_TABVIEW_BTNS_POS_BOTTOM:
            ext->tab_name_ptr = lv_mem_realloc((void*)ext->tab_name_ptr, sizeof(char *) * (ext->tab_cnt + 1));
            break;
        case LV_TABVIEW_BTNS_POS_LEFT:
        case LV_TABVIEW_BTNS_POS_RIGHT:
            ext->tab_name_ptr = lv_mem_realloc((void*)ext->tab_name_ptr, sizeof(char *) * (ext->tab_cnt * 2));
            break;
    }

    LV_ASSERT_MEM(ext->tab_name_ptr);
    if(ext->tab_name_ptr == NULL) return NULL;

    /* FIXME: It is not possible yet to switch tab button position from/to top/bottom from/to left/right at runtime.
     * Method: clean extra \n when switch from LV_TABVIEW_BTNS_POS_LEFT or LV_TABVIEW_BTNS_POS_RIGHT
     * to LV_TABVIEW_BTNS_POS_TOP or LV_TABVIEW_BTNS_POS_BOTTOM.
     */
    switch(ext->btns_pos) {
        case LV_TABVIEW_BTNS_POS_TOP:
        case LV_TABVIEW_BTNS_POS_BOTTOM:
            ext->tab_name_ptr[ext->tab_cnt - 1] = name_dm;
            ext->tab_name_ptr[ext->tab_cnt]     = "";
            break;
        case LV_TABVIEW_BTNS_POS_LEFT:
        case LV_TABVIEW_BTNS_POS_RIGHT:
            if(ext->tab_cnt == 1) {
                ext->tab_name_ptr[0] = name_dm;
                ext->tab_name_ptr[1] = "";
            } else {
                ext->tab_name_ptr[ext->tab_cnt * 2 - 3] = "\n";
                ext->tab_name_ptr[ext->tab_cnt * 2 - 2] = name_dm;
                ext->tab_name_ptr[ext->tab_cnt * 2 - 1] = "";
            }
            break;
    }

    /* The button matrix's map still points to the old `tab_name_ptr` which might be freed by
     * `lv_mem_realloc`. So make its current map invalid*/
    lv_btnm_ext_t * btnm_ext = lv_obj_get_ext_attr(ext->btns);
    btnm_ext->map_p          = NULL;

    lv_btnm_set_map(ext->btns, ext->tab_name_ptr);
    lv_btnm_set_btn_ctrl(ext->btns, ext->tab_cur, LV_BTNM_CTRL_NO_REPEAT);

    /*Modify the indicator size*/
    const lv_style_t * style_tabs = lv_obj_get_style(ext->btns);
    lv_coord_t indic_size;
    lv_coord_t max_h, btn_h, act_y;

    switch(ext->btns_pos) {
        case LV_TABVIEW_BTNS_POS_TOP:
        case LV_TABVIEW_BTNS_POS_BOTTOM:
            indic_size = (lv_obj_get_width(tabview) - style_tabs->body.padding.inner * (ext->tab_cnt - 1) -
                          style_tabs->body.padding.left - style_tabs->body.padding.right) /
                         ext->tab_cnt;
            lv_obj_set_width(ext->indic, indic_size);
            lv_obj_set_x(ext->indic, indic_size * ext->tab_cur + style_tabs->body.padding.inner * ext->tab_cur +
                                         style_tabs->body.padding.left);
            break;
        case LV_TABVIEW_BTNS_POS_LEFT:
        case LV_TABVIEW_BTNS_POS_RIGHT:
            max_h = lv_obj_get_height(ext->btns) - style_tabs->body.padding.top - style_tabs->body.padding.bottom;
            btn_h = max_h - ((ext->tab_cnt - 1) * style_tabs->body.padding.inner);
            btn_h = btn_h / ext->tab_cnt;
            btn_h--; /*-1 because e.g. height = 100 means 101 pixels (0..100)*/
            act_y = style_tabs->body.padding.top + ext->tab_cur * (btn_h + style_tabs->body.padding.inner);

            lv_obj_set_height(ext->indic, btn_h);
            lv_obj_set_y(ext->indic, act_y);
            break;
    }

    /*Set the first btn as active*/
    if(ext->tab_cnt == 1) {
        ext->tab_cur = 0;
    }

    tabview_realign(tabview); /*Set the size of the pages, tab buttons and indicator*/

    lv_tabview_set_tab_act(tabview, ext->tab_cur, false);

    return h;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r12
movq %rdi, %rbx
callq 0x80d4
testb %al, %al
je 0x113d5
movq %r12, %rdi
callq 0x80d4
testb %al, %al
je 0x113e6
movq %rbx, %rdi
callq 0x6ad8
movq %rax, %r15
movq 0x10(%rax), %rdi
xorl %esi, %esi
callq 0x24c9c
movq %rax, %r14
movq %rbx, %rdi
callq 0x5651
movl %eax, %ebp
movq 0x10(%r15), %rdi
callq 0x567b
movswl %bp, %esi
movswl %ax, %edx
movq %r14, %rdi
callq 0x553f
movq %r14, %rdi
movl $0x3, %esi
callq 0x25c83
leaq 0x4ee0e(%rip), %rdx     # 0x600c8
movq %r14, %rdi
xorl %esi, %esi
callq 0x25d1f
leaq 0x4ed6d(%rip), %rdx     # 0x60038
movq %r14, %rdi
movl $0x1, %esi
callq 0x25d1f
cmpq $0x0, 0x4f058(%rip)     # 0x60338
jne 0x112f1
movq %r14, %rdi
callq 0x6a6c
movq %rax, 0x4f047(%rip)     # 0x60338
cmpq $0x0, 0x4f047(%rip)     # 0x60340
jne 0x11312
movq %r14, %rdi
callq 0x26288
movq %rax, %rdi
callq 0x6a6c
movq %rax, 0x4f02e(%rip)     # 0x60340
leaq 0x333(%rip), %rsi       # 0x1164c
movq %r14, %rdi
callq 0x645c
movq %r14, %rdi
callq 0x26288
leaq 0x3d8(%rip), %rsi       # 0x11708
movq %rax, %rdi
callq 0x645c
movq %r12, %rdi
callq 0x2070
leaq 0x1(%rax), %rdi
callq 0x298d9
movq %rax, %r13
movq %rax, %rdi
callq 0x80d4
testb %al, %al
je 0x113f7
testq %r13, %r13
je 0x11408
movq %r13, %rdi
movq %r12, %rsi
callq 0x2040
movzwl 0x26(%r15), %eax
incl %eax
movw %ax, 0x26(%r15)
movzwl %ax, %eax
movl %eax, %ecx
shll $0x4, %ecx
testb $0x40, 0x2a(%r15)
leaq 0x8(,%rax,8), %rsi
movq 0x18(%r15), %rdi
cmovneq %rcx, %rsi
callq 0x299f8
movq %rax, 0x18(%r15)
movq %rax, %rdi
callq 0x80d4
movq 0x18(%r15), %rsi
testb %al, %al
je 0x11410
testq %rsi, %rsi
je 0x11408
testb $0x40, 0x2a(%r15)
movzwl 0x26(%r15), %eax
jne 0x1141e
leal (,%rax,8), %eax
movq %r13, -0x8(%rsi,%rax)
addq 0x18(%r15), %rax
jmp 0x11453
leaq 0x2dd58(%rip), %rdi     # 0x3f134
movq %rbx, %rsi
callq 0x829e
jmp 0x113e4
leaq 0x2dd47(%rip), %rdi     # 0x3f134
movq %r12, %rsi
callq 0x829e
jmp 0x113f5
leaq 0x2dc42(%rip), %rdi     # 0x3f040
movq %r13, %rsi
callq 0x829e
jmp 0x11406
xorl %r14d, %r14d
jmp 0x11580
leaq 0x2dc29(%rip), %rdi     # 0x3f040
callq 0x829e
jmp 0x1141c
cmpl $0x1, %eax
jne 0x11430
movq %r13, (%rsi)
movq 0x18(%r15), %rax
addq $0x8, %rax
jmp 0x11453
shll $0x4, %eax
leaq 0x31f92(%rip), %rcx     # 0x433cc
movq %rcx, -0x18(%rsi,%rax)
movq 0x18(%r15), %rcx
movq %r13, -0x10(%rcx,%rax)
movq 0x18(%r15), %rcx
addq %rcx, %rax
addq $-0x8, %rax
leaq 0x31f73(%rip), %rcx     # 0x433cd
movq %rcx, (%rax)
movq (%r15), %rdi
callq 0x6ad8
movq $0x0, (%rax)
movq (%r15), %rdi
movq 0x18(%r15), %rsi
callq 0x21710
movq (%r15), %rdi
movzwl 0x24(%r15), %esi
movl $0x10, %edx
callq 0x21e33
movq (%r15), %rdi
callq 0x6697
movq %rax, %r12
testb $0x40, 0x2a(%r15)
jne 0x114fd
movq %rbx, %rdi
callq 0x5651
cwtl
movswl 0x1e(%r12), %ecx
movzwl 0x26(%r15), %esi
movl $0x1, %edx
subl %esi, %edx
imull %ecx, %edx
movswl 0x1a(%r12), %ecx
movswl 0x1c(%r12), %edi
addl %ecx, %edi
subl %edi, %eax
addl %edx, %eax
cltd
idivl %esi
movq 0x8(%r15), %rdi
movswl %ax, %ebp
movl %ebp, %esi
callq 0x56f1
addw 0x1e(%r12), %bp
imulw 0x24(%r15), %bp
movq 0x8(%r15), %rdi
addw 0x1a(%r12), %bp
movswl %bp, %esi
callq 0x54c3
jmp 0x1155a
movq (%r15), %rdi
callq 0x567b
movzwl 0x16(%r12), %ecx
movzwl 0x26(%r15), %esi
movl $0x1, %edx
subl %esi, %edx
movzwl 0x1e(%r12), %edi
imull %edi, %edx
movzwl 0x18(%r12), %r8d
addw %cx, %r8w
subl %r8d, %eax
addl %edx, %eax
cwtl
cltd
idivl %esi
leal (%rax,%rdi), %ebp
decl %ebp
imulw 0x24(%r15), %bp
decl %eax
addl %ecx, %ebp
movq 0x8(%r15), %rdi
movswl %ax, %esi
callq 0x572f
movq 0x8(%r15), %rdi
movswl %bp, %esi
callq 0x5501
cmpw $0x1, 0x26(%r15)
jne 0x11569
movw $0x0, 0x24(%r15)
movq %rbx, %rdi
callq 0x117cf
movzwl 0x24(%r15), %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x11c4a
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq