// File: /workspace/github_c_makefile_star2_O0/Matthias-Wandel[P]imgcomp/src/util.c
// Function: EnsurePathExists
int EnsurePathExists(const char * FileName, int filepath)
{
    char NewPath[PATH_MAX*2];
    int a;
    int LastSlash = 0;

    //printf("Ensure path exists '%s' %d\n",FileName, filepath);

    // Extract the path component of the file name.
    strcpy(NewPath, FileName);
    a = strlen(NewPath);
    
    if (!filepath){
        // If it's not a path to a file, add a trailing slash for the existing
        // code to work properly.
        if (NewPath[a-1] != '/'){
            NewPath[a++] = '/';
        }
    }
    
    for (;;){
        a--;
        if (a == 0){
            NewPath[0] = 0;
            break;    
        }
        if (NewPath[a] == '/'){
            struct stat dummy;
            NewPath[a] = 0;
            if (stat(NewPath, &dummy) == 0){
                if (S_ISDIR(dummy.st_mode)){
                    // Break out of loop, and go forward along path making
                    // the directories.
                    if (LastSlash == 0){
                        // Full path exists.  No need to create any directories.
                        return 1;
                    }
                    break;
                }else{
                    // Its a file.
                    printf("Can't create path '%s' due to file conflict\n",NewPath);
                    fprintf(Log,"Can't create path '%s' due to file conflict\n",NewPath);
                    exit(-1);
                }
            }
            if (LastSlash == 0) LastSlash = a;
        }
    }

    // Now work forward.
    for(;FileName[a];a++){
        //printf("%d/%d: '%c'\n",a,LastSlash,FileName[a]);
        if (FileName[a] == '/' || a == 0){
            if (a == LastSlash) break;
            NewPath[a] = FileName[a];
              #ifdef _WIN32
                if (NewPath[1] == ':' && strlen(NewPath) == 2) continue;
            #endif
            fprintf(Log,"Make directory %s\n",NewPath);
            
            if (mkdir(NewPath,0777)){
                printf("Could not create directory '%s'\n",NewPath);
                fprintf(Log,"Could not create directory '%s'\n",NewPath);
                // Failed to create directory.
                exit(-1);
            }
        }
    }
    return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20c0, %rsp           # imm = 0x20C0
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl $0x0, -0x2028(%rbp)
leaq -0x2020(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x30b0
leaq -0x2020(%rbp), %rdi
callq 0x3130
movl %eax, -0x2024(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0xbbf9
movl -0x2024(%rbp), %eax
subl $0x1, %eax
cltq
movsbl -0x2020(%rbp,%rax), %eax
cmpl $0x2f, %eax
je 0xbbf7
movl -0x2024(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x2024(%rbp)
cltq
movb $0x2f, -0x2020(%rbp,%rax)
jmp 0xbbf9
jmp 0xbbfb
movl -0x2024(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x2024(%rbp)
cmpl $0x0, -0x2024(%rbp)
jne 0xbc1f
movb $0x0, -0x2020(%rbp)
jmp 0xbce1
movslq -0x2024(%rbp), %rax
movsbl -0x2020(%rbp,%rax), %eax
cmpl $0x2f, %eax
jne 0xbcdc
movslq -0x2024(%rbp), %rax
movb $0x0, -0x2020(%rbp,%rax)
leaq -0x2020(%rbp), %rdi
leaq -0x20b8(%rbp), %rsi
callq 0x32b0
cmpl $0x0, %eax
jne 0xbcc5
movl -0x20a0(%rbp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x4000, %eax           # imm = 0x4000
jne 0xbc87
cmpl $0x0, -0x2028(%rbp)
jne 0xbc85
movl $0x1, -0x4(%rbp)
jmp 0xbdda
jmp 0xbce1
leaq -0x2020(%rbp), %rsi
leaq 0x55e9(%rip), %rdi      # 0x1127e
movb $0x0, %al
callq 0x3170
leaq 0x9375(%rip), %rax      # 0x15018
movq (%rax), %rdi
leaq -0x2020(%rbp), %rdx
leaq 0x55ca(%rip), %rsi      # 0x1127e
movb $0x0, %al
callq 0x3290
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3480
cmpl $0x0, -0x2028(%rbp)
jne 0xbcda
movl -0x2024(%rbp), %eax
movl %eax, -0x2028(%rbp)
jmp 0xbcdc
jmp 0xbbfb
jmp 0xbce3
movq -0x10(%rbp), %rax
movslq -0x2024(%rbp), %rcx
cmpb $0x0, (%rax,%rcx)
je 0xbdd3
movq -0x10(%rbp), %rax
movslq -0x2024(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2f, %eax
je 0xbd19
cmpl $0x0, -0x2024(%rbp)
jne 0xbdbd
movl -0x2024(%rbp), %eax
cmpl -0x2028(%rbp), %eax
jne 0xbd2c
jmp 0xbdd3
movq -0x10(%rbp), %rax
movslq -0x2024(%rbp), %rcx
movb (%rax,%rcx), %cl
movslq -0x2024(%rbp), %rax
movb %cl, -0x2020(%rbp,%rax)
leaq 0x92c9(%rip), %rax      # 0x15018
movq (%rax), %rdi
leaq -0x2020(%rbp), %rdx
leaq 0x554b(%rip), %rsi      # 0x112ab
movb $0x0, %al
callq 0x3290
leaq -0x2020(%rbp), %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x30c0
cmpl $0x0, %eax
je 0xbdbb
leaq -0x2020(%rbp), %rsi
leaq 0x5533(%rip), %rdi      # 0x112be
movb $0x0, %al
callq 0x3170
leaq 0x927f(%rip), %rax      # 0x15018
movq (%rax), %rdi
leaq -0x2020(%rbp), %rdx
leaq 0x5514(%rip), %rsi      # 0x112be
movb $0x0, %al
callq 0x3290
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3480
jmp 0xbdbd
jmp 0xbdbf
movl -0x2024(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2024(%rbp)
jmp 0xbce3
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20c0, %rsp           # imm = 0x20C0
popq %rbp
retq
nopw %cs:(%rax,%rax)