// File: /workspace/github_c_makefile_star2_O3/OnionIoT[P]lv_example/lvgl/src/lv_draw/lv_img_decoder.c
// Function: lv_img_decoder_built_in_open
lv_res_t lv_img_decoder_built_in_open(lv_img_decoder_t * decoder, lv_img_decoder_dsc_t * dsc)
{
    /*Open the file if it's a file*/
    if(dsc->src_type == LV_IMG_SRC_FILE) {
#if LV_USE_FILESYSTEM

        /*Support only "*.bin" files*/
        if(strcmp(lv_fs_get_ext(dsc->src), "bin")) return LV_RES_INV;

        lv_fs_file_t f;
        lv_fs_res_t res = lv_fs_open(&f, dsc->src, LV_FS_MODE_RD);
        if(res != LV_FS_RES_OK) {
            LV_LOG_WARN("Built-in image decoder can't open the file");
            return LV_RES_INV;
        }

        /*If the file was open successfully save the file descriptor*/
        if(dsc->user_data == NULL) {
            dsc->user_data = lv_mem_alloc(sizeof(lv_img_decoder_built_in_data_t));
            if(dsc->user_data == NULL) {
                LV_LOG_ERROR("img_decoder_built_in_open: out of memory");
                LV_ASSERT_MEM(dsc->user_data);
            }
            memset(dsc->user_data, 0, sizeof(lv_img_decoder_built_in_data_t));
        }

        lv_img_decoder_built_in_data_t * user_data = dsc->user_data;
        user_data->f                               = lv_mem_alloc(sizeof(f));
        if(user_data->f == NULL) {
            LV_LOG_ERROR("img_decoder_built_in_open: out of memory");
            LV_ASSERT_MEM(user_data->f);
        }

        memcpy(user_data->f, &f, sizeof(f));

#else
        LV_LOG_WARN("Image built-in decoder cannot read file because LV_USE_FILESYSTEM = 0");
        return LV_RES_INV;
#endif
    }

    lv_img_cf_t cf = dsc->header.cf;
    /*Process true color formats*/
    if(cf == LV_IMG_CF_TRUE_COLOR || cf == LV_IMG_CF_TRUE_COLOR_ALPHA || cf == LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED) {
        if(dsc->src_type == LV_IMG_SRC_VARIABLE) {
            /* In case of uncompressed formats the image stored in the ROM/RAM.
             * So simply give its pointer*/
            dsc->img_data = ((lv_img_dsc_t *)dsc->src)->data;
            return LV_RES_OK;
        } else {
            /*If it's a file it need to be read line by line later*/
            dsc->img_data = NULL;
            return LV_RES_OK;
        }
    }
    /*Process indexed images. Build a palette*/
    else if(cf == LV_IMG_CF_INDEXED_1BIT || cf == LV_IMG_CF_INDEXED_2BIT || cf == LV_IMG_CF_INDEXED_4BIT ||
            cf == LV_IMG_CF_INDEXED_8BIT) {

#if LV_IMG_CF_INDEXED
        uint8_t px_size       = lv_img_color_format_get_px_size(cf);
        uint32_t palette_size = 1 << px_size;

        /*Allocate the palette*/
        if(dsc->user_data == NULL) {
            dsc->user_data = lv_mem_alloc(sizeof(lv_img_decoder_built_in_data_t));
            if(dsc->user_data == NULL) {
                LV_LOG_ERROR("img_decoder_built_in_open: out of memory");
                LV_ASSERT_MEM(dsc->user_data);
            }
            memset(dsc->user_data, 0, sizeof(lv_img_decoder_built_in_data_t));
        }

        lv_img_decoder_built_in_data_t * user_data = dsc->user_data;
        user_data->palette                         = lv_mem_alloc(palette_size * sizeof(lv_color_t));
        user_data->opa                             = lv_mem_alloc(palette_size * sizeof(lv_opa_t));
        if(user_data->palette == NULL || user_data->opa == NULL) {
            LV_LOG_ERROR("img_decoder_built_in_open: out of memory");
#if LV_USE_FILESYSTEM
            LV_ASSERT_MEM(user_data->f);
#endif
        }

        if(dsc->src_type == LV_IMG_SRC_FILE) {
            /*Read the palette from file*/
#if LV_USE_FILESYSTEM
            lv_fs_seek(user_data->f, 4); /*Skip the header*/
            lv_color32_t cur_color;
            uint32_t i;
            for(i = 0; i < palette_size; i++) {
                lv_fs_read(user_data->f, &cur_color, sizeof(lv_color32_t), NULL);
                user_data->palette[i] = lv_color_make(cur_color.ch.red, cur_color.ch.green, cur_color.ch.blue);
                user_data->opa[i]     = cur_color.ch.alpha;
            }
#else
            LV_LOG_WARN("Image built-in decoder can read the palette because LV_USE_FILESYSTEM = 0");
            return LV_RES_INV;
#endif
        } else {
            /*The palette begins in the beginning of the image data. Just point to it.*/
            lv_color32_t * palette_p = (lv_color32_t *)((lv_img_dsc_t *)dsc->src)->data;

            uint32_t i;
            for(i = 0; i < palette_size; i++) {
                user_data->palette[i] = lv_color_make(palette_p[i].ch.red, palette_p[i].ch.green, palette_p[i].ch.blue);
                user_data->opa[i]     = palette_p[i].ch.alpha;
            }
        }

        dsc->img_data = NULL;
        return LV_RES_OK;
#else
        LV_LOG_WARN("Indexed (palette) images are not enabled in lv_conf.h. See LV_IMG_CF_INDEXED");
        return LV_RES_INV;
#endif
    }
    /*Alpha indexed images. */
    else if(cf == LV_IMG_CF_ALPHA_1BIT || cf == LV_IMG_CF_ALPHA_2BIT || cf == LV_IMG_CF_ALPHA_4BIT ||
            cf == LV_IMG_CF_ALPHA_8BIT) {
#if LV_IMG_CF_ALPHA
        dsc->img_data = NULL;
        return LV_RES_OK; /*Nothing to process*/
#else
        LV_LOG_WARN("Alpha indexed images are not enabled in lv_conf.h. See LV_IMG_CF_ALPHA");
        return LV_RES_INV;
#endif
    }
    /*Unknown format. Can't decode it.*/
    else {
        /*Free the potentially allocated memories*/
        lv_img_decoder_built_in_close(decoder, dsc);

        LV_LOG_WARN("Image decoder open: unknown color format")
        return LV_RES_INV;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
cmpb $0x1, 0x18(%rsi)
jne 0x3dd65
movq 0x8(%rbx), %rdi
callq 0x298a7
leaq 0x5871(%rip), %rsi      # 0x4353a
movq %rax, %rdi
callq 0x20e0
testl %eax, %eax
jne 0x3dec4
movq 0x8(%rbx), %rsi
leaq 0x8(%rsp), %rdi
movl $0x2, %edx
callq 0x292db
testb %al, %al
jne 0x3dec4
movq 0x38(%rbx), %r14
testq %r14, %r14
jne 0x3dd37
movl $0x18, %edi
callq 0x29f85
movq %rax, 0x38(%rbx)
testq %rax, %rax
jne 0x3dd25
xorl %edi, %edi
callq 0x80e4
movl %eax, %ecx
movq 0x38(%rbx), %rax
testb %cl, %cl
je 0x3df61
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq 0x38(%rbx), %r14
movl $0x10, %edi
callq 0x29f85
movq %rax, (%r14)
testq %rax, %rax
jne 0x3dd5d
xorl %edi, %edi
callq 0x80e4
movl %eax, %ecx
movq (%r14), %rax
testb %cl, %cl
je 0x3df3f
movups 0x8(%rsp), %xmm0
movups %xmm0, (%rax)
movl 0x1c(%rbx), %eax
movl %eax, %ecx
andl $0x1f, %ecx
movl %eax, %edx
andl $0x1e, %edx
cmpl $0x4, %edx
setne %dl
cmpl $0x6, %ecx
setne %sil
testb %sil, %dl
jne 0x3dd9f
cmpb $0x0, 0x18(%rbx)
jne 0x3df14
movq 0x8(%rbx), %rax
movq 0x8(%rax), %rax
movq %rax, 0x20(%rbx)
jmp 0x3df1c
leal -0x7(%rcx), %edx
cmpl $0x3, %edx
ja 0x3deb4
andb $0x1f, %al
movzbl %al, %edi
callq 0x3c3ca
movl $0x1, %ebp
movl %eax, %ecx
shll %cl, %ebp
movq 0x38(%rbx), %r12
testq %r12, %r12
jne 0x3de01
movl $0x18, %edi
callq 0x29f85
movq %rax, 0x38(%rbx)
testq %rax, %rax
jne 0x3ddef
xorl %edi, %edi
callq 0x80e4
movl %eax, %ecx
movq 0x38(%rbx), %rax
testb %cl, %cl
je 0x3df50
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq 0x38(%rbx), %r12
movl %ebp, %r14d
leaq (%r14,%r14), %rdi
callq 0x29f85
movq %rax, 0x8(%r12)
movq %r14, %rdi
callq 0x29f85
movq %rax, 0x10(%r12)
cmpq $0x0, 0x8(%r12)
je 0x3de2c
testq %rax, %rax
jne 0x3de3d
movq (%r12), %rdi
callq 0x80e4
testb %al, %al
je 0x3df2d
cmpb $0x1, 0x18(%rbx)
jne 0x3dec8
movq (%r12), %rdi
movl $0x4, %esi
callq 0x2953f
xorl %r13d, %r13d
leaq 0x8(%rsp), %r15
movq (%r12), %rdi
movq %r15, %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x2948f
movq 0x8(%r12), %rax
movzbl 0xa(%rsp), %ecx
movzbl 0x9(%rsp), %edx
movzbl 0x8(%rsp), %esi
shrl $0x3, %esi
andl $0xfc, %edx
leal (%rsi,%rdx,8), %edx
andl $-0x8, %ecx
shll $0x8, %ecx
orl %edx, %ecx
movw %cx, (%rax,%r13,2)
movb 0xb(%rsp), %al
movq 0x10(%r12), %rcx
movb %al, (%rcx,%r13)
incq %r13
cmpq %r13, %r14
jne 0x3de5d
jmp 0x3df14
addl $-0xb, %ecx
cmpl $0x3, %ecx
jbe 0x3df14
movq %rbx, %rsi
callq 0x3e453
xorl %eax, %eax
jmp 0x3df1e
movq 0x8(%rbx), %rax
movq 0x8(%rax), %rax
xorl %ecx, %ecx
movq 0x8(%r12), %rdx
movzbl 0x2(%rax,%rcx,4), %esi
movzbl 0x1(%rax,%rcx,4), %edi
movzbl (%rax,%rcx,4), %r8d
shrl $0x3, %r8d
andl $0xfc, %edi
leal (%r8,%rdi,8), %edi
andl $-0x8, %esi
shll $0x8, %esi
orl %edi, %esi
movw %si, (%rdx,%rcx,2)
movb 0x3(%rax,%rcx,4), %dl
movq 0x10(%r12), %rsi
movb %dl, (%rsi,%rcx)
incq %rcx
cmpq %rcx, %r14
jne 0x3ded2
movq $0x0, 0x20(%rbx)
movb $0x1, %al
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r12), %rsi
leaq 0x1198(%rip), %rdi      # 0x3f0d0
callq 0x8294
jmp 0x3df3d
leaq 0x118a(%rip), %rdi      # 0x3f0d0
movq %rax, %rsi
callq 0x8294
jmp 0x3df4e
leaq 0x1179(%rip), %rdi      # 0x3f0d0
movq %rax, %rsi
callq 0x8294
jmp 0x3df5f
leaq 0x1168(%rip), %rdi      # 0x3f0d0
movq %rax, %rsi
callq 0x8294
jmp 0x3df70