#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 */
scalar_t__ EEXIST;
int NAME_MAX;
int PATH_MAX;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ mkdir(char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
char *strdup(char const *);
char *strerror(scalar_t__);
int strlen(char *);
char *strtok(char *, char *);
int bmkdir_p(const char *path_to_dir, int mode) {
  char *path = strdup(path_to_dir);
  char current_path[PATH_MAX];
  char *latest_path_element;
  current_path[0] = '\0';
  latest_path_element = strtok(path, "/");
  while (strlen(current_path) < PATH_MAX - NAME_MAX - 1) {
    if (latest_path_element == NULL) {
      break;
    } else {
      strcat(current_path, "/");
      strcat(current_path, latest_path_element);
      if (mkdir(current_path, mode) == 0) {
      } else {
        if (errno == EEXIST) {
        } else {
          fprintf(stderr, "Unable to create directory %s: %s", current_path,
                  strerror(errno));
          return 0;
        }
      }
      latest_path_element = strtok(NULL, "/");
    }
  }
  free(path);
  if (latest_path_element == NULL) {
    return 1;
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<bmkdir_p>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
mov    %rsp,%rax
mov    %rax,-0x28(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rax
sub    %rdx,%rax
mov    %rax,-0x48(%rbp)
mov    %rax,%rsp
mov    %rcx,-0x30(%rbp)
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
lea    0xe28(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x38(%rbp)
mov    -0x48(%rbp),%rdi
call   1050 <strlen@plt>
mov    0x2dff(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2dbe(%rip),%rdx
sub    (%rdx),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    12d0 <bmkdir_p+0x150>
cmpq   $0x0,-0x38(%rbp)
jne    1217 <bmkdir_p+0x97>
jmp    12d0 <bmkdir_p+0x150>
mov    -0x48(%rbp),%rdi
lea    0xdde(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x48(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x48(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
jne    124f <bmkdir_p+0xcf>
jmp    12b7 <bmkdir_p+0x137>
mov    0x2d72(%rip),%rax
mov    (%rax),%rax
mov    0x2d78(%rip),%rcx
cmp    (%rcx),%rax
jne    126e <bmkdir_p+0xee>
jmp    12b2 <bmkdir_p+0x132>
mov    0x2d5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2d47(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xd68(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1305 <bmkdir_p+0x185>
jmp    12b7 <bmkdir_p+0x137>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd3e(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x38(%rbp)
jmp    11e1 <bmkdir_p+0x61>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x38(%rbp)
jne    12f7 <bmkdir_p+0x177>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1305 <bmkdir_p+0x185>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bmkdir_p>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
call   1090 <strdup@plt>
mov    %rax,%rdi
mov    0x2e50(%rip),%r14
mov    (%r14),%eax
mov    %rsp,%rcx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
mov    %rcx,%r13
sub    %rax,%r13
mov    %r13,%rsp
neg    %rax
movb   $0x0,(%rcx,%rax,1)
lea    0xe3b(%rip),%rsi
mov    %rdi,-0x30(%rbp)
call   1070 <strtok@plt>
mov    %rax,%rbx
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    0x2dd8(%rip),%rcx
mov    (%rcx),%ecx
not    %ecx
add    (%r14),%ecx
test   %rbx,%rbx
sete   %r14b
cmp    %ecx,%eax
jge    1285 <bmkdir_p+0x105>
test   %rbx,%rbx
je     1285 <bmkdir_p+0x105>
lea    0xdfa(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %r12,%rsi
call   1080 <strcat@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
call   1080 <strcat@plt>
mov    %r13,%rdi
mov    %r15d,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
je     124c <bmkdir_p+0xcc>
mov    0x2d8b(%rip),%rax
mov    (%rax),%rdi
mov    0x2d91(%rip),%rax
cmp    (%rax),%rdi
jne    12a1 <bmkdir_p+0x121>
xor    %edi,%edi
mov    %r12,%rsi
call   1070 <strtok@plt>
mov    %rax,%rbx
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    0x2d50(%rip),%rcx
mov    (%rcx),%ecx
not    %ecx
mov    0x2d7d(%rip),%rdx
add    (%rdx),%ecx
test   %rbx,%rbx
sete   %r14b
cmp    %ecx,%eax
jge    1285 <bmkdir_p+0x105>
test   %rbx,%rbx
jne    1210 <bmkdir_p+0x90>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movzbl %r14b,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d28(%rip),%rax
mov    (%rax),%ebx
call   10a0 <strerror@plt>
lea    0xd4c(%rip),%rsi
mov    %ebx,%edi
mov    %r13,%rdx
mov    %rax,%rcx
call   1060 <fprintf@plt>
xor    %eax,%eax
jmp    1292 <bmkdir_p+0x112>

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

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [2 x i8] c"/\00"
@NAME_MAX = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@stderr = global i32 0
@.str.1 = [34 x i8] c"Unable to create directory %s: %s\00"
define i32 @bmkdir_p(i8* nocapture %0, i32 %1) {
%3 = tail call i8* @strdup(i8* %0)
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
store i8 0, i8* %6
%7 = tail call i8* @strtok(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @strlen(i8* %6)
%9 = load i32, i32* @PATH_MAX
%10 = load i32, i32* @NAME_MAX
%11 = xor i32 %10, -1
%12 = add i32 %9, %11
%13 = icmp sge i32 %8, %12
%14 = icmp eq i8* %7, null
%15 = select i1 %13, i1 true, i1 %14
br i1 %15, label %40, label %16
16: ; preds = %2, %30
%17 = phi i8* [ %31, %30 ], [ %7, %2 ]
%18 = call i32 @strcat(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @strcat(i8* %6, i8* %17)
%20 = call i64 @mkdir(i8* %6, i32 %1)
%21 = icmp eq i64 %20, 0
br i1 %21, label %30, label %22
22: ; preds = %16
%23 = load i64, i64* @errno
%24 = load i64, i64* @EEXIST
%25 = icmp eq i64 %23, %24
br i1 %25, label %30, label %26
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = call i8* @strerror(i64 %23)
%29 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %6, i8* %28)
br label %44
30: ; preds = %22, %16
%31 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%32 = call i32 @strlen(i8* %6)
%33 = load i32, i32* @PATH_MAX
%34 = load i32, i32* @NAME_MAX
%35 = xor i32 %34, -1
%36 = add i32 %33, %35
%37 = icmp sge i32 %32, %36
%38 = icmp eq i8* %31, null
%39 = select i1 %37, i1 true, i1 %38
br i1 %39, label %40, label %16
40: ; preds = %30, %2
%41 = phi i1 [ %14, %2 ], [ %38, %30 ]
%42 = call i32 @free(i8* %3)
%43 = zext i1 %41 to i32
br label %44
44: ; preds = %40, %26
%45 = phi i32 [ 0, %26 ], [ %43, %40 ]
ret i32 %45
}