// File: /workspace/github_c_makefile_star2_O3/bluecmd[P]fst-example/gtkwave/fstapi.c
// Function: fstReaderRecreateHierFile
static int fstReaderRecreateHierFile(struct fstReaderContext *xc)
{
int pass_status = 1;

if(!xc->fh)
        {
        off_t offs_cache = ftello(xc->f);
        char *fnam = (char *)malloc(strlen(xc->filename) + 6 + 16 + 32 + 1);
        unsigned char *mem = (unsigned char *)malloc(FST_GZIO_LEN);
        off_t hl, uclen;
        off_t clen = 0;
        gzFile zhandle = NULL;
        int zfd;
        int htyp = FST_BL_SKIP;

        /* can't handle both set at once should never happen in a real file */
        if(!xc->contains_hier_section_lz4 && xc->contains_hier_section)
                {
                htyp = FST_BL_HIER;
                }
        else
        if(xc->contains_hier_section_lz4 && !xc->contains_hier_section)
                {
                htyp = xc->contains_hier_section_lz4duo ? FST_BL_HIER_LZ4DUO : FST_BL_HIER_LZ4;
                }

        sprintf(fnam, "%s.hier_%d_%p", xc->filename, getpid(), (void *)xc);
        fstReaderFseeko(xc, xc->f, xc->hier_pos, SEEK_SET);
        uclen = fstReaderUint64(xc->f);
#ifndef __MINGW32__
        fflush(xc->f);
#endif
        if(htyp == FST_BL_HIER)
                {
                fstReaderFseeko(xc, xc->f, xc->hier_pos, SEEK_SET);
                uclen = fstReaderUint64(xc->f);
#ifndef __MINGW32__
                fflush(xc->f);
#endif
                zfd = dup(fileno(xc->f));
                zhandle = gzdopen(zfd, "rb");
                if(!zhandle)
                        {
                        close(zfd);
                        free(mem);
                        free(fnam);
                        return(0);
                        }
                }
        else
        if((htyp == FST_BL_HIER_LZ4) || (htyp == FST_BL_HIER_LZ4DUO))
                {
                fstReaderFseeko(xc, xc->f, xc->hier_pos - 8, SEEK_SET); /* get section len */
                clen =  fstReaderUint64(xc->f) - 16;
                uclen = fstReaderUint64(xc->f);
#ifndef __MINGW32__
                fflush(xc->f);
#endif
                }

#ifndef __MINGW32__
        xc->fh = fopen(fnam, "w+b");
        if(!xc->fh)
#endif
                {
                xc->fh = tmpfile_open(&xc->fh_nam);
                free(fnam); fnam = NULL;
                if(!xc->fh)
                        {
                        tmpfile_close(&xc->fh, &xc->fh_nam);
                        free(mem);
                        return(0);
                        }
                }

#ifndef __MINGW32__
        if(fnam) unlink(fnam);
#endif

        if(htyp == FST_BL_HIER)
                {
                for(hl = 0; hl < uclen; hl += FST_GZIO_LEN)
                        {
                        size_t len = ((uclen - hl) > FST_GZIO_LEN) ? FST_GZIO_LEN : (uclen - hl);
                        size_t gzreadlen = gzread(zhandle, mem, len); /* rc should equal len... */
                        size_t fwlen;

                        if(gzreadlen != len)
                                {
                                pass_status = 0;
                                break;
                                }

                        fwlen = fstFwrite(mem, len, 1, xc->fh);
                        if(fwlen != 1)
                                {
                                pass_status = 0;
                                break;
                                }
                        }
                gzclose(zhandle);
                }
        else
        if(htyp == FST_BL_HIER_LZ4DUO)
                {
                unsigned char *lz4_cmem  = (unsigned char *)malloc(clen);
                unsigned char *lz4_ucmem = (unsigned char *)malloc(uclen);
                unsigned char *lz4_ucmem2;
                uint64_t uclen2;
                int skiplen2 = 0;

                fstFread(lz4_cmem, clen, 1, xc->f);

                uclen2 = fstGetVarint64(lz4_cmem, &skiplen2);
                lz4_ucmem2 = (unsigned char *)malloc(uclen2);
                pass_status = (uclen2 == (uint64_t)LZ4_decompress_safe_partial ((char *)lz4_cmem + skiplen2, (char *)lz4_ucmem2, clen - skiplen2, uclen2, uclen2));
                if(pass_status)
                        {
                        pass_status = (uclen == LZ4_decompress_safe_partial ((char *)lz4_ucmem2, (char *)lz4_ucmem, uclen2, uclen, uclen));

                        if(fstFwrite(lz4_ucmem, uclen, 1, xc->fh) != 1)
                                {
                                pass_status = 0;
                                }
                        }

                free(lz4_ucmem2);
                free(lz4_ucmem);
                free(lz4_cmem);
                }
        else
        if(htyp == FST_BL_HIER_LZ4)
                {
                unsigned char *lz4_cmem  = (unsigned char *)malloc(clen);
                unsigned char *lz4_ucmem = (unsigned char *)malloc(uclen);

                fstFread(lz4_cmem, clen, 1, xc->f);
                pass_status = (uclen == LZ4_decompress_safe_partial ((char *)lz4_cmem, (char *)lz4_ucmem, clen, uclen, uclen));

                if(fstFwrite(lz4_ucmem, uclen, 1, xc->fh) != 1)
                        {
                        pass_status = 0;
                        }

                free(lz4_ucmem);
                free(lz4_cmem);
                }
        else /* FST_BL_SKIP */
                {
                pass_status = 0;
                if(xc->fh)
                        {
                        fclose(xc->fh); xc->fh = NULL; /* needed in case .hier file is missing and there are no hier sections */
                        }
                }

        free(mem);
        free(fnam);

        fstReaderFseeko(xc, xc->f, offs_cache, SEEK_SET);
        }

return(pass_status);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl $0x1, %r12d
cmpq $0x0, 0x8(%rdi)
jne 0x9a9b
movq %rdi, %rbx
movq (%rdi), %rdi
callq 0x20b0
movq %rax, -0x40(%rbp)
movq 0x180(%rbx), %r14
movq %r14, %rdi
callq 0x2130
leaq 0x37(%rax), %rdi
callq 0x2270
movq %rax, %r12
movl $0x8000, %edi           # imm = 0x8000
callq 0x2270
movq %rax, %r13
movzbl 0x7a(%rbx), %eax
movl %eax, %ecx
andl $0x50, %ecx
cmpl $0x10, %ecx
je 0x960b
cmpl $0x40, %ecx
jne 0x9614
andb $0x20, %al
cmpb $0x1, %al
movl $0x7, %eax
sbbl $0x0, %eax
movl %eax, -0x48(%rbp)
jmp 0x961b
movl $0x4, -0x48(%rbp)
jmp 0x961b
movl $0xff, -0x48(%rbp)
callq 0x20f0
leaq 0xb2b4(%rip), %rsi      # 0x148db
movq %r12, %rdi
movq %r14, %rdx
movl %eax, %ecx
movq %rbx, %r8
xorl %eax, %eax
callq 0x2370
movq (%rbx), %rdi
movq 0x190(%rbx), %rsi
xorl %edx, %edx
callq 0x2320
testl %eax, %eax
jns 0x9655
orb $0x1, 0x697(%rbx)
movq (%rbx), %rdi
callq 0xadc0
movq %rax, %r15
movq (%rbx), %rdi
callq 0x2280
movl -0x48(%rbp), %r14d
cmpl $0x4, %r14d
movq %r13, -0x50(%rbp)
jne 0x96e9
movq %r12, %r13
movq (%rbx), %rdi
movq 0x190(%rbx), %rsi
xorl %edx, %edx
callq 0x2320
testl %eax, %eax
jns 0x9695
orb $0x1, 0x697(%rbx)
movq (%rbx), %rdi
callq 0xadc0
movq %rax, %r15
movq (%rbx), %rdi
callq 0x2280
movq (%rbx), %rdi
callq 0x2260
movl %eax, %edi
callq 0x2180
movl %eax, %r14d
leaq 0xaf46(%rip), %rsi      # 0x14607
movl %eax, %edi
callq 0x2100
xorl %r12d, %r12d
movq %rax, -0x30(%rbp)
testq %rax, %rax
je 0x9977
movq $0x0, -0x38(%rbp)
movq %r13, %r12
movl -0x48(%rbp), %r14d
jmp 0x9753
movl %r14d, %eax
andl $0xfe, %eax
cmpl $0x6, %eax
jne 0x9743
movq (%rbx), %rdi
movq 0x190(%rbx), %rsi
addq $-0x8, %rsi
xorl %edx, %edx
callq 0x2320
testl %eax, %eax
jns 0x9716
orb $0x1, 0x697(%rbx)
movq (%rbx), %rdi
callq 0xadc0
addq $-0x10, %rax
movq %rax, -0x38(%rbp)
movq (%rbx), %rdi
callq 0xadc0
movq %rax, %r15
movq (%rbx), %rdi
callq 0x2280
movq $0x0, -0x30(%rbp)
jmp 0x9753
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
leaq 0x8(%rbx), %r13
leaq 0xabaa(%rip), %rsi      # 0x14308
movq %r12, %rdi
callq 0x2330
movq %r13, -0x58(%rbp)
movq %rax, (%r13)
testq %rax, %rax
je 0x9786
testq %r12, %r12
movq -0x40(%rbp), %r13
je 0x97c1
movq %r12, %rdi
callq 0x2080
jmp 0x97c4
movq -0x40(%rbp), %r14
callq 0x21f0
movq %rax, %r13
movq $0x0, 0x106a8(%rbx)
movq %rax, 0x8(%rbx)
movq %r12, %rdi
callq 0x2050
xorl %r12d, %r12d
testq %r13, %r13
je 0x9a7f
xorl %r12d, %r12d
movq %r14, %r13
movl -0x48(%rbp), %r14d
jmp 0x97c4
xorl %r12d, %r12d
cmpl $0x4, %r14d
movq %r12, -0x48(%rbp)
jne 0x9862
movl $0x1, %r12d
testq %r15, %r15
jle 0x999b
movq %rbx, -0x38(%rbp)
xorl %r14d, %r14d
movq %r15, %r12
movq -0x50(%rbp), %rbx
movl $0x8000, %eax           # imm = 0x8000
cmpq %rax, %r15
movl $0x8000, %r13d          # imm = 0x8000
cmovlq %r15, %r13
movq -0x30(%rbp), %rdi
movq %rbx, %rsi
movl %r13d, %edx
callq 0x2200
cltq
cmpq %rax, %r13
jne 0x9990
movq -0x58(%rbp), %rax
movq (%rax), %rcx
movl $0x1, %edx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2390
cmpq $0x1, %rax
jne 0x9990
addq $0x8000, %r14           # imm = 0x8000
addq $-0x8000, %r15          # imm = 0x8000
cmpq %r12, %r14
jl 0x97ef
movq -0x40(%rbp), %r13
movq -0x38(%rbp), %rbx
movl $0x1, %r12d
jmp 0x999b
cmpb $0x6, %r14b
je 0x99a9
movzbl %r14b, %eax
cmpl $0x7, %eax
jne 0x9a5e
movq -0x38(%rbp), %r14
movq %r14, %rdi
callq 0x2270
movq %r15, %rdi
movq %rax, %r15
movq %rdi, -0x60(%rbp)
callq 0x2270
movq %rax, -0x30(%rbp)
movq (%rbx), %rcx
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x20c0
xorl %r14d, %r14d
cmpb $0x0, (%r15,%r14)
leaq 0x1(%r14), %r14
js 0x98ae
xorl %r13d, %r13d
movq %r14, %rax
movq %r13, %rcx
shlq $0x7, %rcx
movzbl -0x1(%r15,%rax), %r13d
andl $0x7f, %r13d
orq %rcx, %r13
decq %rax
jne 0x98bf
movq %r13, %rdi
callq 0x2270
movslq %r14d, %rcx
movq %rax, %r14
movq %r15, %rdi
addq %rcx, %rdi
movq -0x38(%rbp), %rdx
subl %ecx, %edx
movq %rax, %rsi
movl %r13d, %ecx
movl %r13d, %r8d
callq 0x10795
cltq
xorl %r12d, %r12d
cmpq %rax, %r13
jne 0x9955
movq %r14, %rdi
movq -0x30(%rbp), %rsi
movl %r13d, %edx
movq -0x60(%rbp), %r12
movl %r12d, %ecx
movl %r12d, %r8d
callq 0x10795
movq %r14, %r13
movslq %eax, %r14
xorq %r12, %r14
movq -0x58(%rbp), %rax
movq (%rax), %rcx
movl $0x1, %edx
movq -0x30(%rbp), %rdi
movq %r12, %rsi
callq 0x2390
xorq $0x1, %rax
xorl %r12d, %r12d
orq %r14, %rax
movq %r13, %r14
sete %r12b
movq %r14, %rdi
callq 0x2050
movq -0x30(%rbp), %rdi
callq 0x2050
movq %r15, %rdi
callq 0x2050
movq -0x40(%rbp), %r13
jmp 0x9a2f
movl %r14d, %edi
callq 0x21b0
movq -0x50(%rbp), %rdi
callq 0x2050
movq %r13, %rdi
jmp 0x9a96
xorl %r12d, %r12d
movq -0x40(%rbp), %r13
movq -0x38(%rbp), %rbx
movq -0x30(%rbp), %rdi
callq 0x2040
jmp 0x9a2f
movq -0x38(%rbp), %r14
movq %r14, %rdi
callq 0x2270
movq %rax, -0x30(%rbp)
movq %r15, %rdi
callq 0x2270
movq %r15, %r12
movq %rax, %r15
movq (%rbx), %rcx
movl $0x1, %edx
movq -0x30(%rbp), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x20c0
movq %r13, %rdi
movq %r15, %rsi
movl %r14d, %edx
movl %r12d, %ecx
movl %r12d, %r8d
callq 0x10795
movslq %eax, %r13
xorq %r12, %r13
movq 0x8(%rbx), %rcx
movl $0x1, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x2390
xorq $0x1, %rax
xorl %r12d, %r12d
orq %r13, %rax
sete %r12b
movq %r15, %rdi
movq -0x40(%rbp), %r13
callq 0x2050
movq -0x30(%rbp), %rdi
callq 0x2050
movq -0x50(%rbp), %r14
movq %r14, %rdi
callq 0x2050
movq -0x48(%rbp), %rdi
callq 0x2050
movq (%rbx), %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x2320
testl %eax, %eax
jns 0x9a9b
orb $0x1, 0x697(%rbx)
jmp 0x9a9b
movq -0x58(%rbp), %r15
movq (%r15), %rdi
xorl %r12d, %r12d
testq %rdi, %rdi
movq -0x50(%rbp), %r14
je 0x9a33
callq 0x2110
movq $0x0, (%r15)
jmp 0x9a33
addq $0x106a8, %rbx          # imm = 0x106A8
movq -0x58(%rbp), %rdi
movq %rbx, %rsi
callq 0x5894
movq -0x50(%rbp), %rdi
callq 0x2050
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq