#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct stat {
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ dir_path;

/* Variables and functions */
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ mkdir(char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(char *, char const *, int);
void make_path(const char *path_ended_with_sep_or_filename) {
  struct stat dir_stat;
  char dir_path[512];
  strncpy(dir_path, path_ended_with_sep_or_filename, sizeof(dir_path));
  char *pPath = dir_path;
  while (*pPath != '\0') {
    if (*pPath == '/' || *pPath == '\\') {
      *pPath = '\0';
      if (stat(dir_path, &dir_stat) != 0) {
        if (mkdir(dir_path, 0777) != 0) {
          fprintf(stderr, "mkdir('%s', 0777) failed!\n", dir_path);
        }
      } else {
        if (!S_ISDIR(dir_stat.st_mode)) {
          fprintf(stderr, "Can't mkdir()! Path points to a file.\n");
        }
      }
      *pPath = '/';
    }
    ++pPath;
  }
}


x86 assembly (clang, -O0):
<make_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x210(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
lea    -0x210(%rbp),%rax
mov    %rax,-0x218(%rbp)
mov    -0x218(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     126b <make_path+0x11b>
mov    -0x218(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     11bb <make_path+0x6b>
mov    -0x218(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5c,%eax
jne    1254 <make_path+0x104>
mov    -0x218(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x210(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     121d <make_path+0xcd>
lea    -0x210(%rbp),%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
je     1218 <make_path+0xc8>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
lea    -0x210(%rbp),%rdx
lea    0xdef(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    124a <make_path+0xfa>
mov    -0x10(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1245 <make_path+0xf5>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    124a <make_path+0xfa>
mov    -0x218(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x218(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x218(%rbp)
jmp    1182 <make_path+0x32>
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rdi,%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x200,%edx
call   1030 <strncpy@plt>
lea    0x8(%rsp),%r14
mov    0x2e5e(%rip),%r13
lea    0xe92(%rip),%r15
lea    0xe70(%rip),%r12
mov    %rbx,%rbp
jmp    11b5 <make_path+0x65>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x8(%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %eax,%eax
je     1209 <make_path+0xb9>
movb   $0x2f,0x0(%rbp)
add    $0x1,%rbp
movzbl 0x0(%rbp),%eax
cmp    $0x2f,%al
je     11d0 <make_path+0x80>
cmp    $0x5c,%al
je     11d0 <make_path+0x80>
test   %al,%al
jne    11b1 <make_path+0x61>
jmp    1219 <make_path+0xc9>
nopw   0x0(%rax,%rax,1)
movb   $0x0,0x0(%rbp)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <stat@plt>
test   %rax,%rax
je     11a0 <make_path+0x50>
mov    %rbx,%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
je     11ad <make_path+0x5d>
mov    0x0(%r13),%edi
mov    %r12,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    11ad <make_path+0x5d>
mov    0x0(%r13),%edi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    11ad <make_path+0x5d>
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [27 x i8] c"mkdir('%s', 0777) failed!\0A\00"
@.str.1 = [39 x i8] c"Can't mkdir()! Path points to a file.\0A\00"
define void @make_path(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = alloca [512 x i8]
%5 = alloca i8*
store i8* %0, i8** %2
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%7 = load i8*, i8** %2
%8 = call i32 @strncpy(i8* %6, i8* %7, i32 512)
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
store i8* %9, i8** %5
br label %10
10: ; preds = %50, %1
%11 = load i8*, i8** %5
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %53
15: ; preds = %10
%16 = load i8*, i8** %5
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 47
br i1 %19, label %25, label %20
20: ; preds = %15
%21 = load i8*, i8** %5
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 92
br i1 %24, label %25, label %50
25: ; preds = %20, %15
%26 = load i8*, i8** %5
store i8 0, i8* %26
%27 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%28 = call i64 @stat(i8* %27, %struct.stat* %3)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %39
30: ; preds = %25
%31 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%32 = call i64 @mkdir(i8* %31, i32 511)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %36)
br label %38
38: ; preds = %34, %30
br label %48
39: ; preds = %25
%40 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%41 = load i32, i32* %40
%42 = call i32 @S_ISDIR(i32 %41)
%43 = icmp ne i32 %42, 0
br i1 %43, label %47, label %44
44: ; preds = %39
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %47
47: ; preds = %44, %39
br label %48
48: ; preds = %47, %38
%49 = load i8*, i8** %5
store i8 47, i8* %49
br label %50
50: ; preds = %48, %20
%51 = load i8*, i8** %5
%52 = getelementptr inbounds i8, i8* %51, i32 1
store i8* %52, i8** %5
br label %10
53: ; preds = %10
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [27 x i8] c"mkdir('%s', 0777) failed!\0A\00"
@.str.1 = [39 x i8] c"Can't mkdir()! Path points to a file.\0A\00"
define void @make_path(i8* %0) {
%2 = alloca %struct.stat
%3 = alloca [512 x i8]
%4 = bitcast %struct.stat* %2 to i8*
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%6 = call i32 @strncpy(i8* %5, i8* %0, i32 512)
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
br label %8
8: ; preds = %28, %1
%9 = phi i8* [ %5, %1 ], [ %29, %28 ]
%10 = load i8, i8* %9
switch i8 %10, label %28 [
i8 0, label %30
i8 47, label %11
i8 92, label %11
]
11: ; preds = %8, %8
store i8 0, i8* %9
%12 = call i64 @stat(i8* %5, %struct.stat* %2)
%13 = icmp eq i64 %12, 0
br i1 %13, label %20, label %14
14: ; preds = %11
%15 = call i64 @mkdir(i8* %5, i32 511)
%16 = icmp eq i64 %15, 0
br i1 %16, label %27, label %17
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %27
20: ; preds = %11
%21 = load i32, i32* %7
%22 = call i32 @S_ISDIR(i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %20, %24, %14, %17
store i8 47, i8* %9
br label %28
28: ; preds = %8, %27
%29 = getelementptr inbounds i8, i8* %9, i64 1
br label %8
30: ; preds = %8
ret void
}