// File: /workspace/github_c_makefile_star2_O3/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, %r14d
orb %al, %cl
jne 0x2cd40
movl %r9d, %ebx
movl %r8d, %ebp
movq %rsi, %r12
movq %rdi, %r13
movq %rdx, 0x18(%rsp)
movq %rsi, %rdi
callq 0xd0d0
movl $0x13, %r14d
cmpq $0x80, %rax
ja 0x2cd40
movq %r15, %rdi
callq 0xd0d0
cmpq $0x80, %rax
ja 0x2cd40
movl $0x1, %edi
movl $0x298, %esi            # imm = 0x298
callq 0xc7f0
testq %rax, %rax
je 0x2cd80
movq %rax, %r14
movq %r13, (%rsp)
leaq 0x1a3d9(%rip), %rdx     # 0x46ea4
leaq 0x20(%rsp), %r13
movl $0x80, %esi
movq %r13, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0xc570
movw %bp, (%r14)
movb %bl, 0x2(%r14)
movq %r13, %rdi
movl $0x21, %esi
callq 0xc4c0
testq %rax, %rax
je 0x2cbad
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 0x2cb2e
movb $0x0, -0x1(%rbx,%rax)
movq (%rsp), %rax
movq 0x8(%rax), %rax
movq (%rax), %r13
testq %r13, %r13
je 0x2cb9b
leaq 0x203(%r14), %rax
movq %rax, 0x10(%rsp)
cmpb $0x0, (%r13)
jne 0x2cb8e
movq 0x8(%r13), %rbp
movq 0xb8(%rbp), %rsi
movq %rbx, %rdi
callq 0xc800
testl %eax, %eax
jne 0x2cb8e
movzwl 0xd0(%rbp), %eax
movw %ax, (%r14)
movl $0x80, %esi
movq 0x10(%rsp), %rdi
leaq 0x1a320(%rip), %rdx     # 0x46ea4
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc570
movq 0x10(%r13), %r13
testq %r13, %r13
jne 0x2cb4a
movzwl (%r14), %ebp
cmpw $-0x1, %bp
je 0x2ccf2
movq 0x8(%rsp), %r13
incq %r13
leaq 0x3(%r14), %rbx
leaq 0x1a2ec(%rip), %rdx     # 0x46ea4
movl $0x80, %esi
movq %rbx, %rdi
movq %r13, %rcx
xorl %eax, %eax
callq 0xc570
movq 0x18(%rsp), %rcx
testq %rcx, %rcx
je 0x2cbf8
leaq 0x83(%r14), %rdi
leaq 0x1a2c2(%rip), %rdx     # 0x46ea4
movl $0x80, %esi
xorl %eax, %eax
movq %rcx, %r12
callq 0xc570
movl $0x203, %edi            # imm = 0x203
jmp 0x2cbfd
movl $0x83, %edi
addq %r14, %rdi
leaq 0x1a29d(%rip), %r13     # 0x46ea4
movl $0x80, %esi
movq %r13, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0xc570
leaq 0x199ba(%rip), %rsi     # 0x465da
leaq 0x20(%rsp), %rdi
callq 0xd080
leaq 0x183(%r14), %r12
leaq 0x9(%r14), %rcx
testq %rax, %rax
cmoveq %rbx, %rcx
movl $0x80, %esi
movq %r12, %rdi
movq %r13, %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 %r13, %rdx
xorl %eax, %eax
callq 0xc570
movq (%rsp), %r12
movq 0x60(%r12), %r15
movq (%r15), %rbx
testq %rbx, %rbx
je 0x2cd02
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2d9b6
testl %eax, %eax
jle 0x2cd1c
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2d9b6
testl %eax, %eax
je 0x2ccf2
js 0x2cd55
movq 0x288(%rbx), %rbx
testq %rbx, %rbx
jne 0x2cca4
movq %r14, %rax
addq $0x288, %rax            # imm = 0x288
movq $0x0, 0x288(%r14)
movq 0x8(%r15), %rcx
movq %rcx, 0x290(%r14)
movq %r14, (%rcx)
movq 0x60(%r12), %rcx
movq %rax, 0x8(%rcx)
jmp 0x2cd3d
movq %r14, %rdi
callq 0xc0c0
movl $0x1, %r14d
jmp 0x2cd40
movq %r14, %rax
addq $0x288, %rax            # imm = 0x288
movq $0x0, 0x288(%r14)
movq %rax, 0x8(%r15)
jmp 0x2cd33
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
movl %r14d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
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 0x2cd3d
callq 0xe28c
jmp 0x2ccfa