// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/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 0x35995
movq %rsi, %rbx
movq %rdi, %r14
movq (%rdi), %r15
testq %r15, %r15
sete %al
testq %rsi, %rsi
sete %cl
orb %al, %cl
jne 0x35995
movq (%rbx), %rbp
testq %rbp, %rbp
sete %al
cmpq %rbp, %r15
sete %cl
orb %al, %cl
je 0x359aa
xorl %r14d, %r14d
movq %r14, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl (%r15), %edx
movq %rdx, 0x8(%rsp)
movl 0x4(%r15), %eax
movq %rax, 0x30(%rsp)
movzwl 0x8(%r15), %ecx
movq %rcx, 0x10(%rsp)
movl (%rbp), %r13d
movzwl 0x8(%rbp), %r12d
movq %r12, 0x18(%rsp)
cmpw %r12w, %cx
movq %r15, %rdi
cmovbq %rbp, %rdi
movl 0x4(%rbp), %eax
movq %rax, 0x28(%rsp)
leaq (%rdx,%r13), %rsi
addq $-0xb, %rsi
addq %rcx, %r12
movl $0xffff, %eax           # imm = 0xFFFF
cmpq %rax, %r12
cmovaeq %rax, %r12
movq %rsi, 0x20(%rsp)
callq 0x32863
movq %r13, %rdx
movq %rax, %r13
addq $-0xa, %rdx
movq 0x8(%rsp), %rax
leaq (%rax,%r13), %rdi
decq %rdi
movq 0x10(%rsp), %rax
cmpw 0x18(%rsp), %ax
jae 0x35a51
leaq 0xa(%r13), %rsi
callq 0x267e0
movq 0x8(%rsp), %rbp
leaq -0x1(%rbp), %rdx
movq %r13, %rdi
movq %r15, %rsi
callq 0x26c50
movq %rbx, %r15
movq %r14, %rbx
movq %rbp, %rax
jmp 0x35a65
addq $0xa, %rbp
movq %rbp, %rsi
callq 0x26c50
movq %r14, %r15
movq 0x8(%rsp), %rax
movq 0x20(%rsp), %rcx
movl %ecx, (%r13)
movw %r12w, 0x8(%r13)
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 0x35441
movq %rax, %r14
movq (%rbx), %rdi
callq 0x328f9
andq $0x0, (%rbx)
movq %r14, (%r15)
jmp 0x35998