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

/* Variables and functions */
int /*<<< orphan*/ R_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
char *dirname(char *);
int mkdir(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int /*<<< orphan*/ strlen(char const *);
int mkpath(const char *path, mode_t mode) {
  char path_str[strlen(path) + 1];
  char *dir;
  snprintf(path_str, sizeof(path_str), "%s", path);
  dir = dirname(path_str);
  if (access(dir, R_OK) < 0) {
    if (mkpath(dir, mode) == -1)
      return -1;
  }
  if (mkdir(path, mode) == -1)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<mkpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%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,-0x28(%rbp)
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xe55(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x40(%rbp),%rdi
call   1060 <dirname@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%rax
jge    1206 <mkpath+0xa6>
mov    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <mkpath@plt>
cmp    $0xffffffff,%eax
jne    1201 <mkpath+0xa1>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    123c <mkpath+0xdc>
jmp    1206 <mkpath+0xa6>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <mkdir@plt>
cmp    $0xffffffff,%eax
jne    122e <mkpath+0xce>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    123c <mkpath+0xdc>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkpath>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
call   1040 <strlen@plt>
lea    0x1(%rax),%esi
mov    %rsp,%rbx
lea    0xf(%rsi),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe70(%rip),%rdx
mov    %rbx,%rdi
mov    %r15,%rcx
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1060 <dirname@plt>
mov    %rax,%rbx
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <access@plt>
test   %rax,%rax
jns    11cc <mkpath+0x6c>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1070 <mkpath@plt>
cmp    $0xffffffff,%eax
je     11ee <mkpath+0x8e>
mov    %r15,%rdi
mov    %r14d,%esi
call   1030 <mkdir@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
sete   %al
neg    %eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%eax
jmp    11e3 <mkpath+0x83>

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@R_OK = global i32 0
define i32 @mkpath(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = load i8*, i8** %4
%11 = call i32 @strlen(i8* %10)
%12 = add nsw i32 %11, 1
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %6
%15 = alloca i8, i64 %13
store i64 %13, i64* %7
%16 = trunc i64 %13 to i32
%17 = load i8*, i8** %4
%18 = call i32 @snprintf(i8* %15, i32 %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i8* @dirname(i8* %15)
store i8* %19, i8** %8
%20 = load i8*, i8** %8
%21 = load i32, i32* @R_OK
%22 = call i64 @access(i8* %20, i32 %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %2
%25 = load i8*, i8** %8
%26 = load i32, i32* %5
%27 = call i32 @mkpath(i8* %25, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %30
29: ; preds = %24
store i32 -1, i32* %3
store i32 1, i32* %9
br label %38
30: ; preds = %24
br label %31
31: ; preds = %30, %2
%32 = load i8*, i8** %4
%33 = load i32, i32* %5
%34 = call i32 @mkdir(i8* %32, i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %31
store i32 -1, i32* %3
store i32 1, i32* %9
br label %38
37: ; preds = %31
store i32 0, i32* %3
store i32 1, i32* %9
br label %38
38: ; preds = %37, %36, %29
%39 = load i8*, i8** %6
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@R_OK = global i32 0
define i32 @mkpath(i8* %0, i32 %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = add nsw i32 %3, 1
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = call i32 @snprintf(i8* %6, i32 %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%8 = call i8* @dirname(i8* %6)
%9 = load i32, i32* @R_OK
%10 = call i64 @access(i8* %8, i32 %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @mkpath(i8* %8, i32 %1)
%14 = icmp eq i32 %13, -1
br i1 %14, label %19, label %15
15: ; preds = %12, %2
%16 = call i32 @mkdir(i8* %0, i32 %1)
%17 = icmp eq i32 %16, -1
%18 = sext i1 %17 to i32
br label %19
19: ; preds = %15, %12
%20 = phi i32 [ -1, %12 ], [ %18, %15 ]
ret i32 %20
}