// File: /workspace/github_c_makefile_star2_O2/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 0x7994
testb %al, %al
je 0x1074b
movq %r12, %rdi
callq 0x7994
testb %al, %al
je 0x1075c
movq %rbx, %rdi
callq 0x6747
movq %rax, %r15
movq 0x10(%rax), %rdi
xorl %esi, %esi
callq 0x230ac
movq %rax, %r14
movq %rbx, %rdi
callq 0x5308
movl %eax, %ebp
movq 0x10(%r15), %rdi
callq 0x5332
movswl %bp, %esi
movswl %ax, %edx
movq %r14, %rdi
callq 0x520e
pushq $0x3
popq %rsi
movq %r14, %rdi
callq 0x23fed
leaq 0x4da98(%rip), %rdx     # 0x5e0c8
movq %r14, %rdi
xorl %esi, %esi
callq 0x24089
leaq 0x4d9f7(%rip), %rdx     # 0x5e038
pushq $0x1
popq %rbp
movq %r14, %rdi
movl %ebp, %esi
callq 0x24089
cmpq $0x0, 0x4dce2(%rip)     # 0x5e338
jne 0x10667
movq %r14, %rdi
callq 0x66db
movq %rax, 0x4dcd1(%rip)     # 0x5e338
cmpq $0x0, 0x4dcd1(%rip)     # 0x5e340
jne 0x10688
movq %r14, %rdi
callq 0x245d9
movq %rax, %rdi
callq 0x66db
movq %rax, 0x4dcb8(%rip)     # 0x5e340
leaq 0x302(%rip), %rsi       # 0x10991
movq %r14, %rdi
callq 0x60e6
movq %r14, %rdi
callq 0x245d9
leaq 0x390(%rip), %rsi       # 0x10a36
movq %rax, %rdi
callq 0x60e6
movq %r12, %rdi
callq 0x2070
leaq 0x1(%rax), %rdi
callq 0x279f9
movq %rax, %r13
movq %rax, %rdi
callq 0x7994
testb %al, %al
je 0x1076d
testq %r13, %r13
je 0x1077e
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 0x27ad9
movq %rax, 0x18(%r15)
movq %rax, %rdi
callq 0x7994
movq 0x18(%r15), %rsi
testb %al, %al
je 0x10786
testq %rsi, %rsi
je 0x1077e
testb $0x40, 0x2a(%r15)
movzwl 0x26(%r15), %eax
jne 0x10794
leal (,%rax,8), %eax
movq %r13, -0x8(%rsi,%rax)
addq 0x18(%r15), %rax
jmp 0x107c9
leaq 0x2a9f2(%rip), %rdi     # 0x3b144
movq %rbx, %rsi
callq 0x7b01
jmp 0x1075a
leaq 0x2a9e1(%rip), %rdi     # 0x3b144
movq %r12, %rsi
callq 0x7b01
jmp 0x1076b
leaq 0x2a8cc(%rip), %rdi     # 0x3b040
movq %r13, %rsi
callq 0x7b01
jmp 0x1077c
xorl %r14d, %r14d
jmp 0x108e4
leaq 0x2a8b3(%rip), %rdi     # 0x3b040
callq 0x7b01
jmp 0x10792
cmpl $0x1, %eax
jne 0x107a6
movq %r13, (%rsi)
movq 0x18(%r15), %rax
addq $0x8, %rax
jmp 0x107c9
shll $0x4, %eax
leaq 0x2ec94(%rip), %rcx     # 0x3f444
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 0x2ec75(%rip), %rcx     # 0x3f445
movq %rcx, (%rax)
movq (%r15), %rdi
callq 0x6747
andq $0x0, (%rax)
movq (%r15), %rdi
movq 0x18(%r15), %rsi
callq 0x1fe53
movq (%r15), %rdi
movzwl 0x24(%r15), %esi
pushq $0x10
popq %rdx
callq 0x204cc
movq (%r15), %rdi
callq 0x6316
movq %rax, %r12
testb $0x40, 0x2a(%r15)
jne 0x10869
movq %rbx, %rdi
callq 0x5308
cwtl
movswl 0x1e(%r12), %ecx
movzwl 0x26(%r15), %esi
subl %esi, %ebp
imull %ecx, %ebp
movswl 0x1a(%r12), %ecx
movswl 0x1c(%r12), %edx
addl %ecx, %edx
subl %edx, %eax
addl %ebp, %eax
cltd
idivl %esi
movq 0x8(%r15), %rdi
movswl %ax, %ebp
movl %ebp, %esi
callq 0x53a8
addw 0x1e(%r12), %bp
imulw 0x24(%r15), %bp
movq 0x8(%r15), %rdi
addw 0x1a(%r12), %bp
movswl %bp, %esi
callq 0x5192
jmp 0x108bf
movq (%r15), %rdi
callq 0x5332
movzwl 0x16(%r12), %ecx
movzwl 0x26(%r15), %esi
subl %esi, %ebp
movzwl 0x1e(%r12), %edi
imull %edi, %ebp
movzwl 0x18(%r12), %edx
addw %cx, %dx
subl %edx, %eax
addl %ebp, %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 0x53e6
movq 0x8(%r15), %rdi
movswl %bp, %esi
callq 0x51d0
cmpw $0x1, 0x26(%r15)
jne 0x108cd
andw $0x0, 0x24(%r15)
movq %rbx, %rdi
callq 0x10ae4
movzwl 0x24(%r15), %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x10f12
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq