#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 */
int EEXIST;
int ENOTDIR;
int PATH_MAX;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int errno;
scalar_t__ mkdir(char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
char *strtok_r(char *, char *, char **);
int do_mkdir_p(const char *path, int mode) {
  int ret;
  char *str, full[PATH_MAX], tbuf[PATH_MAX];
  full[0] = '\0';
  strcpy(tbuf, path);
  str = tbuf;
  while (1) {
    char *tmp, *seg;
    seg = strtok_r(str, "/", &tmp);
    if (!seg)
      break;
    str = NULL;
    strcat(full, "/");
    strcat(full, seg);
    if (mkdir(full, mode) < 0) {
      struct stat sbuf;
      ret = errno;
      if (ret != EEXIST)
        return ret;
      if (stat(full, &sbuf))
        return -errno;
      if (!S_ISDIR(sbuf.st_mode))
        return -ENOTDIR;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_mkdir_p>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e72(%rip),%rcx
mov    (%rcx),%eax
mov    %eax,%edx
mov    %rsp,%rax
mov    %rax,-0x28(%rbp)
mov    %rdx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rax
sub    %rsi,%rax
mov    %rax,-0x68(%rbp)
mov    %rax,%rsp
mov    %rdx,-0x30(%rbp)
mov    (%rcx),%ecx
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rdi
sub    %rdx,%rdi
mov    %rdi,-0x60(%rbp)
mov    %rdi,%rsp
mov    %rcx,-0x38(%rbp)
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x60(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11d4 <do_mkdir_p+0x74>
mov    -0x20(%rbp),%rdi
lea    0xe21(%rip),%rsi
lea    -0x40(%rbp),%rdx
call   1060 <strtok_r@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    11fc <do_mkdir_p+0x9c>
jmp    12cd <do_mkdir_p+0x16d>
mov    -0x68(%rbp),%rdi
movq   $0x0,-0x20(%rbp)
lea    0xdf1(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x68(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x68(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
jge    12c8 <do_mkdir_p+0x168>
mov    0x2d8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2d83(%rip),%rcx
cmp    (%rcx),%eax
je     1267 <do_mkdir_p+0x107>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    12db <do_mkdir_p+0x17b>
mov    -0x68(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     1298 <do_mkdir_p+0x138>
xor    %eax,%eax
mov    0x2d41(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    12db <do_mkdir_p+0x17b>
mov    -0x50(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    12c3 <do_mkdir_p+0x163>
xor    %eax,%eax
mov    0x2d46(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    12db <do_mkdir_p+0x17b>
jmp    12c8 <do_mkdir_p+0x168>
jmp    11cf <do_mkdir_p+0x6f>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_mkdir_p>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%rsi
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rsp,%r15
sub    %rax,%r15
mov    %r15,%rsp
movb   $0x0,(%rbx)
mov    %r15,%rdi
call   1030 <strcpy@plt>
lea    0xe54(%rip),%rsi
lea    -0x38(%rbp),%rdx
mov    %r15,%rdi
call   1060 <strtok_r@plt>
test   %rax,%rax
je     1259 <do_mkdir_p+0xf9>
mov    %rax,%r12
lea    0xe35(%rip),%r15
lea    -0x38(%rbp),%r13
jmp    11f5 <do_mkdir_p+0x95>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
mov    %r15,%rsi
mov    %r13,%rdx
call   1060 <strtok_r@plt>
mov    %rax,%r12
test   %rax,%rax
je     1259 <do_mkdir_p+0xf9>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <strcat@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1080 <strcat@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
jns    11e0 <do_mkdir_p+0x80>
mov    0x2da6(%rip),%rax
mov    (%rax),%eax
mov    0x2da5(%rip),%rcx
cmp    (%rcx),%eax
jne    125b <do_mkdir_p+0xfb>
mov    %rbx,%rdi
lea    -0x30(%rbp),%rsi
call   1070 <stat@plt>
test   %rax,%rax
jne    126a <do_mkdir_p+0x10a>
mov    -0x30(%rbp),%edi
call   1050 <S_ISDIR@plt>
test   %eax,%eax
jne    11e0 <do_mkdir_p+0x80>
mov    0x2da5(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    125b <do_mkdir_p+0xfb>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
mov    0x2d55(%rip),%rcx
sub    (%rcx),%eax
jmp    125b <do_mkdir_p+0xfb>

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [2 x i8] c"/\00"
@errno = global i32 0
@EEXIST = global i32 0
@ENOTDIR = global i32 0
define i32 @do_mkdir_p(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca %struct.stat
%14 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%15 = load i32, i32* @PATH_MAX
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %8
%18 = alloca i8, i64 %16
store i64 %16, i64* %9
%19 = load i32, i32* @PATH_MAX
%20 = zext i32 %19 to i64
%21 = alloca i8, i64 %20
store i64 %20, i64* %10
%22 = getelementptr inbounds i8, i8* %18, i64 0
store i8 0, i8* %22
%23 = load i8*, i8** %4
%24 = call i32 @strcpy(i8* %21, i8* %23)
store i8* %21, i8** %7
br label %25
25: ; preds = %61, %2
br label %26
26: ; preds = %25
%27 = load i8*, i8** %7
%28 = call i8* @strtok_r(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %11)
store i8* %28, i8** %12
%29 = load i8*, i8** %12
%30 = icmp ne i8* %29, null
br i1 %30, label %32, label %31
31: ; preds = %26
br label %62
32: ; preds = %26
store i8* null, i8** %7
%33 = call i32 @strcat(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%34 = load i8*, i8** %12
%35 = call i32 @strcat(i8* %18, i8* %34)
%36 = load i32, i32* %5
%37 = call i64 @mkdir(i8* %18, i32 %36)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %61
39: ; preds = %32
%40 = load i32, i32* @errno
store i32 %40, i32* %6
%41 = load i32, i32* %6
%42 = load i32, i32* @EEXIST
%43 = icmp ne i32 %41, %42
br i1 %43, label %44, label %46
44: ; preds = %39
%45 = load i32, i32* %6
store i32 %45, i32* %3
store i32 1, i32* %14
br label %63
46: ; preds = %39
%47 = call i64 @stat(i8* %18, %struct.stat* %13)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = load i32, i32* @errno
%51 = sub nsw i32 0, %50
store i32 %51, i32* %3
store i32 1, i32* %14
br label %63
52: ; preds = %46
%53 = getelementptr inbounds %struct.stat, %struct.stat* %13, i32 0, i32 0
%54 = load i32, i32* %53
%55 = call i32 @S_ISDIR(i32 %54)
%56 = icmp ne i32 %55, 0
br i1 %56, label %60, label %57
57: ; preds = %52
%58 = load i32, i32* @ENOTDIR
%59 = sub nsw i32 0, %58
store i32 %59, i32* %3
store i32 1, i32* %14
br label %63
60: ; preds = %52
br label %61
61: ; preds = %60, %32
br label %25
62: ; preds = %31
store i32 0, i32* %3
store i32 1, i32* %14
br label %63
63: ; preds = %62, %57, %49, %44
%64 = load i8*, i8** %8
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [2 x i8] c"/\00"
@errno = global i32 0
@EEXIST = global i32 0
@ENOTDIR = global i32 0
define i32 @do_mkdir_p(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = alloca i8, i64 %6
store i8 0, i8* %7
%9 = call i32 @strcpy(i8* %8, i8* %0)
%10 = bitcast i8** %3 to i8*
%11 = call i8* @strtok_r(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %3)
%12 = icmp eq i8* %11, null
br i1 %12, label %45, label %13
13: ; preds = %2
%14 = bitcast %struct.stat* %4 to i8*
%15 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
br label %16
16: ; preds = %13, %42
%17 = phi i8* [ %11, %13 ], [ %43, %42 ]
%18 = call i32 @strcat(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @strcat(i8* %7, i8* %17)
%20 = call i64 @mkdir(i8* %7, i32 %1)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %42
22: ; preds = %16
%23 = load i32, i32* @errno
%24 = load i32, i32* @EEXIST
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %39
26: ; preds = %22
%27 = call i64 @stat(i8* %7, %struct.stat* %4)
%28 = icmp eq i64 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = load i32, i32* @errno
%31 = sub nsw i32 0, %30
br label %39
32: ; preds = %26
%33 = load i32, i32* %15
%34 = call i32 @S_ISDIR(i32 %33)
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %32
%37 = load i32, i32* @ENOTDIR
%38 = sub nsw i32 0, %37
br label %39
39: ; preds = %22, %36, %29
%40 = phi i32 [ %38, %36 ], [ %31, %29 ], [ %23, %22 ]
br label %46
41: ; preds = %32
br label %42
42: ; preds = %16, %41
%43 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %3)
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %16
45: ; preds = %42, %2
br label %46
46: ; preds = %39, %45
%47 = phi i32 [ 0, %45 ], [ %40, %39 ]
ret i32 %47
}