// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq %rdx, %r14
movl %esi, %ebp
movq %rdi, %r15
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rsi
movaps %xmm0, (%rsi)
movaps %xmm0, 0x10(%rsi)
movq $0x0, 0x20(%rsi)
movl $0x2, %edx
movl %ebp, %edi
callq 0x30e0
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpq $0x2, %rax
jne 0x533c
movzwl 0x20(%rsp), %eax
movl %eax, %ecx
rolw $0x8, %cx
movw %cx, 0x20(%rsp)
cmpl $0x100, %eax            # imm = 0x100
jne 0x5331
leaq 0x28(%rsp), %rsi
movl $0x8, %edx
movl %ebp, %edi
callq 0x30e0
cmpq $0x8, %rax
jne 0x533c
leaq 0x30(%rsp), %rsi
movl $0x4, %edx
movl %ebp, %edi
callq 0x30e0
cmpq $0x4, %rax
jne 0x533c
leaq 0x34(%rsp), %rsi
movl -0x4(%rsi), %eax
bswapl %eax
movl %eax, -0x4(%rsi)
movl $0x4, %edx
movl %ebp, %edi
callq 0x30e0
cmpq $0x4, %rax
jne 0x533c
leaq 0x38(%rsp), %rsi
movl -0x4(%rsi), %eax
bswapl %eax
movl %eax, -0x4(%rsi)
movl $0x4, %edx
movl %ebp, %edi
callq 0x30e0
cmpq $0x4, %rax
jne 0x533c
leaq 0x3c(%rsp), %rsi
movl -0x4(%rsi), %eax
bswapl %eax
movl %eax, -0x4(%rsi)
movl $0x4, %edx
movl %ebp, %edi
callq 0x30e0
cmpq $0x4, %rax
jne 0x533c
leaq 0x40(%rsp), %rsi
movl -0x4(%rsi), %eax
bswapl %eax
movl %eax, -0x4(%rsi)
movl $0x4, %edx
movl %ebp, %edi
callq 0x30e0
cmpq $0x4, %rax
jne 0x533c
movl 0x3c(%rsp), %edi
movl 0x40(%rsp), %eax
bswapl %eax
movl %eax, 0x40(%rsp)
movq 0x70(%r15), %rax
testq %rdi, %rdi
je 0x534d
testq %rax, %rax
movq %rdi, 0x8(%rsp)
je 0x53ef
callq 0x3160
movl 0x38(%rsp), %ecx
testq %rcx, %rcx
je 0x5452
movq %rax, %r13
xorl %eax, %eax
movq 0x8(%rsp), %r12
movq %rcx, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movl %ebp, %edi
movq %r13, %rsi
movq %r12, %rdx
callq 0x30e0
cmpq %r12, %rax
jne 0x533c
movq %r13, %rdi
leaq 0x4(%rsp), %rsi
callq *0x70(%r15)
movl 0x18(%r15), %edx
movq %r15, %rdi
movq %rax, %rsi
callq 0x3f35
movq 0x18(%rsp), %rax
addl 0x4(%rsp), %eax
movq 0x10(%rsp), %rcx
decq %rcx
jne 0x52e2
jmp 0x5454
callq 0x3040
movl $0x54, (%rax)
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x38(%rsp), %r13d
testq %rax, %rax
je 0x5464
testq %r13, %r13
je 0x54e1
xorl %eax, %eax
leaq 0x4(%rsp), %rsi
xorl %r12d, %r12d
movq %rax, 0x18(%rsp)
movl $0x4, %edx
movl %ebp, %edi
callq 0x30e0
cmpq $0x4, %rax
jne 0x533c
movq %r13, 0x10(%rsp)
movl 0x4(%rsp), %edi
movq %rdi, 0x8(%rsp)
callq 0x3160
movq %rax, %r13
movl %ebp, %edi
movq %rax, %rsi
movq 0x8(%rsp), %rdx
callq 0x30e0
movl 0x4(%rsp), %ecx
cmpq %rcx, %rax
jne 0x533c
addl %ecx, %r12d
movq %r13, %rdi
leaq 0x4(%rsp), %r13
movq %r13, %rsi
callq *0x70(%r15)
movl 0x18(%r15), %edx
movq %r15, %rdi
movq %rax, %rsi
callq 0x3f35
movq %r13, %rsi
movq 0x18(%rsp), %rax
addl 0x4(%rsp), %eax
movq 0x10(%rsp), %r13
decq %r13
jne 0x536e
jmp 0x54e6
movl 0x38(%rsp), %eax
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x5446
movq 0x18(%rsp), %rax
movq 0x8(%rsp), %r12
movq %rax, 0x10(%rsp)
movq %r12, %rdi
callq 0x3160
movq %rax, %r13
movl %ebp, %edi
movq %rax, %rsi
movq %r12, %rdx
callq 0x30e0
cmpq %r12, %rax
jne 0x533c
movl 0x18(%r15), %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x3f35
movq 0x10(%rsp), %rax
decq %rax
jne 0x5407
movq 0x18(%rsp), %rax
imull 0x8(%rsp), %eax
jmp 0x5454
xorl %eax, %eax
movq 0x8(%rsp), %r12
imull 0x38(%rsp), %r12d
jmp 0x54e6
testq %r13, %r13
je 0x54e1
xorl %r12d, %r12d
movq %r13, 0x10(%rsp)
movq %r12, 0x8(%rsp)
movl $0x4, %edx
movl %ebp, %edi
leaq 0x4(%rsp), %rsi
callq 0x30e0
cmpq $0x4, %rax
jne 0x533c
movl 0x4(%rsp), %r12d
movq %r12, %rdi
callq 0x3160
movq %rax, %r13
movl %ebp, %edi
movq %rax, %rsi
movq %r12, %rdx
callq 0x30e0
movl 0x4(%rsp), %ecx
cmpq %rcx, %rax
jne 0x533c
movq 0x8(%rsp), %r12
addl %ecx, %r12d
movl 0x18(%r15), %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x3f35
movl %r12d, %eax
movq 0x10(%rsp), %r13
decq %r13
jne 0x546c
jmp 0x54e6
xorl %r12d, %r12d
xorl %eax, %eax
movq %rax, %r13
leaq 0x4(%rsp), %rsi
movl $0x4, %edx
movl %ebp, %edi
callq 0x30e0
cmpq $0x4, %rax
jne 0x533c
movl 0x4(%rsp), %eax
bswapl %eax
movl %eax, 0x4(%rsp)
cmpl 0x34(%rsp), %r12d
je 0x551b
cmpl 0x30(%rsp), %eax
je 0x5551
movl %ebp, %edi
xorl %esi, %esi
movl $0x1, %edx
callq 0x30a0
movq %rax, %r15
movl %ebp, %edi
xorl %esi, %esi
movl $0x2, %edx
callq 0x30a0
cmpq %rax, %r15
jne 0x5551
testq %r14, %r14
je 0x554a
movl %r13d, %eax
movq %rax, (%r14)
xorl %ebx, %ebx
jmp 0x533c
callq 0x3040
movl $0x47, (%rax)
jmp 0x533c