#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 */
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct stat *, int /*<<< orphan*/, int);
scalar_t__ mkdir(char *, int);
int stat(char *, struct stat *);
int /*<<< orphan*/ strcpy(char *, char *);
int xmap_mkdir(char *path) {
  struct stat st;
  char fullpath[256];
  char *p = NULL;
  int level = -1, ret = -1;
  if (path) {
    strcpy(fullpath, path);
    p = fullpath;
    while (*p != '\0') {
      if (*p == '/') {
        level++;
        while (*p != '\0' && *p == '/' && *(p + 1) == '/')
          ++p;
        if (level > 0) {
          *p = '\0';
          memset(&st, 0, sizeof(struct stat));
          ret = stat(fullpath, &st);
          if (ret == 0 && !S_ISDIR(st.st_mode))
            return -1;
          if (ret != 0 && mkdir(fullpath, 0755) != 0)
            return -1;
          *p = '/';
        }
      }
      ++p;
    }
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<xmap_mkdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x128(%rbp)
movl   $0xffffffff,-0x12c(%rbp)
ff ff ff
movl   $0xffffffff,-0x130(%rbp)
ff ff ff
cmpq   $0x0,-0x10(%rbp)
je     1320 <xmap_mkdir+0x1d0>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x120(%rbp),%rax
mov    %rax,-0x128(%rbp)
mov    -0x128(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1314 <xmap_mkdir+0x1c4>
mov    -0x128(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    12fd <xmap_mkdir+0x1ad>
mov    -0x12c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x12c(%rbp)
mov    -0x128(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x131(%rbp)
je     1229 <xmap_mkdir+0xd9>
mov    -0x128(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x2f,%ecx
mov    %al,-0x131(%rbp)
jne    1229 <xmap_mkdir+0xd9>
mov    -0x128(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2f,%eax
sete   %al
mov    %al,-0x131(%rbp)
mov    -0x131(%rbp),%al
test   $0x1,%al
jne    123c <xmap_mkdir+0xec>
jmp    1253 <xmap_mkdir+0x103>
mov    -0x128(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x128(%rbp)
jmp    11dc <xmap_mkdir+0x8c>
cmpl   $0x0,-0x12c(%rbp)
jle    12f8 <xmap_mkdir+0x1a8>
mov    -0x128(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
lea    -0x120(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
mov    %eax,-0x130(%rbp)
cmpl   $0x0,-0x130(%rbp)
jne    12ba <xmap_mkdir+0x16a>
mov    -0x18(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    12ba <xmap_mkdir+0x16a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1327 <xmap_mkdir+0x1d7>
cmpl   $0x0,-0x130(%rbp)
je     12ee <xmap_mkdir+0x19e>
lea    -0x120(%rbp),%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
je     12ee <xmap_mkdir+0x19e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1327 <xmap_mkdir+0x1d7>
mov    -0x128(%rbp),%rax
movb   $0x2f,(%rax)
jmp    12fd <xmap_mkdir+0x1ad>
mov    -0x128(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x128(%rbp)
jmp    11a7 <xmap_mkdir+0x57>
movl   $0x0,-0x4(%rbp)
jmp    1327 <xmap_mkdir+0x1d7>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xmap_mkdir>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x110,%rsp
test   %rdi,%rdi
je     120a <xmap_mkdir+0xba>
mov    %rdi,%rsi
lea    0x10(%rsp),%r15
mov    %r15,%rdi
call   1030 <strcpy@plt>
mov    $0xffffffff,%eax
lea    0x8(%rsp),%r12
xor    %r14d,%r14d
mov    %r15,%rbx
jmp    1198 <xmap_mkdir+0x48>
nopw   0x0(%rax,%rax,1)
test   %cl,%cl
je     1210 <xmap_mkdir+0xc0>
add    $0x1,%rbx
mov    (%rbx),%cl
cmp    $0x2f,%cl
jne    1190 <xmap_mkdir+0x40>
add    $0xffffffffffffffff,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmpb   $0x2f,0x2(%rbx)
lea    0x1(%rbx),%rbx
je     11b0 <xmap_mkdir+0x60>
lea    0x1(%rax),%ebp
test   %eax,%eax
js     1206 <xmap_mkdir+0xb6>
movb   $0x0,(%rbx)
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1070 <stat@plt>
test   %eax,%eax
je     11f6 <xmap_mkdir+0xa6>
mov    %r15,%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
je     1203 <xmap_mkdir+0xb3>
jmp    120a <xmap_mkdir+0xba>
mov    0x8(%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %eax,%eax
je     120a <xmap_mkdir+0xba>
movb   $0x2f,(%rbx)
mov    %ebp,%eax
jmp    1194 <xmap_mkdir+0x44>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
define i32 @xmap_mkdir(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca [256 x i8]
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i8* null, i8** %6
store i32 -1, i32* %7
store i32 -1, i32* %8
%9 = load i8*, i8** %3
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %81
11: ; preds = %1
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%13 = load i8*, i8** %3
%14 = call i32 @strcpy(i8* %12, i8* %13)
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
store i8* %15, i8** %6
br label %16
16: ; preds = %77, %11
%17 = load i8*, i8** %6
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %80
21: ; preds = %16
%22 = load i8*, i8** %6
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 47
br i1 %25, label %26, label %77
26: ; preds = %21
%27 = load i32, i32* %7
%28 = add nsw i32 %27, 1
store i32 %28, i32* %7
br label %29
29: ; preds = %47, %26
%30 = load i8*, i8** %6
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %45
34: ; preds = %29
%35 = load i8*, i8** %6
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 47
br i1 %38, label %39, label %45
39: ; preds = %34
%40 = load i8*, i8** %6
%41 = getelementptr inbounds i8, i8* %40, i64 1
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %43, 47
br label %45
45: ; preds = %39, %34, %29
%46 = phi i1 [ false, %34 ], [ false, %29 ], [ %44, %39 ]
br i1 %46, label %47, label %50
47: ; preds = %45
%48 = load i8*, i8** %6
%49 = getelementptr inbounds i8, i8* %48, i32 1
store i8* %49, i8** %6
br label %29
50: ; preds = %45
%51 = load i32, i32* %7
%52 = icmp sgt i32 %51, 0
br i1 %52, label %53, label %76
53: ; preds = %50
%54 = load i8*, i8** %6
store i8 0, i8* %54
%55 = call i32 @memset(%struct.stat* %4, i32 0, i32 4)
%56 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%57 = call i32 @stat(i8* %56, %struct.stat* %4)
store i32 %57, i32* %8
%58 = load i32, i32* %8
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %66
60: ; preds = %53
%61 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%62 = load i32, i32* %61
%63 = call i32 @S_ISDIR(i32 %62)
%64 = icmp ne i32 %63, 0
br i1 %64, label %66, label %65
65: ; preds = %60
store i32 -1, i32* %2
br label %82
66: ; preds = %60, %53
%67 = load i32, i32* %8
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %74
69: ; preds = %66
%70 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%71 = call i64 @mkdir(i8* %70, i32 493)
%72 = icmp ne i64 %71, 0
br i1 %72, label %73, label %74
73: ; preds = %69
store i32 -1, i32* %2
br label %82
74: ; preds = %69, %66
%75 = load i8*, i8** %6
store i8 47, i8* %75
br label %76
76: ; preds = %74, %50
br label %77
77: ; preds = %76, %21
%78 = load i8*, i8** %6
%79 = getelementptr inbounds i8, i8* %78, i32 1
store i8* %79, i8** %6
br label %16
80: ; preds = %16
store i32 0, i32* %2
br label %82
81: ; preds = %1
store i32 -1, i32* %2
br label %82
82: ; preds = %81, %80, %73, %65
%83 = load i32, i32* %2
ret i32 %83
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define i32 @xmap_mkdir(i8* %0) {
%2 = alloca %struct.stat
%3 = alloca [256 x i8]
%4 = bitcast %struct.stat* %2 to i8*
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = icmp eq i8* %0, null
br i1 %6, label %38, label %7
7: ; preds = %1
%8 = call i32 @strcpy(i8* %5, i8* %0)
%9 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
br label %10
10: ; preds = %34, %7
%11 = phi i8* [ %5, %7 ], [ %37, %34 ]
%12 = phi i32 [ -1, %7 ], [ %36, %34 ]
%13 = load i8, i8* %11
switch i8 %13, label %34 [
i8 0, label %38
i8 47, label %14
]
14: ; preds = %10, %14
%15 = phi i8* [ %16, %14 ], [ %11, %10 ]
%16 = getelementptr inbounds i8, i8* %15, i64 1
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 47
br i1 %18, label %14, label %19
19: ; preds = %14
%20 = add nsw i32 %12, 1
%21 = icmp sgt i32 %12, -1
br i1 %21, label %22, label %34
22: ; preds = %19
store i8 0, i8* %15
%23 = call i32 @memset(%struct.stat* %2, i32 0, i32 4)
%24 = call i32 @stat(i8* %5, %struct.stat* %2)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = load i32, i32* %9
%28 = call i32 @S_ISDIR(i32 %27)
%29 = icmp eq i32 %28, 0
br i1 %29, label %38, label %33
30: ; preds = %22
%31 = call i64 @mkdir(i8* %5, i32 493)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %26, %30
store i8 47, i8* %15
br label %34
34: ; preds = %10, %19, %33
%35 = phi i8* [ %15, %33 ], [ %15, %19 ], [ %11, %10 ]
%36 = phi i32 [ %20, %33 ], [ %20, %19 ], [ %12, %10 ]
%37 = getelementptr inbounds i8, i8* %35, i64 1
br label %10
38: ; preds = %10, %30, %26, %1
%39 = phi i32 [ -1, %1 ], [ 0, %10 ], [ -1, %30 ], [ -1, %26 ]
ret i32 %39
}