#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 /*<<< orphan*/ st_mode;
};

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
scalar_t__ mkdir(char *, int);
scalar_t__ stat(char *, struct stat *);
char *strdup(char *);
char *strrchr(char *, char);
extern int tcmkpath(char *path, int mode) {
  char *p = strdup(path);
  char *e = strrchr(p, '/');
  struct stat st;
  int ret = -1;
  if (e > p) {
    *e = 0;
    if (tcmkpath(p, mode))
      goto end;
  }
  if (stat(path, &st)) {
    if (errno != ENOENT)
      goto end;
    if (mkdir(path, mode))
      goto end;
  } else if (!S_ISDIR(st.st_mode)) {
    goto end;
  }
  ret = 0;
end:
  free(p);
  return ret;
}


x86 assembly (clang, -O0):
<tcmkpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x20(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    11d9 <tcmkpath+0x69>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1070 <tcmkpath@plt>
cmp    $0x0,%eax
je     11d4 <tcmkpath+0x64>
jmp    1251 <tcmkpath+0xe1>
jmp    11d9 <tcmkpath+0x69>
mov    -0x8(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     122f <tcmkpath+0xbf>
mov    0x2de9(%rip),%rax
mov    (%rax),%rax
mov    0x2dcf(%rip),%rcx
cmp    (%rcx),%rax
je     120f <tcmkpath+0x9f>
jmp    1251 <tcmkpath+0xe1>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
je     122a <tcmkpath+0xba>
jmp    1251 <tcmkpath+0xe1>
jmp    124a <tcmkpath+0xda>
mov    -0x28(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1245 <tcmkpath+0xd5>
jmp    1251 <tcmkpath+0xe1>
jmp    124a <tcmkpath+0xda>
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcmkpath>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
call   1090 <strdup@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
cmp    %rbx,%rax
jbe    11ae <tcmkpath+0x3e>
movb   $0x0,(%rax)
mov    %rbx,%rdi
mov    %r14d,%esi
call   1070 <tcmkpath@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
jne    1200 <tcmkpath+0x90>
mov    %rsp,%rsi
mov    %r15,%rdi
call   1080 <stat@plt>
test   %rax,%rax
je     11eb <tcmkpath+0x7b>
mov    0x2e1b(%rip),%rax
mov    (%rax),%rax
mov    0x2e01(%rip),%rcx
mov    $0xffffffff,%ebp
cmp    (%rcx),%rax
jne    1200 <tcmkpath+0x90>
mov    %r15,%rdi
mov    %r14d,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
jne    1200 <tcmkpath+0x90>
jmp    11f7 <tcmkpath+0x87>
mov    (%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %eax,%eax
je     11fb <tcmkpath+0x8b>
xor    %ebp,%ebp
jmp    1200 <tcmkpath+0x90>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
define i32 @tcmkpath(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.stat
%8 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = load i8*, i8** %3
%10 = call i8* @strdup(i8* %9)
store i8* %10, i8** %5
%11 = load i8*, i8** %5
%12 = call i8* @strrchr(i8* %11, i8 signext 47)
store i8* %12, i8** %6
store i32 -1, i32* %8
%13 = load i8*, i8** %6
%14 = load i8*, i8** %5
%15 = icmp ugt i8* %13, %14
br i1 %15, label %16, label %24
16: ; preds = %2
%17 = load i8*, i8** %6
store i8 0, i8* %17
%18 = load i8*, i8** %5
%19 = load i32, i32* %4
%20 = call i32 @tcmkpath(i8* %18, i32 %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %16
br label %48
23: ; preds = %16
br label %24
24: ; preds = %23, %2
%25 = load i8*, i8** %3
%26 = call i64 @stat(i8* %25, %struct.stat* %7)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %40
28: ; preds = %24
%29 = load i64, i64* @errno
%30 = load i64, i64* @ENOENT
%31 = icmp ne i64 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %28
br label %48
33: ; preds = %28
%34 = load i8*, i8** %3
%35 = load i32, i32* %4
%36 = call i64 @mkdir(i8* %34, i32 %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %33
br label %48
39: ; preds = %33
br label %47
40: ; preds = %24
%41 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @S_ISDIR(i32 %42)
%44 = icmp ne i32 %43, 0
br i1 %44, label %46, label %45
45: ; preds = %40
br label %48
46: ; preds = %40
br label %47
47: ; preds = %46, %39
store i32 0, i32* %8
br label %48
48: ; preds = %47, %45, %38, %32, %22
%49 = load i8*, i8** %5
%50 = call i32 @free(i8* %49)
%51 = load i32, i32* %8
ret i32 %51
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
define i32 @tcmkpath(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = tail call i8* @strdup(i8* %0)
%5 = tail call i8* @strrchr(i8* dereferenceable(1) %4, i8 signext 47)
%6 = bitcast %struct.stat* %3 to i8*
%7 = icmp ugt i8* %5, %4
br i1 %7, label %8, label %11
8: ; preds = %2
store i8 0, i8* %5
%9 = tail call i32 @tcmkpath(i8* %4, i32 %1)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %27
11: ; preds = %8, %2
%12 = call i64 @stat(i8* %0, %struct.stat* %3)
%13 = icmp eq i64 %12, 0
br i1 %13, label %21, label %14
14: ; preds = %11
%15 = load i64, i64* @errno
%16 = load i64, i64* @ENOENT
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %27
18: ; preds = %14
%19 = call i64 @mkdir(i8* %0, i32 %1)
%20 = icmp eq i64 %19, 0
br i1 %20, label %26, label %27
21: ; preds = %11
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @S_ISDIR(i32 %23)
%25 = icmp eq i32 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %21, %18
br label %27
27: ; preds = %21, %18, %14, %8, %26
%28 = phi i32 [ -1, %8 ], [ -1, %14 ], [ -1, %18 ], [ 0, %26 ], [ -1, %21 ]
%29 = call i32 @free(i8* %4)
ret i32 %28
}