// File: /workspace/github_c_makefile_star2_O2/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 0x3920d
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r14
movq %rsi, %rdi
callq 0xd150
cmpq $0x100, %rax            # imm = 0x100
jae 0x3922d
movq %r12, %rdi
callq 0xcd50
testq %rax, %rax
je 0x3924d
movq %rax, %rbx
leaq 0xbad8(%rip), %r13      # 0x44b86
movq %rbx, %rdi
movq %r13, %rsi
callq 0xd100
testq %rax, %rax
je 0x390d4
movb $0x47, 0x1(%rax)
addq $0xa, %rax
movb (%rax), %cl
movb %cl, -0x8(%rax)
incq %rax
testb %cl, %cl
jne 0x390c6
jmp 0x390ae
movq %rbx, %rax
incq %rax
xorl %ebp, %ebp
movzbl -0x1(%rax), %ecx
cmpl $0x26, %ecx
je 0x390eb
testl %ecx, %ecx
jne 0x390f4
jmp 0x390f9
cmpb $0x47, (%rax)
sete %cl
addb %cl, %bpl
incq %rax
jmp 0x390dc
testq %r15, %r15
jne 0x39107
testb %bpl, %bpl
jne 0x391e6
movq 0x1b0(%r14), %rdi
callq 0xc0d0
andq $0x0, 0x1b0(%r14)
testq %r15, %r15
je 0x391d0
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 0x391e6
movq 0x378(%r14), %rdi
callq 0x2c85b
movq 0x380(%r14), %rdi
callq 0x2c85b
movq 0x388(%r14), %rdi
callq 0x2c85b
movsd (%r15), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x39189
movsd %xmm0, 0x188(%r14)
movq 0x8(%r15), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0xc0b0
testl %eax, %eax
jne 0x391c1
movq 0x10(%r15), %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
callq 0xc0b0
testl %eax, %eax
jne 0x391c1
movq 0x18(%r15), %rsi
pushq $0x2
popq %rdx
movq %r14, %rdi
callq 0xc0b0
testl %eax, %eax
je 0x391d0
movl %eax, %ebp
movq %rbx, %rdi
callq 0xc0d0
jmp 0x39274
movq %rbx, 0x1b0(%r14)
movb $0x1, 0x1ac(%r14)
xorl %ebp, %ebp
jmp 0x39274
movq 0x19e0b(%rip), %rax     # 0x52ff8
movq (%rax), %rdi
leaq 0xb99a(%rip), %rsi      # 0x44b91
movq %r12, %rdx
xorl %eax, %eax
callq 0xc8d0
movq %rbx, %rdi
callq 0xc0d0
pushq $0xd
jmp 0x39273
movq 0x19de4(%rip), %rax     # 0x52ff8
movq (%rax), %rcx
leaq 0xb89f(%rip), %rdi      # 0x44abd
pushq $0x57
popq %rsi
pushq $0x1
popq %rdx
callq 0xd000
pushq $0xc
jmp 0x39273
movq 0x19dc4(%rip), %rax     # 0x52ff8
movq (%rax), %rcx
leaq 0xb8d7(%rip), %rdi      # 0x44b15
pushq $0x70
popq %rsi
pushq $0x1
popq %rdx
callq 0xd000
pushq $0x14
jmp 0x39273
movq 0x19da4(%rip), %rax     # 0x52ff8
movq (%rax), %rdi
leaq 0x3e82(%rip), %rsi      # 0x3d0e0
leaq 0xa77b(%rip), %rdx      # 0x439e0
movl $0x25b9, %ecx           # imm = 0x25B9
xorl %eax, %eax
callq 0xc8d0
pushq $0x1
popq %rbp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq