// File: /workspace/github_c_makefile_star2_O3/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 0x30(%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 0x5311
movzwl 0x30(%rsp), %eax
movl %eax, %ecx
rolw $0x8, %cx
movw %cx, 0x30(%rsp)
cmpl $0x100, %eax            # imm = 0x100
jne 0x5306
leaq 0x38(%rsp), %rsi
movl $0x8, %edx
movl %ebp, %edi
callq 0x30e0
cmpq $0x8, %rax
jne 0x5311
leaq 0x40(%rsp), %rsi
movl $0x4, %edx
movl %ebp, %edi
callq 0x30e0
cmpq $0x4, %rax
jne 0x5311
leaq 0x44(%rsp), %rsi
movl -0x4(%rsi), %eax
bswapl %eax
movl %eax, -0x4(%rsi)
movl $0x4, %edx
movl %ebp, %edi
callq 0x30e0
cmpq $0x4, %rax
jne 0x5311
leaq 0x48(%rsp), %rsi
movl -0x4(%rsi), %eax
bswapl %eax
movl %eax, -0x4(%rsi)
movl $0x4, %edx
movl %ebp, %edi
callq 0x30e0
cmpq $0x4, %rax
jne 0x5311
leaq 0x4c(%rsp), %rsi
movl -0x4(%rsi), %eax
bswapl %eax
movl %eax, -0x4(%rsi)
movl $0x4, %edx
movl %ebp, %edi
callq 0x30e0
cmpq $0x4, %rax
jne 0x5311
leaq 0x50(%rsp), %rsi
movl -0x4(%rsi), %eax
bswapl %eax
movl %eax, -0x4(%rsi)
movl $0x4, %edx
movl %ebp, %edi
callq 0x30e0
cmpq $0x4, %rax
jne 0x5311
movl 0x4c(%rsp), %edi
movq 0x70(%r15), %rax
testq %rdi, %rdi
je 0x5322
testq %rax, %rax
movq %rdi, 0x10(%rsp)
je 0x53cb
callq 0x3160
movl 0x48(%rsp), %r13d
testq %r13, %r13
je 0x5435
xorl %r12d, %r12d
movq %r13, 0x28(%rsp)
movq %r13, %rcx
movq %rcx, 0x20(%rsp)
movl %r12d, 0x18(%rsp)
movl %ebp, %edi
movq %rax, %r13
movq %rax, %rsi
movq 0x10(%rsp), %r12
movq %r12, %rdx
callq 0x30e0
cmpq %r12, %rax
jne 0x5311
movq %r13, %rdi
leaq 0xc(%rsp), %rsi
callq *0x70(%r15)
movl 0x18(%r15), %edx
movq %r15, %rdi
movq %rax, %rsi
callq 0x3f2b
movl 0x18(%rsp), %r12d
addl 0xc(%rsp), %r12d
movq 0x20(%rsp), %rcx
decq %rcx
movq %r13, %rax
jne 0x52a1
movq 0x10(%rsp), %rax
movq 0x28(%rsp), %rcx
jmp 0x543f
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 0x48(%rsp), %r13d
testq %rax, %rax
je 0x544c
testq %r13, %r13
je 0x54cc
xorl %r12d, %r12d
leaq 0xc(%rsp), %rsi
movq $0x0, 0x10(%rsp)
movl $0x4, %edx
movl %ebp, %edi
callq 0x30e0
cmpq $0x4, %rax
jne 0x5311
movq %r13, 0x18(%rsp)
movl 0xc(%rsp), %edi
movq %rdi, 0x20(%rsp)
callq 0x3160
movq %rax, %r13
movl %ebp, %edi
movq %rax, %rsi
movq 0x20(%rsp), %rdx
callq 0x30e0
movl 0xc(%rsp), %ecx
cmpq %rcx, %rax
jne 0x5311
movq 0x10(%rsp), %rax
addl %ecx, %eax
movq %rax, 0x10(%rsp)
movq %r13, %rdi
leaq 0xc(%rsp), %r13
movq %r13, %rsi
callq *0x70(%r15)
movl 0x18(%r15), %edx
movq %r15, %rdi
movq %rax, %rsi
callq 0x3f2b
movq %r13, %rsi
addl 0xc(%rsp), %r12d
movq 0x18(%rsp), %r13
decq %r13
jne 0x534a
jmp 0x54d8
movl 0x48(%rsp), %eax
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0x5422
movq 0x28(%rsp), %rax
movq 0x10(%rsp), %r12
movq %rax, 0x18(%rsp)
movq %r12, %rdi
callq 0x3160
movq %rax, %r13
movl %ebp, %edi
movq %rax, %rsi
movq %r12, %rdx
callq 0x30e0
cmpq %r12, %rax
jne 0x5311
movl 0x18(%r15), %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x3f2b
movq 0x18(%rsp), %rax
decq %rax
jne 0x53e3
movq 0x10(%rsp), %rax
movl %eax, %r12d
movq 0x28(%rsp), %rcx
imull %ecx, %r12d
jmp 0x543f
xorl %ecx, %ecx
xorl %r12d, %r12d
movq 0x10(%rsp), %rax
imull %ecx, %eax
movq %rax, 0x10(%rsp)
jmp 0x54d8
testq %r13, %r13
je 0x54cc
movq $0x0, 0x10(%rsp)
movq %r13, 0x18(%rsp)
movl $0x4, %edx
movl %ebp, %edi
leaq 0xc(%rsp), %rsi
callq 0x30e0
cmpq $0x4, %rax
jne 0x5311
movl 0xc(%rsp), %r13d
movq %r13, %rdi
callq 0x3160
movl %ebp, %edi
movq %rax, 0x20(%rsp)
movq %rax, %rsi
movq %r13, %rdx
callq 0x30e0
cmpq %r13, %rax
movq 0x10(%rsp), %r12
jne 0x5311
addl %r13d, %r12d
movl 0x18(%r15), %edx
movq %r15, %rdi
movq 0x20(%rsp), %rsi
callq 0x3f2b
movq %r12, 0x10(%rsp)
movq 0x18(%rsp), %r13
decq %r13
jne 0x545a
jmp 0x54d8
movq $0x0, 0x10(%rsp)
xorl %r12d, %r12d
leaq 0xc(%rsp), %rsi
movl $0x4, %edx
movl %ebp, %edi
callq 0x30e0
cmpq $0x4, %rax
jne 0x5311
movl 0xc(%rsp), %eax
bswapl %eax
movl %eax, 0xc(%rsp)
movq 0x10(%rsp), %rcx
cmpl 0x44(%rsp), %ecx
je 0x550e
cmpl 0x40(%rsp), %eax
je 0x5544
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 0x5544
testq %r14, %r14
je 0x553d
movl %r12d, %eax
movq %rax, (%r14)
xorl %ebx, %ebx
jmp 0x5311
callq 0x3040
movl $0x47, (%rax)
jmp 0x5311