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

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *malloc_nofail(scalar_t__);
int /*<<< orphan*/ mkdir(char *, int);
int stat(char *, struct stat *);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
char *strrchr(char *, char);
void create_parents(const char *filename) {
  struct stat st;
  int rv = -1;
  char *fn = malloc_nofail(strlen(filename) + 1), *f;
  strcpy(fn, filename);
  f = strrchr(fn, '/');
  if (f == NULL)
    goto out;
  *f = '\0';
  if (stat(fn, &st) == 0)
    goto out;
  *f = '/';
  f = strchr(fn, '/');
  while (f != NULL) {
    *f = '\0';
    if (!rv || (rv = stat(fn, &st)) != 0) {
      mkdir(fn, 0777);
    }
    *f = '/';
    f = strchr(f + 1, '/');
  }
out:
  free(fn);
}


x86 assembly (clang, -O0):
<create_parents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1030 <malloc_nofail@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    $0x2f,%esi
call   1090 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11db <create_parents+0x5b>
jmp    1280 <create_parents+0x100>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   10a0 <stat@plt>
cmp    $0x0,%eax
jne    11fd <create_parents+0x7d>
jmp    1280 <create_parents+0x100>
mov    -0x28(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x20(%rbp),%rdi
mov    $0x2f,%esi
call   1080 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     127b <create_parents+0xfb>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
cmpl   $0x0,-0x14(%rbp)
je     124b <create_parents+0xcb>
mov    -0x20(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   10a0 <stat@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     1259 <create_parents+0xd9>
mov    -0x20(%rbp),%rdi
mov    $0x1ff,%esi
call   1060 <mkdir@plt>
mov    -0x28(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
mov    $0x2f,%esi
call   1080 <strchr@plt>
mov    %rax,-0x28(%rbp)
jmp    1216 <create_parents+0x96>
jmp    1280 <create_parents+0x100>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_parents>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1070 <strlen@plt>
lea    0x1(%rax),%rdi
call   1030 <malloc_nofail@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1050 <strcpy@plt>
mov    %r14,%rdi
mov    $0x2f,%esi
call   1090 <strrchr@plt>
test   %rax,%rax
je     124c <create_parents+0xcc>
mov    %rax,%rbx
movb   $0x0,(%rax)
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   10a0 <stat@plt>
test   %eax,%eax
je     124c <create_parents+0xcc>
movb   $0x2f,(%rbx)
mov    %r14,%rdi
mov    $0x2f,%esi
call   1080 <strchr@plt>
test   %rax,%rax
je     124c <create_parents+0xcc>
mov    %rax,%rbx
mov    $0xffffffff,%r12d
lea    0x8(%rsp),%r15
jmp    122c <create_parents+0xac>
nopl   (%rax)
mov    %r14,%rdi
mov    $0x1ff,%esi
call   1060 <mkdir@plt>
mov    %ebp,%r12d
movb   $0x2f,(%rbx)
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1080 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     124c <create_parents+0xcc>
movb   $0x0,(%rbx)
xor    %ebp,%ebp
test   %r12d,%r12d
je     1200 <create_parents+0x80>
mov    %r14,%rdi
mov    %r15,%rsi
call   10a0 <stat@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1200 <create_parents+0x80>
xor    %r12d,%r12d
jmp    1210 <create_parents+0x90>
mov    %r14,%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <free@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
define void @create_parents(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %2
store i32 -1, i32* %4
%7 = load i8*, i8** %2
%8 = call i64 @strlen(i8* %7)
%9 = add nsw i64 %8, 1
%10 = call i8* @malloc_nofail(i64 %9)
store i8* %10, i8** %5
%11 = load i8*, i8** %5
%12 = load i8*, i8** %2
%13 = call i32 @strcpy(i8* %11, i8* %12)
%14 = load i8*, i8** %5
%15 = call i8* @strrchr(i8* %14, i8 signext 47)
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %1
br label %49
19: ; preds = %1
%20 = load i8*, i8** %6
store i8 0, i8* %20
%21 = load i8*, i8** %5
%22 = call i32 @stat(i8* %21, %struct.stat* %3)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19
br label %49
25: ; preds = %19
%26 = load i8*, i8** %6
store i8 47, i8* %26
%27 = load i8*, i8** %5
%28 = call i8* @strchr(i8* %27, i8 signext 47)
store i8* %28, i8** %6
br label %29
29: ; preds = %43, %25
%30 = load i8*, i8** %6
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %48
32: ; preds = %29
%33 = load i8*, i8** %6
store i8 0, i8* %33
%34 = load i32, i32* %4
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %32
%37 = load i8*, i8** %5
%38 = call i32 @stat(i8* %37, %struct.stat* %3)
store i32 %38, i32* %4
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %36, %32
%41 = load i8*, i8** %5
%42 = call i32 @mkdir(i8* %41, i32 511)
br label %43
43: ; preds = %40, %36
%44 = load i8*, i8** %6
store i8 47, i8* %44
%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 %29
48: ; preds = %29
br label %49
49: ; preds = %48, %24, %18
%50 = load i8*, i8** %5
%51 = call i32 @free(i8* %50)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define void @create_parents(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = add nsw i64 %4, 1
%6 = tail call i8* @malloc_nofail(i64 %5)
%7 = tail call i32 @strcpy(i8* %6, i8* %0)
%8 = tail call i8* @strrchr(i8* dereferenceable(1) %6, i8 signext 47)
%9 = icmp eq i8* %8, null
br i1 %9, label %31, label %10
10: ; preds = %1
store i8 0, i8* %8
%11 = call i32 @stat(i8* %6, %struct.stat* %2)
%12 = icmp eq i32 %11, 0
br i1 %12, label %31, label %13
13: ; preds = %10
store i8 47, i8* %8
%14 = tail call i8* @strchr(i8* dereferenceable(1) %6, i8 signext 47)
%15 = icmp eq i8* %14, null
br i1 %15, label %31, label %16
16: ; preds = %13, %26
%17 = phi i8* [ %29, %26 ], [ %14, %13 ]
%18 = phi i32 [ %27, %26 ], [ -1, %13 ]
store i8 0, i8* %17
%19 = icmp eq i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = call i32 @stat(i8* %6, %struct.stat* %2)
%22 = icmp eq i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %20, %16
%24 = phi i32 [ %21, %20 ], [ 0, %16 ]
%25 = tail call i32 @mkdir(i8* %6, i32 511)
br label %26
26: ; preds = %23, %20
%27 = phi i32 [ %24, %23 ], [ 0, %20 ]
store i8 47, i8* %17
%28 = getelementptr inbounds i8, i8* %17, i64 1
%29 = tail call i8* @strchr(i8* %28, i8 signext 47)
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %16
31: ; preds = %26, %13, %10, %1
%32 = tail call i32 @free(i8* %6)
ret void
}