#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;
  int /*<<< orphan*/ member_0;
};

/* 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 force_mkdir(char *path) {
  char fullpath[256];
  int level = -1, ret = -1;
  struct stat st = {0};
  char *p = NULL;
  if (path) {
    strcpy(fullpath, path);
    p = fullpath;
    while (*p != '\0') {
      if (*p == '/') {
        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 = '/';
        }
        level++;
      }
      ++p;
    }
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<force_mkdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x114(%rbp)
ff ff ff
movl   $0xffffffff,-0x118(%rbp)
ff ff ff
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
movq   $0x0,-0x128(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     133c <force_mkdir+0x1ec>
lea    -0x110(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x110(%rbp),%rax
mov    %rax,-0x128(%rbp)
mov    -0x128(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1330 <force_mkdir+0x1e0>
mov    -0x128(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1319 <force_mkdir+0x1c9>
jmp    11e5 <force_mkdir+0x95>
mov    -0x128(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x129(%rbp)
je     1232 <force_mkdir+0xe2>
mov    -0x128(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x2f,%ecx
mov    %al,-0x129(%rbp)
jne    1232 <force_mkdir+0xe2>
mov    -0x128(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2f,%eax
sete   %al
mov    %al,-0x129(%rbp)
mov    -0x129(%rbp),%al
test   $0x1,%al
jne    1245 <force_mkdir+0xf5>
jmp    125c <force_mkdir+0x10c>
mov    -0x128(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x128(%rbp)
jmp    11e5 <force_mkdir+0x95>
cmpl   $0x0,-0x114(%rbp)
jle    130a <force_mkdir+0x1ba>
mov    -0x128(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
lea    -0x110(%rbp),%rdi
lea    -0x120(%rbp),%rsi
call   1070 <stat@plt>
mov    %eax,-0x118(%rbp)
cmpl   $0x0,-0x118(%rbp)
jne    12cc <force_mkdir+0x17c>
mov    -0x120(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    12cc <force_mkdir+0x17c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1343 <force_mkdir+0x1f3>
cmpl   $0x0,-0x118(%rbp)
je     1300 <force_mkdir+0x1b0>
lea    -0x110(%rbp),%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
je     1300 <force_mkdir+0x1b0>
movl   $0xffffffff,-0x4(%rbp)
jmp    1343 <force_mkdir+0x1f3>
mov    -0x128(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x114(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x114(%rbp)
mov    -0x128(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x128(%rbp)
jmp    11ba <force_mkdir+0x6a>
movl   $0x0,-0x4(%rbp)
jmp    1343 <force_mkdir+0x1f3>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<force_mkdir>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x110,%rsp
movq   $0x0,0x8(%rsp)
test   %rdi,%rdi
je     121a <force_mkdir+0xca>
mov    %rdi,%rsi
lea    0x10(%rsp),%r15
mov    %r15,%rdi
call   1030 <strcpy@plt>
mov    $0xffffffff,%ebx
lea    0x8(%rsp),%r12
xor    %r14d,%r14d
mov    %r15,%rbp
jmp    11a8 <force_mkdir+0x58>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
test   %al,%al
je     1220 <force_mkdir+0xd0>
add    $0x1,%rbp
mov    0x0(%rbp),%al
cmp    $0x2f,%al
jne    11a0 <force_mkdir+0x50>
add    $0xffffffffffffffff,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmpb   $0x2f,0x2(%rbp)
lea    0x1(%rbp),%rbp
je     11c0 <force_mkdir+0x70>
test   %ebx,%ebx
jle    1215 <force_mkdir+0xc5>
movb   $0x0,0x0(%rbp)
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1070 <stat@plt>
test   %eax,%eax
je     1204 <force_mkdir+0xb4>
mov    %r15,%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
je     1211 <force_mkdir+0xc1>
jmp    121a <force_mkdir+0xca>
mov    0x8(%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %eax,%eax
je     121a <force_mkdir+0xca>
movb   $0x2f,0x0(%rbp)
add    $0x1,%ebx
jmp    11a4 <force_mkdir+0x54>
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, i32 }
define i32 @force_mkdir(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [256 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i8*
store i8* %0, i8** %3
store i32 -1, i32* %5
store i32 -1, i32* %6
%9 = bitcast %struct.stat* %7 to i8*
store i8* null, i8** %8
%10 = load i8*, i8** %3
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %82
12: ; preds = %1
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%14 = load i8*, i8** %3
%15 = call i32 @strcpy(i8* %13, i8* %14)
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
store i8* %16, i8** %8
br label %17
17: ; preds = %78, %12
%18 = load i8*, i8** %8
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %81
22: ; preds = %17
%23 = load i8*, i8** %8
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 47
br i1 %26, label %27, label %78
27: ; preds = %22
br label %28
28: ; preds = %46, %27
%29 = load i8*, i8** %8
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %44
33: ; preds = %28
%34 = load i8*, i8** %8
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 47
br i1 %37, label %38, label %44
38: ; preds = %33
%39 = load i8*, i8** %8
%40 = getelementptr inbounds i8, i8* %39, i64 1
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp eq i32 %42, 47
br label %44
44: ; preds = %38, %33, %28
%45 = phi i1 [ false, %33 ], [ false, %28 ], [ %43, %38 ]
br i1 %45, label %46, label %49
46: ; preds = %44
%47 = load i8*, i8** %8
%48 = getelementptr inbounds i8, i8* %47, i32 1
store i8* %48, i8** %8
br label %28
49: ; preds = %44
%50 = load i32, i32* %5
%51 = icmp sgt i32 %50, 0
br i1 %51, label %52, label %75
52: ; preds = %49
%53 = load i8*, i8** %8
store i8 0, i8* %53
%54 = call i32 @memset(%struct.stat* %7, i32 0, i32 8)
%55 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%56 = call i32 @stat(i8* %55, %struct.stat* %7)
store i32 %56, i32* %6
%57 = load i32, i32* %6
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %65
59: ; preds = %52
%60 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%61 = load i32, i32* %60
%62 = call i32 @S_ISDIR(i32 %61)
%63 = icmp ne i32 %62, 0
br i1 %63, label %65, label %64
64: ; preds = %59
store i32 -1, i32* %2
br label %83
65: ; preds = %59, %52
%66 = load i32, i32* %6
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %73
68: ; preds = %65
%69 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%70 = call i64 @mkdir(i8* %69, i32 493)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %73
72: ; preds = %68
store i32 -1, i32* %2
br label %83
73: ; preds = %68, %65
%74 = load i8*, i8** %8
store i8 47, i8* %74
br label %75
75: ; preds = %73, %49
%76 = load i32, i32* %5
%77 = add nsw i32 %76, 1
store i32 %77, i32* %5
br label %78
78: ; preds = %75, %22
%79 = load i8*, i8** %8
%80 = getelementptr inbounds i8, i8* %79, i32 1
store i8* %80, i8** %8
br label %17
81: ; preds = %17
store i32 0, i32* %2
br label %83
82: ; preds = %1
store i32 -1, i32* %2
br label %83
83: ; preds = %82, %81, %72, %64
%84 = load i32, i32* %2
ret i32 %84
}

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