// File: /workspace/github_c_makefile_star2_O3/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 0x80e4
testb %al, %al
je 0x1140a
movq %r12, %rdi
callq 0x80e4
testb %al, %al
je 0x1141b
movq %rbx, %rdi
callq 0x6af7
movq %rax, %r15
movq 0x10(%rax), %rdi
xorl %esi, %esi
callq 0x25398
movq %rax, %r14
movq %rbx, %rdi
callq 0x566d
movl %eax, %ebp
movq 0x10(%r15), %rdi
callq 0x5697
movswl %bp, %esi
movswl %ax, %edx
movq %r14, %rdi
callq 0x5557
movq %r14, %rdi
movl $0x3, %esi
callq 0x2636f
leaq 0x4edd9(%rip), %rdx     # 0x600c8
movq %r14, %rdi
xorl %esi, %esi
callq 0x2640b
leaq 0x4ed38(%rip), %rdx     # 0x60038
movq %r14, %rdi
movl $0x1, %esi
callq 0x2640b
cmpq $0x0, 0x4f023(%rip)     # 0x60338
jne 0x11326
movq %r14, %rdi
callq 0x6a8b
movq %rax, 0x4f012(%rip)     # 0x60338
cmpq $0x0, 0x4f012(%rip)     # 0x60340
jne 0x11347
movq %r14, %rdi
callq 0x2695e
movq %rax, %rdi
callq 0x6a8b
movq %rax, 0x4eff9(%rip)     # 0x60340
leaq 0x333(%rip), %rsi       # 0x11681
movq %r14, %rdi
callq 0x647b
movq %r14, %rdi
callq 0x2695e
leaq 0x3da(%rip), %rsi       # 0x1173f
movq %rax, %rdi
callq 0x647b
movq %r12, %rdi
callq 0x2070
leaq 0x1(%rax), %rdi
callq 0x29f85
movq %rax, %r13
movq %rax, %rdi
callq 0x80e4
testb %al, %al
je 0x1142c
testq %r13, %r13
je 0x1143d
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 0x2a06f
movq %rax, 0x18(%r15)
movq %rax, %rdi
callq 0x80e4
movq 0x18(%r15), %rsi
testb %al, %al
je 0x11445
testq %rsi, %rsi
je 0x1143d
testb $0x40, 0x2a(%r15)
movzwl 0x26(%r15), %eax
jne 0x11453
leal (,%rax,8), %eax
movq %r13, -0x8(%rsi,%rax)
addq 0x18(%r15), %rax
jmp 0x11488
leaq 0x2ddd3(%rip), %rdi     # 0x3f1e4
movq %rbx, %rsi
callq 0x8294
jmp 0x11419
leaq 0x2ddc2(%rip), %rdi     # 0x3f1e4
movq %r12, %rsi
callq 0x8294
jmp 0x1142a
leaq 0x2dc9d(%rip), %rdi     # 0x3f0d0
movq %r13, %rsi
callq 0x8294
jmp 0x1143b
xorl %r14d, %r14d
jmp 0x115b5
leaq 0x2dc84(%rip), %rdi     # 0x3f0d0
callq 0x8294
jmp 0x11451
cmpl $0x1, %eax
jne 0x11465
movq %r13, (%rsi)
movq 0x18(%r15), %rax
addq $0x8, %rax
jmp 0x11488
shll $0x4, %eax
leaq 0x3214d(%rip), %rcx     # 0x435bc
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 0x3212e(%rip), %rcx     # 0x435bd
movq %rcx, (%rax)
movq (%r15), %rdi
callq 0x6af7
movq $0x0, (%rax)
movq (%r15), %rdi
movq 0x18(%r15), %rsi
callq 0x21f33
movq (%r15), %rdi
movzwl 0x24(%r15), %esi
movl $0x10, %edx
callq 0x225f2
movq (%r15), %rdi
callq 0x66b6
movq %rax, %r12
testb $0x40, 0x2a(%r15)
jne 0x11532
movq %rbx, %rdi
callq 0x566d
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 0x570d
addw 0x1e(%r12), %bp
imulw 0x24(%r15), %bp
movq 0x8(%r15), %rdi
addw 0x1a(%r12), %bp
movswl %bp, %esi
callq 0x54db
jmp 0x1158f
movq (%r15), %rdi
callq 0x5697
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 0x574b
movq 0x8(%r15), %rdi
movswl %bp, %esi
callq 0x5519
cmpw $0x1, 0x26(%r15)
jne 0x1159e
movw $0x0, 0x24(%r15)
movq %rbx, %rdi
callq 0x11808
movzwl 0x24(%r15), %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x11c83
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq