// File: /workspace/github_c_makefile_star2_O2/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 $0x8c8, %rsp            # imm = 0x8C8
movl %edx, %r15d
movq %rsi, %rbx
movq %rdi, 0x10(%rsp)
callq 0x35a7
movl %eax, 0x1c(%rsp)
cmpl $0x0, 0x1cce3(%rip)     # 0x20300
jle 0x363c
leaq 0xe1f1(%rip), %rdi      # 0x11817
leaq 0xddb2(%rip), %rsi      # 0x113df
movl $0x3c8, %edx            # imm = 0x3C8
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc2a1
cmpl $0x4, %r15d
je 0x3679
cmpl $0x1, %r15d
jne 0x36be
leaq 0xc0(%rsp), %r14
movl $0x27f6, %edx           # imm = 0x27F6
movq %r14, %rdi
movq %rbx, %rsi
callq 0xba3e
pushq $0x2e
popq %rsi
movq %r14, %rdi
callq 0x2150
testq %rax, %rax
jne 0x36a1
movq %r14, 0x28(%rsp)
pushq $0x3
jmp 0x36d0
leaq 0xc0(%rsp), %r14
movl $0x27f6, %edx           # imm = 0x27F6
movq %r14, %rdi
movq %rbx, %rsi
callq 0xba3e
pushq $0x2e
popq %rsi
movq %r14, %rdi
callq 0x2150
testq %rax, %rax
je 0x36c9
movq %rax, %rcx
movb $0x0, (%rax)
incq %rcx
movq %rcx, 0x20(%rsp)
leaq 0xc0(%rsp), %rax
movq %rax, 0x28(%rsp)
jmp 0x36db
movq $0x0, 0x28(%rsp)
jmp 0x36d2
movq %r14, 0x28(%rsp)
pushq $0x6
popq %r15
movq $0x0, 0x20(%rsp)
leaq 0xdd2a(%rip), %rdx      # 0x1140c
leaq 0x4c0(%rsp), %r12
movl $0x3ff, %esi            # imm = 0x3FF
movq %r12, %rdi
movq 0x10(%rsp), %rcx
movq %rbx, %r14
movq %rbx, %r8
xorl %eax, %eax
callq 0x2140
pushq $0x1
popq %rbp
leaq 0x30(%rsp), %r13
decl %r15d
leaq 0xde0a(%rip), %rbx      # 0x11520
movq %r12, %rdi
movq %r13, %rsi
callq 0x2240
cmpl $-0x1, %eax
je 0x3812
cmpl $0x5, %r15d
ja 0x380b
movslq (%rbx,%r15,4), %rax
addq %rbx, %rax
jmpq *%rax
movq 0x20(%rsp), %rax
movq %rax, (%rsp)
movl $0x3ff, %esi            # imm = 0x3FF
movq %r12, %rdi
leaq 0xe0f8(%rip), %rdx      # 0x1184d
jmp 0x37b4
movq %r14, (%rsp)
movl $0x3ff, %esi            # imm = 0x3FF
movq %r12, %rdi
leaq 0xe0f8(%rip), %rdx      # 0x11862
movq 0x10(%rsp), %rcx
movl %ebp, %r8d
movl 0x1c(%rsp), %r9d
xorl %eax, %eax
callq 0x2140
jmp 0x380b
movl $0x3ff, %esi            # imm = 0x3FF
movq %r12, %rdi
leaq 0xe0c7(%rip), %rdx      # 0x11859
jmp 0x37f9
movq 0x20(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x1c(%rsp), %eax
movl %eax, (%rsp)
movl $0x3ff, %esi            # imm = 0x3FF
movq %r12, %rdi
leaq 0xe0ba(%rip), %rdx      # 0x1186e
movq 0x10(%rsp), %rcx
movq 0x28(%rsp), %r8
jmp 0x3801
movl $0x3ff, %esi            # imm = 0x3FF
movq %r12, %rdi
leaq 0xe075(%rip), %rdx      # 0x11844
movq 0x10(%rsp), %rcx
movl %ebp, %r8d
movq %r14, %r9
xorl %eax, %eax
callq 0x2140
jmp 0x380b
movl 0x1c(%rsp), %eax
movl %eax, (%rsp)
movl $0x3ff, %esi            # imm = 0x3FF
movq %r12, %rdi
leaq 0xe084(%rip), %rdx      # 0x1187d
movq 0x10(%rsp), %rcx
movq %r14, %r8
movl %ebp, %r9d
xorl %eax, %eax
callq 0x2140
incl %ebp
jmp 0x3716
cmpl $0x2, %ebp
jb 0x383a
pushq $0x2f
popq %rsi
movq %r12, %rdi
callq 0x2150
leaq 0x1(%rax), %rsi
testq %rax, %rax
cmoveq %r12, %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
callq 0xba3e
cmpl $0x0, 0x1cabf(%rip)     # 0x20300
jle 0x3860
leaq 0xe03f(%rip), %rdi      # 0x11889
leaq 0xdb8e(%rip), %rsi      # 0x113df
movl $0x420, %edx            # imm = 0x420
movq %r14, %rcx
xorl %eax, %eax
callq 0xc2a1
xorl %eax, %eax
addq $0x8c8, %rsp            # imm = 0x8C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq