// File: /workspace/github_c_makefile_star2_O1/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
movl $0xc, %ebp
orb %al, %cl
jne 0x2cb09
movl %r9d, %ebx
movl %r8d, %r13d
movq %rsi, %r12
movq %rdi, %r14
movq %rdx, 0x18(%rsp)
movq %rsi, %rdi
callq 0xd0d0
movl $0x13, %ebp
cmpq $0x80, %rax
ja 0x2cb09
movq %r15, %rdi
callq 0xd0d0
cmpq $0x80, %rax
ja 0x2cb09
movq %r14, (%rsp)
movl $0x1, %edi
movl $0x298, %esi            # imm = 0x298
callq 0xc7f0
testq %rax, %rax
je 0x2cb1d
movq %rax, %r14
leaq 0x1a56f(%rip), %rdx     # 0x46db8
leaq 0x20(%rsp), %rbp
movl $0x80, %esi
movq %rbp, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0xc570
movw %r13w, (%r14)
movb %bl, 0x2(%r14)
movq %rbp, %rdi
movl $0x21, %esi
callq 0xc4c0
testq %rax, %rax
je 0x2c928
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 0xc480
cmpb $0x27, -0x1(%rax,%rbx)
jne 0x2c8ac
movb $0x0, -0x1(%rbx,%rax)
movq (%rsp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rbp
testq %rbp, %rbp
je 0x2c915
leaq 0x203(%r14), %rax
movq %rax, 0x10(%rsp)
cmpb $0x0, (%rbp)
jne 0x2c90c
movq 0x8(%rbp), %r13
movq 0xb8(%r13), %rsi
movq %rbx, %rdi
callq 0xc800
testl %eax, %eax
jne 0x2c90c
movzwl 0xd0(%r13), %eax
movw %ax, (%r14)
movl $0x80, %esi
movq 0x10(%rsp), %rdi
leaq 0x1a4b6(%rip), %rdx     # 0x46db8
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc570
movq 0x10(%rbp), %rbp
testq %rbp, %rbp
jne 0x2c8c8
cmpw $-0x1, (%r14)
je 0x2caf8
movq 0x8(%rsp), %rbp
incq %rbp
leaq 0x3(%r14), %r13
leaq 0x1a485(%rip), %rdx     # 0x46db8
movl $0x80, %esi
movq %r13, %rdi
movq %rbp, %rcx
xorl %eax, %eax
callq 0xc570
movq 0x18(%rsp), %rcx
testq %rcx, %rcx
je 0x2c973
leaq 0x83(%r14), %rdi
leaq 0x1a45b(%rip), %rdx     # 0x46db8
movl $0x80, %esi
xorl %eax, %eax
movq %rcx, %r12
callq 0xc570
movl $0x203, %edi            # imm = 0x203
jmp 0x2c978
movl $0x83, %edi
addq %r14, %rdi
leaq 0x1a436(%rip), %rbx     # 0x46db8
xorl %ebp, %ebp
movl $0x80, %esi
movq %rbx, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0xc570
leaq 0x19c2d(%rip), %rsi     # 0x465ca
leaq 0x20(%rsp), %rdi
callq 0xd080
leaq 0x183(%r14), %r12
leaq 0x9(%r14), %rcx
testq %rax, %rax
cmoveq %r13, %rcx
movl $0x80, %esi
movq %r12, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0xc570
movq %r12, %rdi
callq 0xcd50
leaq 0x203(%r14), %rdi
callq 0xcd50
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 0xc570
movq (%rsp), %rax
movq 0x60(%rax), %r15
movq (%r15), %rbx
testq %rbx, %rbx
je 0x2cac2
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2d8db
testl %eax, %eax
jle 0x2cac2
movq (%r15), %rbx
testq %rbx, %rbx
je 0x2ca8e
leaq 0x288(%r14), %r15
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2d8db
testl %eax, %eax
je 0x2ca79
xorl %ecx, %ecx
testl %eax, %eax
jns 0x2ca7e
movq 0x290(%rbx), %rax
movq %rax, 0x290(%r14)
movq %rbx, 0x288(%r14)
movq 0x290(%rbx), %rax
movq %r14, (%rax)
movq %r15, 0x290(%rbx)
movl $0x1, %ecx
jmp 0x2ca7e
movl $0x7, %ecx
testl %ecx, %ecx
jne 0x2caf3
movq 0x288(%rbx), %rbx
testq %rbx, %rbx
jne 0x2ca37
movq %r14, %rax
addq $0x288, %rax            # imm = 0x288
movq $0x0, 0x288(%r14)
movq (%rsp), %rdx
movq 0x60(%rdx), %rcx
movq 0x8(%rcx), %rcx
movq %rcx, 0x290(%r14)
movq %r14, (%rcx)
movq 0x60(%rdx), %rcx
movq %rax, 0x8(%rcx)
jmp 0x2cb07
movq %r14, %rax
addq $0x288, %rax            # imm = 0x288
leaq 0x8(%r15), %rcx
leaq 0x290(%rbx), %rdx
testq %rbx, %rbx
cmoveq %rcx, %rdx
movq %rbx, 0x288(%r14)
movq %rax, (%rdx)
movq %r14, (%r15)
movq %r15, 0x290(%r14)
jmp 0x2cb09
cmpl $0x7, %ecx
jne 0x2cb07
movq %r14, %rdi
callq 0xc0c0
movl $0x1, %ebp
jmp 0x2cb09
xorl %ebp, %ebp
movl %ebp, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0xe2d5
jmp 0x2cb00