#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;
};

/* Variables and functions */
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
scalar_t__ mkdir(char *, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ stat(char *, struct stat *);
char *strchr(char *, char);
char *strdup(char const *);
int mkpathto(const char *path) {
  char *p = strdup(path);
  if (!p) {
    printf("strdup error\n");
    return 1;
  }
  int return_value = 0;
  for (char *slash = strchr(p, '/'); slash != NULL;
       slash = strchr(slash + 1, '/')) {
    *slash = '\0';
    struct stat st;
    if (0 == stat(p, &st)) {
      if (!S_ISDIR(st.st_mode)) {
        printf("mkpath error: %s is not a dir.\n", p);
        return_value = 1;
        break;
      }
    } else if (mkdir(p, 0777)) {
      printf("mkpath error: cannot create dir %s.\n", p);
      return_value = 1;
      break;
    }
    *slash = '/';
  }
  free(p);
  return return_value;
}


x86 assembly (clang, -O0):
<mkpathto>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ae <mkpathto+0x3e>
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1294 <mkpathto+0x124>
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1285 <mkpathto+0x115>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <stat@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1228 <mkpathto+0xb8>
mov    -0x30(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1223 <mkpathto+0xb3>
mov    -0x18(%rbp),%rsi
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x1c(%rbp)
jmp    1285 <mkpathto+0x115>
jmp    1263 <mkpathto+0xf3>
mov    -0x18(%rbp),%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
je     125e <mkpathto+0xee>
mov    -0x18(%rbp),%rsi
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x1c(%rbp)
jmp    1285 <mkpathto+0x115>
jmp    1263 <mkpathto+0xf3>
mov    -0x28(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,-0x28(%rbp)
jmp    11c7 <mkpathto+0x57>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkpathto>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
call   10a0 <strdup@plt>
test   %rax,%rax
je     1221 <mkpathto+0xa1>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
xor    %r15d,%r15d
test   %rax,%rax
je     124b <mkpathto+0xcb>
mov    %rax,%rbp
mov    %rsp,%r12
lea    0xe3e(%rip),%r13
lea    0xe57(%rip),%r14
jmp    11f9 <mkpathto+0x79>
nopl   0x0(%rax,%rax,1)
mov    (%rsp),%edi
call   1080 <S_ISDIR@plt>
test   %eax,%eax
je     1235 <mkpathto+0xb5>
movb   $0x2f,0x0(%rbp)
add    $0x1,%rbp
mov    %rbp,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
mov    %rax,%rbp
test   %rax,%rax
je     124b <mkpathto+0xcb>
movb   $0x0,0x0(%rbp)
mov    %rbx,%rdi
mov    %r12,%rsi
call   1090 <stat@plt>
test   %rax,%rax
je     11d0 <mkpathto+0x50>
mov    %rbx,%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
je     11dc <mkpathto+0x5c>
jmp    1238 <mkpathto+0xb8>
lea    0xe1d(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%r15d
jmp    1253 <mkpathto+0xd3>
mov    %r13,%r14
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%r15d
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [14 x i8] c"strdup error\0A\00"
@.str.1 = [32 x i8] c"mkpath error: %s is not a dir.\0A\00"
@.str.2 = [37 x i8] c"mkpath error: cannot create dir %s.\0A\00"
define i32 @mkpathto(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.stat
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i8* @strdup(i8* %8)
store i8* %9, i8** %4
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %14, label %12
12: ; preds = %1
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %2
br label %52
14: ; preds = %1
store i32 0, i32* %5
%15 = load i8*, i8** %4
%16 = call i8* @strchr(i8* %15, i8 signext 47)
store i8* %16, i8** %6
br label %17
17: ; preds = %44, %14
%18 = load i8*, i8** %6
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %48
20: ; preds = %17
%21 = load i8*, i8** %6
store i8 0, i8* %21
%22 = load i8*, i8** %4
%23 = call i64 @stat(i8* %22, %struct.stat* %7)
%24 = icmp eq i64 0, %23
br i1 %24, label %25, label %34
25: ; preds = %20
%26 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @S_ISDIR(i32 %27)
%29 = icmp ne i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %25
%31 = load i8*, i8** %4
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %31)
store i32 1, i32* %5
br label %48
33: ; preds = %25
br label %42
34: ; preds = %20
%35 = load i8*, i8** %4
%36 = call i64 @mkdir(i8* %35, i32 511)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = load i8*, i8** %4
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %39)
store i32 1, i32* %5
br label %48
41: ; preds = %34
br label %42
42: ; preds = %41, %33
%43 = load i8*, i8** %6
store i8 47, i8* %43
br label %44
44: ; preds = %42
%45 = load i8*, i8** %6
%46 = getelementptr inbounds i8, i8* %45, i64 1
%47 = call i8* @strchr(i8* %46, i8 signext 47)
store i8* %47, i8** %6
br label %17
48: ; preds = %38, %30, %17
%49 = load i8*, i8** %4
%50 = call i32 @free(i8* %49)
%51 = load i32, i32* %5
store i32 %51, i32* %2
br label %52
52: ; preds = %48, %12
%53 = load i32, i32* %2
ret i32 %53
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str.1 = [32 x i8] c"mkpath error: %s is not a dir.\0A\00"
@.str.2 = [37 x i8] c"mkpath error: cannot create dir %s.\0A\00"
@str = [13 x i8] c"strdup error\00"
define i32 @mkpathto(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = tail call i8* @strdup(i8* %0)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %34
7: ; preds = %1
%8 = tail call i8* @strchr(i8* %3, i8 signext 47)
%9 = icmp eq i8* %8, null
br i1 %9, label %31, label %10
10: ; preds = %7
%11 = bitcast %struct.stat* %2 to i8*
%12 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
br label %13
13: ; preds = %10, %27
%14 = phi i8* [ %8, %10 ], [ %29, %27 ]
store i8 0, i8* %14
%15 = call i64 @stat(i8* %3, %struct.stat* %2)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = load i32, i32* %12
%19 = call i32 @S_ISDIR(i32 %18)
%20 = icmp eq i32 %19, 0
br i1 %20, label %24, label %27
21: ; preds = %13
%22 = call i64 @mkdir(i8* %3, i32 511)
%23 = icmp eq i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %21, %17
%25 = phi i8* [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), %17 ], [ getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), %21 ]
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) %25, i8* %3)
br label %31
27: ; preds = %21, %17
store i8 47, i8* %14
%28 = getelementptr inbounds i8, i8* %14, i64 1
%29 = call i8* @strchr(i8* %28, i8 signext 47)
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %13
31: ; preds = %27, %7, %24
%32 = phi i32 [ 1, %24 ], [ 0, %7 ], [ 0, %27 ]
%33 = call i32 @free(i8* %3)
br label %34
34: ; preds = %31, %5
%35 = phi i32 [ %32, %31 ], [ 1, %5 ]
ret i32 %35
}