#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 */
int /*<<< orphan*/ E2BIG;
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *strtok(char *, char *);
int utils_mkdir_safe(char const *, int);
int utils_mkdir_p(const char *path, int mask) {
  int res;
  char *ptok;
  char pt[256];
  char bp[256];
  if (!path || strlen(path) == 0) {
    errno = EINVAL;
    return -1;
  }
  if (strlen(path) > 256) {
    errno = E2BIG;
    return -1;
  }
  res = 0;
  pt[0] = bp[0] = '\0';
  strcpy(bp, path);
  ptok = strtok(bp, "/");
  if (ptok == NULL)
    return utils_mkdir_safe(path, mask);
  do {
    if (ptok != bp)
      strcat(pt, "/");
    strcat(pt, ptok);
    res = utils_mkdir_safe(pt, mask);
    if (res != 0)
      return res;
  } while ((ptok = strtok(NULL, "/")) != NULL);
  return 0;
}


x86 assembly (clang, -O0):
<utils_mkdir_p>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     117f <utils_mkdir_p+0x2f>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%eax
jne    119d <utils_mkdir_p+0x4d>
mov    0x2e4a(%rip),%rax
mov    (%rax),%ecx
mov    0x2e49(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12b7 <utils_mkdir_p+0x167>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x100,%eax
jle    11cf <utils_mkdir_p+0x7f>
mov    0x2e30(%rip),%rax
mov    (%rax),%ecx
mov    0x2e17(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12b7 <utils_mkdir_p+0x167>
movl   $0x0,-0x18(%rbp)
movb   $0x0,-0x220(%rbp)
movb   $0x0,-0x120(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x220(%rbp),%rdi
lea    0xdfe(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    122a <utils_mkdir_p+0xda>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <utils_mkdir_safe@plt>
mov    %eax,-0x4(%rbp)
jmp    12b7 <utils_mkdir_p+0x167>
jmp    122f <utils_mkdir_p+0xdf>
mov    -0x20(%rbp),%rax
lea    -0x220(%rbp),%rcx
cmp    %rcx,%rax
je     1256 <utils_mkdir_p+0x106>
lea    -0x120(%rbp),%rdi
lea    0xdaf(%rip),%rsi
call   1070 <strcat@plt>
lea    -0x120(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <strcat@plt>
lea    -0x120(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <utils_mkdir_safe@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     128d <utils_mkdir_p+0x13d>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b7 <utils_mkdir_p+0x167>
jmp    1292 <utils_mkdir_p+0x142>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd63(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    122f <utils_mkdir_p+0xdf>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utils_mkdir_p>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
test   %rdi,%rdi
je     118d <utils_mkdir_p+0x3d>
mov    %esi,%r12d
mov    %rdi,%rbp
call   1040 <strlen@plt>
test   %eax,%eax
je     118d <utils_mkdir_p+0x3d>
mov    %rbp,%rdi
call   1040 <strlen@plt>
cmp    $0x101,%eax
jl     11b8 <utils_mkdir_p+0x68>
mov    0x2e5d(%rip),%rax
jmp    1194 <utils_mkdir_p+0x44>
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    0x2e3b(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
movb   $0x0,(%rsp)
movb   $0x0,0x100(%rsp)
mov    %rsp,%r14
mov    %r14,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
lea    0xe27(%rip),%rsi
mov    %r14,%rdi
call   1060 <strtok@plt>
test   %rax,%rax
je     1243 <utils_mkdir_p+0xf3>
mov    %rax,%rbx
lea    0x100(%rsp),%r13
lea    0xe08(%rip),%r15
nopl   0x0(%rax,%rax,1)
cmp    %r14,%rbx
je     1210 <utils_mkdir_p+0xc0>
mov    %r13,%rdi
mov    %r15,%rsi
call   1070 <strcat@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
call   1070 <strcat@plt>
mov    %r13,%rdi
mov    %r12d,%esi
call   1050 <utils_mkdir_safe@plt>
test   %eax,%eax
jne    124e <utils_mkdir_p+0xfe>
xor    %ebp,%ebp
xor    %edi,%edi
mov    %r15,%rsi
call   1060 <strtok@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1200 <utils_mkdir_p+0xb0>
jmp    11a4 <utils_mkdir_p+0x54>
mov    %rbp,%rdi
mov    %r12d,%esi
call   1050 <utils_mkdir_safe@plt>
mov    %eax,%ebp
jmp    11a4 <utils_mkdir_p+0x54>

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@E2BIG = global i32 0
@.str = [2 x i8] c"/\00"
define i32 @utils_mkdir_p(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca [256 x i8]
%9 = alloca [256 x i8]
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i32 @strlen(i8* %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12, %2
%17 = load i32, i32* @EINVAL
store i32 %17, i32* @errno
store i32 -1, i32* %3
br label %62
18: ; preds = %12
%19 = load i8*, i8** %4
%20 = call i32 @strlen(i8* %19)
%21 = icmp sgt i32 %20, 256
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = load i32, i32* @E2BIG
store i32 %23, i32* @errno
store i32 -1, i32* %3
br label %62
24: ; preds = %18
store i32 0, i32* %6
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
store i8 0, i8* %25
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
store i8 0, i8* %26
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%28 = load i8*, i8** %4
%29 = call i32 @strcpy(i8* %27, i8* %28)
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%31 = call i8* @strtok(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %31, i8** %7
%32 = load i8*, i8** %7
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %38
34: ; preds = %24
%35 = load i8*, i8** %4
%36 = load i32, i32* %5
%37 = call i32 @utils_mkdir_safe(i8* %35, i32 %36)
store i32 %37, i32* %3
br label %62
38: ; preds = %24
br label %39
39: ; preds = %58, %38
%40 = load i8*, i8** %7
%41 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%42 = icmp ne i8* %40, %41
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%45 = call i32 @strcat(i8* %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %46
46: ; preds = %43, %39
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%48 = load i8*, i8** %7
%49 = call i32 @strcat(i8* %47, i8* %48)
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%51 = load i32, i32* %5
%52 = call i32 @utils_mkdir_safe(i8* %50, i32 %51)
store i32 %52, i32* %6
%53 = load i32, i32* %6
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %46
%56 = load i32, i32* %6
store i32 %56, i32* %3
br label %62
57: ; preds = %46
br label %58
58: ; preds = %57
%59 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %59, i8** %7
%60 = icmp ne i8* %59, null
br i1 %60, label %39, label %61
61: ; preds = %58
store i32 0, i32* %3
br label %62
62: ; preds = %61, %55, %34, %22, %16
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@E2BIG = global i32 0
@.str = [2 x i8] c"/\00"
define i32 @utils_mkdir_p(i8* %0, i32 %1) {
%3 = alloca [256 x i8]
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = icmp eq i8* %0, null
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = tail call i32 @strlen(i8* %0)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8, %2
%12 = load i32, i32* @EINVAL
store i32 %12, i32* @errno
br label %36
13: ; preds = %8
%14 = tail call i32 @strlen(i8* %0)
%15 = icmp sgt i32 %14, 256
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = load i32, i32* @E2BIG
store i32 %17, i32* @errno
br label %36
18: ; preds = %13
store i8 0, i8* %6
store i8 0, i8* %5
%19 = call i32 @strcpy(i8* %6, i8* %0)
%20 = call i8* @strtok(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @utils_mkdir_safe(i8* %0, i32 %1)
br label %36
24: ; preds = %18, %33
%25 = phi i8* [ %34, %33 ], [ %20, %18 ]
%26 = icmp eq i8* %25, %6
br i1 %26, label %29, label %27
27: ; preds = %24
%28 = call i32 @strcat(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %27, %24
%30 = call i32 @strcat(i8* %5, i8* %25)
%31 = call i32 @utils_mkdir_safe(i8* %5, i32 %1)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %24
36: ; preds = %33, %29, %22, %16, %11
%37 = phi i32 [ -1, %11 ], [ -1, %16 ], [ %23, %22 ], [ 0, %33 ], [ %31, %29 ]
ret i32 %37
}