#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 PATH_MAX;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ errno;
scalar_t__ is_dir(char const *);
int mkdir(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, ...);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
char *strerror(int /*<<< orphan*/);
char *strtok(char *, char *);
int create_dir(const char *dirname) {
  int ret;
  char local_dirname[PATH_MAX];
  char sub_dirname[PATH_MAX];
  char *pstr = NULL;
  int first_loop = 1;
  if (dirname == NULL)
    return -1;
  if (is_dir(dirname))
    return 0;
  strcpy(local_dirname, dirname);
  pstr = strtok(local_dirname, "/");
  while (pstr != NULL) {
    if (strcmp(pstr, ".") == 0) {
      strcpy(sub_dirname, pstr);
    } else {
      sprintf(sub_dirname, "%s/%s", sub_dirname, pstr);
    }
    if (*dirname == '/' && first_loop) {
      sprintf(sub_dirname, "/%s", pstr);
      first_loop = 0;
    }
    pstr = strtok(NULL, "/");
    if (is_dir(sub_dirname))
      continue;
    ret = mkdir(sub_dirname, S_IRWXU);
    if (ret != 0) {
      printf("Unable to create directory %s : %s\n", local_dirname,
             strerror(errno));
      return ret;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<create_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
mov    %rdx,-0x20(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x50(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x28(%rbp)
mov    (%rax),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x1,-0x3c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1203 <create_dir+0x83>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    1362 <create_dir+0x1e2>
mov    -0x10(%rbp),%rdi
call   1060 <is_dir@plt>
cmp    $0x0,%rax
je     1229 <create_dir+0xa9>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    1362 <create_dir+0x1e2>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x50(%rbp),%rdi
lea    0xdbf(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1354 <create_dir+0x1d4>
mov    -0x38(%rbp),%rdi
lea    0xda2(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1281 <create_dir+0x101>
mov    -0x48(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    129a <create_dir+0x11a>
mov    -0x48(%rbp),%rdx
mov    -0x38(%rbp),%rcx
lea    0xd74(%rip),%rsi
mov    %rdx,%rdi
mov    $0x0,%al
call   1090 <sprintf@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    12d1 <create_dir+0x151>
cmpl   $0x0,-0x3c(%rbp)
je     12d1 <create_dir+0x151>
mov    -0x48(%rbp),%rdi
mov    -0x38(%rbp),%rdx
lea    0xd43(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
movl   $0x0,-0x3c(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd24(%rip),%rsi
call   1080 <strtok@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,-0x38(%rbp)
call   1060 <is_dir@plt>
cmp    $0x0,%rax
je     12fd <create_dir+0x17d>
jmp    124a <create_dir+0xca>
mov    -0x48(%rbp),%rdi
mov    0x2cd8(%rip),%rax
mov    (%rax),%esi
call   1040 <mkdir@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     134f <create_dir+0x1cf>
mov    0x2cad(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x50(%rbp),%rsi
mov    %rax,%rdx
lea    0xcd2(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    1362 <create_dir+0x1e2>
jmp    124a <create_dir+0xca>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_dir>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    %rsp,%rbx
sub    %rax,%rbx
mov    %rbx,%rsp
test   %rdi,%rdi
je     12cb <create_dir+0x14b>
mov    %rdi,%r15
call   1060 <is_dir@plt>
xor    %r13d,%r13d
test   %rax,%rax
jne    12d6 <create_dir+0x156>
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
lea    0xe20(%rip),%rsi
mov    %r14,%rdi
call   1080 <strtok@plt>
test   %rax,%rax
je     12d6 <create_dir+0x156>
mov    %rax,%r12
mov    %r14,-0x30(%rbp)
mov    $0x1,%r14d
jmp    1209 <create_dir+0x89>
test   %r12,%r12
je     12d3 <create_dir+0x153>
mov    %r12,%rdi
lea    0xdef(%rip),%rsi
call   1070 <strcmp@plt>
mov    %rbx,%rdi
test   %rax,%rax
je     1240 <create_dir+0xc0>
lea    0xddd(%rip),%rsi
mov    %rbx,%rdx
mov    %r12,%rcx
xor    %eax,%eax
call   1090 <sprintf@plt>
cmpb   $0x2f,(%r15)
je     124e <create_dir+0xce>
jmp    126a <create_dir+0xea>
nopl   0x0(%rax)
mov    %r12,%rsi
call   1030 <strcpy@plt>
cmpb   $0x2f,(%r15)
jne    126a <create_dir+0xea>
test   %r14d,%r14d
je     126a <create_dir+0xea>
xor    %r14d,%r14d
mov    %rbx,%rdi
lea    0xda6(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1090 <sprintf@plt>
xor    %edi,%edi
lea    0xd8d(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1060 <is_dir@plt>
test   %rax,%rax
jne    1200 <create_dir+0x80>
mov    0x2d4d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1040 <mkdir@plt>
test   %eax,%eax
je     1200 <create_dir+0x80>
mov    %eax,%r13d
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd4d(%rip),%rdi
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
call   1050 <printf@plt>
jmp    12d6 <create_dir+0x156>
mov    $0xffffffff,%r13d
jmp    12d6 <create_dir+0x156>
xor    %r13d,%r13d
mov    %r13d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [4 x i8] c"/%s\00"
@S_IRWXU = global i32 0
@.str.4 = [36 x i8] c"Unable to create directory %s : %s\0A\00"
@errno = global i32 0
define i32 @create_dir(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %5
%14 = alloca i8, i64 %12
store i64 %12, i64* %6
%15 = load i32, i32* @PATH_MAX
%16 = zext i32 %15 to i64
%17 = alloca i8, i64 %16
store i64 %16, i64* %7
store i8* null, i8** %8
store i32 1, i32* %9
%18 = load i8*, i8** %3
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %1
store i32 -1, i32* %2
store i32 1, i32* %10
br label %71
21: ; preds = %1
%22 = load i8*, i8** %3
%23 = call i64 @is_dir(i8* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 0, i32* %2
store i32 1, i32* %10
br label %71
26: ; preds = %21
%27 = load i8*, i8** %3
%28 = call i32 @strcpy(i8* %14, i8* %27)
%29 = call i8* @strtok(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %29, i8** %8
br label %30
30: ; preds = %69, %58, %26
%31 = load i8*, i8** %8
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %70
33: ; preds = %30
%34 = load i8*, i8** %8
%35 = call i64 @strcmp(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = load i8*, i8** %8
%39 = call i32 @strcpy(i8* %17, i8* %38)
br label %43
40: ; preds = %33
%41 = load i8*, i8** %8
%42 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %17, i8* %41)
br label %43
43: ; preds = %40, %37
%44 = load i8*, i8** %3
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 47
br i1 %47, label %48, label %54
48: ; preds = %43
%49 = load i32, i32* %9
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i8*, i8** %8
%53 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %52)
store i32 0, i32* %9
br label %54
54: ; preds = %51, %48, %43
%55 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %55, i8** %8
%56 = call i64 @is_dir(i8* %17)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %54
br label %30
59: ; preds = %54
%60 = load i32, i32* @S_IRWXU
%61 = call i32 @mkdir(i8* %17, i32 %60)
store i32 %61, i32* %4
%62 = load i32, i32* %4
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %69
64: ; preds = %59
%65 = load i32, i32* @errno
%66 = call i8* @strerror(i32 %65)
%67 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %14, i8* %66)
%68 = load i32, i32* %4
store i32 %68, i32* %2
store i32 1, i32* %10
br label %71
69: ; preds = %59
br label %30
70: ; preds = %30
store i32 0, i32* %2
store i32 1, i32* %10
br label %71
71: ; preds = %70, %64, %25, %20
%72 = load i8*, i8** %5
%73 = load i32, i32* %2
ret i32 %73
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [4 x i8] c"/%s\00"
@S_IRWXU = global i32 0
@.str.4 = [36 x i8] c"Unable to create directory %s : %s\0A\00"
@errno = global i32 0
define i32 @create_dir(i8* %0) {
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = alloca i8, i64 %3
%6 = icmp eq i8* %0, null
br i1 %6, label %45, label %7
7: ; preds = %1
%8 = tail call i64 @is_dir(i8* %0)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %45
10: ; preds = %7
%11 = call i32 @strcpy(i8* %4, i8* %0)
%12 = call i8* @strtok(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i8* %12, null
br i1 %13, label %45, label %14
14: ; preds = %10, %35
%15 = phi i32 [ %31, %35 ], [ 1, %10 ]
%16 = phi i8* [ %32, %35 ], [ %12, %10 ]
%17 = call i64 @strcmp(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 @strcpy(i8* %5, i8* %16)
br label %23
21: ; preds = %14
%22 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %5, i8* %16)
br label %23
23: ; preds = %21, %19
%24 = load i8, i8* %0
%25 = icmp eq i8 %24, 47
%26 = icmp ne i32 %15, 0
%27 = select i1 %25, i1 %26, i1 false
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %16)
br label %30
30: ; preds = %28, %23
%31 = phi i32 [ 0, %28 ], [ %15, %23 ]
%32 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%33 = call i64 @is_dir(i8* %5)
%34 = icmp eq i64 %33, 0
br i1 %34, label %37, label %35
35: ; preds = %30, %37
%36 = icmp eq i8* %32, null
br i1 %36, label %45, label %14
37: ; preds = %30
%38 = load i32, i32* @S_IRWXU
%39 = call i32 @mkdir(i8* %5, i32 %38)
%40 = icmp eq i32 %39, 0
br i1 %40, label %35, label %41
41: ; preds = %37
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %4, i8* %43)
br label %45
45: ; preds = %35, %10, %7, %1, %41
%46 = phi i32 [ %39, %41 ], [ -1, %1 ], [ 0, %7 ], [ 0, %10 ], [ 0, %35 ]
ret i32 %46
}