// File: /workspace/github_c_makefile_star2_O1/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, %r14d
cmpq $0x0, 0x8(%rdi)
jne 0x8071
movq %rdi, %r13
movq (%rdi), %rdi
callq 0x20b0
movq %rax, %r15
movq 0x180(%r13), %r14
movq %r14, %rdi
callq 0x2130
leaq 0x37(%rax), %rdi
callq 0x2270
movq %rax, %r12
movl $0x8000, %edi           # imm = 0x8000
callq 0x2270
movq %rax, -0x58(%rbp)
movb 0x7a(%r13), %al
movl %eax, %ecx
andb $0x50, %cl
movl $0x4, %ebx
cmpb $0x10, %cl
je 0x7bc5
movl $0xff, %ebx
cmpb $0x40, %cl
jne 0x7bc5
andb $0x20, %al
cmpb $0x1, %al
movl $0x7, %ebx
sbbl $0x0, %ebx
callq 0x20f0
leaq 0xdd0a(%rip), %rsi      # 0x158db
movq %r12, %rdi
movq %r14, %rdx
movl %eax, %ecx
movq %r13, %r8
xorl %eax, %eax
callq 0x2370
movq (%r13), %rdi
movq 0x190(%r13), %rsi
xorl %edx, %edx
callq 0x2320
testl %eax, %eax
jns 0x7c01
orb $0x1, 0x697(%r13)
movq (%r13), %rdi
callq 0x948d
movq %rax, %r14
movq (%r13), %rdi
callq 0x2280
cmpl $0x4, %ebx
movq %r15, -0x60(%rbp)
movl %ebx, -0x50(%rbp)
jne 0x7c97
movq (%r13), %rdi
movq 0x190(%r13), %rsi
xorl %edx, %edx
callq 0x2320
testl %eax, %eax
jns 0x7c40
orb $0x1, 0x697(%r13)
movq (%r13), %rdi
callq 0x948d
movq %rax, %rbx
movq (%r13), %rdi
callq 0x2280
movq (%r13), %rdi
callq 0x2260
movl %eax, %edi
callq 0x2180
movl %eax, %r15d
leaq 0xd998(%rip), %rsi      # 0x15607
movl %eax, %edi
callq 0x2100
xorl %r14d, %r14d
movq %rax, -0x30(%rbp)
testq %rax, %rax
je 0x7f3d
movq $0x0, -0x38(%rbp)
movq %rbx, %r14
movq -0x60(%rbp), %r15
jmp 0x7d05
movl %ebx, %eax
andl $0xfe, %eax
cmpl $0x6, %eax
jne 0x7cf5
movq (%r13), %rdi
movq 0x190(%r13), %rsi
addq $-0x8, %rsi
xorl %edx, %edx
callq 0x2320
testl %eax, %eax
jns 0x7cc5
orb $0x1, 0x697(%r13)
movq (%r13), %rdi
callq 0x948d
addq $-0x10, %rax
movq %rax, -0x38(%rbp)
movq (%r13), %rdi
callq 0x948d
movq %rax, %r14
movq (%r13), %rdi
callq 0x2280
movq $0x0, -0x30(%rbp)
jmp 0x7d05
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
leaq 0x8(%r13), %rbx
leaq 0xd5f8(%rip), %rsi      # 0x15308
movq %r12, %rdi
callq 0x2330
movq %rax, (%rbx)
testq %rax, %rax
jne 0x7d5a
movq %r14, -0x40(%rbp)
callq 0x21f0
movq %rax, %r15
movq $0x0, 0x106a8(%r13)
movq %rax, 0x8(%r13)
movq %r12, %rdi
callq 0x2050
xorl %r14d, %r14d
testq %r15, %r15
je 0x8056
xorl %r12d, %r12d
movq -0x40(%rbp), %r14
movq -0x60(%rbp), %r15
testq %r12, %r12
je 0x7d67
movq %r12, %rdi
callq 0x2080
movl -0x50(%rbp), %eax
cmpl $0x4, %eax
jne 0x7e26
movq %r12, -0x48(%rbp)
movq %r13, -0x38(%rbp)
testq %r14, %r14
jle 0x7f56
movq %rbx, -0x50(%rbp)
movl $0x1, %r12d
movq %r14, -0x40(%rbp)
movq %r14, %rbx
movl $0x8000, %r13d          # imm = 0x8000
movl $0x8000, %eax           # imm = 0x8000
cmpq %rax, %rbx
movl $0x8000, %r15d          # imm = 0x8000
cmovlq %rbx, %r15
movq -0x30(%rbp), %rdi
movq -0x58(%rbp), %rsi
movl %r15d, %edx
callq 0x2200
movslq %eax, %rcx
movl $0x0, %eax
movl $0x0, %r14d
cmpq %rcx, %r15
jne 0x7dfb
movq -0x50(%rbp), %rax
movq (%rax), %rcx
movl $0x1, %edx
movq -0x58(%rbp), %rdi
movq %r15, %rsi
callq 0x2390
cmpq $0x1, %rax
sete %al
movl $0x0, %ecx
cmovnel %ecx, %r12d
movl %r12d, %r14d
testb %al, %al
je 0x7f5c
leaq 0x8000(%r13), %rax
addq $-0x8000, %rbx          # imm = 0x8000
movl %r14d, %r12d
cmpq -0x40(%rbp), %r13
movq %rax, %r13
jl 0x7d9b
jmp 0x7f5c
cmpb $0x6, %al
je 0x7fa5
movzbl %al, %eax
cmpl $0x7, %eax
jne 0x8036
movq %rbx, -0x50(%rbp)
movq %r12, -0x48(%rbp)
movq -0x38(%rbp), %rbx
movq %rbx, %rdi
callq 0x2270
movq %rax, %r12
movq %r14, -0x40(%rbp)
movq %r14, %rdi
callq 0x2270
movq %rax, -0x30(%rbp)
movq (%r13), %rcx
movl $0x1, %edx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x20c0
xorl %ebx, %ebx
cmpb $0x0, (%r12,%rbx)
leaq 0x1(%rbx), %rbx
js 0x7e77
xorl %r14d, %r14d
movq %rbx, %rax
movq %r14, %rcx
shlq $0x7, %rcx
movzbl -0x1(%r12,%rax), %r14d
andl $0x7f, %r14d
orq %rcx, %r14
decq %rax
jne 0x7e88
movq %r14, %rdi
callq 0x2270
movq %rax, %r15
movslq %ebx, %rax
movq %r12, %rdi
addq %rax, %rdi
movq -0x38(%rbp), %rdx
subl %eax, %edx
movq %r15, %rsi
movl %r14d, %ecx
movl %r14d, %r8d
callq 0x10e4a
cltq
cmpq %rax, %r14
sete %al
jne 0x7f1e
movq %r15, %rdi
movq %r15, -0x38(%rbp)
movq -0x30(%rbp), %r15
movq %r15, %rsi
movl %r14d, %edx
movq -0x40(%rbp), %r14
movl %r14d, %ecx
movl %r14d, %r8d
callq 0x10e4a
movslq %eax, %rbx
xorq %r14, %rbx
movq -0x50(%rbp), %rax
movq (%rax), %rcx
movl $0x1, %edx
movq %r15, %rdi
movq -0x38(%rbp), %r15
movq %r14, %rsi
callq 0x2390
xorq $0x1, %rax
orq %rbx, %rax
sete %al
movzbl %al, %r14d
movq %r15, %rdi
callq 0x2050
movq -0x30(%rbp), %rdi
callq 0x2050
movq %r12, %rdi
callq 0x2050
jmp 0x7f69
movl %r15d, %edi
callq 0x21b0
movq -0x58(%rbp), %rdi
callq 0x2050
movq %r12, %rdi
jmp 0x806c
movl $0x1, %r14d
movq -0x30(%rbp), %rdi
callq 0x2040
movq -0x38(%rbp), %r13
movq -0x60(%rbp), %r15
movq -0x48(%rbp), %r12
movq -0x58(%rbp), %rdi
callq 0x2050
movq %r12, %rdi
callq 0x2050
movq (%r13), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x2320
testl %eax, %eax
jns 0x8071
orb $0x1, 0x697(%r13)
jmp 0x8071
movq -0x38(%rbp), %rbx
movq %rbx, %rdi
callq 0x2270
movq %rax, -0x30(%rbp)
movq %r14, %rdi
callq 0x2270
movq %r12, -0x48(%rbp)
movq %rax, -0x40(%rbp)
movq (%r13), %rcx
movl $0x1, %edx
movq -0x30(%rbp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x20c0
movq %r12, %rdi
movq -0x40(%rbp), %r12
movq %r12, %rsi
movl %ebx, %edx
movl %r14d, %ecx
movl %r14d, %r8d
callq 0x10e4a
movslq %eax, %rbx
xorq %r14, %rbx
movq 0x8(%r13), %rcx
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rsi
callq 0x2390
xorq $0x1, %rax
xorl %r14d, %r14d
orq %rbx, %rax
sete %r14b
movq %r12, %rdi
movq -0x48(%rbp), %r12
callq 0x2050
movq -0x30(%rbp), %rdi
callq 0x2050
jmp 0x7f71
movq (%rbx), %rdi
xorl %r14d, %r14d
testq %rdi, %rdi
je 0x7f71
callq 0x2110
movq $0x0, (%rbx)
jmp 0x7f71
addq $0x106a8, %r13          # imm = 0x106A8
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3ee0
movq -0x58(%rbp), %rdi
callq 0x2050
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq