// File: /workspace/github_c_makefile_star2_O3/salextpuru[P]speccy-toochain/src/mhmt/mhmt-main.c
// Function: do_files
ULONG do_files(void)
{
    char * pack_ext;
    char * depk_ext;
    LONG ext_pos;

    // if there is no output filename, create it
    if( !wrk.fname_out ) {
        depk_ext = ".dpk";

        if( wrk.packtype==PK_MLZ )
            pack_ext = ".mlz";

        else if( wrk.packtype==PK_HRM )
            pack_ext = ".hrm";

        else if( wrk.packtype==PK_HST )
            pack_ext = ".hst";

        else
            pack_ext = ".pak"; // all have the same size, as well as depk_ext - 4 bytes!

        wrk.fname_out = (char *)malloc( 5 + strlen(wrk.fname_in) );

        if( !wrk.fname_out ) {
            printf("Can't allocate memory for output filename!\n");
            return 0;
        }

        strcpy(wrk.fname_out, wrk.fname_in);

        if( !wrk.mode ) { // packing
            strcat(wrk.fname_out, pack_ext);

        } else { // depacking
            ext_pos = strlen( wrk.fname_out ) - 4;

            if( (ext_pos>=0) && (!strcmp(&wrk.fname_out[ext_pos], pack_ext)) )
                strcpy( &wrk.fname_out[ext_pos], depk_ext );

            else
                strcat( wrk.fname_out, depk_ext );
        }
    }

    //open files
    wrk.file_in=fopen(wrk.fname_in,"rb");

    if(!wrk.file_in) {
        printf("Cannot open input file \"%s\"!\n",wrk.fname_in);
        return 0;
    }

    wrk.file_out=fopen(wrk.fname_out,"wb");

    if(!wrk.file_out) {
        printf("Cannot create output file \"%s\"!\n",wrk.fname_out);
        return 0;
    }

    // get length of input file
    if( fseek(wrk.file_in,0,SEEK_END) ) {
        printf("Cannot fseek() input file \"%s\"!\n",wrk.fname_in);
        return 0;
    }

    wrk.inlen=(ULONG)ftell(wrk.file_in);

    if( wrk.inlen==(ULONG)(-1L)  ) {
        printf("Cannot ftell() length of input file \"%s\"!\n",wrk.fname_in);
        wrk.inlen=0;
        return 0;

    } else if( wrk.inlen<16 ) {
        printf("Input file \"%s\" is smaller than 16 bytes - I won't process it!\n",wrk.fname_in);
        return 0;
    }

    if( fseek(wrk.file_in,0,SEEK_SET) ) {
        printf("Cannot fseek() input file \"%s\"!\n",wrk.fname_in);
        return 0;
    }

    // load input file in mem
    wrk.indata=(UBYTE *)malloc(wrk.inlen);

    if( !wrk.indata ) {
        printf("Cannot allocate %d bytes of memory for loading input file \"%s\"!\n", wrk.inlen, wrk.fname_in);
        return 0;
    }

    if( wrk.inlen!=fread(wrk.indata,1,wrk.inlen,wrk.file_in) ) {
        printf("Cannot successfully load input file \"%s\" in memory!\n",wrk.fname_in);
        return 0;
    }

    return 1;// no errors
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
leaq 0x8331(%rip), %r13      # 0xb320
cmpq $0x0, 0x28(%r13)
je 0x3052
movq 0x20(%r13), %rbx
leaq 0x363d(%rip), %rsi      # 0x663e
movq %rbx, %rdi
callq 0x10b0
movq %rax, 0x30(%r13)
testq %rax, %rax
je 0x306f
movq 0x28(%r13), %rdi
leaq 0x3642(%rip), %rsi      # 0x665f
callq 0x10b0
movq %rax, 0x38(%r13)
testq %rax, %rax
je 0x307c
movq 0x30(%r13), %rdi
xorl %ebx, %ebx
xorl %esi, %esi
movl $0x2, %edx
callq 0x10a0
testl %eax, %eax
je 0x312c
movq 0x20(%r13), %rsi
leaq 0x3633(%rip), %rdi      # 0x6683
jmp 0x3089
movl (%r13), %eax
decl %eax
cmpl $0x2, %eax
ja 0x309c
movl %eax, %eax
leaq 0x3d26(%rip), %rcx      # 0x6d8c
movslq (%rcx,%rax,4), %r14
addq %rcx, %r14
jmp 0x30a3
movq 0x20(%r13), %rsi
leaq 0x35c7(%rip), %rdi      # 0x6641
jmp 0x3087
movq 0x28(%r13), %rsi
leaq 0x35db(%rip), %rdi      # 0x6662
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x1040
movl %ebx, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x3596(%rip), %r14      # 0x6639
movq 0x20(%r13), %rbx
movq %rbx, %rdi
callq 0x1050
leaq 0x5(%rax), %rdi
callq 0x10e0
movq %rax, 0x28(%r13)
testq %rax, %rax
je 0x3119
movq %rax, %r15
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1070
cmpl $0x0, 0x8(%r13)
je 0x3153
movq %r15, %rdi
callq 0x1050
addl $-0x4, %eax
js 0x30fe
movl %eax, %eax
movq %r15, %r12
addq %rax, %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x1100
testl %eax, %eax
je 0x31ef
movq %r15, %rdi
callq 0x1050
movl $0x6b70642e, (%r15,%rax) # imm = 0x6B70642E
movb $0x0, 0x4(%r15,%rax)
jmp 0x2ffa
leaq 0x3b2f(%rip), %rdi      # 0x6c4f
callq 0x1120
xorl %ebx, %ebx
jmp 0x3090
movq 0x30(%r13), %rdi
callq 0x1030
movl %eax, 0x48(%r13)
cmpl $-0x1, %eax
je 0x3170
cmpl $0xf, %eax
ja 0x3191
movq 0x20(%r13), %rsi
leaq 0x3581(%rip), %rdi      # 0x66cf
jmp 0x3087
movq %r15, %rdi
callq 0x1050
movl (%r14), %ecx
movl %ecx, (%r15,%rax)
movb 0x4(%r14), %cl
movb %cl, 0x4(%r15,%rax)
jmp 0x2ffa
movq 0x20(%r13), %rsi
leaq 0x3529(%rip), %rdi      # 0x66a4
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x1040
movl $0x0, 0x48(%r13)
jmp 0x3090
movq 0x30(%r13), %rdi
xorl %ebx, %ebx
xorl %esi, %esi
xorl %edx, %edx
callq 0x10a0
testl %eax, %eax
jne 0x3045
movl 0x48(%r13), %r14d
movq %r14, %rdi
callq 0x10e0
movq %rax, 0x40(%r13)
testq %rax, %rax
je 0x3202
movq 0x30(%r13), %rcx
movl $0x1, %esi
movq %rax, %rdi
movq %r14, %rdx
callq 0x1110
movl $0x1, %ebx
cmpq %r14, %rax
je 0x3090
movq 0x20(%r13), %rsi
leaq 0x3566(%rip), %rdi      # 0x6750
jmp 0x3087
movb $0x0, 0x4(%r12)
movl $0x6b70642e, (%r12)     # imm = 0x6B70642E
jmp 0x2ffa
movq 0x20(%r13), %rdx
leaq 0x3502(%rip), %rdi      # 0x670f
xorl %ebx, %ebx
movl %r14d, %esi
xorl %eax, %eax
callq 0x1040
jmp 0x3090