// File: /workspace/github_c_makefile_star2_O1/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 $0xc8, %rsp
movq %rcx, %rbx
movl %esi, %ebp
movq %rdi, %r14
cmpq $0x0, 0x290(%rdi)
jne 0x31037
movl %edx, %r12d
callq 0xc930
movq %rax, 0x290(%r14)
movb $0x1, 0x8(%rax)
testq %rax, %rax
je 0x3143d
movl $0x1, %edi
movl $0x20, %esi
callq 0xc7f0
testq %rax, %rax
je 0x31444
movq %rax, %r15
leaq 0x15e0d(%rip), %rdi     # 0x46dd8
callq 0xcce0
movq %rax, (%r15)
testq %rax, %rax
je 0x31485
leaq 0x15dfe(%rip), %rdx     # 0x46de1
leaq 0x40(%rsp), %r13
movl $0x80, %esi
movq %r13, %rdi
movl %r12d, %ecx
xorl %eax, %eax
callq 0xc570
movq %r13, %rdi
callq 0xcce0
movq %rax, 0x8(%r15)
testq %rax, %rax
je 0x3148c
movq %r15, %rax
addq $0x18, %rax
movq $0x0, 0x18(%r15)
movq 0x220(%r14), %rcx
movq 0x8(%rcx), %rcx
movq %r15, (%rcx)
movq 0x220(%r14), %rcx
movq %rax, 0x8(%rcx)
movl $0x1, %edi
movl $0x80, %esi
callq 0xc7f0
testq %rax, %rax
je 0x313f6
movq %rax, %r12
movb $0x2, 0x1(%rax)
movl 0x40(%rbx), %eax
testb %al, %al
movl $0x2, %ecx
cmovnel %eax, %ecx
movb %cl, 0x1(%r12)
movb $0x1, (%r12)
movq 0x28(%rbx), %rdi
callq 0xcce0
movq %rax, 0x60(%r12)
movq 0x38(%rbx), %rdi
callq 0xcce0
movq %rax, 0x68(%r12)
movq $0x0, 0x54(%r12)
movb 0xb0(%rbx), %al
movb %al, 0x70(%r12)
movsd 0x68(%rbx), %xmm0
movsd 0x70(%rbx), %xmm1
mulsd 0x8(%rbx), %xmm0
mulsd 0x10(%rbx), %xmm1
movsd 0x14237(%rip), %xmm2    # 0x452f0
movapd %xmm2, %xmm3
divsd 0x80(%rbx), %xmm3
divsd 0x88(%rbx), %xmm2
mulsd %xmm0, %xmm3
movsd %xmm3, 0x8(%rsp)
mulsd %xmm1, %xmm2
movsd %xmm2, (%rsp)
movsd %xmm3, 0x68(%rbx)
movsd %xmm2, 0x70(%rbx)
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x31510
movsd 0x154e8(%rip), %xmm0    # 0x465e8
movsd 0x8(%rsp), %xmm2
mulsd %xmm0, %xmm2
movsd 0xf3be(%rip), %xmm1     # 0x404d0
addsd %xmm1, %xmm2
cvttsd2si %xmm2, %rax
movl %eax, 0x48(%r12)
movsd (%rsp), %xmm2
mulsd %xmm0, %xmm2
addsd %xmm1, %xmm2
cvttsd2si %xmm2, %rax
movl %eax, 0x4c(%r12)
movq 0x290(%r14), %rdi
movq %r12, %rsi
callq 0xce40
movq 0x30(%rbx), %r13
testq %r13, %r13
je 0x31327
movl $0x1, %edi
movl $0x20, %esi
callq 0xc7f0
testq %rax, %rax
je 0x31404
movq %rax, %r15
leaq 0x15c86(%rip), %rdi     # 0x46dfb
callq 0xcce0
movq %rax, (%r15)
testq %rax, %rax
je 0x31412
movl %ebp, 0x8(%rsp)
leaq 0x15c75(%rip), %rsi     # 0x46e06
movq %r13, %rdi
callq 0xd080
movq %rax, %rbp
testq %rax, %rax
movq %r13, (%rsp)
je 0x311aa
xorl %r13d, %r13d
jmp 0x311c0
leaq 0x15c5f(%rip), %rsi     # 0x46e10
movq %r13, %rdi
callq 0xd080
testq %rax, %rax
setne %r13b
testq %rbp, %rbp
je 0x311ef
movq $0x0, 0x10(%r15)
movq (%rsp), %rax
leaq 0x8(%rax), %rdi
callq 0xcce0
movq %rax, 0x8(%r15)
testq %rax, %rax
je 0x31493
movb $0x23, (%rax)
jmp 0x3128a
leaq 0x15c24(%rip), %rdi     # 0x46e1a
callq 0xcce0
movq %rax, 0x10(%r15)
testb %r13b, %r13b
je 0x31269
testq %rax, %rax
je 0x314de
movq (%rsp), %r13
leaq 0x9(%r13), %rbp
movq %rbp, %rdi
movl $0x3a, %esi
callq 0xc4c0
testq %rax, %rax
jne 0x3123f
leaq 0x15bf5(%rip), %rsi     # 0x46e23
movq %r13, %rdi
callq 0xd080
testq %rax, %rax
je 0x3149a
leaq 0x1(%r13), %rdi
movl $0x1, %esi
callq 0xcf60
movq %rax, 0x8(%r15)
testq %rax, %rax
je 0x314fc
movabsq $0x2f2f2f3a656c6966, %rcx # imm = 0x2F2F2F3A656C6966
movq %rcx, (%rax)
jmp 0x3128a
testq %rax, %rax
je 0x314e8
movq 0x30(%rbx), %rdi
xorl %esi, %esi
callq 0xcf60
movq %rax, 0x8(%r15)
testq %rax, %rax
je 0x314f2
movq (%rsp), %r13
movq %r13, %rdi
callq 0xd0d0
movzwl 0x1f6(%r14), %edx
cmpq %rdx, %rax
ja 0x31420
leaq 0x10(%rsp), %rsi
movq %r13, 0x8(%rsi)
movq 0x70(%r14), %rdi
callq 0x3dcb4
testq %rax, %rax
je 0x312eb
cmpl $0x0, (%rax)
movl 0x8(%rsp), %ebp
je 0x312ef
movq (%r15), %rdi
callq 0xc0c0
movq 0x8(%r15), %rdi
callq 0xc0c0
movq 0x10(%r15), %rdi
callq 0xc0c0
movq %r15, %rdi
callq 0xc0c0
jmp 0x31317
movl 0x8(%rsp), %ebp
movq %r15, %rax
addq $0x18, %rax
movq $0x0, 0x18(%r15)
movq 0x228(%r14), %rcx
movq 0x8(%rcx), %rcx
movq %r15, (%rcx)
movq 0x228(%r14), %rcx
movq %rax, 0x8(%rcx)
movq %r14, %rdi
movq %r13, %rsi
callq 0x3157f
movl %eax, 0x58(%r12)
movq 0xa0(%rbx), %rax
testq %rax, %rax
je 0x31353
leaq 0x10(%rsp), %rsi
movq %rax, 0x8(%rsi)
movq 0x70(%r14), %rdi
callq 0x3dcb4
testq %rax, %rax
je 0x31353
cmpl $0x0, (%rax)
jne 0x313e0
movl $0x1, %edi
movl $0x20, %esi
callq 0xc7f0
testq %rax, %rax
je 0x313fd
movq %rax, %r15
leaq 0x15b18(%rip), %rdi     # 0x46e8d
callq 0xcce0
movq %rax, (%r15)
testq %rax, %rax
je 0x3140b
movq 0x78(%rbx), %r8
leaq 0x15b03(%rip), %rdx     # 0x46e94
leaq 0x40(%rsp), %r13
movl $0x20, %esi
movq %r13, %rdi
movl %ebp, %ecx
xorl %eax, %eax
callq 0xc570
movq %r13, %rdi
callq 0xcce0
movq %rax, 0x8(%r15)
testq %rax, %rax
je 0x31419
movq %r15, %rax
addq $0x18, %rax
movq $0x0, 0x18(%r15)
movq 0x228(%r14), %rcx
movq 0x8(%rcx), %rcx
movq %r15, (%rcx)
movq 0x228(%r14), %rcx
movq %rax, 0x8(%rcx)
movq 0xa0(%rbx), %rsi
movq %r14, %rdi
callq 0x3157f
movl %eax, 0x54(%r12)
jmp 0x31473
callq 0xe981
jmp 0x31473
callq 0xe957
jmp 0x31449
callq 0xe8d9
jmp 0x31449
callq 0xe92d
jmp 0x3144c
callq 0xe8af
jmp 0x3144c
callq 0xe903
jmp 0x3144c
movq 0x24bd1(%rip), %rax     # 0x55ff8
movq (%rax), %rdi
leaq 0x159f5(%rip), %rsi     # 0x46e26
movq %r13, %rcx
xorl %eax, %eax
callq 0xc870
jmp 0x3144c
callq 0xea1d
jmp 0x31473
callq 0xe9f9
xorl %r15d, %r15d
testq %r15, %r15
je 0x31473
movq (%r15), %rdi
callq 0xc0c0
movq 0x8(%r15), %rdi
callq 0xc0c0
movq 0x10(%r15), %rdi
callq 0xc0c0
movq %r15, %rdi
callq 0xc0c0
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0xe9d5
jmp 0x3144c
callq 0xe9ab
jmp 0x3144c
callq 0xe7b3
jmp 0x3144c
movq %rbp, %rdi
movl $0x1, %esi
callq 0xcf60
movq %rax, 0x8(%r15)
testq %rax, %rax
je 0x31506
movq 0x8(%r15), %rbp
xorl %r13d, %r13d
cmpb $0x5c, (%rbp,%r13)
jne 0x314c5
movb $0x2f, (%rbp,%r13)
incq %r13
movq 0x8(%r15), %rbp
movq %rbp, %rdi
callq 0xc480
cmpq %rax, %r13
jbe 0x314b7
jmp 0x3128a
callq 0xe885
jmp 0x3144c
callq 0xe807
jmp 0x3144c
callq 0xe7dd
jmp 0x3144c
callq 0xe831
jmp 0x3144c
callq 0xe85b
jmp 0x3144c