// File: rurban[P]dgnlib/cpl_conv.cpp
// Function: CPLUnlinkTree
int CPLUnlinkTree( const char *pszPath )

{
/* -------------------------------------------------------------------- */
/*      First, ensure there isn't any such file yet.                    */
/* -------------------------------------------------------------------- */
    VSIStatBuf sStatBuf;

    if( VSIStat( pszPath, &sStatBuf ) != 0 )
    {
        CPLError( CE_Failure, CPLE_AppDefined, 
                  "It seems no file system object called '%s' exists.",
                  pszPath );

        return errno;
    }

/* -------------------------------------------------------------------- */
/*      If it's a simple file, just delete it.                          */
/* -------------------------------------------------------------------- */
    if( VSI_ISREG( sStatBuf.st_mode ) )
    {
        if( VSIUnlink( pszPath ) != 0 )
        {
            CPLError( CE_Failure, CPLE_AppDefined, 
                      "Failed to unlink %s.\n%s", 
                      pszPath, VSIStrerror( errno ) );
            return errno;
        }
        else
            return 0;
    }

/* -------------------------------------------------------------------- */
/*      If it is a directory recurse then unlink the directory.         */
/* -------------------------------------------------------------------- */
    else if( VSI_ISDIR( sStatBuf.st_mode ) )
    {
        char **papszItems = CPLReadDir( pszPath );
        int  i;

        for( i = 0; papszItems != NULL && papszItems[i] != NULL; i++ )
        {
            char *pszSubPath;
            int nErr;

            if( EQUAL(papszItems[i],".") || EQUAL(papszItems[i],"..") )
                continue;

            pszSubPath = CPLStrdup(
                CPLFormFilename( pszPath, papszItems[i], NULL ) );

            nErr = CPLUnlinkTree( pszSubPath );
            CPLFree( pszSubPath );

            if( nErr != 0 )
            {
                CSLDestroy( papszItems );
                return nErr;
            }
        }
        
        CSLDestroy( papszItems );

        if( VSIRmdir( pszPath ) != 0 )
        {
            CPLError( CE_Failure, CPLE_AppDefined, 
                      "Failed to unlink %s.\n%s", 
                      pszPath, VSIStrerror( errno ) );
            return errno;
        }
        else
            return 0;
    }

/* -------------------------------------------------------------------- */
/*      otherwise report an error.                                      */
/* -------------------------------------------------------------------- */
    else
    {
        CPLError( CE_Failure, CPLE_AppDefined, 
                  "Failed to unlink %s.\nUnrecognised filesystem object.",
                  pszPath );
        return 1000;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0xa050
cmpl $0x0, %eax
je 0x536f
movq -0x10(%rbp), %rcx
movl $0x3, %edi
movl $0x1, %esi
leaq 0x14126(%rip), %rdx     # 0x1947f
movb $0x0, %al
callq 0x5740
callq 0x2040
movl (%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0x55a8
movl -0x88(%rbp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x53e3
movq -0x10(%rbp), %rdi
callq 0x9a50
cmpl $0x0, %eax
je 0x53d7
movq -0x10(%rbp), %rax
movq %rax, -0xc8(%rbp)
callq 0x2040
movl (%rax), %edi
callq 0xa170
movq -0xc8(%rbp), %rcx
movq %rax, %r8
movl $0x3, %edi
movl $0x1, %esi
leaq 0x140f1(%rip), %rdx     # 0x194b2
movb $0x0, %al
callq 0x5740
callq 0x2040
movl (%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0x55a8
movl $0x0, -0x4(%rbp)
jmp 0x55a8
movl -0x88(%rbp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x5585
movq -0x10(%rbp), %rdi
callq 0x56b0
movq %rax, -0xa8(%rbp)
movl $0x0, -0xac(%rbp)
xorl %eax, %eax
cmpq $0x0, -0xa8(%rbp)
movb %al, -0xc9(%rbp)
je 0x5441
movq -0xa8(%rbp), %rax
movslq -0xac(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
setne %al
movb %al, -0xc9(%rbp)
movb -0xc9(%rbp), %al
testb $0x1, %al
jne 0x5450
jmp 0x551d
movq -0xa8(%rbp), %rax
movslq -0xac(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x15587(%rip), %rsi     # 0x1a9f0
callq 0x2300
cmpl $0x0, %eax
je 0x5496
movq -0xa8(%rbp), %rax
movslq -0xac(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x1403e(%rip), %rsi     # 0x194ca
callq 0x2300
cmpl $0x0, %eax
jne 0x5498
jmp 0x5509
movq -0x10(%rbp), %rdi
movq -0xa8(%rbp), %rax
movslq -0xac(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x6c90
movq %rax, %rdi
callq 0x31e0
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rdi
callq 0x5320
movl %eax, -0xbc(%rbp)
movq -0xb8(%rbp), %rdi
callq 0xa000
cmpl $0x0, -0xbc(%rbp)
je 0x5507
movq -0xa8(%rbp), %rdi
callq 0x7450
movl -0xbc(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x55a8
jmp 0x5509
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0x5413
movq -0xa8(%rbp), %rdi
callq 0x7450
movq -0x10(%rbp), %rdi
callq 0x9aa0
cmpl $0x0, %eax
je 0x557c
movq -0x10(%rbp), %rax
movq %rax, -0xd8(%rbp)
callq 0x2040
movl (%rax), %edi
callq 0xa170
movq -0xd8(%rbp), %rcx
movq %rax, %r8
movl $0x3, %edi
movl $0x1, %esi
leaq 0x13f49(%rip), %rdx     # 0x194b2
movb $0x0, %al
callq 0x5740
callq 0x2040
movl (%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0x55a8
movl $0x0, -0x4(%rbp)
jmp 0x55a8
movq -0x10(%rbp), %rcx
movl $0x3, %edi
movl $0x1, %esi
leaq 0x13f33(%rip), %rdx     # 0x194cd
movb $0x0, %al
callq 0x5740
movl $0x3e8, -0x4(%rbp)      # imm = 0x3E8
movl -0x4(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)