// File: /workspace/github_c_makefile_star2_O3/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 0x3a8b0
movq %rcx, %r15
movq %r8, 0x20(%rsp)
cmpb $0x0, (%rcx)
je 0x3a8b0
movq %rdi, %r14
cmpw $-0x5, 0x12e(%rdi)
jae 0x3a8a5
movq %r9, %rbx
movl %esi, %ebp
movzwl %dx, %r12d
movq %r14, %rdi
movl %r12d, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x39d04
movl %eax, %r13d
testl %eax, %eax
jne 0x3a8b0
movl %r12d, 0x1c(%rsp)
leaq 0xc9c8(%rip), %rsi      # 0x46ef2
movq %r15, %rdi
callq 0xd080
movq %rax, %r13
cmpq $0x1, %rax
movl $0xc, %r12d
sbbl $0x0, %r12d
leaq 0xc9b2(%rip), %rsi      # 0x46efc
movq %r15, %rdi
callq 0xd080
testq %rax, %rax
movl $0xd, %ecx
cmovel %r12d, %ecx
movl %ecx, 0x18(%rsp)
testq %rbx, %rbx
je 0x3a5c4
movq %rax, %r12
movq %rbx, %rdi
callq 0xcce0
testq %rax, %rax
je 0x3a8ff
movq %rax, %rbx
orq %r12, %r13
je 0x3a587
addq $0x9, %r15
movq %r15, %rdi
callq 0xcce0
testq %rax, %rax
je 0x3a8ca
movq %rax, %r15
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x3a612
callq 0xcce0
movq %rax, 0x8(%rsp)
testq %rax, %rax
jne 0x3a61b
callq 0xf38d
movl $0x1, %r13d
jmp 0x3a942
orq %rax, %r13
sete %r13b
movl $0x9, %edi
movq %rax, %r12
jne 0x3a5f0
leaq 0xd072(%rip), %rsi      # 0x4764e
movq %r15, %rdi
callq 0xd080
testq %rax, %rax
movl $0x7, %edi
cmoveq %rax, %rdi
addq %r15, %rdi
callq 0xcce0
testq %rax, %rax
je 0x3a932
movq %rax, %rbx
testb %r13b, %r13b
je 0x3a583
jmp 0x3a587
movq $0x0, 0x8(%rsp)
cmpl $0xc, 0x18(%rsp)
jne 0x3a63e
movq %rbx, %rdi
callq 0xcce0
movq %rax, 0x10(%rsp)
testq %rax, %rax
jne 0x3a647
callq 0xf48a
jmp 0x3a912
movq $0x0, 0x10(%rsp)
movq %r15, %rdi
movl $0x23, %esi
callq 0xc4c0
testq %rax, %rax
je 0x3a682
movq %rax, %r13
movq 0x10(%rsp), %rdi
callq 0xc0c0
leaq 0x1(%r13), %rdi
callq 0xcce0
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x3a90d
movb $0x0, (%r13)
movl 0x18(%rsp), %eax
cmpl $0xd, %eax
je 0x3a690
cmpl $0xb, %eax
jne 0x3a6b1
movq %r15, %rdi
xorl %esi, %esi
callq 0xcf60
testq %rax, %rax
je 0x3a906
movq %rax, %r13
movq %r15, %rdi
callq 0xc0c0
movq %r13, %r15
testq %r12, %r12
je 0x3a7b2
xorl %r13d, %r13d
cmpb $0x2f, (%r15,%r13)
jne 0x3a6c9
movb $0x5c, (%r15,%r13)
incq %r13
movq %r15, %rdi
callq 0xc480
cmpq %rax, %r13
jbe 0x3a6bd
xorl %r13d, %r13d
cmpb $0x2f, (%rbx,%r13)
jne 0x3a6e8
movb $0x5c, (%rbx,%r13)
incq %r13
movq %rbx, %rdi
callq 0xc480
cmpq %rax, %r13
jbe 0x3a6dc
movq %r15, %rdi
movl $0x3a, %esi
callq 0xc4c0
testq %rax, %rax
jne 0x3a722
leaq 0xc7fe(%rip), %rsi      # 0x46f0f
movq %r15, %rdi
callq 0xd080
testq %rax, %rax
je 0x3a8c2
movq %r15, %rdi
callq 0xc480
movq %rax, %r13
addq $0x9, %r13
movl $0x1, %edi
movq %r13, %rsi
callq 0xc7f0
testq %rax, %rax
je 0x3a922
movq %rax, %r12
leaq 0xcf05(%rip), %rdx      # 0x47656
movq %rax, %rdi
movq %r13, %rsi
movq %r15, %rcx
xorl %eax, %eax
callq 0xc570
cmpb $0x2e, (%r15)
jne 0x3a789
cmpb $0x5c, 0x1(%r15)
jne 0x3a789
leaq 0x2(%r15), %r13
movq %r15, %rdi
callq 0xc480
leaq -0x1(%rax), %rdx
movq %r15, %rdi
movq %r13, %rsi
callq 0xccd0
testq %r12, %r12
je 0x3a7b2
movq %r15, %rdi
callq 0xc0c0
movq %r12, %rdi
callq 0xcce0
testq %rax, %rax
je 0x3a94f
movq %rax, %r15
movq %r12, %rdi
callq 0xc0c0
movq %r15, %rdi
callq 0xd0d0
movzwl 0x1f6(%r14), %edx
cmpq %rdx, %rax
ja 0x3a8d7
movq 0x20(%rsp), %r8
testq %r8, %r8
jne 0x3a7dc
movq 0x298(%r14), %r8
movq %r14, %rdi
movl %ebp, %esi
movl 0x1c(%rsp), %edx
movq %rbx, %rcx
callq 0xc0f0
testl %eax, %eax
je 0x3a82d
movl %eax, %r13d
xorl %ebp, %ebp
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %r12
movq %rbx, %rdi
callq 0xc0c0
movq %r15, %rdi
callq 0xc0c0
movq %rbp, %rdi
callq 0xc0c0
movq %r14, %rdi
callq 0xc0c0
movq %r12, %rdi
callq 0xc0c0
jmp 0x3a8b0
movl $0x1, %edi
movl $0x68, %esi
callq 0xc7f0
testq %rax, %rax
je 0x3a96a
movq %rax, %r12
movl %ebp, (%rax)
movl 0x1c(%rsp), %r13d
movw %r13w, 0x4(%rax)
movl 0x18(%rsp), %eax
movl %eax, 0x8(%r12)
movq %r15, 0x20(%r12)
movq 0x10(%rsp), %rax
movq %rax, 0x30(%r12)
movq 0x8(%rsp), %rax
movq %rax, 0x38(%r12)
movq 0x28(%r14), %rdi
movl %ebp, %esi
callq 0x3f05b
movq 0x20(%rax), %rdi
movq %r12, %rsi
movl %r13d, %edx
callq 0x3edcf
movq %rbx, %rdi
callq 0xc0c0
incw 0x12e(%r14)
xorl %r13d, %r13d
jmp 0x3a8b0
callq 0xf4f6
movl $0x19, %r13d
movl %r13d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r12d, %r12d
jmp 0x3a761
callq 0xf4ae
movl $0x1, %r13d
jmp 0x3a93f
movq 0x1c71a(%rip), %rax     # 0x56ff8
movq (%rax), %rdi
leaq 0xcd79(%rip), %rsi      # 0x47661
xorl %ebp, %ebp
movq %r15, %rcx
xorl %eax, %eax
callq 0xc870
movl $0x18, %r13d
jmp 0x3a7f6
callq 0xf369
jmp 0x3a937
callq 0xf3d5
jmp 0x3a927
callq 0xf3b1
movl $0x1, %r13d
xorl %ebp, %ebp
xorl %r14d, %r14d
jmp 0x3a7fb
callq 0xf3f9
movl $0x1, %r13d
jmp 0x3a7f4
callq 0xf4d2
movl $0x1, %r13d
xorl %ebx, %ebx
xorl %r15d, %r15d
xorl %ebp, %ebp
xorl %r14d, %r14d
xorl %r12d, %r12d
jmp 0x3a800
callq 0xf41d
movl $0x1, %r13d
xorl %r15d, %r15d
movq 0x10(%rsp), %r14
movq %r12, %rbp
jmp 0x3a7fb
callq 0xf441
movl $0x1, %r13d
xorl %ebp, %ebp
movq 0x8(%rsp), %r12
movq 0x10(%rsp), %r14
jmp 0x3a800