#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 */
typedef int /*<<< orphan*/ tmp;

/* Variables and functions */
int /*<<< orphan*/ PATH_MAX;
int /*<<< orphan*/ is_dir(char *);
scalar_t__ mkdir(char *, int);
char *strchr(char *, char);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *strrchr(char *, char);
int make_parent_dir(const char *path) {
  char tmp[PATH_MAX + 1];
  char *p;
  strncpy(tmp, path, sizeof(tmp));
  tmp[sizeof(tmp) - 1] = 0;
  p = strrchr(tmp, '/');
  if (!p)
    return 0;
  *(p + 1) = 0;
  p = tmp;
  while (*p == '/')
    p++;
  while ((p = strchr(p, '/'))) {
    *p = 0;
    if (!is_dir(tmp) && mkdir(tmp, 0755))
      return -1;
    *p = '/';
    while (*p == '/')
      p++;
  }
  return 0;
}


x86 assembly (clang, -O0):
<make_parent_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rax,-0x38(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rax
sub    $0x1,%rax
movb   $0x0,(%rdi,%rax,1)
mov    $0x2f,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d6 <make_parent_dir+0x86>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    12a7 <make_parent_dir+0x157>
mov    -0x40(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,0x1(%rcx)
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1207 <make_parent_dir+0xb7>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11e6 <make_parent_dir+0x96>
jmp    120c <make_parent_dir+0xbc>
mov    -0x28(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1299 <make_parent_dir+0x149>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
call   1060 <is_dir@plt>
cmp    $0x0,%eax
jne    126c <make_parent_dir+0x11c>
mov    -0x40(%rbp),%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
je     126c <make_parent_dir+0x11c>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    12a7 <make_parent_dir+0x157>
mov    -0x28(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1294 <make_parent_dir+0x144>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1273 <make_parent_dir+0x123>
jmp    120c <make_parent_dir+0xbc>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_parent_dir>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rsi
mov    0x2e8c(%rip),%rax
mov    (%rax),%ebx
add    $0x1,%ebx
mov    %rsp,%r15
lea    0xf(%rbx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %r15,%rdi
mov    %ebx,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%rbx,%r15,1)
mov    %r15,%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     1227 <make_parent_dir+0xd7>
movb   $0x0,0x1(%rax)
lea    -0x1(%r15),%rdi
nopl   0x0(%rax,%rax,1)
cmpb   $0x2f,0x1(%rdi)
lea    0x1(%rdi),%rdi
je     11b0 <make_parent_dir+0x60>
mov    $0x2f,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1227 <make_parent_dir+0xd7>
mov    %rax,%rbx
nopl   0x0(%rax)
movb   $0x0,(%rbx)
mov    %r15,%rdi
call   1060 <is_dir@plt>
test   %eax,%eax
jne    11f1 <make_parent_dir+0xa1>
mov    %r15,%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
jne    1221 <make_parent_dir+0xd1>
movb   $0x2f,(%rbx)
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmpb   $0x2f,0x1(%rbx)
lea    0x1(%rbx),%rbx
je     1200 <make_parent_dir+0xb0>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11d0 <make_parent_dir+0x80>
jmp    1227 <make_parent_dir+0xd7>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
define i32 @make_parent_dir(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i32, i32* @PATH_MAX
%9 = add nsw i32 %8, 1
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %4
%12 = alloca i8, i64 %10
store i64 %10, i64* %5
%13 = load i8*, i8** %3
%14 = trunc i64 %10 to i32
%15 = call i32 @strncpy(i8* %12, i8* %13, i32 %14)
%16 = sub i64 %10, 1
%17 = getelementptr inbounds i8, i8* %12, i64 %16
store i8 0, i8* %17
%18 = call i8* @strrchr(i8* %12, i8 signext 47)
store i8* %18, i8** %6
%19 = load i8*, i8** %6
%20 = icmp ne i8* %19, null
br i1 %20, label %22, label %21
21: ; preds = %1
store i32 0, i32* %2
store i32 1, i32* %7
br label %58
22: ; preds = %1
%23 = load i8*, i8** %6
%24 = getelementptr inbounds i8, i8* %23, i64 1
store i8 0, i8* %24
store i8* %12, i8** %6
br label %25
25: ; preds = %30, %22
%26 = load i8*, i8** %6
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 47
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = load i8*, i8** %6
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %6
br label %25
33: ; preds = %25
br label %34
34: ; preds = %56, %33
%35 = load i8*, i8** %6
%36 = call i8* @strchr(i8* %35, i8 signext 47)
store i8* %36, i8** %6
%37 = icmp ne i8* %36, null
br i1 %37, label %38, label %57
38: ; preds = %34
%39 = load i8*, i8** %6
store i8 0, i8* %39
%40 = call i32 @is_dir(i8* %12)
%41 = icmp ne i32 %40, 0
br i1 %41, label %46, label %42
42: ; preds = %38
%43 = call i64 @mkdir(i8* %12, i32 493)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %42
store i32 -1, i32* %2
store i32 1, i32* %7
br label %58
46: ; preds = %42, %38
%47 = load i8*, i8** %6
store i8 47, i8* %47
br label %48
48: ; preds = %53, %46
%49 = load i8*, i8** %6
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp eq i32 %51, 47
br i1 %52, label %53, label %56
53: ; preds = %48
%54 = load i8*, i8** %6
%55 = getelementptr inbounds i8, i8* %54, i32 1
store i8* %55, i8** %6
br label %48
56: ; preds = %48
br label %34
57: ; preds = %34
store i32 0, i32* %2
store i32 1, i32* %7
br label %58
58: ; preds = %57, %45, %21
%59 = load i8*, i8** %4
%60 = load i32, i32* %2
ret i32 %60
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
define i32 @make_parent_dir(i8* %0) {
%2 = load i32, i32* @PATH_MAX
%3 = add nsw i32 %2, 1
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = call i32 @strncpy(i8* %5, i8* %0, i32 %3)
%7 = add nsw i64 %4, -1
%8 = getelementptr inbounds i8, i8* %5, i64 %7
store i8 0, i8* %8
%9 = call i8* @strrchr(i8* %5, i8 signext 47)
%10 = icmp eq i8* %9, null
br i1 %10, label %37, label %11
11: ; preds = %1
%12 = getelementptr inbounds i8, i8* %9, i64 1
store i8 0, i8* %12
br label %13
13: ; preds = %13, %11
%14 = phi i8* [ %5, %11 ], [ %17, %13 ]
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 47
%17 = getelementptr inbounds i8, i8* %14, i64 1
br i1 %16, label %13, label %18
18: ; preds = %13
%19 = call i8* @strchr(i8* %14, i8 signext 47)
%20 = icmp eq i8* %19, null
br i1 %20, label %37, label %24
21: ; preds = %32
%22 = call i8* @strchr(i8* %34, i8 signext 47)
%23 = icmp eq i8* %22, null
br i1 %23, label %37, label %24
24: ; preds = %18, %21
%25 = phi i8* [ %22, %21 ], [ %19, %18 ]
store i8 0, i8* %25
%26 = call i32 @is_dir(i8* %5)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = call i64 @mkdir(i8* %5, i32 493)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %28, %24
store i8 47, i8* %25
br label %32
32: ; preds = %31, %32
%33 = phi i8* [ %25, %31 ], [ %34, %32 ]
%34 = getelementptr inbounds i8, i8* %33, i64 1
%35 = load i8, i8* %34
%36 = icmp eq i8 %35, 47
br i1 %36, label %32, label %21
37: ; preds = %28, %21, %18, %1
%38 = phi i32 [ 0, %1 ], [ 0, %18 ], [ -1, %28 ], [ 0, %21 ]
ret i32 %38
}