#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
scalar_t__ EEXIST;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ die(char *, char *);
scalar_t__ errno;
scalar_t__ mkdir(char *, int);
scalar_t__ stat(char *, struct stat *);
char *strchr(char *, char);
void make_dir(char *path) {
  char *sep = (path[0] == '/' ? path + 1 : path);
  for (;;) {
    sep = strchr(sep, '/');
    if (sep)
      *sep = 0;
    if (mkdir(path, 0777) < 0 && errno != EEXIST)
      die("Cannot create directory %s: %m", path);
    if (!sep)
      break;
    *sep++ = '/';
  }
  struct stat st;
  if (stat(path, &st) < 0)
    die("Cannot stat %s: %m", path);
  if (!S_ISDIR(st.st_mode))
    die("Cannot create %s: already exists, but not a directory", path);
}


x86 assembly (clang, -O0):
<make_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    117d <make_dir+0x2d>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1185 <make_dir+0x35>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b1 <make_dir+0x61>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
jge    11f3 <make_dir+0xa3>
mov    0x2e08(%rip),%rax
mov    (%rax),%rax
mov    0x2e06(%rip),%rcx
cmp    (%rcx),%rax
je     11f3 <make_dir+0xa3>
mov    -0x8(%rbp),%rsi
lea    0xe12(%rip),%rdi
call   1070 <die@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1203 <make_dir+0xb3>
jmp    121a <make_dir+0xca>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x2f,(%rax)
jmp    118d <make_dir+0x3d>
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jge    1241 <make_dir+0xf1>
mov    -0x8(%rbp),%rsi
lea    0xde3(%rip),%rdi
call   1070 <die@plt>
mov    -0x18(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1262 <make_dir+0x112>
mov    -0x8(%rbp),%rsi
lea    0xdd5(%rip),%rdi
call   1070 <die@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_dir>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r15
xor    %edi,%edi
cmpb   $0x2f,(%r15)
sete   %dil
add    %r15,%rdi
mov    0x2e64(%rip),%r12
mov    0x2e65(%rip),%r13
lea    0xe7e(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0x2f,%esi
call   1040 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11a5 <make_dir+0x55>
movb   $0x0,(%rbx)
mov    %r15,%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
test   %rax,%rax
jns    11d0 <make_dir+0x80>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     11d0 <make_dir+0x80>
mov    %r14,%rdi
mov    %r15,%rsi
call   1070 <die@plt>
nopl   0x0(%rax)
test   %rbx,%rbx
je     11e1 <make_dir+0x91>
mov    %rbx,%rdi
add    $0x1,%rdi
movb   $0x2f,(%rbx)
jmp    1190 <make_dir+0x40>
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
call   1060 <stat@plt>
test   %rax,%rax
jns    1202 <make_dir+0xb2>
lea    0xe25(%rip),%rdi
mov    %r15,%rsi
call   1070 <die@plt>
mov    0x8(%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %eax,%eax
jne    121e <make_dir+0xce>
lea    0xe1c(%rip),%rdi
mov    %r15,%rsi
call   1070 <die@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@EEXIST = global i64 0
@.str = [31 x i8] c"Cannot create directory %s: %m\00"
@.str.1 = [19 x i8] c"Cannot stat %s: %m\00"
@.str.2 = [54 x i8] c"Cannot create %s: already exists, but not a directory\00"
define void @make_dir(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.stat
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = getelementptr inbounds i8, i8* %5, i64 0
%7 = load i8, i8* %6
%8 = sext i8 %7 to i32
%9 = icmp eq i32 %8, 47
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i8*, i8** %2
%12 = getelementptr inbounds i8, i8* %11, i64 1
br label %15
13: ; preds = %1
%14 = load i8*, i8** %2
br label %15
15: ; preds = %13, %10
%16 = phi i8* [ %12, %10 ], [ %14, %13 ]
store i8* %16, i8** %3
br label %17
17: ; preds = %39, %15
%18 = load i8*, i8** %3
%19 = call i8* @strchr(i8* %18, i8 signext 47)
store i8* %19, i8** %3
%20 = load i8*, i8** %3
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = load i8*, i8** %3
store i8 0, i8* %23
br label %24
24: ; preds = %22, %17
%25 = load i8*, i8** %2
%26 = call i64 @mkdir(i8* %25, i32 511)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %24
%29 = load i64, i64* @errno
%30 = load i64, i64* @EEXIST
%31 = icmp ne i64 %29, %30
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = load i8*, i8** %2
%34 = call i32 @die(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %33)
br label %35
35: ; preds = %32, %28, %24
%36 = load i8*, i8** %3
%37 = icmp ne i8* %36, null
br i1 %37, label %39, label %38
38: ; preds = %35
br label %42
39: ; preds = %35
%40 = load i8*, i8** %3
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %3
store i8 47, i8* %40
br label %17
42: ; preds = %38
%43 = load i8*, i8** %2
%44 = call i64 @stat(i8* %43, %struct.stat* %4)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = load i8*, i8** %2
%48 = call i32 @die(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %47)
br label %49
49: ; preds = %46, %42
%50 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i32 @S_ISDIR(i32 %51)
%53 = icmp ne i32 %52, 0
br i1 %53, label %57, label %54
54: ; preds = %49
%55 = load i8*, i8** %2
%56 = call i32 @die(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0), i8* %55)
br label %57
57: ; preds = %54, %49
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@EEXIST = global i64 0
@.str = [31 x i8] c"Cannot create directory %s: %m\00"
@.str.1 = [19 x i8] c"Cannot stat %s: %m\00"
@.str.2 = [54 x i8] c"Cannot create %s: already exists, but not a directory\00"
define void @make_dir(i8* %0) {
%2 = alloca %struct.stat
%3 = load i8, i8* %0
%4 = icmp eq i8 %3, 47
%5 = zext i1 %4 to i64
%6 = getelementptr i8, i8* %0, i64 %5
br label %7
7: ; preds = %22, %1
%8 = phi i8* [ %6, %1 ], [ %23, %22 ]
%9 = tail call i8* @strchr(i8* dereferenceable(1) %8, i8 signext 47)
%10 = icmp eq i8* %9, null
br i1 %10, label %12, label %11
11: ; preds = %7
store i8 0, i8* %9
br label %12
12: ; preds = %11, %7
%13 = tail call i64 @mkdir(i8* %0, i32 511)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %12
%16 = load i64, i64* @errno
%17 = load i64, i64* @EEXIST
%18 = icmp eq i64 %16, %17
br i1 %18, label %21, label %19
19: ; preds = %15
%20 = tail call i32 @die(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %21
21: ; preds = %19, %15, %12
br i1 %10, label %24, label %22
22: ; preds = %21
%23 = getelementptr inbounds i8, i8* %9, i64 1
store i8 47, i8* %9
br label %7
24: ; preds = %21
%25 = bitcast %struct.stat* %2 to i8*
%26 = call i64 @stat(i8* %0, %struct.stat* %2)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @die(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %30
30: ; preds = %28, %24
%31 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @S_ISDIR(i32 %32)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = call i32 @die(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %37
37: ; preds = %35, %30
ret void
}