#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 /*<<< orphan*/ free(char *);
int mkdir(char *, int /*<<< orphan*/);
char *strcat(char *, char *);
scalar_t__ strlen(char const *);
char *strtok(char *, char *);
char *xmalloc(scalar_t__);
char *xstrdup(char const *);
int mkpath(const char *path, mode_t mode) {
  int retcode, ret;
  char *pathincr, *token, *tmp;
  retcode = 0;
  pathincr = xmalloc(strlen(path) + 1);
  pathincr[0] = '\0';
  if (path[0] == '/')
    pathincr = strcat(pathincr, "/");
  tmp = xstrdup(path);
  token = strtok(tmp, "/");
  pathincr = strcat(pathincr, token);
  retcode = mkdir(pathincr, mode);
  while ((token = strtok(NULL, "/"))) {
    pathincr = strcat(pathincr, "/");
    pathincr = strcat(pathincr, token);
    ret = mkdir(pathincr, mode);
    retcode = !retcode ? ret : retcode;
  }
  free(tmp);
  free(pathincr);
  return retcode;
}


x86 assembly (clang, -O0):
<mkpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1060 <xmalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11ca <mkpath+0x5a>
mov    -0x20(%rbp),%rdi
lea    0xe3f(%rip),%rsi
call   1090 <strcat@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <xstrdup@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xe1e(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1090 <strcat@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <mkdir@plt>
mov    %eax,-0x10(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xdea(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1283 <mkpath+0x113>
mov    -0x20(%rbp),%rdi
lea    0xdcc(%rip),%rsi
call   1090 <strcat@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1090 <strcat@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <mkdir@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    1272 <mkpath+0x102>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    1278 <mkpath+0x108>
mov    -0x10(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    120b <mkpath+0x9b>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkpath>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%rbx
call   1050 <strlen@plt>
lea    0x1(%rax),%rdi
call   1060 <xmalloc@plt>
mov    %rax,%r13
movb   $0x0,(%rax)
cmpb   $0x2f,(%rbx)
jne    11aa <mkpath+0x3a>
mov    %r13,%rdi
call   1050 <strlen@plt>
movw   $0x2f,0x0(%r13,%rax,1)
mov    %rbx,%rdi
call   1070 <xstrdup@plt>
mov    %rax,%r14
lea    0xe44(%rip),%rbx
mov    %rax,%rdi
mov    %rbx,%rsi
call   1080 <strtok@plt>
mov    %r13,%rdi
mov    %rax,%rsi
call   1090 <strcat@plt>
mov    %r13,%rdi
mov    %r15d,%esi
call   1040 <mkdir@plt>
mov    %eax,%ebp
xor    %edi,%edi
mov    %rbx,%rsi
call   1080 <strtok@plt>
test   %rax,%rax
je     123d <mkpath+0xcd>
mov    %rax,%rbx
lea    0xe08(%rip),%r12
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1050 <strlen@plt>
movw   $0x2f,0x0(%r13,%rax,1)
mov    %r13,%rdi
mov    %rbx,%rsi
call   1090 <strcat@plt>
mov    %r13,%rdi
mov    %r15d,%esi
call   1040 <mkdir@plt>
test   %ebp,%ebp
cmove  %eax,%ebp
xor    %edi,%edi
mov    %r12,%rsi
call   1080 <strtok@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1200 <mkpath+0x90>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
define i32 @mkpath(i8* %0, i32 %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = add nsw i64 %3, 1
%5 = tail call i8* @xmalloc(i64 %4)
store i8 0, i8* %5
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 47
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = tail call i64 @strlen(i8* %5)
%10 = getelementptr i8, i8* %5, i64 %9
%11 = bitcast i8* %10 to i16*
store i16 47, i16* %11
br label %12
12: ; preds = %8, %2
%13 = tail call i8* @xstrdup(i8* %0)
%14 = tail call i8* @strtok(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%15 = tail call i8* @strcat(i8* %5, i8* dereferenceable(1) %14)
%16 = tail call i32 @mkdir(i8* %5, i32 %1)
%17 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%18 = icmp eq i8* %17, null
br i1 %18, label %31, label %19
19: ; preds = %12, %19
%20 = phi i8* [ %29, %19 ], [ %17, %12 ]
%21 = phi i32 [ %28, %19 ], [ %16, %12 ]
%22 = tail call i64 @strlen(i8* dereferenceable(1) %5)
%23 = getelementptr i8, i8* %5, i64 %22
%24 = bitcast i8* %23 to i16*
store i16 47, i16* %24
%25 = tail call i8* @strcat(i8* dereferenceable(1) %5, i8* %20)
%26 = tail call i32 @mkdir(i8* %5, i32 %1)
%27 = icmp eq i32 %21, 0
%28 = select i1 %27, i32 %26, i32 %21
%29 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %19
31: ; preds = %19, %12
%32 = phi i32 [ %16, %12 ], [ %28, %19 ]
%33 = tail call i32 @free(i8* %13)
%34 = tail call i32 @free(i8* %5)
ret i32 %32
}