// File: /workspace/github_c_makefile_star2_O1/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 0x3bf4f
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r14
movq %rsi, %rdi
callq 0xd0d0
cmpq $0x100, %rax            # imm = 0x100
jae 0x3bf5b
movq %r12, %rdi
callq 0xcce0
testq %rax, %rax
je 0x3bf67
movq %rax, %rbx
leaq 0xbce6(%rip), %rsi      # 0x47ab1
movq %rax, %rdi
callq 0xd080
testq %rax, %rax
je 0x3be03
leaq 0xbcd2(%rip), %r13      # 0x47ab1
movb $0x47, 0x1(%rax)
addq $0xa, %rax
movb (%rax), %cl
movb %cl, -0x8(%rax)
incq %rax
testb %cl, %cl
jne 0x3bde7
movq %rbx, %rdi
movq %r13, %rsi
callq 0xd080
testq %rax, %rax
jne 0x3bddf
movq %rbx, %rax
incq %rax
xorl %ebp, %ebp
movzbl -0x1(%rax), %ecx
cmpl $0x26, %ecx
je 0x3be1a
testl %ecx, %ecx
jne 0x3be23
jmp 0x3be28
cmpb $0x47, (%rax)
sete %cl
addb %cl, %bpl
incq %rax
jmp 0x3be0b
testq %r15, %r15
jne 0x3be36
testb %bpl, %bpl
jne 0x3bf25
movq 0x1b0(%r14), %rdi
callq 0xc0c0
movq $0x0, 0x1b0(%r14)
testq %r15, %r15
je 0x3bf03
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 0x3bf25
movq 0x378(%r14), %rdi
callq 0x2f90a
movq 0x380(%r14), %rdi
callq 0x2f90a
movq 0x388(%r14), %rdi
callq 0x2f90a
movsd (%r15), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x3bebb
movsd %xmm0, 0x188(%r14)
movq 0x8(%r15), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0xc0a0
testl %eax, %eax
jne 0x3bef7
movq 0x10(%r15), %rsi
movq %r14, %rdi
movl $0x1, %edx
callq 0xc0a0
testl %eax, %eax
jne 0x3bef7
movq 0x18(%r15), %rsi
movq %r14, %rdi
movl $0x2, %edx
callq 0xc0a0
testl %eax, %eax
je 0x3bf03
movl %eax, %ebp
movq %rbx, %rdi
callq 0xc0c0
jmp 0x3bf14
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 0x1a0cc(%rip), %rax     # 0x55ff8
movq (%rax), %rdi
leaq 0xbb86(%rip), %rsi      # 0x47abc
movq %r12, %rdx
xorl %eax, %eax
callq 0xc870
movq %rbx, %rdi
callq 0xc0c0
movl $0xd, %ebp
jmp 0x3bf14
callq 0xf4b1
movl $0xc, %ebp
jmp 0x3bf14
callq 0xf495
movl $0x14, %ebp
jmp 0x3bf14
callq 0xf471
movl $0x1, %ebp
jmp 0x3bf14