#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 *malloc(scalar_t__);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, scalar_t__);
char *strchr(char *, char);
scalar_t__ strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, scalar_t__);
int mkpath(const char *path, mode_t mode) {
  char *pp;
  char *sp;
  int status;
  char *copypath;
  if (path == NULL)
    return 0;
  copypath = malloc(strlen(path) + 1);
  memset(copypath, 0, strlen(path) + 1);
  strncpy(copypath, path, strlen(path));
  status = 0;
  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):
<mkpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1196 <mkpath+0x26>
movl   $0x0,-0x4(%rbp)
jmp    12a9 <mkpath+0x139>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    %rax,%rdx
add    $0x1,%rdx
xor    %esi,%esi
call   1080 <memset@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %rax,%rdx
call   1040 <strncpy@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x2c(%rbp)
mov    %al,-0x51(%rbp)
jne    1235 <mkpath+0xc5>
mov    -0x20(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
setne  %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
test   $0x1,%al
jne    1245 <mkpath+0xd5>
jmp    1281 <mkpath+0x111>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     1270 <mkpath+0x100>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <do_mkdir@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    120a <mkpath+0x9a>
cmpl   $0x0,-0x2c(%rbp)
jne    129a <mkpath+0x12a>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <do_mkdir@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkpath>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     120f <mkpath+0x9f>
mov    %esi,%r15d
mov    %rdi,%r14
call   1060 <strlen@plt>
lea    0x1(%rax),%rbx
mov    %rbx,%rdi
call   1090 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1080 <memset@plt>
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1040 <strncpy@plt>
mov    %r12,%rbp
jmp    11f0 <mkpath+0x80>
cs nopw 0x0(%rax,%rax,1)
movb   $0x0,(%rbx)
mov    %r12,%rdi
mov    %r15d,%esi
call   1050 <do_mkdir@plt>
mov    %eax,%r13d
movb   $0x2f,(%rbx)
add    $0x1,%rbx
mov    %rbx,%rbp
test   %r13d,%r13d
jne    1222 <mkpath+0xb2>
mov    %rbp,%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     1214 <mkpath+0xa4>
mov    %rax,%rbx
cmp    %rbp,%rax
jne    11d0 <mkpath+0x60>
xor    %r13d,%r13d
jmp    11e4 <mkpath+0x74>
xor    %r13d,%r13d
jmp    122a <mkpath+0xba>
mov    %r14,%rdi
mov    %r15d,%esi
call   1050 <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 @mkpath(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = load i8*, i8** %4
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 0, i32* %3
br label %62
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i64 @strlen(i8* %14)
%16 = add nsw i64 %15, 1
%17 = call i8* @malloc(i64 %16)
store i8* %17, i8** %9
%18 = load i8*, i8** %9
%19 = load i8*, i8** %4
%20 = call i64 @strlen(i8* %19)
%21 = add nsw i64 %20, 1
%22 = call i32 @memset(i8* %18, i32 0, i64 %21)
%23 = load i8*, i8** %9
%24 = load i8*, i8** %4
%25 = load i8*, i8** %4
%26 = call i64 @strlen(i8* %25)
%27 = call i32 @strncpy(i8* %23, i8* %24, i64 %26)
store i32 0, i32* %8
%28 = load i8*, i8** %9
store i8* %28, i8** %6
br label %29
29: ; preds = %48, %13
%30 = load i32, i32* %8
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = load i8*, i8** %6
%34 = call i8* @strchr(i8* %33, i8 signext 47)
store i8* %34, i8** %7
%35 = icmp ne i8* %34, null
br label %36
36: ; preds = %32, %29
%37 = phi i1 [ false, %29 ], [ %35, %32 ]
br i1 %37, label %38, label %51
38: ; preds = %36
%39 = load i8*, i8** %7
%40 = load i8*, i8** %6
%41 = icmp ne i8* %39, %40
br i1 %41, label %42, label %48
42: ; preds = %38
%43 = load i8*, i8** %7
store i8 0, i8* %43
%44 = load i8*, i8** %9
%45 = load i32, i32* %5
%46 = call i32 @do_mkdir(i8* %44, i32 %45)
store i32 %46, i32* %8
%47 = load i8*, i8** %7
store i8 47, i8* %47
br label %48
48: ; preds = %42, %38
%49 = load i8*, i8** %7
%50 = getelementptr inbounds i8, i8* %49, i64 1
store i8* %50, i8** %6
br label %29
51: ; preds = %36
%52 = load i32, i32* %8
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %51
%55 = load i8*, i8** %4
%56 = load i32, i32* %5
%57 = call i32 @do_mkdir(i8* %55, i32 %56)
store i32 %57, i32* %8
br label %58
58: ; preds = %54, %51
%59 = load i8*, i8** %9
%60 = call i32 @free(i8* %59)
%61 = load i32, i32* %8
store i32 %61, i32* %3
br label %62
62: ; preds = %58, %12
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
define i32 @mkpath(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %28, label %4
4: ; preds = %2
%5 = tail call i64 @strlen(i8* %0)
%6 = add nsw i64 %5, 1
%7 = tail call i8* @malloc(i64 %6)
%8 = tail call i32 @memset(i8* %7, i32 0, i64 %6)
%9 = tail call i64 @strlen(i8* %0)
%10 = tail call i32 @strncpy(i8* %7, i8* %0, i64 %9)
br label %11
11: ; preds = %4, %19
%12 = phi i8* [ %7, %4 ], [ %21, %19 ]
%13 = tail call i8* @strchr(i8* dereferenceable(1) %12, i8 signext 47)
%14 = icmp eq i8* %13, null
br i1 %14, label %23, label %15
15: ; preds = %11
%16 = icmp eq i8* %13, %12
br i1 %16, label %19, label %17
17: ; preds = %15
store i8 0, i8* %13
%18 = tail call i32 @do_mkdir(i8* %7, i32 %1)
store i8 47, i8* %13
br label %19
19: ; preds = %17, %15
%20 = phi i32 [ %18, %17 ], [ 0, %15 ]
%21 = getelementptr inbounds i8, i8* %13, i64 1
%22 = icmp eq i32 %20, 0
br i1 %22, label %11, label %25
23: ; preds = %11
%24 = tail call i32 @do_mkdir(i8* %0, i32 %1)
br label %25
25: ; preds = %19, %23
%26 = phi i32 [ %24, %23 ], [ %20, %19 ]
%27 = tail call i32 @free(i8* %7)
br label %28
28: ; preds = %2, %25
%29 = phi i32 [ %26, %25 ], [ 0, %2 ]
ret i32 %29
}