// File: /workspace/github_c_makefile_star2_O1/seojangho[P]clee/simclist.c
// Function: list_dump_filedescriptor
int list_dump_filedescriptor(const list_t *restrict l, int fd, size_t *restrict len) {
    struct list_entry_s *x;
    void *ser_buf;
    uint32_t bufsize;
    struct timeval timeofday;
    struct list_dump_header_s header;

    if (l->attrs.meter == NULL && l->attrs.serializer == NULL) {
        errno = ENOTTY;
        return -1;
    }

    /****       DUMP FORMAT      ****

    [ ver   timestamp   |  totlen   numels  elemlen     hash    |   DATA ]
    
    where DATA can be:
    @ for constant-size list (element size is constant; elemlen > 0)
    [ elem    elem    ...    elem ]
    @ for other lists (element size dictated by element_meter each time; elemlen <= 0)
    [ size elem     size elem       ...     size elem ]
    
    all integers are encoded in NETWORK BYTE FORMAT
    *****/


    /* prepare HEADER */
    /* version */
    header.ver = htons( SIMCLIST_DUMPFORMAT_VERSION );

    /* timestamp */
    gettimeofday(&timeofday, NULL);
    header.timestamp = (int64_t)timeofday.tv_sec * 1000000 + (int64_t)timeofday.tv_usec;
    header.timestamp = hton64(header.timestamp);

    header.rndterm = htonl((int32_t)get_random());

    /* total list size is postprocessed afterwards */

    /* number of elements */
    header.numels = htonl(l->numels);

    /* include an hash, if possible */
    if (l->attrs.hasher != NULL) {
        if (htonl(list_hash(l, & header.listhash)) != 0) {
            /* could not compute list hash! */
            return -1;
        }
    } else {
        header.listhash = htonl(0);
    }

    header.totlistlen = header.elemlen = 0;

    /* leave room for the header at the beginning of the file */
    if (lseek(fd, SIMCLIST_DUMPFORMAT_HEADERLEN, SEEK_SET) < 0) {
        /* errno set by lseek() */
        return -1;
    }

    /* write CONTENT */
    if (l->numels > 0) {
        /* SPECULATE that the list has constant element size */
        
        if (l->attrs.serializer != NULL) {  /* user user-specified serializer */
            /* get preliminary length of serialized element in header.elemlen */
            ser_buf = l->attrs.serializer(l->head_sentinel->next->data, & header.elemlen);
            free(ser_buf);
            /* request custom serialization of each element */
            for (x = l->head_sentinel->next; x != l->tail_sentinel; x = x->next) {
                ser_buf = l->attrs.serializer(x->data, &bufsize);
                header.totlistlen += bufsize;
                if (header.elemlen != 0) {      /* continue on speculation */
                    if (header.elemlen != bufsize) {
                        free(ser_buf);
                        /* constant element length speculation broken! */
                        header.elemlen = 0;
                        header.totlistlen = 0;
                        x = l->head_sentinel;
                        if (lseek(fd, SIMCLIST_DUMPFORMAT_HEADERLEN, SEEK_SET) < 0) {
                            /* errno set by lseek() */
                            return -1;
                        }
                        /* restart from the beginning */
                        continue;
                    }
                    /* speculation confirmed */
                    WRITE_ERRCHECK(fd, ser_buf, bufsize);
                } else {                        /* speculation found broken */
                    WRITE_ERRCHECK(fd, & bufsize, sizeof(size_t));
                    WRITE_ERRCHECK(fd, ser_buf, bufsize);
                }
                free(ser_buf);
            }
        } else if (l->attrs.meter != NULL) {
            header.elemlen = (uint32_t)l->attrs.meter(l->head_sentinel->next->data);

            /* serialize the element straight from its data */
            for (x = l->head_sentinel->next; x != l->tail_sentinel; x = x->next) {
                bufsize = l->attrs.meter(x->data);
                header.totlistlen += bufsize;
                if (header.elemlen != 0) {
                    if (header.elemlen != bufsize) {
                        /* constant element length speculation broken! */
                        header.elemlen = 0;
                        header.totlistlen = 0;
                        x = l->head_sentinel;
                        /* restart from the beginning */
                        continue;
                    }
                    WRITE_ERRCHECK(fd, x->data, bufsize);
                } else {
                    WRITE_ERRCHECK(fd, &bufsize, sizeof(size_t));
                    WRITE_ERRCHECK(fd, x->data, bufsize);
                }
            }
        }
        /* adjust endianness */
        header.elemlen = htonl(header.elemlen);
        header.totlistlen = htonl(header.totlistlen);
    }

    /* write random terminator */
    WRITE_ERRCHECK(fd, & header.rndterm, sizeof(header.rndterm));        /* list terminator */


    /* write header */
    lseek(fd, 0, SEEK_SET);

    WRITE_ERRCHECK(fd, & header.ver, sizeof(header.ver));                        /* version */
    WRITE_ERRCHECK(fd, & header.timestamp, sizeof(header.timestamp));            /* timestamp */
    WRITE_ERRCHECK(fd, & header.rndterm, sizeof(header.rndterm));                /* random terminator */

    WRITE_ERRCHECK(fd, & header.totlistlen, sizeof(header.totlistlen));          /* total length of elements */
    WRITE_ERRCHECK(fd, & header.numels, sizeof(header.numels));                  /* number of elements */
    WRITE_ERRCHECK(fd, & header.elemlen, sizeof(header.elemlen));                /* size of each element, or 0 for independent */
    WRITE_ERRCHECK(fd, & header.listhash, sizeof(header.listhash));             /* list hash, or 0 for "ignore" */


    /* possibly store total written length in "len" */
    if (len != NULL) {
        *len = sizeof(header) + ntohl(header.totlistlen);
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, %rbp
movl %esi, %r14d
movq %rdi, %r12
movq 0x50(%rdi), %r13
testq %r13, %r13
jne 0x4e3c
cmpq $0x0, 0x68(%r12)
je 0x4fb4
movw $0x100, 0x10(%rsp)      # imm = 0x100
leaq 0x48(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x30b0
imulq $0xf4240, (%r15), %rax  # imm = 0xF4240
addq 0x8(%r15), %rax
bswapq %rax
movq %rax, 0x18(%rsp)
callq 0x31e0
bswapl %eax
movl %eax, 0x20(%rsp)
movl 0x18(%r12), %ebx
movl %ebx, %eax
bswapl %eax
movl %eax, 0x28(%rsp)
cmpq $0x0, 0x60(%r12)
je 0x4ea2
leaq 0x30(%rsp), %rsi
movq %r12, %rdi
callq 0x4bd3
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x5166
jmp 0x4eaa
movl $0x0, 0x30(%rsp)
xorl %eax, %eax
movl %eax, 0x2c(%rsp)
movl %eax, 0x24(%rsp)
movl $0x1e, %esi
movl %r14d, %edi
xorl %edx, %edx
callq 0x30a0
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testq %rax, %rax
js 0x5166
testl %ebx, %ebx
je 0x507f
movq %rbp, 0x38(%rsp)
movq 0x68(%r12), %r15
testq %r15, %r15
je 0x4fca
movq (%r12), %rbx
movq 0x8(%rbx), %rax
movq (%rax), %rdi
leaq 0x2c(%rsp), %rsi
callq *%r15
movq %rax, %rdi
callq 0x3030
movq 0x8(%r12), %rbp
movq %rbx, 0x40(%rsp)
movq 0x8(%rbx), %rbx
cmpq %rbp, %rbx
je 0x5060
leaq 0xc(%rsp), %r12
movq (%rbx), %rdi
movq %r12, %rsi
callq *%r15
movl 0xc(%rsp), %edx
addl %edx, 0x24(%rsp)
movq %rax, %r13
movl 0x2c(%rsp), %eax
testl %eax, %eax
je 0x4f71
cmpl %edx, %eax
je 0x4f8a
movq %r13, %rdi
callq 0x3030
xorl %eax, %eax
movl %eax, 0x2c(%rsp)
movl %eax, 0x24(%rsp)
movl $0x1e, %esi
movl %r14d, %edi
xorl %edx, %edx
callq 0x30a0
movq 0x40(%rsp), %rbx
testq %rax, %rax
jns 0x4fa2
jmp 0x4fbf
movl $0x8, %edx
movl %r14d, %edi
movq %r12, %rsi
callq 0x3070
testq %rax, %rax
js 0x4fbf
movl 0xc(%rsp), %edx
movl %r14d, %edi
movq %r13, %rsi
callq 0x3070
testq %rax, %rax
js 0x4fbf
movq %r13, %rdi
callq 0x3030
movq 0x8(%rbx), %rbx
cmpq %rbp, %rbx
jne 0x4f24
jmp 0x5060
callq 0x3040
movl $0x19, (%rax)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
jmp 0x5166
testq %r13, %r13
je 0x5060
movq (%r12), %rbx
movq 0x8(%rbx), %rax
movq (%rax), %rdi
callq *%r13
movl %eax, 0x2c(%rsp)
movq 0x8(%r12), %r15
movq 0x8(%rbx), %rbp
cmpq %r15, %rbp
je 0x5060
xorl %r12d, %r12d
movq (%rbp), %rdi
callq *%r13
addl %eax, 0x24(%rsp)
movl %eax, 0xc(%rsp)
movl 0x2c(%rsp), %ecx
testl %ecx, %ecx
je 0x5019
cmpl %eax, %ecx
jne 0x504a
movq (%rbp), %rsi
movl %eax, %edx
jmp 0x5038
movl $0x8, %edx
movl %r14d, %edi
leaq 0xc(%rsp), %rsi
callq 0x3070
testq %rax, %rax
js 0x4fbf
movq (%rbp), %rsi
movl 0xc(%rsp), %edx
movl %r14d, %edi
callq 0x3070
testq %rax, %rax
jns 0x5057
jmp 0x4fbf
movl %r12d, 0x2c(%rsp)
movl %r12d, 0x24(%rsp)
movq %rbx, %rbp
movq 0x8(%rbp), %rbp
cmpq %r15, %rbp
jne 0x4ff6
movl 0x24(%rsp), %eax
movl 0x2c(%rsp), %ecx
bswapl %ecx
movl %ecx, 0x2c(%rsp)
bswapl %eax
movl %eax, 0x24(%rsp)
movq 0x38(%rsp), %rbp
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
leaq 0x20(%rsp), %r12
movl $0x4, %edx
movl %r14d, %edi
movq %r12, %rsi
callq 0x3070
testq %rax, %rax
js 0x5166
movl %r14d, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x30a0
leaq 0x10(%rsp), %rsi
movl $0x2, %edx
movl %r14d, %edi
callq 0x3070
testq %rax, %rax
js 0x5166
leaq 0x18(%rsp), %rsi
movl $0x8, %edx
movl %r14d, %edi
callq 0x3070
testq %rax, %rax
js 0x5166
movl $0x4, %edx
movl %r14d, %edi
movq %r12, %rsi
callq 0x3070
testq %rax, %rax
js 0x5166
leaq 0x24(%rsp), %rsi
movl $0x4, %edx
movl %r14d, %edi
callq 0x3070
testq %rax, %rax
js 0x5166
leaq 0x28(%rsp), %rsi
movl $0x4, %edx
movl %r14d, %edi
callq 0x3070
testq %rax, %rax
js 0x5166
movl $0x4, %edx
movl %r14d, %edi
leaq 0x2c(%rsp), %rsi
callq 0x3070
testq %rax, %rax
js 0x5166
leaq 0x30(%rsp), %rsi
movl $0x4, %edx
movl %r14d, %edi
callq 0x3070
testq %rax, %rax
js 0x5166
xorl %r15d, %r15d
testq %rbp, %rbp
je 0x5166
movl 0x24(%rsp), %eax
bswapl %eax
addq $0x28, %rax
movq %rax, (%rbp)
movl %r15d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq