// File: /workspace/github_c_makefile_star2_O2/Paxa[P]fast_excel/libxlsxwriter/src/workbook.c
// Function: store_defined_name
STATIC lxw_error
_store_defined_name(lxw_workbook *self, const char *name,
                    const char *app_name, const char *formula, int16_t index,
                    uint8_t hidden)
{
    lxw_sheet *sheet;
    lxw_worksheet *worksheet;
    lxw_defined_name *defined_name;
    lxw_defined_name *list_defined_name;
    char name_copy[LXW_DEFINED_NAME_LENGTH];
    char *tmp_str;
    char *worksheet_name;

    /* Do some checks on the input data */
    if (!name || !formula)
        return LXW_ERROR_NULL_PARAMETER_IGNORED;

    if (lxw_utf8_strlen(name) > LXW_DEFINED_NAME_LENGTH ||
        lxw_utf8_strlen(formula) > LXW_DEFINED_NAME_LENGTH) {
        return LXW_ERROR_128_STRING_LENGTH_EXCEEDED;
    }

    /* Allocate a new defined_name to be added to the linked list of names. */
    defined_name = calloc(1, sizeof(struct lxw_defined_name));
    RETURN_ON_MEM_ERROR(defined_name, LXW_ERROR_MEMORY_MALLOC_FAILED);

    /* Copy the user input string. */
    lxw_strcpy(name_copy, name);

    /* Set the worksheet index or -1 for a global defined name. */
    defined_name->index = index;
    defined_name->hidden = hidden;

    /* Check for local defined names like like "Sheet1!name". */
    tmp_str = strchr(name_copy, '!');

    if (tmp_str == NULL) {
        /* The name is global. We just store the defined name string. */
        lxw_strcpy(defined_name->name, name_copy);
    }
    else {
        /* The name is worksheet local. We need to extract the sheet name
         * and map it to a sheet index. */

        /* Split the into the worksheet name and defined name. */
        *tmp_str = '\0';
        tmp_str++;
        worksheet_name = name_copy;

        /* Remove any worksheet quoting. */
        if (worksheet_name[0] == '\'')
            worksheet_name++;
        if (worksheet_name[strlen(worksheet_name) - 1] == '\'')
            worksheet_name[strlen(worksheet_name) - 1] = '\0';

        /* Search for worksheet name to get the equivalent worksheet index. */
        STAILQ_FOREACH(sheet, self->sheets, list_pointers) {
            if (sheet->is_chartsheet)
                continue;
            else
                worksheet = sheet->u.worksheet;

            if (strcmp(worksheet_name, worksheet->name) == 0) {
                defined_name->index = worksheet->index;
                lxw_strcpy(defined_name->normalised_sheetname,
                           worksheet_name);
            }
        }

        /* If we didn't find the worksheet name we exit. */
        if (defined_name->index == -1)
            goto mem_error;

        lxw_strcpy(defined_name->name, tmp_str);
    }

    /* Print titles and repeat title pass in the name used for App.xml. */
    if (app_name) {
        lxw_strcpy(defined_name->app_name, app_name);
        lxw_strcpy(defined_name->normalised_sheetname, app_name);
    }
    else {
        lxw_strcpy(defined_name->app_name, name);
    }

    /* We need to normalize the defined names for sorting. This involves
     * removing any _xlnm namespace  and converting it to lowercase. */
    tmp_str = strstr(name_copy, "_xlnm.");

    if (tmp_str)
        lxw_strcpy(defined_name->normalised_name, defined_name->name + 6);
    else
        lxw_strcpy(defined_name->normalised_name, defined_name->name);

    lxw_str_tolower(defined_name->normalised_name);
    lxw_str_tolower(defined_name->normalised_sheetname);

    /* Strip leading "=" from the formula. */
    if (formula[0] == '=')
        lxw_strcpy(defined_name->formula, formula + 1);
    else
        lxw_strcpy(defined_name->formula, formula);

    /* We add the defined name to the list in sorted order. */
    list_defined_name = TAILQ_FIRST(self->defined_names);

    if (list_defined_name == NULL ||
        _compare_defined_names(defined_name, list_defined_name) < 1) {
        /* List is empty or defined name goes to the head. */
        TAILQ_INSERT_HEAD(self->defined_names, defined_name, list_pointers);
        return LXW_NO_ERROR;
    }

    TAILQ_FOREACH(list_defined_name, self->defined_names, list_pointers) {
        int res = _compare_defined_names(defined_name, list_defined_name);

        /* The entry already exists. We exit and don't overwrite. */
        if (res == 0)
            goto mem_error;

        /* New defined name is inserted in sorted order before other entries. */
        if (res < 0) {
            TAILQ_INSERT_BEFORE(list_defined_name, defined_name,
                                list_pointers);
            return LXW_NO_ERROR;
        }
    }

    /* If the entry wasn't less than any of the entries in the list we add it
     * to the end. */
    TAILQ_INSERT_TAIL(self->defined_names, defined_name, list_pointers);
    return LXW_NO_ERROR;

mem_error:
    free(defined_name);
    return LXW_ERROR_MEMORY_MALLOC_FAILED;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rcx, %r15
testq %rsi, %rsi
sete %al
testq %rcx, %rcx
sete %cl
orb %al, %cl
jne 0x2967d
movl %r9d, %ebp
movl %r8d, %ebx
movq %rdx, %r13
movq %rsi, %r12
movq %rdi, (%rsp)
movq %rsi, %rdi
callq 0xd150
pushq $0x13
popq %r14
cmpq $0x80, %rax
ja 0x29681
movq %r15, %rdi
callq 0xd150
cmpq $0x80, %rax
ja 0x29681
pushq $0x1
popq %rdi
movl $0x298, %esi            # imm = 0x298
callq 0xc850
testq %rax, %rax
je 0x29869
movq %rax, %r14
movq %r13, 0x10(%rsp)
leaq 0x1a8e0(%rip), %rdx     # 0x43e90
leaq 0x20(%rsp), %r13
movl $0x80, %esi
movq %r13, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0xc5b0
movw %bx, (%r14)
movb %bpl, 0x2(%r14)
pushq $0x21
popq %rsi
movq %r13, %rdi
callq 0xc500
testq %rax, %rax
je 0x296a6
movq %rax, 0x8(%rsp)
movb $0x0, (%rax)
xorl %eax, %eax
cmpb $0x27, 0x20(%rsp)
sete %al
leaq (%rsp,%rax), %rbx
addq $0x20, %rbx
movq %rbx, %rdi
callq 0xc4c0
cmpb $0x27, -0x1(%rax,%rbx)
jne 0x29611
movb $0x0, -0x1(%rbx,%rax)
incq 0x8(%rsp)
movq (%rsp), %rax
movq 0x8(%rax), %r13
leaq 0x203(%r14), %rax
movq %rax, 0x18(%rsp)
movq (%r13), %r13
testq %r13, %r13
je 0x29696
cmpb $0x0, (%r13)
jne 0x29677
movq 0x8(%r13), %rbp
movq 0xb8(%rbp), %rsi
movq %rbx, %rdi
callq 0xc860
testl %eax, %eax
jne 0x29677
movzwl 0xd0(%rbp), %eax
movw %ax, (%r14)
movl $0x80, %esi
movq 0x18(%rsp), %rdi
leaq 0x1a823(%rip), %rdx     # 0x43e90
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc5b0
addq $0x10, %r13
jmp 0x2962a
pushq $0xc
popq %r14
movl %r14d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpw $-0x1, (%r14)
movq 0x8(%rsp), %r13
je 0x297c2
leaq 0x3(%r14), %rbp
leaq 0x1a7df(%rip), %rdx     # 0x43e90
movl $0x80, %esi
movq %rbp, %rdi
movq %r13, %rcx
xorl %eax, %eax
callq 0xc5b0
movq 0x10(%rsp), %rbx
testq %rbx, %rbx
je 0x296f4
leaq 0x83(%r14), %rdi
leaq 0x1a7b5(%rip), %rdx     # 0x43e90
movl $0x80, %esi
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc5b0
movl $0x203, %edi            # imm = 0x203
movq %rbx, %r12
jmp 0x296f9
movl $0x83, %edi
addq %r14, %rdi
leaq 0x1a78d(%rip), %rbx     # 0x43e90
movl $0x80, %esi
movq %rbx, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0xc5b0
leaq 0x19e9e(%rip), %rsi     # 0x435ba
leaq 0x20(%rsp), %rdi
callq 0xd100
leaq 0x183(%r14), %r12
leaq 0x9(%r14), %rcx
testq %rax, %rax
cmoveq %rbp, %rcx
movl $0x80, %esi
movq %r12, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0xc5b0
movq %r12, %rdi
callq 0xcdd0
leaq 0x203(%r14), %rdi
callq 0xcdd0
xorl %ecx, %ecx
cmpb $0x3d, (%r15)
sete %cl
leaq 0x103(%r14), %rdi
addq %r15, %rcx
movl $0x80, %esi
movq %rbx, %rdx
xorl %eax, %eax
callq 0xc5b0
movq (%rsp), %rax
movq 0x60(%rax), %r15
movq (%r15), %rbx
testq %rbx, %rbx
je 0x297d1
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2a6d5
testl %eax, %eax
jle 0x297e8
testq %rbx, %rbx
je 0x29811
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2a6d5
testl %eax, %eax
je 0x297c2
js 0x2983e
movq 0x288(%rbx), %rbx
jmp 0x2979f
movq %r14, %rdi
callq 0xc0d0
pushq $0x1
jmp 0x2967f
movq %r14, %rax
addq $0x288, %rax            # imm = 0x288
andq $0x0, 0x288(%r14)
movq %rax, 0x8(%r15)
jmp 0x297ff
movq %r14, %rax
addq $0x288, %rax            # imm = 0x288
movq %rbx, 0x288(%r14)
movq %rax, 0x290(%rbx)
movq %r14, (%r15)
movq %r15, 0x290(%r14)
xorl %r14d, %r14d
jmp 0x29681
andq $0x0, 0x288(%r14)
movq %r14, %rax
addq $0x288, %rax            # imm = 0x288
movq 0x8(%r15), %rcx
movq %rcx, 0x290(%r14)
movq %r14, (%rcx)
movq (%rsp), %rcx
movq 0x60(%rcx), %rcx
movq %rax, 0x8(%rcx)
jmp 0x29809
movq 0x290(%rbx), %rax
movq %r14, %rcx
addq $0x288, %rcx            # imm = 0x288
movq %rax, 0x290(%r14)
movq %rbx, 0x288(%r14)
movq %r14, (%rax)
movq %rcx, 0x290(%rbx)
jmp 0x29809
movq 0x29788(%rip), %rax     # 0x52ff8
movq (%rax), %rdi
leaq 0x13866(%rip), %rsi     # 0x3d0e0
leaq 0x190d0(%rip), %rdx     # 0x42951
movl $0x2af, %ecx            # imm = 0x2AF
xorl %eax, %eax
callq 0xc8d0
jmp 0x297ca