#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 EEXIST;
int errno;
int /*<<< orphan*/ free(char *);
scalar_t__ mkdir(char *, int);
char *strdup(char const *);
int make_directory(const char *dir) {
  char *s = strdup(dir);
  char *path = s;
  char c;
  int ret = 0;
  do {
    c = 0;
    while (*s) {
      if (*s == '/') {
        do {
          ++s;
        } while (*s == '/');
        c = *s;
        *s = 0;
        break;
      }
      ++s;
    }
    if (mkdir(path, 0777) < 0) {
      if (errno != EEXIST) {
        ret = -errno;
        break;
      }
    }
    if (!c)
      goto out;
    *s = c;
  } while (1);
out:
  free(path);
  if (ret)
    errno = -ret;
  return ret;
}


x86 assembly (clang, -O0):
<make_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strdup@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
movb   $0x0,-0x19(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     11c0 <make_directory+0x90>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11af <make_directory+0x7f>
jmp    117e <make_directory+0x4e>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     117e <make_directory+0x4e>
mov    -0x10(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x19(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
jmp    11c0 <make_directory+0x90>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    115c <make_directory+0x2c>
mov    -0x18(%rbp),%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
jge    1208 <make_directory+0xd8>
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    0x2df8(%rip),%rcx
cmp    (%rcx),%eax
je     1203 <make_directory+0xd3>
xor    %eax,%eax
mov    0x2ddf(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x20(%rbp)
jmp    122f <make_directory+0xff>
jmp    1208 <make_directory+0xd8>
cmpb   $0x0,-0x19(%rbp)
jne    1217 <make_directory+0xe7>
jmp    1234 <make_directory+0x104>
mov    -0x19(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    $0x1,%al
test   $0x1,%al
jne    1158 <make_directory+0x28>
jmp    122f <make_directory+0xff>
jmp    1234 <make_directory+0x104>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x20(%rbp)
je     1255 <make_directory+0x125>
xor    %ecx,%ecx
sub    -0x20(%rbp),%ecx
mov    0x2d85(%rip),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_directory>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
call   1050 <strdup@plt>
mov    %rax,%r12
mov    0x2e8e(%rip),%r14
mov    0x2e8f(%rip),%r15
mov    %rax,%rbx
jmp    1164 <make_directory+0x34>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%rbx
mov    (%rbx),%r13b
test   %r13b,%r13b
je     1192 <make_directory+0x62>
cmp    $0x2f,%r13b
jne    1160 <make_directory+0x30>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl 0x1(%rbx),%r13d
add    $0x1,%rbx
cmp    $0x2f,%r13b
je     1180 <make_directory+0x50>
movb   $0x0,(%rbx)
mov    %r12,%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
jns    11ac <make_directory+0x7c>
mov    (%r14),%ebp
cmp    (%r15),%ebp
jne    11c2 <make_directory+0x92>
test   %r13b,%r13b
je     11b6 <make_directory+0x86>
mov    %r13b,(%rbx)
jmp    1164 <make_directory+0x34>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %ebp,%ebp
jmp    11cc <make_directory+0x9c>
mov    %r12,%rdi
call   1030 <free@plt>
neg    %ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@EEXIST = global i32 0
define i32 @make_directory(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i8* @strdup(i8* %7)
store i8* %8, i8** %3
%9 = load i8*, i8** %3
store i8* %9, i8** %4
store i32 0, i32* %6
br label %10
10: ; preds = %55, %1
store i8 0, i8* %5
br label %11
11: ; preds = %33, %10
%12 = load i8*, i8** %3
%13 = load i8, i8* %12
%14 = icmp ne i8 %13, 0
br i1 %14, label %15, label %36
15: ; preds = %11
%16 = load i8*, i8** %3
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 47
br i1 %19, label %20, label %33
20: ; preds = %15
br label %21
21: ; preds = %24, %20
%22 = load i8*, i8** %3
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %3
br label %24
24: ; preds = %21
%25 = load i8*, i8** %3
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 47
br i1 %28, label %21, label %29
29: ; preds = %24
%30 = load i8*, i8** %3
%31 = load i8, i8* %30
store i8 %31, i8* %5
%32 = load i8*, i8** %3
store i8 0, i8* %32
br label %36
33: ; preds = %15
%34 = load i8*, i8** %3
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %3
br label %11
36: ; preds = %29, %11
%37 = load i8*, i8** %4
%38 = call i64 @mkdir(i8* %37, i32 511)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %48
40: ; preds = %36
%41 = load i32, i32* @errno
%42 = load i32, i32* @EEXIST
%43 = icmp ne i32 %41, %42
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = load i32, i32* @errno
%46 = sub nsw i32 0, %45
store i32 %46, i32* %6
br label %56
47: ; preds = %40
br label %48
48: ; preds = %47, %36
%49 = load i8, i8* %5
%50 = icmp ne i8 %49, 0
br i1 %50, label %52, label %51
51: ; preds = %48
br label %57
52: ; preds = %48
%53 = load i8, i8* %5
%54 = load i8*, i8** %3
store i8 %53, i8* %54
br label %55
55: ; preds = %52
br i1 true, label %10, label %56
56: ; preds = %55, %44
br label %57
57: ; preds = %56, %51
%58 = load i8*, i8** %4
%59 = call i32 @free(i8* %58)
%60 = load i32, i32* %6
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %57
%63 = load i32, i32* %6
%64 = sub nsw i32 0, %63
store i32 %64, i32* @errno
br label %65
65: ; preds = %62, %57
%66 = load i32, i32* %6
ret i32 %66
}

LLVM IR (-O2):
@errno = global i32 0
@EEXIST = global i32 0
define i32 @make_directory(i8* nocapture %0) {
%2 = tail call i8* @strdup(i8* %0)
br label %3
3: ; preds = %14, %1
%4 = phi i8* [ %2, %1 ], [ %15, %14 ]
%5 = load i8, i8* %4
switch i8 %5, label %12 [
i8 0, label %16
i8 47, label %6
]
6: ; preds = %3, %6
%7 = phi i8* [ %8, %6 ], [ %4, %3 ]
%8 = getelementptr inbounds i8, i8* %7, i64 1
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 47
br i1 %10, label %6, label %11
11: ; preds = %6
store i8 0, i8* %8
br label %16
12: ; preds = %3
%13 = getelementptr inbounds i8, i8* %4, i64 1
br label %14
14: ; preds = %12, %29
%15 = phi i8* [ %13, %12 ], [ %17, %29 ]
br label %3
16: ; preds = %3, %11
%17 = phi i8* [ %8, %11 ], [ %4, %3 ]
%18 = phi i8 [ %9, %11 ], [ %5, %3 ]
%19 = tail call i64 @mkdir(i8* %2, i32 511)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = load i32, i32* @errno
%23 = load i32, i32* @EEXIST
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %30
25: ; preds = %21, %16
%26 = icmp eq i8 %18, 0
br i1 %26, label %27, label %29
27: ; preds = %25
%28 = tail call i32 @free(i8* %2)
br label %33
29: ; preds = %25
store i8 %18, i8* %17
br label %14
30: ; preds = %21
%31 = tail call i32 @free(i8* %2)
%32 = sub nsw i32 0, %22
br label %33
33: ; preds = %30, %27
%34 = phi i32 [ 0, %27 ], [ %32, %30 ]
ret i32 %34
}