// File: /workspace/github_c_makefile_star2_O1/Paxa[P]fast_excel/libxlsxwriter/src/worksheet.c
// Function: worksheet_write_url_opt
lxw_error
worksheet_write_url_opt(lxw_worksheet *self,
                        lxw_row_t row_num,
                        lxw_col_t col_num, const char *url,
                        lxw_format *user_format, const char *string,
                        const char *tooltip)
{
    lxw_cell *link;
    char *string_copy = NULL;
    char *url_copy = NULL;
    char *url_external = NULL;
    char *url_string = NULL;
    char *tooltip_copy = NULL;
    char *found_string;
    char *tmp_string = NULL;
    lxw_format *format = NULL;
    size_t string_size;
    size_t i;
    lxw_error err = LXW_ERROR_MEMORY_MALLOC_FAILED;
    enum cell_types link_type = HYPERLINK_URL;

    if (!url || !*url)
        return LXW_ERROR_NULL_PARAMETER_IGNORED;

    /* Check the Excel limit of URLS per worksheet. */
    if (self->hlink_count > LXW_MAX_NUMBER_URLS) {
        LXW_WARN("worksheet_write_url()/_opt(): URL ignored since it exceeds "
                 "the maximum number of allowed worksheet URLs (65530).");
        return LXW_ERROR_WORKSHEET_MAX_NUMBER_URLS_EXCEEDED;
    }

    err = _check_dimensions(self, row_num, col_num, LXW_FALSE, LXW_FALSE);
    if (err)
        return err;

    /* Reset default error condition. */
    err = LXW_ERROR_MEMORY_MALLOC_FAILED;

    /* Set the URI scheme from internal links. */
    found_string = strstr(url, "internal:");
    if (found_string)
        link_type = HYPERLINK_INTERNAL;

    /* Set the URI scheme from external links. */
    found_string = strstr(url, "external:");
    if (found_string)
        link_type = HYPERLINK_EXTERNAL;

    if (string) {
        string_copy = lxw_strdup(string);
        GOTO_LABEL_ON_MEM_ERROR(string_copy, mem_error);
    }
    else {
        if (link_type == HYPERLINK_URL) {
            /* Strip the mailto header. */
            found_string = strstr(url, "mailto:");
            if (found_string)
                string_copy = lxw_strdup(url + sizeof("mailto"));
            else
                string_copy = lxw_strdup(url);
        }
        else {
            string_copy = lxw_strdup(url + sizeof("__ternal"));
        }
        GOTO_LABEL_ON_MEM_ERROR(string_copy, mem_error);
    }

    if (url) {
        if (link_type == HYPERLINK_URL)
            url_copy = lxw_strdup(url);
        else
            url_copy = lxw_strdup(url + sizeof("__ternal"));

        GOTO_LABEL_ON_MEM_ERROR(url_copy, mem_error);
    }

    if (tooltip) {
        tooltip_copy = lxw_strdup(tooltip);
        GOTO_LABEL_ON_MEM_ERROR(tooltip_copy, mem_error);
    }

    if (link_type == HYPERLINK_INTERNAL) {
        url_string = lxw_strdup(string_copy);
        GOTO_LABEL_ON_MEM_ERROR(url_string, mem_error);
    }

    /* Split url into the link and optional anchor/location. */
    found_string = strchr(url_copy, '#');

    if (found_string) {
        free(url_string);
        url_string = lxw_strdup(found_string + 1);
        GOTO_LABEL_ON_MEM_ERROR(url_string, mem_error);

        *found_string = '\0';
    }

    /* Escape the URL. */
    if (link_type == HYPERLINK_URL || link_type == HYPERLINK_EXTERNAL) {
        tmp_string = lxw_escape_url_characters(url_copy, LXW_FALSE);
        GOTO_LABEL_ON_MEM_ERROR(tmp_string, mem_error);

        free(url_copy);
        url_copy = tmp_string;
    }

    if (link_type == HYPERLINK_EXTERNAL) {
        /* External Workbook links need to be modified into the right format.
         * The URL will look something like "c:\temp\file.xlsx#Sheet!A1". */

        /* For external links change the dir separator from Unix to DOS. */
        for (i = 0; i <= strlen(url_copy); i++)
            if (url_copy[i] == '/')
                url_copy[i] = '\\';

        for (i = 0; i <= strlen(string_copy); i++)
            if (string_copy[i] == '/')
                string_copy[i] = '\\';

        /* Look for Windows style "C:/" link or Windows share "\\" link. */
        found_string = strchr(url_copy, ':');
        if (!found_string)
            found_string = strstr(url_copy, "\\\\");

        if (found_string) {
            /* Add the file:/// URI to the url if non-local. */
            string_size = sizeof("file:///") + strlen(url_copy);
            url_external = calloc(1, string_size);
            GOTO_LABEL_ON_MEM_ERROR(url_external, mem_error);

            lxw_snprintf(url_external, string_size, "file:///%s", url_copy);

        }

        /* Convert a ./dir/file.xlsx link to dir/file.xlsx. */
        found_string = strstr(url_copy, ".\\");
        if (found_string == url_copy)
            memmove(url_copy, url_copy + 2, strlen(url_copy) - 1);

        if (url_external) {
            free(url_copy);
            url_copy = lxw_strdup(url_external);
            GOTO_LABEL_ON_MEM_ERROR(url_copy, mem_error);

            free(url_external);
            url_external = NULL;
        }

    }

    /* Check if URL exceeds Excel's length limit. */
    if (lxw_utf8_strlen(url_copy) > self->max_url_length) {
        LXW_WARN_FORMAT2("worksheet_write_url()/_opt(): URL exceeds "
                         "Excel's allowable length of %d characters: %s",
                         self->max_url_length, url_copy);
        err = LXW_ERROR_WORKSHEET_MAX_URL_LENGTH_EXCEEDED;
        goto mem_error;
    }

    /* Use the default URL format if none is specified. */
    if (!user_format)
        format = self->default_url_format;
    else
        format = user_format;

    err = worksheet_write_string(self, row_num, col_num, string_copy, format);
    if (err)
        goto mem_error;

    /* Reset default error condition. */
    err = LXW_ERROR_MEMORY_MALLOC_FAILED;

    link = _new_hyperlink_cell(row_num, col_num, link_type, url_copy,
                               url_string, tooltip_copy);
    GOTO_LABEL_ON_MEM_ERROR(link, mem_error);

    _insert_hyperlink(self, row_num, col_num, link);

    free(string_copy);
    self->hlink_count++;
    return LXW_NO_ERROR;

mem_error:
    free(string_copy);
    free(url_copy);
    free(url_external);
    free(url_string);
    free(tooltip_copy);
    return err;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl $0xc, %r13d
testq %rcx, %rcx
je 0x3a5d1
movq %rcx, %r14
movq %r8, 0x20(%rsp)
cmpb $0x0, (%rcx)
je 0x3a5d1
movq %rdi, %r15
cmpw $-0x5, 0x12e(%rdi)
jae 0x3a5c6
movq %r9, %rbx
movl %esi, %ebp
movzwl %dx, %r12d
movq %r15, %rdi
movl %edx, 0x1c(%rsp)
movl %r12d, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x39aa6
movl %eax, %r13d
testl %eax, %eax
jne 0x3a5d1
movl %r12d, 0x18(%rsp)
leaq 0xcbca(%rip), %rsi      # 0x46e06
movq %r14, %rdi
callq 0xd080
movq %rax, %r13
cmpq $0x1, %rax
movl $0xc, %r12d
sbbl $0x0, %r12d
leaq 0xcbb4(%rip), %rsi      # 0x46e10
movq %r14, %rdi
callq 0xd080
testq %rax, %rax
movl $0xd, %ecx
cmovel %r12d, %ecx
movl %ecx, 0x14(%rsp)
testq %rbx, %rbx
je 0x3a293
movq %rax, %r12
movq %rbx, %rdi
callq 0xcce0
testq %rax, %rax
jne 0x3a2d0
callq 0xefb5
jmp 0x3a64a
movq %rax, %rcx
orq %r13, %rcx
movq %rax, %r12
jne 0x3a2be
leaq 0xd2bd(%rip), %rsi      # 0x47562
movq %r14, %rdi
callq 0xd080
testq %rax, %rax
movl $0x7, %edi
cmoveq %rax, %rdi
addq %r14, %rdi
jmp 0x3a2c2
leaq 0x9(%r14), %rdi
callq 0xcce0
testq %rax, %rax
je 0x3a645
movq %rax, %rbx
xorl %eax, %eax
orq %r12, %r13
setne %al
leaq (%rax,%rax,8), %rax
addq %rax, %r14
movq %r14, %rdi
callq 0xcce0
testq %rax, %rax
je 0x3a5ed
movq %rax, %r14
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x3a31e
callq 0xcce0
movq %rax, (%rsp)
testq %rax, %rax
jne 0x3a326
callq 0xefd9
movl $0x1, %r13d
jmp 0x3a655
movq $0x0, (%rsp)
cmpl $0xc, 0x14(%rsp)
jne 0x3a349
movq %rbx, %rdi
callq 0xcce0
movq %rax, 0x8(%rsp)
testq %rax, %rax
jne 0x3a352
callq 0xf0d5
jmp 0x3a62e
movq $0x0, 0x8(%rsp)
movq %r14, %rdi
movl $0x23, %esi
callq 0xc4c0
testq %rax, %rax
je 0x3a38d
movq %rax, %r13
movq 0x8(%rsp), %rdi
callq 0xc0c0
leaq 0x1(%r13), %rdi
callq 0xcce0
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x3a629
movb $0x0, (%r13)
movl 0x14(%rsp), %eax
cmpl $0xd, %eax
je 0x3a39b
cmpl $0xb, %eax
jne 0x3a3bc
movq %r14, %rdi
xorl %esi, %esi
callq 0xcf60
testq %rax, %rax
je 0x3a622
movq %rax, %r13
movq %r14, %rdi
callq 0xc0c0
movq %r13, %r14
testq %r12, %r12
je 0x3a4ce
xorl %r13d, %r13d
cmpb $0x2f, (%r14,%r13)
jne 0x3a3d4
movb $0x5c, (%r14,%r13)
incq %r13
movq %r14, %rdi
callq 0xc480
cmpq %rax, %r13
jbe 0x3a3c8
xorl %r13d, %r13d
cmpb $0x2f, (%rbx,%r13)
jne 0x3a3f3
movb $0x5c, (%rbx,%r13)
incq %r13
movq %rbx, %rdi
callq 0xc480
cmpq %rax, %r13
jbe 0x3a3e7
movq %r14, %rdi
movl $0x3a, %esi
callq 0xc4c0
testq %rax, %rax
jne 0x3a42d
leaq 0xca07(%rip), %rsi      # 0x46e23
movq %r14, %rdi
callq 0xd080
testq %rax, %rax
je 0x3a5e3
movq %r14, %rdi
callq 0xc480
movq %rax, %r13
addq $0x9, %r13
movl $0x1, %edi
movq %r13, %rsi
callq 0xc7f0
testq %rax, %rax
je 0x3a63e
movl %ebp, %r12d
leaq 0xd10e(%rip), %rdx      # 0x4756a
movq %rax, %rbp
movq %rax, %rdi
movq %r13, %rsi
movq %r14, %rcx
xorl %eax, %eax
callq 0xc570
leaq 0xd0ff(%rip), %rsi      # 0x47575
movl $0x2, %edx
movq %r14, %rdi
callq 0xc1a0
testl %eax, %eax
jne 0x3a4a2
leaq 0x2(%r14), %r13
movq %r14, %rdi
callq 0xc480
leaq -0x1(%rax), %rdx
movq %r14, %rdi
movq %r13, %rsi
callq 0xccd0
testq %rbp, %rbp
je 0x3a4cb
movq %r14, %rdi
callq 0xc0c0
movq %rbp, %rdi
callq 0xcce0
testq %rax, %rax
je 0x3a662
movq %rax, %r14
movq %rbp, %rdi
callq 0xc0c0
movl %r12d, %ebp
movq %r14, %rdi
callq 0xd0d0
movzwl 0x1f6(%r15), %edx
cmpq %rdx, %rax
ja 0x3a5fa
movq 0x20(%rsp), %r8
testq %r8, %r8
jne 0x3a4f8
movq 0x298(%r15), %r8
movq %r15, %rdi
movl %ebp, %esi
movl 0x18(%rsp), %edx
movq %rbx, %rcx
callq 0xc0f0
testl %eax, %eax
je 0x3a548
movl %eax, %r13d
xorl %ebp, %ebp
movq 0x8(%rsp), %r12
movq (%rsp), %r15
movq %rbx, %rdi
callq 0xc0c0
movq %r14, %rdi
callq 0xc0c0
movq %rbp, %rdi
callq 0xc0c0
movq %r12, %rdi
callq 0xc0c0
movq %r15, %rdi
callq 0xc0c0
jmp 0x3a5d1
movl $0x1, %edi
movl $0x68, %esi
callq 0xc7f0
movq %rax, %r13
testq %rax, %rax
je 0x3a675
movl %ebp, (%r13)
movl 0x1c(%rsp), %eax
movw %ax, 0x4(%r13)
movl 0x14(%rsp), %eax
movl %eax, 0x8(%r13)
movq %r14, 0x20(%r13)
movq 0x8(%rsp), %rax
movq %rax, 0x30(%r13)
movq (%rsp), %rax
movq %rax, 0x38(%r13)
testq %r13, %r13
je 0x3a67f
movq 0x28(%r15), %rdi
movl %ebp, %esi
callq 0x3ee08
movq 0x20(%rax), %rdi
movq %r13, %rsi
movl 0x18(%rsp), %edx
callq 0x3eb8f
movq %rbx, %rdi
callq 0xc0c0
incw 0x12e(%r15)
xorl %r13d, %r13d
jmp 0x3a5d1
callq 0xf141
movl $0x19, %r13d
movl %r13d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebp, %r12d
xorl %ebp, %ebp
jmp 0x3a46f
callq 0xf0f9
movl $0x1, %r13d
jmp 0x3a652
movq 0x1b9f7(%rip), %rax     # 0x55ff8
movq (%rax), %rdi
leaq 0xcf6d(%rip), %rsi      # 0x47578
xorl %ebp, %ebp
movq %r14, %rcx
xorl %eax, %eax
callq 0xc870
movl $0x18, %r13d
jmp 0x3a512
callq 0xf021
jmp 0x3a684
callq 0xeffd
movl $0x1, %r13d
xorl %ebp, %ebp
xorl %r12d, %r12d
jmp 0x3a517
callq 0xf045
jmp 0x3a684
callq 0xf11d
movl $0x1, %r13d
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %ebp, %ebp
xorl %r12d, %r12d
xorl %r15d, %r15d
jmp 0x3a51b
callq 0xf069
movl $0x1, %r13d
xorl %r14d, %r14d
jmp 0x3a512
callq 0xf08d
jmp 0x3a58d
callq 0xf0b1
movl $0x1, %r13d
jmp 0x3a510