#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 */

/* Variables and functions */
scalar_t__ EEXIST;
scalar_t__ ENOENT;
scalar_t__ errno;
int mkdir(char *, int);
char *strrchr(char *, char);
int create_directories(char *path) {
  char *lastslash = strrchr(path, '/');
  if (lastslash == NULL)
    return 0;
  *lastslash = '\0';
  int ret = mkdir(path, 0777);
  if (ret == -1 && errno == ENOENT) {
    if ((ret = create_directories(path)) == 0)
      ret = mkdir(path, 0777);
  }
  *lastslash = '/';
  if (ret == -1 && errno == EEXIST)
    ret = 0;
  return ret;
}


x86 assembly (clang, -O0):
<create_directories>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1165 <create_directories+0x35>
movl   $0x0,-0x4(%rbp)
jmp    1204 <create_directories+0xd4>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11cc <create_directories+0x9c>
mov    0x2e4a(%rip),%rax
mov    (%rax),%rax
mov    0x2e30(%rip),%rcx
cmp    (%rcx),%rax
jne    11cc <create_directories+0x9c>
mov    -0x10(%rbp),%rdi
call   1050 <create_directories@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    11c7 <create_directories+0x97>
mov    -0x10(%rbp),%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
mov    %eax,-0x1c(%rbp)
jmp    11cc <create_directories+0x9c>
mov    -0x18(%rbp),%rax
movb   $0x2f,(%rax)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11fe <create_directories+0xce>
mov    0x2df4(%rip),%rax
mov    (%rax),%rax
mov    0x2df2(%rip),%rcx
cmp    (%rcx),%rax
jne    11fe <create_directories+0xce>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_directories>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     1164 <create_directories+0x34>
mov    %rax,%rbx
movb   $0x0,(%rax)
mov    %r14,%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
cmp    $0xffffffff,%eax
je     1168 <create_directories+0x38>
movb   $0x2f,(%rbx)
jmp    11b8 <create_directories+0x88>
xor    %eax,%eax
jmp    11b8 <create_directories+0x88>
mov    0x2e69(%rip),%r15
mov    (%r15),%rax
mov    0x2e4f(%rip),%rcx
cmp    (%rcx),%rax
jne    11a1 <create_directories+0x71>
mov    %r14,%rdi
call   1050 <create_directories@plt>
test   %eax,%eax
jne    1197 <create_directories+0x67>
mov    %r14,%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
movb   $0x2f,(%rbx)
cmp    $0xffffffff,%eax
jne    11b8 <create_directories+0x88>
jmp    11a4 <create_directories+0x74>
movb   $0x2f,(%rbx)
mov    (%r15),%rcx
mov    0x2e32(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
setne  %al
neg    %eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@ENOENT = global i64 0
@EEXIST = global i64 0
define i32 @create_directories(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i8* @strrchr(i8* %6, i8 signext 47)
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 0, i32* %2
br label %40
11: ; preds = %1
%12 = load i8*, i8** %4
store i8 0, i8* %12
%13 = load i8*, i8** %3
%14 = call i32 @mkdir(i8* %13, i32 511)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %29
17: ; preds = %11
%18 = load i64, i64* @errno
%19 = load i64, i64* @ENOENT
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %29
21: ; preds = %17
%22 = load i8*, i8** %3
%23 = call i32 @create_directories(i8* %22)
store i32 %23, i32* %5
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i8*, i8** %3
%27 = call i32 @mkdir(i8* %26, i32 511)
store i32 %27, i32* %5
br label %28
28: ; preds = %25, %21
br label %29
29: ; preds = %28, %17, %11
%30 = load i8*, i8** %4
store i8 47, i8* %30
%31 = load i32, i32* %5
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %38
33: ; preds = %29
%34 = load i64, i64* @errno
%35 = load i64, i64* @EEXIST
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %33
store i32 0, i32* %5
br label %38
38: ; preds = %37, %33, %29
%39 = load i32, i32* %5
store i32 %39, i32* %2
br label %40
40: ; preds = %38, %10
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
@errno = global i64 0
@ENOENT = global i64 0
@EEXIST = global i64 0
define i32 @create_directories(i8* %0) {
%2 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%3 = icmp eq i8* %2, null
br i1 %3, label %26, label %4
4: ; preds = %1
store i8 0, i8* %2
%5 = tail call i32 @mkdir(i8* %0, i32 511)
%6 = icmp eq i32 %5, -1
br i1 %6, label %8, label %7
7: ; preds = %4
store i8 47, i8* %2
br label %26
8: ; preds = %4
%9 = load i64, i64* @errno
%10 = load i64, i64* @ENOENT
%11 = icmp eq i64 %9, %10
br i1 %11, label %13, label %12
12: ; preds = %8
store i8 47, i8* %2
br label %21
13: ; preds = %8
%14 = tail call i32 @create_directories(i8* %0)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @mkdir(i8* %0, i32 511)
br label %18
18: ; preds = %13, %16
%19 = phi i32 [ %17, %16 ], [ %14, %13 ]
store i8 47, i8* %2
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %26
21: ; preds = %12, %18
%22 = load i64, i64* @errno
%23 = load i64, i64* @EEXIST
%24 = icmp ne i64 %22, %23
%25 = sext i1 %24 to i32
br label %26
26: ; preds = %7, %21, %18, %1
%27 = phi i32 [ 0, %1 ], [ %19, %18 ], [ %25, %21 ], [ %5, %7 ]
ret i32 %27
}