// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x40e55
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
je 0x40e55
cmpq $0x0, -0x18(%rbp)
je 0x40e55
movq -0x18(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x40e62
movq $0x0, -0x8(%rbp)
jmp 0x41099
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
cmpq (%rcx), %rax
jne 0x40e7f
movq $0x0, -0x8(%rbp)
jmp 0x41099
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl (%rax), %eax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movzwl 0x8(%rax), %eax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl (%rax), %eax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movzwl 0x8(%rax), %eax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x38(%rbp), %rax
jb 0x40ef0
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x60(%rbp)
movl $0x1, -0x3c(%rbp)
jmp 0x40f1d
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x60(%rbp)
movl $0x0, -0x3c(%rbp)
movq -0x20(%rbp), %rax
addq -0x30(%rbp), %rax
subq $0xa, %rax
subq $0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x28(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpq $0xffff, -0x70(%rbp)    # imm = 0xFFFF
jae 0x40f54
movq -0x70(%rbp), %rax
movq %rax, -0x88(%rbp)
jmp 0x40f62
movl $0xffff, %eax           # imm = 0xFFFF
movq %rax, -0x88(%rbp)
jmp 0x40f62
movq -0x88(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl 0x4(%rax), %eax
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl 0x4(%rax), %eax
movq %rax, -0x80(%rbp)
movq -0x50(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x3ae40
movq %rax, -0x50(%rbp)
cmpl $0x0, -0x3c(%rbp)
je 0x40fc3
movq -0x50(%rbp), %rdi
addq -0x58(%rbp), %rdi
addq $-0x1, %rdi
movq -0x48(%rbp), %rsi
addq $0xa, %rsi
movq -0x60(%rbp), %rdx
subq $0xa, %rdx
callq 0x26c60
jmp 0x40ff9
movq -0x50(%rbp), %rdi
addq -0x60(%rbp), %rdi
addq $-0x1, %rdi
movq -0x50(%rbp), %rsi
addq $0xa, %rsi
movq -0x58(%rbp), %rdx
subq $0xa, %rdx
callq 0x267e0
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x60(%rbp), %rdx
subq $0x1, %rdx
callq 0x26c60
movq -0x68(%rbp), %rax
movl %eax, %ecx
movq -0x50(%rbp), %rax
movl %ecx, (%rax)
movq -0x70(%rbp), %rax
movw %ax, %cx
movq -0x50(%rbp), %rax
movw %cx, 0x8(%rax)
movq -0x20(%rbp), %rax
subq $0x1, %rax
movq -0x80(%rbp), %rcx
subq $0xa, %rcx
addq %rcx, %rax
movl %eax, %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x50(%rbp), %rdi
movq -0x50(%rbp), %rsi
addq -0x78(%rbp), %rsi
callq 0x406e0
movq %rax, -0x50(%rbp)
cmpl $0x0, -0x3c(%rbp)
je 0x4106f
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0x3afa0
movq -0x18(%rbp), %rax
movq $0x0, (%rax)
movq -0x50(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
jmp 0x41091
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x3afa0
movq -0x10(%rbp), %rax
movq $0x0, (%rax)
movq -0x50(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x50(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)