// File: /workspace/github_c_makefile_star2_O3/Paxa[P]fast_excel/libxlsxwriter/src/worksheet.c
// Function: worksheet_set_header_opt
lxw_error
worksheet_set_header_opt(lxw_worksheet *self, const char *string,
                         lxw_header_footer_options *options)
{
    lxw_error err;
    char *tmp_header;
    char *found_string;
    char *offset_string;
    uint8_t placeholder_count = 0;
    uint8_t image_count = 0;

    if (!string) {
        LXW_WARN_FORMAT("worksheet_set_header_opt/footer_opt(): "
                        "header/footer string cannot be NULL.");
        return LXW_ERROR_NULL_PARAMETER_IGNORED;
    }

    if (lxw_utf8_strlen(string) > LXW_HEADER_FOOTER_MAX) {
        LXW_WARN_FORMAT("worksheet_set_header_opt/footer_opt(): "
                        "header/footer string exceeds Excel's limit of "
                        "255 characters.");
        return LXW_ERROR_255_STRING_LENGTH_EXCEEDED;
    }

    tmp_header = lxw_strdup(string);
    RETURN_ON_MEM_ERROR(tmp_header, LXW_ERROR_MEMORY_MALLOC_FAILED);

    /* Replace &[Picture] with &G which is used internally by Excel. */
    while ((found_string = strstr(tmp_header, "&[Picture]"))) {
        found_string++;
        *found_string = 'G';

        do {
            found_string++;
            offset_string = found_string + sizeof("Picture");
            *found_string = *offset_string;
        } while (*offset_string);
    }

    /* Count &G placeholders and ensure there are sufficient images. */
    found_string = tmp_header;
    while (*found_string) {
        if (*found_string == '&' && *(found_string + 1) == 'G')
            placeholder_count++;
        found_string++;
    }

    if (placeholder_count > 0 && !options) {
        LXW_WARN_FORMAT1("worksheet_set_header_opt/footer_opt(): "
                         "the number of &G/&[Picture] placeholders in option "
                         "string \"%s\" does not match the number of supplied "
                         "images.", string);

        free(tmp_header);
        return LXW_ERROR_PARAMETER_VALIDATION;
    }

    /* Free any previous header string so we can overwrite it. */
    free(self->header);
    self->header = NULL;

    if (options) {
        /* Ensure there are enough images to match the placeholders. There is
         * a potential bug where there are sufficient images but in the wrong
         * positions but we don't currently try to deal with that.*/
        if (options->image_left)
            image_count++;
        if (options->image_center)
            image_count++;
        if (options->image_right)
            image_count++;

        if (placeholder_count != image_count) {
            LXW_WARN_FORMAT1("worksheet_set_header_opt/footer_opt(): "
                             "the number of &G/&[Picture] placeholders in option "
                             "string \"%s\" does not match the number of supplied "
                             "images.", string);

            free(tmp_header);
            return LXW_ERROR_PARAMETER_VALIDATION;
        }

        /* Free any existing header image objects. */
        _free_object_properties(self->header_left_object_props);
        _free_object_properties(self->header_center_object_props);
        _free_object_properties(self->header_right_object_props);

        if (options->margin > 0.0)
            self->margin_header = options->margin;

        err = _worksheet_set_header_footer_image(self,
                                                 options->image_left,
                                                 HEADER_LEFT);
        if (err) {
            free(tmp_header);
            return err;
        }

        err = _worksheet_set_header_footer_image(self,
                                                 options->image_center,
                                                 HEADER_CENTER);
        if (err) {
            free(tmp_header);
            return err;
        }

        err = _worksheet_set_header_footer_image(self,
                                                 options->image_right,
                                                 HEADER_RIGHT);
        if (err) {
            free(tmp_header);
            return err;
        }
    }

    self->header = tmp_header;
    self->header_footer_changed = LXW_TRUE;

    return LXW_NO_ERROR;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
testq %rsi, %rsi
je 0x3c217
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r14
movq %rsi, %rdi
callq 0xd0d0
cmpq $0x100, %rax            # imm = 0x100
jae 0x3c223
movq %r12, %rdi
callq 0xcce0
testq %rax, %rax
je 0x3c22f
movq %rax, %rbx
leaq 0xbb07(%rip), %rsi      # 0x47b9a
movq %rax, %rdi
callq 0xd080
testq %rax, %rax
je 0x3c0cb
leaq 0xbaf3(%rip), %r13      # 0x47b9a
movb $0x47, 0x1(%rax)
addq $0xa, %rax
movb (%rax), %cl
movb %cl, -0x8(%rax)
incq %rax
testb %cl, %cl
jne 0x3c0af
movq %rbx, %rdi
movq %r13, %rsi
callq 0xd080
testq %rax, %rax
jne 0x3c0a7
movq %rbx, %rax
incq %rax
xorl %ebp, %ebp
movzbl -0x1(%rax), %ecx
cmpl $0x26, %ecx
je 0x3c0e2
testl %ecx, %ecx
jne 0x3c0eb
jmp 0x3c0f0
cmpb $0x47, (%rax)
sete %cl
addb %cl, %bpl
incq %rax
jmp 0x3c0d3
testq %r15, %r15
jne 0x3c0fe
testb %bpl, %bpl
jne 0x3c1ed
movq 0x1b0(%r14), %rdi
callq 0xc0c0
movq $0x0, 0x1b0(%r14)
testq %r15, %r15
je 0x3c1cb
xorl %eax, %eax
cmpq $0x0, 0x8(%r15)
setne %al
leal 0x1(%rax), %ecx
cmpq $0x0, 0x10(%r15)
movzbl %cl, %ecx
cmovel %eax, %ecx
cmpq $0x1, 0x18(%r15)
sbbb $-0x1, %cl
cmpb %cl, %bpl
jne 0x3c1ed
movq 0x378(%r14), %rdi
callq 0x2fa62
movq 0x380(%r14), %rdi
callq 0x2fa62
movq 0x388(%r14), %rdi
callq 0x2fa62
movsd (%r15), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x3c183
movsd %xmm0, 0x188(%r14)
movq 0x8(%r15), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0xc0a0
testl %eax, %eax
jne 0x3c1bf
movq 0x10(%r15), %rsi
movq %r14, %rdi
movl $0x1, %edx
callq 0xc0a0
testl %eax, %eax
jne 0x3c1bf
movq 0x18(%r15), %rsi
movq %r14, %rdi
movl $0x2, %edx
callq 0xc0a0
testl %eax, %eax
je 0x3c1cb
movl %eax, %ebp
movq %rbx, %rdi
callq 0xc0c0
jmp 0x3c1dc
movq %rbx, 0x1b0(%r14)
movb $0x1, 0x1ac(%r14)
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1ae04(%rip), %rax     # 0x56ff8
movq (%rax), %rdi
leaq 0xb9a7(%rip), %rsi      # 0x47ba5
movq %r12, %rdx
xorl %eax, %eax
callq 0xc870
movq %rbx, %rdi
callq 0xc0c0
movl $0xd, %ebp
jmp 0x3c1dc
callq 0xf894
movl $0xc, %ebp
jmp 0x3c1dc
callq 0xf878
movl $0x14, %ebp
jmp 0x3c1dc
callq 0xf854
movl $0x1, %ebp
jmp 0x3c1dc