// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
movl $0x28, %edx
callq 0x30e0
movl -0x14(%rbp), %edi
leaq -0x48(%rbp), %rsi
movl $0x2, %edx
callq 0x3100
cmpq $0x2, %rax
je 0x67f9
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6cb1
jmp 0x67fb
movzwl -0x48(%rbp), %edi
callq 0x31a0
movw %ax, -0x48(%rbp)
movzwl -0x48(%rbp), %eax
cmpl $0x1, %eax
je 0x6828
callq 0x3040
movl $0x54, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6cb1
jmp 0x682a
movl -0x14(%rbp), %edi
leaq -0x48(%rbp), %rsi
addq $0x8, %rsi
movl $0x8, %edx
callq 0x3100
cmpq $0x8, %rax
je 0x6851
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6cb1
jmp 0x6853
jmp 0x6855
movl -0x14(%rbp), %edi
leaq -0x48(%rbp), %rsi
addq $0x10, %rsi
movl $0x4, %edx
callq 0x3100
cmpq $0x4, %rax
je 0x687c
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6cb1
jmp 0x687e
movl -0x38(%rbp), %edi
callq 0x3200
movl %eax, -0x38(%rbp)
movl -0x14(%rbp), %edi
leaq -0x48(%rbp), %rsi
addq $0x14, %rsi
movl $0x4, %edx
callq 0x3100
cmpq $0x4, %rax
je 0x68b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6cb1
jmp 0x68b2
movl -0x34(%rbp), %edi
callq 0x3200
movl %eax, -0x34(%rbp)
movl -0x14(%rbp), %edi
leaq -0x48(%rbp), %rsi
addq $0x18, %rsi
movl $0x4, %edx
callq 0x3100
cmpq $0x4, %rax
je 0x68e4
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6cb1
jmp 0x68e6
movl -0x30(%rbp), %edi
callq 0x3200
movl %eax, -0x30(%rbp)
movl -0x14(%rbp), %edi
leaq -0x48(%rbp), %rsi
addq $0x1c, %rsi
movl $0x4, %edx
callq 0x3100
cmpq $0x4, %rax
je 0x6918
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6cb1
jmp 0x691a
movl -0x2c(%rbp), %edi
callq 0x3200
movl %eax, -0x2c(%rbp)
movl -0x14(%rbp), %edi
leaq -0x48(%rbp), %rsi
addq $0x20, %rsi
movl $0x4, %edx
callq 0x3100
cmpq $0x4, %rax
je 0x694c
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6cb1
jmp 0x694e
movl -0x28(%rbp), %edi
callq 0x3200
movl %eax, -0x28(%rbp)
movl $0x0, -0x64(%rbp)
movl $0x0, -0x60(%rbp)
cmpl $0x0, -0x2c(%rbp)
jbe 0x6a87
movq -0x10(%rbp), %rax
cmpq $0x0, 0x70(%rax)
je 0x6a0a
movl -0x2c(%rbp), %eax
movl %eax, %edi
callq 0x3180
movq %rax, -0x58(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
movl -0x30(%rbp), %ecx
cmpq %rcx, %rax
jae 0x6a08
jmp 0x69a4
movl -0x14(%rbp), %edi
movq -0x58(%rbp), %rsi
movl -0x2c(%rbp), %eax
movl %eax, %edx
callq 0x3100
movl -0x2c(%rbp), %ecx
cmpq %rcx, %rax
je 0x69c9
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6cb1
jmp 0x69cb
movq -0x10(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movq -0x58(%rbp), %rdi
leaq -0x5c(%rbp), %rsi
callq *%rax
movq -0x70(%rbp), %rdi
movq %rax, %rsi
callq 0x4940
movl -0x5c(%rbp), %eax
addl -0x64(%rbp), %eax
movl %eax, -0x64(%rbp)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x6996
jmp 0x6a78
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
movl -0x30(%rbp), %ecx
cmpq %rcx, %rax
jae 0x6a6e
movl -0x2c(%rbp), %eax
movl %eax, %edi
callq 0x3180
movq %rax, -0x58(%rbp)
movl -0x14(%rbp), %edi
movq -0x58(%rbp), %rsi
movl -0x2c(%rbp), %eax
movl %eax, %edx
callq 0x3100
movl -0x2c(%rbp), %ecx
cmpq %rcx, %rax
je 0x6a51
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6cb1
jmp 0x6a53
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x4940
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x6a12
movl -0x30(%rbp), %eax
imull -0x2c(%rbp), %eax
movl %eax, -0x64(%rbp)
movl -0x30(%rbp), %eax
imull -0x2c(%rbp), %eax
movl %eax, -0x60(%rbp)
jmp 0x6bfb
movq -0x10(%rbp), %rax
cmpq $0x0, 0x70(%rax)
je 0x6b58
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
movl -0x30(%rbp), %ecx
cmpq %rcx, %rax
jae 0x6b53
jmp 0x6ab0
movl -0x14(%rbp), %edi
leaq -0x5c(%rbp), %rsi
movl $0x4, %edx
callq 0x3100
cmpq $0x4, %rax
je 0x6ad3
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6cb1
jmp 0x6ad5
movl -0x5c(%rbp), %eax
movl %eax, %edi
callq 0x3180
movq %rax, -0x58(%rbp)
movl -0x14(%rbp), %edi
movq -0x58(%rbp), %rsi
movl -0x5c(%rbp), %eax
movl %eax, %edx
callq 0x3100
movl -0x5c(%rbp), %ecx
cmpq %rcx, %rax
je 0x6b08
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6cb1
jmp 0x6b0a
movl -0x5c(%rbp), %eax
addl -0x60(%rbp), %eax
movl %eax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movq -0x58(%rbp), %rdi
leaq -0x5c(%rbp), %rsi
callq *%rax
movq -0x78(%rbp), %rdi
movq %rax, %rsi
callq 0x4940
movl -0x5c(%rbp), %eax
addl -0x64(%rbp), %eax
movl %eax, -0x64(%rbp)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x6a9e
jmp 0x6bf9
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
movl -0x30(%rbp), %ecx
cmpq %rcx, %rax
jae 0x6bf3
jmp 0x6b72
movl -0x14(%rbp), %edi
leaq -0x5c(%rbp), %rsi
movl $0x4, %edx
callq 0x3100
cmpq $0x4, %rax
je 0x6b95
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6cb1
jmp 0x6b97
movl -0x5c(%rbp), %eax
movl %eax, %edi
callq 0x3180
movq %rax, -0x58(%rbp)
movl -0x14(%rbp), %edi
movq -0x58(%rbp), %rsi
movl -0x5c(%rbp), %eax
movl %eax, %edx
callq 0x3100
movl -0x5c(%rbp), %ecx
cmpq %rcx, %rax
je 0x6bca
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6cb1
jmp 0x6bcc
movl -0x5c(%rbp), %eax
addl -0x60(%rbp), %eax
movl %eax, -0x60(%rbp)
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x4940
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x6b60
movl -0x60(%rbp), %eax
movl %eax, -0x64(%rbp)
jmp 0x6bfb
jmp 0x6bfd
movl -0x14(%rbp), %edi
leaq -0x5c(%rbp), %rsi
movl $0x4, %edx
callq 0x3100
cmpq $0x4, %rax
je 0x6c20
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6cb1
jmp 0x6c22
movl -0x5c(%rbp), %edi
callq 0x3200
movl %eax, -0x5c(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x34(%rbp), %eax
je 0x6c51
movl -0x5c(%rbp), %eax
cmpl -0x38(%rbp), %eax
jne 0x6c51
callq 0x3040
movl $0x47, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6cb1
movl -0x14(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
movl $0x1, %edx
callq 0x30a0
movq %rax, -0x80(%rbp)
movl -0x14(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x30a0
movq %rax, %rcx
movq -0x80(%rbp), %rax
cmpq %rcx, %rax
je 0x6c97
callq 0x3040
movl $0x47, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6cb1
cmpq $0x0, -0x20(%rbp)
je 0x6caa
movl -0x64(%rbp), %eax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)