#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*/ mode_t;

/* Variables and functions */
int do_mkdir(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *strchr(char *, char);
char *strdup(char const *);
int mkpath2(const char *path, mode_t mode) {
  char *sp;
  char *copypath = strdup(path);
  int status = 0;
  char *pp = copypath;
  while (status == 0 && (sp = strchr(pp, '/')) != 0) {
    if (sp != pp) {
      *sp = '\0';
      status = do_mkdir(copypath, mode);
      *sp = '/';
    }
    pp = sp + 1;
  }
  if (status == 0)
    status = do_mkdir(path, mode);
  free(copypath);
  return status;
}


x86 assembly (clang, -O0):
<mkpath2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x24(%rbp)
mov    %al,-0x31(%rbp)
jne    1196 <mkpath2+0x56>
mov    -0x30(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    11a6 <mkpath2+0x66>
jmp    11e2 <mkpath2+0xa2>
mov    -0x18(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     11d1 <mkpath2+0x91>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <do_mkdir@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    116b <mkpath2+0x2b>
cmpl   $0x0,-0x24(%rbp)
jne    11fb <mkpath2+0xbb>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <do_mkdir@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkpath2>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
call   1060 <strdup@plt>
mov    %rax,%r12
mov    %rax,%rbx
jmp    1182 <mkpath2+0x42>
xchg   %ax,%ax
movb   $0x0,0x0(%rbp)
mov    %r12,%rdi
mov    %r15d,%esi
call   1040 <do_mkdir@plt>
mov    %eax,%r13d
movb   $0x2f,0x0(%rbp)
add    $0x1,%rbp
mov    %rbp,%rbx
test   %r13d,%r13d
jne    11af <mkpath2+0x6f>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11a1 <mkpath2+0x61>
mov    %rax,%rbp
cmp    %rbx,%rax
jne    1160 <mkpath2+0x20>
xor    %r13d,%r13d
jmp    1176 <mkpath2+0x36>
mov    %r14,%rdi
mov    %r15d,%esi
call   1040 <do_mkdir@plt>
mov    %eax,%r13d
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @mkpath2(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
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** %6
store i32 0, i32* %7
%11 = load i8*, i8** %6
store i8* %11, i8** %8
br label %12
12: ; preds = %31, %2
%13 = load i32, i32* %7
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i8*, i8** %8
%17 = call i8* @strchr(i8* %16, i8 signext 47)
store i8* %17, i8** %5
%18 = icmp ne i8* %17, null
br label %19
19: ; preds = %15, %12
%20 = phi i1 [ false, %12 ], [ %18, %15 ]
br i1 %20, label %21, label %34
21: ; preds = %19
%22 = load i8*, i8** %5
%23 = load i8*, i8** %8
%24 = icmp ne i8* %22, %23
br i1 %24, label %25, label %31
25: ; preds = %21
%26 = load i8*, i8** %5
store i8 0, i8* %26
%27 = load i8*, i8** %6
%28 = load i32, i32* %4
%29 = call i32 @do_mkdir(i8* %27, i32 %28)
store i32 %29, i32* %7
%30 = load i8*, i8** %5
store i8 47, i8* %30
br label %31
31: ; preds = %25, %21
%32 = load i8*, i8** %5
%33 = getelementptr inbounds i8, i8* %32, i64 1
store i8* %33, i8** %8
br label %12
34: ; preds = %19
%35 = load i32, i32* %7
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %34
%38 = load i8*, i8** %3
%39 = load i32, i32* %4
%40 = call i32 @do_mkdir(i8* %38, i32 %39)
store i32 %40, i32* %7
br label %41
41: ; preds = %37, %34
%42 = load i8*, i8** %6
%43 = call i32 @free(i8* %42)
%44 = load i32, i32* %7
ret i32 %44
}

LLVM IR (-O2):
define i32 @mkpath2(i8* %0, i32 %1) {
%3 = tail call i8* @strdup(i8* %0)
br label %4
4: ; preds = %2, %12
%5 = phi i8* [ %3, %2 ], [ %14, %12 ]
%6 = tail call i8* @strchr(i8* dereferenceable(1) %5, i8 signext 47)
%7 = icmp eq i8* %6, null
br i1 %7, label %16, label %8
8: ; preds = %4
%9 = icmp eq i8* %6, %5
br i1 %9, label %12, label %10
10: ; preds = %8
store i8 0, i8* %6
%11 = tail call i32 @do_mkdir(i8* %3, i32 %1)
store i8 47, i8* %6
br label %12
12: ; preds = %10, %8
%13 = phi i32 [ %11, %10 ], [ 0, %8 ]
%14 = getelementptr inbounds i8, i8* %6, i64 1
%15 = icmp eq i32 %13, 0
br i1 %15, label %4, label %18
16: ; preds = %4
%17 = tail call i32 @do_mkdir(i8* %0, i32 %1)
br label %18
18: ; preds = %12, %16
%19 = phi i32 [ %17, %16 ], [ %13, %12 ]
%20 = tail call i32 @free(i8* %3)
ret i32 %19
}