#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;
};
typedef int mode_t;

/* Variables and functions */
char *dirname(char *);
int /*<<< orphan*/ free(char *);
scalar_t__ mkdir(char *, int);
scalar_t__ stat(char *, struct stat *);
char *strdup(char *);
int strlen(char *);
int mkdir_recursive(char *path) {
  char *subpath, *fullpath;
  mode_t mode = 0775;
  int ok = 0;
  struct stat fileStat;
  fullpath = strdup(path);
  subpath = dirname(path);
  if (strlen(subpath) > 1)
    ok = mkdir_recursive(subpath);
  if (stat(fullpath, &fileStat) < 0)
    if (mkdir(fullpath, mode) < 0)
      ok = -1;
  free(fullpath);
  return ok;
}


x86 assembly (clang, -O0):
<mkdir_recursive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1fd,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <dirname@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x1,%eax
jle    11c2 <mkdir_recursive+0x52>
mov    -0x10(%rbp),%rdi
call   1090 <mkdir_recursive@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jge    11fb <mkdir_recursive+0x8b>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
jge    11f6 <mkdir_recursive+0x86>
movl   $0xffffffff,-0x20(%rbp)
jmp    11fb <mkdir_recursive+0x8b>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdir_recursive>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1080 <strdup@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1060 <dirname@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <strlen@plt>
xor    %ebp,%ebp
cmp    $0x2,%eax
jl     11a7 <mkdir_recursive+0x37>
mov    %rbx,%rdi
call   1090 <mkdir_recursive@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1070 <stat@plt>
test   %rax,%rax
jns    11d1 <mkdir_recursive+0x61>
mov    %r14,%rdi
mov    $0x1fd,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovs  %eax,%ebp
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
define i32 @mkdir_recursive(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.stat
store i8* %0, i8** %2
store i32 509, i32* %5
store i32 0, i32* %6
%8 = load i8*, i8** %2
%9 = call i8* @strdup(i8* %8)
store i8* %9, i8** %4
%10 = load i8*, i8** %2
%11 = call i8* @dirname(i8* %10)
store i8* %11, i8** %3
%12 = load i8*, i8** %3
%13 = call i32 @strlen(i8* %12)
%14 = icmp sgt i32 %13, 1
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = load i8*, i8** %3
%17 = call i32 @mkdir_recursive(i8* %16)
store i32 %17, i32* %6
br label %18
18: ; preds = %15, %1
%19 = load i8*, i8** %4
%20 = call i64 @stat(i8* %19, %struct.stat* %7)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %18
%23 = load i8*, i8** %4
%24 = load i32, i32* %5
%25 = call i64 @mkdir(i8* %23, i32 %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %22
store i32 -1, i32* %6
br label %28
28: ; preds = %27, %22
br label %29
29: ; preds = %28, %18
%30 = load i8*, i8** %4
%31 = call i32 @free(i8* %30)
%32 = load i32, i32* %6
ret i32 %32
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define i32 @mkdir_recursive(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call i8* @strdup(i8* %0)
%5 = tail call i8* @dirname(i8* %0)
%6 = tail call i32 @strlen(i8* %5)
%7 = icmp sgt i32 %6, 1
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @mkdir_recursive(i8* %5)
br label %10
10: ; preds = %8, %1
%11 = phi i32 [ %9, %8 ], [ 0, %1 ]
%12 = call i64 @stat(i8* %4, %struct.stat* %2)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = call i64 @mkdir(i8* %4, i32 509)
%16 = icmp slt i64 %15, 0
%17 = select i1 %16, i32 -1, i32 %11
br label %18
18: ; preds = %14, %10
%19 = phi i32 [ %11, %10 ], [ %17, %14 ]
%20 = call i32 @free(i8* %4)
ret i32 %19
}