// File: /workspace/github_c_makefile_star2_O1/zhou-yuxin[P]redis-aof-no-cow/src/ziplist.c
// Function: ziplistMerge
unsigned char *ziplistMerge(unsigned char **first, unsigned char **second) {
    /* If any params are null, we can't merge, so NULL. */
    if (first == NULL || *first == NULL || second == NULL || *second == NULL)
        return NULL;

    /* Can't merge same list into itself. */
    if (*first == *second)
        return NULL;

    size_t first_bytes = intrev32ifbe(ZIPLIST_BYTES(*first));
    size_t first_len = intrev16ifbe(ZIPLIST_LENGTH(*first));

    size_t second_bytes = intrev32ifbe(ZIPLIST_BYTES(*second));
    size_t second_len = intrev16ifbe(ZIPLIST_LENGTH(*second));

    int append;
    unsigned char *source, *target;
    size_t target_bytes, source_bytes;
    /* Pick the largest ziplist so we can resize easily in-place.
     * We must also track if we are now appending or prepending to
     * the target ziplist. */
    if (first_len >= second_len) {
        /* retain first, append second to first. */
        target = *first;
        target_bytes = first_bytes;
        source = *second;
        source_bytes = second_bytes;
        append = 1;
    } else {
        /* else, retain second, prepend first to second. */
        target = *second;
        target_bytes = second_bytes;
        source = *first;
        source_bytes = first_bytes;
        append = 0;
    }

    /* Calculate final bytes (subtract one pair of metadata) */
    size_t zlbytes = first_bytes + second_bytes -
                     ZIPLIST_HEADER_SIZE - ZIPLIST_END_SIZE;
    size_t zllength = first_len + second_len;

    /* Combined zl length should be limited within UINT16_MAX */
    zllength = zllength < UINT16_MAX ? zllength : UINT16_MAX;

    /* Save offset positions before we start ripping memory apart. */
    size_t first_offset = intrev32ifbe(ZIPLIST_TAIL_OFFSET(*first));
    size_t second_offset = intrev32ifbe(ZIPLIST_TAIL_OFFSET(*second));

    /* Extend target to new zlbytes then append or prepend source. */
    target = zrealloc(target, zlbytes);
    if (append) {
        /* append == appending to target */
        /* Copy source after target (copying over original [END]):
         *   [TARGET - END, SOURCE - HEADER] */
        memcpy(target + target_bytes - ZIPLIST_END_SIZE,
               source + ZIPLIST_HEADER_SIZE,
               source_bytes - ZIPLIST_HEADER_SIZE);
    } else {
        /* !append == prepending to target */
        /* Move target *contents* exactly size of (source - [END]),
         * then copy source into vacataed space (source - [END]):
         *   [SOURCE - END, TARGET - HEADER] */
        memmove(target + source_bytes - ZIPLIST_END_SIZE,
                target + ZIPLIST_HEADER_SIZE,
                target_bytes - ZIPLIST_HEADER_SIZE);
        memcpy(target, source, source_bytes - ZIPLIST_END_SIZE);
    }

    /* Update header metadata. */
    ZIPLIST_BYTES(target) = intrev32ifbe(zlbytes);
    ZIPLIST_LENGTH(target) = intrev16ifbe(zllength);
    /* New tail offset is:
     *   + N bytes of first ziplist
     *   - 1 byte for [END] of first ziplist
     *   + M bytes for the offset of the original tail of the second ziplist
     *   - J bytes for HEADER because second_offset keeps no header. */
    ZIPLIST_TAIL_OFFSET(target) = intrev32ifbe(
                                   (first_bytes - ZIPLIST_END_SIZE) +
                                   (second_offset - ZIPLIST_HEADER_SIZE));

    /* __ziplistCascadeUpdate just fixes the prev length values until it finds a
     * correct prev length value (then it assumes the rest of the list is okay).
     * We tell CascadeUpdate to start at the first ziplist's tail element to fix
     * the merge seam. */
    target = __ziplistCascadeUpdate(target, target+first_offset);

    /* Now free and NULL out what we didn't realloc */
    if (append) {
        zfree(*second);
        *second = NULL;
        *first = target;
    } else {
        zfree(*first);
        *first = NULL;
        *second = target;
    }
    return target;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
testq %rdi, %rdi
je 0x3e8e3
movq %rsi, %rbx
movq %rdi, %r14
movq (%rdi), %r12
testq %r12, %r12
sete %al
testq %rsi, %rsi
sete %cl
orb %al, %cl
jne 0x3e8e3
movq (%rbx), %r15
testq %r15, %r15
sete %al
cmpq %r15, %r12
sete %cl
orb %al, %cl
je 0x3e8f8
xorl %r14d, %r14d
movq %r14, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl (%r12), %ecx
movq %rcx, (%rsp)
movl 0x4(%r12), %eax
movq %rax, 0x30(%rsp)
movzwl 0x8(%r12), %edx
movq %rdx, 0x8(%rsp)
movl (%r15), %ebp
movzwl 0x8(%r15), %r8d
movq %r8, 0x10(%rsp)
cmpw %r8w, %dx
movq %r12, %rdi
cmovbq %r15, %rdi
movl 0x4(%r15), %eax
movq %rax, 0x28(%rsp)
leaq (%rcx,%rbp), %rsi
addq $-0xb, %rsi
movq %r8, %rcx
addq %rdx, %rcx
movl $0xffff, %eax           # imm = 0xFFFF
cmpq %rax, %rcx
cmovaeq %rax, %rcx
movq %rcx, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
callq 0x3b349
movq %rax, %r13
movq (%rsp), %rax
movq %rax, %rdx
movq 0x8(%rsp), %rcx
cmpw 0x10(%rsp), %cx
cmovbq %rbp, %rdx
cmovbq %rax, %rbp
cmovbq %r12, %r15
jae 0x3e9b2
leaq -0x1(,%r13), %rdi
addq %rbp, %rdi
leaq 0xa(%r13), %rsi
addq $-0xa, %rdx
callq 0x267f0
decq %rbp
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x26c60
movq %rbx, %r15
movq %r14, %rbx
jmp 0x3e9cf
leaq (%rdx,%r13), %rdi
decq %rdi
addq $0xa, %r15
addq $-0xa, %rbp
movq %r15, %rsi
movq %rbp, %rdx
callq 0x26c60
movq %r14, %r15
movq 0x18(%rsp), %rax
movl %eax, (%r13)
movq 0x20(%rsp), %rax
movw %ax, 0x8(%r13)
movq (%rsp), %rax
movq 0x28(%rsp), %rcx
addl %ecx, %eax
addl $-0xb, %eax
movl %eax, 0x4(%r13)
movq %r13, %rsi
addq 0x30(%rsp), %rsi
movq %r13, %rdi
callq 0x3e31b
movq %rax, %r14
movq (%rbx), %rdi
callq 0x3b3df
movq $0x0, (%rbx)
movq %r14, (%r15)
jmp 0x3e8e6