// File: /workspace/github_c_makefile_star2_O2/seojangho[P]clee/simclist.c
// Function: list_restore_filedescriptor
int list_restore_filedescriptor(list_t *restrict l, int fd, size_t *restrict len) {
    struct list_dump_header_s header;
    unsigned long cnt;
    void *buf;
    uint32_t elsize, totreadlen, totmemorylen;

    memset(& header, 0, sizeof(header));

    /* read header */
    
    /* version */
    READ_ERRCHECK(fd, &header.ver, sizeof(header.ver));
    header.ver = ntohs(header.ver);
    if (header.ver != SIMCLIST_DUMPFORMAT_VERSION) {
        errno = EILSEQ;
        return -1;
    }

    /* timestamp */
    READ_ERRCHECK(fd, & header.timestamp, sizeof(header.timestamp));

    /* list terminator */
    READ_ERRCHECK(fd, & header.rndterm, sizeof(header.rndterm));

    header.rndterm = ntohl(header.rndterm);

    /* total list size */
    READ_ERRCHECK(fd, & header.totlistlen, sizeof(header.totlistlen));
    header.totlistlen = ntohl(header.totlistlen);

    /* number of elements */
    READ_ERRCHECK(fd, & header.numels, sizeof(header.numels));
    header.numels = ntohl(header.numels);

    /* length of every element, or '0' = variable */
    READ_ERRCHECK(fd, & header.elemlen, sizeof(header.elemlen));
    header.elemlen = ntohl(header.elemlen);

    /* list hash, or 0 = 'ignore' */
    READ_ERRCHECK(fd, & header.listhash, sizeof(header.listhash));
    header.listhash = ntohl(header.listhash);


    /* read content */
    totreadlen = totmemorylen = 0;
    if (header.elemlen > 0) {   
        /* elements have constant size = header.elemlen */
        if (l->attrs.unserializer != NULL) {
            /* use unserializer */
            buf = malloc(header.elemlen);
            for (cnt = 0; cnt < header.numels; cnt++) {
                READ_ERRCHECK(fd, buf, header.elemlen);
                list_append(l, l->attrs.unserializer(buf, & elsize));
                totmemorylen += elsize;
            }
        } else {
            /* copy verbatim into memory */
            for (cnt = 0; cnt < header.numels; cnt++) {
                buf = malloc(header.elemlen);
                READ_ERRCHECK(fd, buf, header.elemlen);
                list_append(l, buf);
            }
            totmemorylen = header.numels * header.elemlen;
        }
        totreadlen = header.numels * header.elemlen;
    } else {
        /* elements have variable size. Each element is preceded by its size */
        if (l->attrs.unserializer != NULL) {
            /* use unserializer */
            for (cnt = 0; cnt < header.numels; cnt++) {
                READ_ERRCHECK(fd, & elsize, sizeof(elsize));
                buf = malloc((size_t)elsize);
                READ_ERRCHECK(fd, buf, elsize);
                totreadlen += elsize;
                list_append(l, l->attrs.unserializer(buf, & elsize));
                totmemorylen += elsize;
            }
        } else {
            /* copy verbatim into memory */
            for (cnt = 0; cnt < header.numels; cnt++) {
                READ_ERRCHECK(fd, & elsize, sizeof(elsize));
                buf = malloc(elsize);
                READ_ERRCHECK(fd, buf, elsize);
                totreadlen += elsize;
                list_append(l, buf);
            }
            totmemorylen = totreadlen;
        }
    }

    READ_ERRCHECK(fd, &elsize, sizeof(elsize));  /* read list terminator */
    elsize = ntohl(elsize);

    /* possibly verify the list consistency */
    /* wrt hash */
    /* don't do that
    if (header.listhash != 0 && header.listhash != list_hash(l)) {
        errno = ECANCELED;
        return -1;
    }
    */

    /* wrt header */
    if (totreadlen != header.totlistlen && (int32_t)elsize == header.rndterm) {
        errno = EPROTO;
        return -1;
    }

    /* wrt file */
    if (lseek(fd, 0, SEEK_CUR) != lseek(fd, 0, SEEK_END)) {
        errno = EPROTO;
        return -1;
    }

    if (len != NULL) {
        *len = totmemorylen;
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, %r14
movl %esi, %ebp
movq %rdi, %r15
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rsi
movaps %xmm0, (%rsi)
movaps %xmm0, 0x10(%rsi)
andq $0x0, 0x20(%rsi)
pushq $0x2
popq %rdx
movl %ebp, %edi
callq 0x30e0
pushq $-0x1
popq %rbx
cmpq $0x2, %rax
jne 0x4ef7
movzwl 0x20(%rsp), %eax
movl %eax, %ecx
rolw $0x8, %cx
movw %cx, 0x20(%rsp)
cmpw $0x100, %ax             # imm = 0x100
jne 0x4eec
leaq 0x28(%rsp), %rsi
pushq $0x8
popq %rdx
movl %ebp, %edi
callq 0x30e0
cmpq $0x8, %rax
jne 0x4ef7
leaq 0x30(%rsp), %rsi
pushq $0x4
popq %rdx
movl %ebp, %edi
callq 0x30e0
cmpq $0x4, %rax
jne 0x4ef7
leaq 0x34(%rsp), %rsi
movl -0x4(%rsi), %eax
bswapl %eax
movl %eax, -0x4(%rsi)
pushq $0x4
popq %rdx
movl %ebp, %edi
callq 0x30e0
cmpq $0x4, %rax
jne 0x4ef7
leaq 0x38(%rsp), %rsi
movl -0x4(%rsi), %eax
bswapl %eax
movl %eax, -0x4(%rsi)
pushq $0x4
popq %rdx
movl %ebp, %edi
callq 0x30e0
cmpq $0x4, %rax
jne 0x4ef7
leaq 0x3c(%rsp), %rsi
movl -0x4(%rsi), %eax
bswapl %eax
movl %eax, -0x4(%rsi)
pushq $0x4
popq %rdx
movl %ebp, %edi
callq 0x30e0
cmpq $0x4, %rax
jne 0x4ef7
leaq 0x40(%rsp), %rsi
movl -0x4(%rsi), %eax
bswapl %eax
movl %eax, -0x4(%rsi)
pushq $0x4
popq %rdx
movl %ebp, %edi
callq 0x30e0
cmpq $0x4, %rax
jne 0x4ef7
movl 0x3c(%rsp), %ecx
movq 0x70(%r15), %rax
movq %rcx, 0x10(%rsp)
testq %rcx, %rcx
je 0x4f08
testq %rax, %rax
je 0x5006
movq %r14, 0x50(%rsp)
movq 0x10(%rsp), %rdi
callq 0x3160
movq %rax, %r14
movl 0x38(%rsp), %r13d
xorl %eax, %eax
movq %r13, 0x18(%rsp)
movq 0x10(%rsp), %r12
subq $0x1, %r13
movl %eax, 0xc(%rsp)
jb 0x504a
movl %ebp, %edi
movq %r14, %rsi
movq %r12, %rdx
callq 0x30e0
cmpq %r12, %rax
jne 0x4ef7
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
callq *0x70(%r15)
movq %r15, %rdi
movq %rax, %rsi
callq 0x3e74
movl 0xc(%rsp), %eax
addl 0x8(%rsp), %eax
jmp 0x4eab
callq 0x3040
movl $0x54, (%rax)
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x38(%rsp), %r13d
xorl %r12d, %r12d
testq %rax, %rax
je 0x4ffe
movl $0x0, 0xc(%rsp)
subq $0x1, %r13
jb 0x506c
movl %ebp, %edi
leaq 0x8(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x30e0
cmpq $0x4, %rax
jne 0x4ef7
movq %r13, 0x10(%rsp)
movq %r12, 0x18(%rsp)
movl 0x8(%rsp), %r12d
movq %r12, %rdi
callq 0x3160
movq %rax, %r13
movl %ebp, %edi
movq %rax, %rsi
movq %r12, %rdx
callq 0x30e0
movl 0x8(%rsp), %r12d
cmpq %r12, %rax
jne 0x4ef7
addl 0x18(%rsp), %r12d
movq %r13, %rdi
leaq 0x8(%rsp), %rsi
callq *0x70(%r15)
movq %r15, %rdi
movq %rax, %rsi
callq 0x3e74
movl 0xc(%rsp), %eax
addl 0x8(%rsp), %eax
movl %eax, 0xc(%rsp)
movq 0x10(%rsp), %r13
jmp 0x4f21
movl %ebp, %edi
leaq 0x8(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x30e0
cmpq $0x4, %rax
jne 0x4ef7
movq %r13, 0x10(%rsp)
movq %r12, 0x18(%rsp)
movl 0x8(%rsp), %r12d
movq %r12, %rdi
callq 0x3160
movq %rax, %r13
movl %ebp, %edi
movq %rax, %rsi
movq %r12, %rdx
callq 0x30e0
cmpq %r12, %rax
jne 0x4ef7
addl 0x18(%rsp), %r12d
movq %r15, %rdi
movq %r13, %rsi
callq 0x3e74
movq 0x10(%rsp), %r13
subq $0x1, %r13
jae 0x4fa0
jmp 0x5067
movl 0x38(%rsp), %r12d
movq %r12, 0x18(%rsp)
subq $0x1, %r12
jb 0x505c
movq 0x10(%rsp), %rdi
callq 0x3160
movq %rax, %r13
movl %ebp, %edi
movq %rax, %rsi
movq 0x10(%rsp), %rdx
callq 0x30e0
cmpq 0x10(%rsp), %rax
jne 0x4ef7
movq %r15, %rdi
movq %r13, %rsi
callq 0x3e74
jmp 0x5010
movq 0x10(%rsp), %r12
imull 0x18(%rsp), %r12d
movq 0x50(%rsp), %r14
jmp 0x506c
movq 0x18(%rsp), %r12
imull 0x10(%rsp), %r12d
movl %r12d, 0xc(%rsp)
leaq 0x8(%rsp), %rsi
pushq $0x4
popq %rdx
movl %ebp, %edi
callq 0x30e0
cmpq $0x4, %rax
jne 0x4ef7
movl 0x8(%rsp), %eax
bswapl %eax
movl %eax, 0x8(%rsp)
cmpl 0x34(%rsp), %r12d
je 0x509c
cmpl 0x30(%rsp), %eax
je 0x50cf
pushq $0x1
popq %rdx
movl %ebp, %edi
xorl %esi, %esi
callq 0x30a0
movq %rax, %r15
pushq $0x2
popq %rdx
movl %ebp, %edi
xorl %esi, %esi
callq 0x30a0
cmpq %rax, %r15
jne 0x50cf
testq %r14, %r14
je 0x50c8
movl 0xc(%rsp), %eax
movq %rax, (%r14)
xorl %ebx, %ebx
jmp 0x4ef7
callq 0x3040
movl $0x47, (%rax)
jmp 0x4ef7