// File: /workspace/github_c_makefile_star2_O3/inflex[P]ripMIME/mime.c
// Function: MIME_test_uniquename
int MIME_test_uniquename( char *path, char *fname, int method )
{
    struct stat buf;

    char newname[_MIME_STRLEN_MAX +1];
    char scr[_MIME_STRLEN_MAX +1]; /** Scratch var **/
    char *frontname, *extention;

    int cleared = 0;
    int count = 1;

    int randval = get_random_value();

    if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_test_uniquename:DEBUG: Start (%s)",FL,fname);

    frontname = extention = NULL;  // shuts the compiler up

    if (method == _MIME_RENAME_METHOD_INFIX)
    {
        PLD_strncpy(scr,fname, _MIMEH_STRLEN_MAX);
        frontname = scr;
        extention = strrchr(scr,'.');

        if (extention)
        {
            *extention = '\0';
            extention++;
        }
        else
        {
            method = _MIME_RENAME_METHOD_POSTFIX;
        }
    }

  if (method == _MIME_RENAME_METHOD_RANDINFIX)
    {
        PLD_strncpy(scr,fname, _MIMEH_STRLEN_MAX);
        frontname = scr;
        extention = strrchr(scr,'.');

        if (extention)
        {
            *extention = '\0';
            extention++;
        }
        else
        {
            method = _MIME_RENAME_METHOD_RANDPOSTFIX;
        }
    }

    snprintf(newname,_MIME_STRLEN_MAX,"%s/%s",path,fname);
    while (!cleared)
    {
        if ((stat(newname, &buf) == -1))
        {
            cleared++;
        }
        else
        {
            if (method == _MIME_RENAME_METHOD_PREFIX)
            {
                snprintf(newname,_MIME_STRLEN_MAX,"%s/%d_%s",path,count,fname);
            }
            else
                if (method == _MIME_RENAME_METHOD_INFIX)
                {
                    snprintf(newname,_MIME_STRLEN_MAX,"%s/%s_%d.%s",path,frontname,count,extention);
                }
                else
                    if (method == _MIME_RENAME_METHOD_POSTFIX)
                    {
                        snprintf(newname,_MIME_STRLEN_MAX,"%s/%s_%d",path,fname,count);
                    }
                    else
                        /* Handle randome methots*/
                        if (method == _MIME_RENAME_METHOD_RANDPREFIX)
                        {
                            snprintf(newname,_MIME_STRLEN_MAX,"%s/%d_%d_%s",path,count,randval,fname);
                        }
                        else
                            if (method == _MIME_RENAME_METHOD_RANDINFIX)
                            {
                                snprintf(newname,_MIME_STRLEN_MAX,"%s/%s_%d_%d.%s",path,frontname,count,randval,extention);
                            }
                            else
                                if (method == _MIME_RENAME_METHOD_RANDPOSTFIX)
                                {
                                    snprintf(newname,_MIME_STRLEN_MAX,"%s/%s_%d_%d",path,fname,count,randval);
                                }
            count++;
        }
    }
    if (count > 1)
    {
        frontname = strrchr(newname,'/');
        if (frontname) frontname++;
        else frontname = newname;

        PLD_strncpy(fname, frontname, _MIMEH_FILENAMELEN_MAX); //FIXME - this assumes that the buffer space is at least MIME_STRLEN_MAX sized.
    }
    if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_test_uniquename:DEBUG: Done (%s)",FL,fname);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x8d8, %rsp            # imm = 0x8D8
movl %edx, %r15d
movq %rsi, %rbx
movq %rdi, 0x18(%rsp)
callq 0x36b7
movl %eax, 0x24(%rsp)
cmpl $0x0, 0x1dbcf(%rip)     # 0x21300
jle 0x3750
leaq 0xf0dd(%rip), %rdi      # 0x12817
leaq 0xec9e(%rip), %rsi      # 0x123df
movl $0x3c8, %edx            # imm = 0x3C8
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc84f
cmpl $0x4, %r15d
je 0x378e
cmpl $0x1, %r15d
jne 0x37d0
leaq 0xd0(%rsp), %r14
movl $0x27f6, %edx           # imm = 0x27F6
movq %r14, %rdi
movq %rbx, %rsi
callq 0xbede
movq %r14, %rdi
movl $0x2e, %esi
callq 0x2150
testq %rax, %rax
jne 0x37b8
movl $0x3, %r15d
jmp 0x37db
leaq 0xd0(%rsp), %r14
movl $0x27f6, %edx           # imm = 0x27F6
movq %r14, %rdi
movq %rbx, %rsi
callq 0xbede
movq %r14, %rdi
movl $0x2e, %esi
callq 0x2150
testq %rax, %rax
je 0x37d5
movq %rax, %rcx
movb $0x0, (%rax)
incq %rcx
movq %rcx, 0x28(%rsp)
leaq 0xd0(%rsp), %r14
jmp 0x37e4
xorl %r14d, %r14d
jmp 0x37db
movl $0x6, %r15d
movq $0x0, 0x28(%rsp)
leaq 0xec21(%rip), %rdx      # 0x1240c
leaq 0x4d0(%rsp), %r12
movl $0x3ff, %esi            # imm = 0x3FF
movq %r12, %rdi
movq 0x18(%rsp), %rcx
movq %rbx, %r8
xorl %eax, %eax
callq 0x2140
leaq 0x40(%rsp), %rsi
movq %r12, %rdi
callq 0x2240
cmpl $-0x1, %eax
je 0x3970
movl %eax, 0x34(%rsp)
movq %r14, 0x38(%rsp)
movq %rbx, %r14
movl $0x1, %r12d
decl %r15d
leaq 0x4d0(%rsp), %rbp
leaq 0x40(%rsp), %r13
leaq 0xecd7(%rip), %rbx      # 0x12520
cmpl $0x5, %r15d
ja 0x392a
movslq (%rbx,%r15,4), %rax
addq %rbx, %rax
jmpq *%rax
movq 0x28(%rsp), %rax
movq %rax, (%rsp)
movl $0x3ff, %esi            # imm = 0x3FF
movq %rbp, %rdi
leaq 0xefd9(%rip), %rdx      # 0x1284d
jmp 0x38d3
movq %r14, (%rsp)
movl $0x3ff, %esi            # imm = 0x3FF
movq %rbp, %rdi
leaq 0xefd9(%rip), %rdx      # 0x12862
movq 0x18(%rsp), %rcx
movl %r12d, %r8d
movl 0x24(%rsp), %r9d
xorl %eax, %eax
callq 0x2140
jmp 0x392a
movl $0x3ff, %esi            # imm = 0x3FF
movq %rbp, %rdi
leaq 0xefa8(%rip), %rdx      # 0x12859
jmp 0x3918
movq 0x28(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x24(%rsp), %eax
movl %eax, (%rsp)
movl $0x3ff, %esi            # imm = 0x3FF
movq %rbp, %rdi
leaq 0xef9b(%rip), %rdx      # 0x1286e
movq 0x18(%rsp), %rcx
movq 0x38(%rsp), %r8
jmp 0x3920
movl $0x3ff, %esi            # imm = 0x3FF
movq %rbp, %rdi
leaq 0xef56(%rip), %rdx      # 0x12844
movq 0x18(%rsp), %rcx
movl %r12d, %r8d
movq %r14, %r9
xorl %eax, %eax
callq 0x2140
jmp 0x392a
movl 0x24(%rsp), %eax
movl %eax, (%rsp)
movl $0x3ff, %esi            # imm = 0x3FF
movq %rbp, %rdi
leaq 0xef65(%rip), %rdx      # 0x1287d
movq 0x18(%rsp), %rcx
movq %r14, %r8
movl %r12d, %r9d
xorl %eax, %eax
callq 0x2140
incl %r12d
movq %rbp, %rdi
movq %r13, %rsi
callq 0x2240
cmpl $-0x1, %eax
jne 0x3849
cmpl $-0x1, 0x34(%rsp)
movq %r14, %rbx
je 0x3970
movq %rbp, %rdi
movl $0x2f, %esi
callq 0x2150
leaq 0x1(%rax), %rsi
testq %rax, %rax
cmoveq %rbp, %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rdi
callq 0xbede
cmpl $0x0, 0x1d989(%rip)     # 0x21300
jle 0x3996
leaq 0xef09(%rip), %rdi      # 0x12889
leaq 0xea58(%rip), %rsi      # 0x123df
movl $0x420, %edx            # imm = 0x420
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc84f
xorl %eax, %eax
addq $0x8d8, %rsp            # imm = 0x8D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq