// File: /workspace/github_c_makefile_star2_O3/Paxa[P]fast_excel/libxlsxwriter/src/worksheet.c
// Function: lxw_worksheet_prepare_image
void
lxw_worksheet_prepare_image(lxw_worksheet *self,
                            uint32_t image_ref_id, uint32_t drawing_id,
                            lxw_object_properties *object_props)
{
    lxw_drawing_object *drawing_object;
    lxw_rel_tuple *relationship;
    double width;
    double height;
    char *url;
    char *found_string;
    size_t i;
    char filename[LXW_FILENAME_LENGTH];
    enum cell_types link_type = HYPERLINK_URL;

    if (!self->drawing) {
        self->drawing = lxw_drawing_new();
        self->drawing->embedded = LXW_TRUE;
        RETURN_VOID_ON_MEM_ERROR(self->drawing);

        relationship = calloc(1, sizeof(lxw_rel_tuple));
        GOTO_LABEL_ON_MEM_ERROR(relationship, mem_error);

        relationship->type = lxw_strdup("/drawing");
        GOTO_LABEL_ON_MEM_ERROR(relationship->type, mem_error);

        lxw_snprintf(filename, LXW_FILENAME_LENGTH,
                     "../drawings/drawing%d.xml", drawing_id);

        relationship->target = lxw_strdup(filename);
        GOTO_LABEL_ON_MEM_ERROR(relationship->target, mem_error);

        STAILQ_INSERT_TAIL(self->external_drawing_links, relationship,
                           list_pointers);
    }

    drawing_object = calloc(1, sizeof(lxw_drawing_object));
    RETURN_VOID_ON_MEM_ERROR(drawing_object);

    drawing_object->anchor = LXW_OBJECT_MOVE_DONT_SIZE;
    if (object_props->object_position)
        drawing_object->anchor = object_props->object_position;

    drawing_object->type = LXW_DRAWING_IMAGE;
    drawing_object->description = lxw_strdup(object_props->description);
    drawing_object->tip = lxw_strdup(object_props->tip);
    drawing_object->rel_index = 0;
    drawing_object->url_rel_index = 0;
    drawing_object->decorative = object_props->decorative;

    /* Scale to user scale. */
    width = object_props->width * object_props->x_scale;
    height = object_props->height * object_props->y_scale;

    /* Scale by non 96dpi resolutions. */
    width *= 96.0 / object_props->x_dpi;
    height *= 96.0 / object_props->y_dpi;

    object_props->width = width;
    object_props->height = height;

    _worksheet_position_object_emus(self, object_props, drawing_object);

    /* Convert from pixels to emus. */
    drawing_object->width = (uint32_t) (0.5 + width * 9525);
    drawing_object->height = (uint32_t) (0.5 + height * 9525);

    lxw_add_drawing_object(self->drawing, drawing_object);

    if (object_props->url) {
        url = object_props->url;

        relationship = calloc(1, sizeof(lxw_rel_tuple));
        GOTO_LABEL_ON_MEM_ERROR(relationship, mem_error);

        relationship->type = lxw_strdup("/hyperlink");
        GOTO_LABEL_ON_MEM_ERROR(relationship->type, mem_error);

        /* Check the link type. Default to external hyperlinks. */
        if (strstr(url, "internal:"))
            link_type = HYPERLINK_INTERNAL;
        else if (strstr(url, "external:"))
            link_type = HYPERLINK_EXTERNAL;
        else
            link_type = HYPERLINK_URL;

        /* Set the relationship object for each type of link. */
        if (link_type == HYPERLINK_INTERNAL) {
            relationship->target_mode = NULL;
            relationship->target = lxw_strdup(url + sizeof("internal") - 1);
            GOTO_LABEL_ON_MEM_ERROR(relationship->target, mem_error);

            /* We need to prefix the internal link/range with #. */
            relationship->target[0] = '#';
        }
        else if (link_type == HYPERLINK_EXTERNAL) {
            relationship->target_mode = lxw_strdup("External");
            GOTO_LABEL_ON_MEM_ERROR(relationship->target_mode, mem_error);

            /* Look for Windows style "C:/" link or Windows share "\\" link. */
            found_string = strchr(url + sizeof("external:") - 1, ':');
            if (!found_string)
                found_string = strstr(url, "\\\\");

            if (found_string) {
                /* Copy the url with some space at the start to overwrite
                 * "external:" with "file:///". */
                relationship->target = lxw_escape_url_characters(url + 1,
                                                                 LXW_TRUE);
                GOTO_LABEL_ON_MEM_ERROR(relationship->target, mem_error);

                /* Add the file:/// URI to the url if absolute path. */
                memcpy(relationship->target, "file:///",
                       sizeof("file:///") - 1);
            }
            else {
                /* Copy the relative url without "external:". */
                relationship->target =
                    lxw_escape_url_characters(url + sizeof("external:") - 1,
                                              LXW_TRUE);
                GOTO_LABEL_ON_MEM_ERROR(relationship->target, mem_error);

                /* Switch backslash to forward slash. */
                for (i = 0; i <= strlen(relationship->target); i++)
                    if (relationship->target[i] == '\\')
                        relationship->target[i] = '/';
            }

        }
        else {
            relationship->target_mode = lxw_strdup("External");
            GOTO_LABEL_ON_MEM_ERROR(relationship->target_mode, mem_error);

            relationship->target =
                lxw_escape_url_characters(object_props->url, LXW_FALSE);
            GOTO_LABEL_ON_MEM_ERROR(relationship->target, mem_error);
        }

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

        if (!_find_drawing_rel_index(self, url)) {
            STAILQ_INSERT_TAIL(self->drawing_links, relationship,
                               list_pointers);
        }
        else {
            free(relationship->type);
            free(relationship->target);
            free(relationship->target_mode);
            free(relationship);
        }

        drawing_object->url_rel_index = _get_drawing_rel_index(self, url);

    }

    if (!_find_drawing_rel_index(self, object_props->md5)) {
        relationship = calloc(1, sizeof(lxw_rel_tuple));
        GOTO_LABEL_ON_MEM_ERROR(relationship, mem_error);

        relationship->type = lxw_strdup("/image");
        GOTO_LABEL_ON_MEM_ERROR(relationship->type, mem_error);

        lxw_snprintf(filename, 32, "../media/image%d.%s", image_ref_id,
                     object_props->extension);

        relationship->target = lxw_strdup(filename);
        GOTO_LABEL_ON_MEM_ERROR(relationship->target, mem_error);

        STAILQ_INSERT_TAIL(self->drawing_links, relationship, list_pointers);
    }

    drawing_object->rel_index =
        _get_drawing_rel_index(self, object_props->md5);

    return;

mem_error:
    if (relationship) {
        free(relationship->type);
        free(relationship->target);
        free(relationship->target_mode);
        free(relationship);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rcx, %r14
movl %esi, %r13d
movq %rdi, %rbx
cmpq $0x0, 0x290(%rdi)
jne 0x312bc
movl %edx, %ebp
callq 0xc930
movq %rax, 0x290(%rbx)
movb $0x1, 0x8(%rax)
movl $0x1, %edi
movl $0x20, %esi
callq 0xc7f0
testq %rax, %rax
je 0x316b8
movq %rax, %r15
leaq 0x15c73(%rip), %rdi     # 0x46ec4
callq 0xcce0
movq %rax, (%r15)
testq %rax, %rax
je 0x316c2
leaq 0x15c64(%rip), %rdx     # 0x46ecd
leaq 0x30(%rsp), %r12
movl $0x80, %esi
movq %r12, %rdi
movl %ebp, %ecx
xorl %eax, %eax
callq 0xc570
movq %r12, %rdi
callq 0xcce0
movq %rax, 0x8(%r15)
testq %rax, %rax
je 0x316c9
movq %r15, %rax
addq $0x18, %rax
movq $0x0, 0x18(%r15)
movq 0x220(%rbx), %rcx
movq 0x8(%rcx), %rcx
movq %r15, (%rcx)
movq 0x220(%rbx), %rcx
movq %rax, 0x8(%rcx)
movl $0x1, %edi
movl $0x80, %esi
callq 0xc7f0
testq %rax, %rax
je 0x3165f
movq %rax, %r12
movl 0x40(%r14), %eax
testb %al, %al
movl $0x2, %ecx
cmovnel %eax, %ecx
movb %cl, 0x1(%r12)
movb $0x1, (%r12)
movq 0x28(%r14), %rdi
callq 0xcce0
movq %rax, 0x60(%r12)
movq 0x38(%r14), %rdi
callq 0xcce0
movq %rax, 0x68(%r12)
movq $0x0, 0x54(%r12)
movb 0xb0(%r14), %al
movb %al, 0x70(%r12)
movupd 0x8(%r14), %xmm0
movupd 0x68(%r14), %xmm1
mulpd %xmm0, %xmm1
movupd 0x80(%r14), %xmm0
movapd 0x1533f(%rip), %xmm2    # 0x46680
divpd %xmm0, %xmm2
mulpd %xmm1, %xmm2
movapd %xmm2, 0x10(%rsp)
movupd %xmm2, 0x68(%r14)
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x3176b
movapd 0x10(%rsp), %xmm2
mulpd 0x1531f(%rip), %xmm2    # 0x46690
addpd 0x152e7(%rip), %xmm2    # 0x46660
cvttpd2dq %xmm2, %xmm0
movapd %xmm0, %xmm1
psrad $0x1f, %xmm1
addpd 0x152e2(%rip), %xmm2    # 0x46670
cvttpd2dq %xmm2, %xmm2
andpd %xmm1, %xmm2
orpd %xmm0, %xmm2
movlpd %xmm2, 0x48(%r12)
movq 0x290(%rbx), %rdi
movq %r12, %rsi
callq 0xce40
movq 0x30(%r14), %rbp
testq %rbp, %rbp
je 0x3158f
movl $0x1, %edi
movl $0x20, %esi
callq 0xc7f0
testq %rax, %rax
je 0x31673
movq %rax, %r15
leaq 0x15b08(%rip), %rdi     # 0x46ee7
callq 0xcce0
movq %rax, (%r15)
testq %rax, %rax
je 0x31687
movq %rax, 0x28(%rsp)
leaq 0x15af6(%rip), %rsi     # 0x46ef2
movq %rbp, %rdi
callq 0xd080
testq %rax, %rax
movq %rbp, 0x10(%rsp)
movl %r13d, 0xc(%rsp)
je 0x3143d
leaq 0x8(%rbp), %rdi
callq 0xcce0
movq %rax, 0x8(%r15)
testq %rax, %rax
je 0x316d0
movq %rax, %rbp
movb $0x23, (%rax)
movq $0x0, 0x20(%rsp)
jmp 0x314f7
leaq 0x15ab8(%rip), %rsi     # 0x46efc
movq %rbp, %rdi
callq 0xd080
movq %rax, %rbp
leaq 0x15ab0(%rip), %rdi     # 0x46f06
callq 0xcce0
movq %rax, 0x10(%r15)
testq %rbp, %rbp
movq %rax, 0x20(%rsp)
je 0x314d3
testq %rax, %rax
je 0x31716
movq 0x10(%rsp), %r13
leaq 0x9(%r13), %rbp
movq %rbp, %rdi
movl $0x3a, %esi
callq 0xc4c0
testq %rax, %rax
jne 0x314a5
leaq 0x15a7b(%rip), %rsi     # 0x46f0f
movq %r13, %rdi
callq 0xd080
testq %rax, %rax
je 0x316d7
leaq 0x1(%r13), %rdi
movl $0x1, %esi
callq 0xcf60
movq %rax, 0x8(%r15)
testq %rax, %rax
je 0x3171d
movq %rax, %rbp
movabsq $0x2f2f2f3a656c6966, %rax # imm = 0x2F2F2F3A656C6966
movq %rax, (%rbp)
jmp 0x314f7
testq %rax, %rax
je 0x31724
movq 0x30(%r14), %rdi
xorl %esi, %esi
callq 0xcf60
movq %rax, %rbp
movq %rax, 0x8(%r15)
testq %rax, %rax
je 0x3172b
movq 0x10(%rsp), %r13
movq %r13, %rdi
callq 0xd0d0
movzwl 0x1f6(%rbx), %edx
cmpq %rdx, %rax
ja 0x3169b
movq 0x70(%rbx), %rdi
movq %r13, %rsi
callq 0x3de97
testq %rax, %rax
je 0x31550
cmpl $0x0, (%rax)
je 0x31550
movq 0x28(%rsp), %rdi
callq 0xc0c0
movq %rbp, %rdi
callq 0xc0c0
movq 0x20(%rsp), %rdi
callq 0xc0c0
movq %r15, %rdi
callq 0xc0c0
jmp 0x31578
movq %r15, %rax
addq $0x18, %rax
movq $0x0, 0x18(%r15)
movq 0x228(%rbx), %rcx
movq 0x8(%rcx), %rcx
movq %r15, (%rcx)
movq 0x228(%rbx), %rcx
movq %rax, 0x8(%rcx)
movl 0xc(%rsp), %r13d
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0x317ba
movl %eax, 0x58(%r12)
movq 0xa0(%r14), %r15
testq %r15, %r15
je 0x315b5
movq 0x70(%rbx), %rdi
movq %r15, %rsi
callq 0x3de97
testq %rax, %rax
je 0x315b5
cmpl $0x0, (%rax)
jne 0x3164a
movl $0x1, %edi
movl $0x20, %esi
callq 0xc7f0
testq %rax, %rax
je 0x31669
movq %rax, %r15
leaq 0x159a2(%rip), %rdi     # 0x46f79
callq 0xcce0
movq %rax, (%r15)
testq %rax, %rax
je 0x3167d
movq 0x78(%r14), %r8
leaq 0x1598d(%rip), %rdx     # 0x46f80
movl %r13d, %ecx
leaq 0x30(%rsp), %r13
movl $0x20, %esi
movq %r13, %rdi
xorl %eax, %eax
callq 0xc570
movq %r13, %rdi
callq 0xcce0
movq %rax, 0x8(%r15)
testq %rax, %rax
je 0x31691
movq %r15, %rax
addq $0x18, %rax
movq $0x0, 0x18(%r15)
movq 0x228(%rbx), %rcx
movq 0x8(%rcx), %rcx
movq %r15, (%rcx)
movq 0x228(%rbx), %rcx
movq %rax, 0x8(%rcx)
movq 0xa0(%r14), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x317ba
movl %eax, 0x54(%r12)
jmp 0x31752
callq 0xec6b
jmp 0x31752
callq 0xec41
jmp 0x31752
callq 0xebc3
jmp 0x31752
callq 0xec17
jmp 0x31730
callq 0xeb99
jmp 0x31730
callq 0xebed
jmp 0x31730
movq 0x25956(%rip), %rax     # 0x56ff8
movq (%rax), %rdi
leaq 0x15866(%rip), %rsi     # 0x46f12
movq %r13, %rcx
xorl %eax, %eax
callq 0xc870
jmp 0x31730
callq 0xece3
jmp 0x31752
callq 0xecbf
jmp 0x31730
callq 0xec95
jmp 0x31730
callq 0xea9d
jmp 0x31730
movq %rbp, %rdi
movl $0x1, %esi
callq 0xcf60
movq %rax, 0x8(%r15)
testq %rax, %rax
je 0x31764
movq %rax, %rbp
xorl %r13d, %r13d
cmpb $0x5c, (%rbp,%r13)
jne 0x31701
movb $0x2f, (%rbp,%r13)
incq %r13
movq %rbp, %rdi
callq 0xc480
cmpq %rax, %r13
jbe 0x316f3
jmp 0x314f7
callq 0xeb1b
jmp 0x31730
callq 0xeac7
jmp 0x31730
callq 0xeb6f
jmp 0x31730
callq 0xeb45
movq (%r15), %rdi
callq 0xc0c0
movq 0x8(%r15), %rdi
callq 0xc0c0
movq 0x10(%r15), %rdi
callq 0xc0c0
movq %r15, %rdi
callq 0xc0c0
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0xeaf1
jmp 0x31730