// File: /workspace/github_c_makefile_star2_O1/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, %r12d
movq %rsi, %rbx
movq %rdi, 0x18(%rsp)
callq 0x36e3
movl %eax, 0x24(%rsp)
cmpl $0x0, 0x1db9c(%rip)     # 0x21300
jle 0x3783
leaq 0xf07a(%rip), %rdi      # 0x127e7
leaq 0xec6b(%rip), %rsi      # 0x123df
movl $0x3c8, %edx            # imm = 0x3C8
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc960
cmpl $0x1, %r12d
jne 0x37c6
leaq 0xd0(%rsp), %r14
movl $0x27f6, %edx           # imm = 0x27F6
movq %r14, %rdi
movq %rbx, %rsi
callq 0xbfb6
movq %r14, 0x30(%rsp)
movq %r14, %rdi
movl $0x2e, %esi
callq 0x2150
testq %rax, %rax
je 0x37d1
movb $0x0, (%rax)
incq %rax
movl $0x1, %r12d
jmp 0x37d9
movq $0x0, 0x30(%rsp)
jmp 0x37d7
movl $0x3, %r12d
xorl %eax, %eax
cmpl $0x4, %r12d
jne 0x3824
leaq 0xd0(%rsp), %r14
movl $0x27f6, %edx           # imm = 0x27F6
movq %r14, %rdi
movq %rbx, %rsi
callq 0xbfb6
movq %r14, 0x30(%rsp)
movq %r14, %rdi
movl $0x2e, %esi
callq 0x2150
testq %rax, %rax
je 0x381c
movb $0x0, (%rax)
incq %rax
movl $0x4, %r12d
jmp 0x3824
movl $0x6, %r12d
xorl %eax, %eax
movq %rax, 0x38(%rsp)
leaq 0xebdc(%rip), %rdx      # 0x1240c
leaq 0x4d0(%rsp), %rbp
movl $0x3ff, %esi            # imm = 0x3FF
movq %rbp, %rdi
movq 0x18(%rsp), %rcx
movq %rbx, 0x28(%rsp)
movq %rbx, %r8
xorl %eax, %eax
callq 0x2140
movl $0x1, %r15d
leaq 0x40(%rsp), %rbx
decl %r12d
leaq 0xecb7(%rip), %r13      # 0x12520
xorl %r14d, %r14d
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x2250
cmpl $-0x1, %eax
je 0x38aa
cmpl $0x5, %r12d
ja 0x3972
movslq (%r13,%r12,4), %rax
addq %r13, %rax
jmpq *%rax
movq 0x38(%rsp), %rax
movq %rax, (%rsp)
movl $0x3ff, %esi            # imm = 0x3FF
movq %rbp, %rdi
leaq 0xef75(%rip), %rdx      # 0x1281d
jmp 0x3917
movl $0x1, %r14d
jmp 0x3975
movq 0x28(%rsp), %rax
movq %rax, (%rsp)
movl $0x3ff, %esi            # imm = 0x3FF
movq %rbp, %rdi
leaq 0xef65(%rip), %rdx      # 0x12832
movq 0x18(%rsp), %rcx
movl %r15d, %r8d
movl 0x24(%rsp), %r9d
xorl %eax, %eax
callq 0x2140
jmp 0x3972
movl $0x3ff, %esi            # imm = 0x3FF
movq %rbp, %rdi
leaq 0xef34(%rip), %rdx      # 0x12829
jmp 0x395e
movq 0x38(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x24(%rsp), %eax
movl %eax, (%rsp)
movl $0x3ff, %esi            # imm = 0x3FF
movq %rbp, %rdi
leaq 0xef27(%rip), %rdx      # 0x1283e
movq 0x18(%rsp), %rcx
movq 0x30(%rsp), %r8
jmp 0x3968
movl $0x3ff, %esi            # imm = 0x3FF
movq %rbp, %rdi
leaq 0xeee2(%rip), %rdx      # 0x12814
movq 0x18(%rsp), %rcx
movl %r15d, %r8d
movq 0x28(%rsp), %r9
xorl %eax, %eax
callq 0x2140
jmp 0x3972
movl 0x24(%rsp), %eax
movl %eax, (%rsp)
movl $0x3ff, %esi            # imm = 0x3FF
movq %rbp, %rdi
leaq 0xeeef(%rip), %rdx      # 0x1284d
movq 0x18(%rsp), %rcx
movq 0x28(%rsp), %r8
movl %r15d, %r9d
xorl %eax, %eax
callq 0x2140
incl %r15d
testl %r14d, %r14d
je 0x386c
cmpl $0x2, %r15d
movq 0x28(%rsp), %rbx
jl 0x39ae
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 0xbfb6
cmpl $0x0, 0x1d94b(%rip)     # 0x21300
jle 0x39d4
leaq 0xee9b(%rip), %rdi      # 0x12859
leaq 0xea1a(%rip), %rsi      # 0x123df
movl $0x420, %edx            # imm = 0x420
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc960
xorl %eax, %eax
addq $0x8d8, %rsp            # imm = 0x8D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq