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

/* Variables and functions */
int /*<<< orphan*/ free(char *);
scalar_t__ mkdir(char *, int);
scalar_t__ stat(char *, struct stat *);
char *strdup(char *);
size_t strlen(char *);
char *strrchr(char *, char);
int /*<<< orphan*/ wcstombs(char *, int /*<<< orphan*/ const *, int);
__attribute__((visibility("default"))) int
osal_mkdirp(const wchar_t *_dirpath) {
  char dirpath[260];
  wcstombs(dirpath, _dirpath, 260);
  struct stat fileinfo;
  int dirpathlen = strlen(dirpath);
  char *currpath = strdup(dirpath);
  while (strlen(currpath) > 1) {
    char *lastslash = strrchr(currpath, '/');
    if (lastslash == NULL)
      break;
    *lastslash = 0;
  }
  while (strlen(currpath) < dirpathlen) {
    if (strlen(currpath) > 0 && stat(currpath, &fileinfo) != 0)
      break;
    currpath[strlen(currpath)] = '/';
  }
  do {
    if (stat(currpath, &fileinfo) != 0) {
      if (mkdir(currpath, 0700) != 0) {
        free(currpath);
        return 1;
      }
    }
    if (strlen(currpath) == dirpathlen)
      break;
    else
      currpath[strlen(currpath)] = '/';
  } while (1);
  free(currpath);
  return 0;
}


x86 assembly (clang, -O0):
<osal_mkdirp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x104,%edx
call   1080 <wcstombs@plt>
lea    -0x120(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x12c(%rbp)
lea    -0x120(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x138(%rbp)
mov    -0x138(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x1,%rax
jbe    1209 <osal_mkdirp+0x99>
mov    -0x138(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x140(%rbp)
cmpq   $0x0,-0x140(%rbp)
jne    11fa <osal_mkdirp+0x8a>
jmp    1209 <osal_mkdirp+0x99>
mov    -0x140(%rbp),%rax
movb   $0x0,(%rax)
jmp    11b9 <osal_mkdirp+0x49>
jmp    120e <osal_mkdirp+0x9e>
mov    -0x138(%rbp),%rdi
call   1050 <strlen@plt>
movslq -0x12c(%rbp),%rcx
cmp    %rcx,%rax
jae    128f <osal_mkdirp+0x11f>
mov    -0x138(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x0,%rax
jbe    1262 <osal_mkdirp+0xf2>
mov    -0x138(%rbp),%rdi
lea    -0x128(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     1262 <osal_mkdirp+0xf2>
jmp    128f <osal_mkdirp+0x11f>
mov    -0x138(%rbp),%rax
mov    %rax,-0x148(%rbp)
mov    -0x138(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x148(%rbp),%rax
movb   $0x2f,(%rax,%rcx,1)
jmp    120e <osal_mkdirp+0x9e>
jmp    1294 <osal_mkdirp+0x124>
mov    -0x138(%rbp),%rdi
lea    -0x128(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     12e9 <osal_mkdirp+0x179>
mov    -0x138(%rbp),%rdi
mov    $0x1c0,%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
je     12e4 <osal_mkdirp+0x174>
mov    -0x138(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    1359 <osal_mkdirp+0x1e9>
jmp    12e9 <osal_mkdirp+0x179>
mov    -0x138(%rbp),%rdi
call   1050 <strlen@plt>
movslq -0x12c(%rbp),%rcx
cmp    %rcx,%rax
jne    130a <osal_mkdirp+0x19a>
jmp    1346 <osal_mkdirp+0x1d6>
mov    -0x138(%rbp),%rax
mov    %rax,-0x150(%rbp)
mov    -0x138(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x150(%rbp),%rax
movb   $0x2f,(%rax,%rcx,1)
jmp    1337 <osal_mkdirp+0x1c7>
mov    $0x1,%al
test   $0x1,%al
jne    1294 <osal_mkdirp+0x124>
jmp    1346 <osal_mkdirp+0x1d6>
mov    -0x138(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<osal_mkdirp>:
push   %rbp
push   %r14
push   %rbx
sub    $0x120,%rsp
mov    %rdi,%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x104,%edx
call   1080 <wcstombs@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1090 <strdup@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <strlen@plt>
cmp    $0x2,%rax
jb     11e3 <osal_mkdirp+0x73>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     11e3 <osal_mkdirp+0x73>
movb   $0x0,(%rax)
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    $0x1,%rax
ja     11c0 <osal_mkdirp+0x50>
movslq %r14d,%rbp
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    %rbp,%rax
jae    122e <osal_mkdirp+0xbe>
lea    0x8(%rsp),%r14
jmp    1219 <osal_mkdirp+0xa9>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <strlen@plt>
movb   $0x2f,(%rbx,%rax,1)
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    %rbp,%rax
jae    122e <osal_mkdirp+0xbe>
test   %rax,%rax
je     1200 <osal_mkdirp+0x90>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <stat@plt>
test   %rax,%rax
je     1200 <osal_mkdirp+0x90>
lea    0x8(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <stat@plt>
test   %rax,%rax
je     1262 <osal_mkdirp+0xf2>
mov    %rbx,%rdi
mov    $0x1c0,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
jne    1279 <osal_mkdirp+0x109>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    %rbp,%rax
je     1275 <osal_mkdirp+0x105>
movb   $0x2f,(%rbx,%rax,1)
jmp    1240 <osal_mkdirp+0xd0>
xor    %ebp,%ebp
jmp    127e <osal_mkdirp+0x10e>
mov    $0x1,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x120,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
define i32 @osal_mkdirp(i32* %0) {
%2 = alloca [260 x i8]
%3 = alloca %struct.stat
%4 = getelementptr inbounds [260 x i8], [260 x i8]* %2, i64 0, i64 0
%5 = call i32 @wcstombs(i8* %4, i32* %0, i32 260)
%6 = bitcast %struct.stat* %3 to i8*
%7 = call i64 @strlen(i8* %4)
%8 = call i8* @strdup(i8* %4)
%9 = call i64 @strlen(i8* dereferenceable(1) %8)
%10 = icmp ugt i64 %9, 1
br i1 %10, label %11, label %17
11: ; preds = %1, %14
%12 = call i8* @strrchr(i8* dereferenceable(1) %8, i8 signext 47)
%13 = icmp eq i8* %12, null
br i1 %13, label %17, label %14
14: ; preds = %11
store i8 0, i8* %12
%15 = call i64 @strlen(i8* dereferenceable(1) %8)
%16 = icmp ugt i64 %15, 1
br i1 %16, label %11, label %17
17: ; preds = %14, %11, %1
%18 = shl i64 %7, 32
%19 = ashr exact i64 %18, 32
%20 = call i64 @strlen(i8* dereferenceable(1) %8)
%21 = icmp ult i64 %20, %19
br i1 %21, label %22, label %33
22: ; preds = %17, %28
%23 = phi i64 [ %31, %28 ], [ %20, %17 ]
%24 = icmp eq i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = call i64 @stat(i8* %8, %struct.stat* %3)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %25, %22
%29 = call i64 @strlen(i8* dereferenceable(1) %8)
%30 = getelementptr inbounds i8, i8* %8, i64 %29
store i8 47, i8* %30
%31 = call i64 @strlen(i8* dereferenceable(1) %8)
%32 = icmp ult i64 %31, %19
br i1 %32, label %22, label %33
33: ; preds = %28, %25, %17
br label %34
34: ; preds = %33, %43
%35 = call i64 @stat(i8* %8, %struct.stat* %3)
%36 = icmp eq i64 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %34
%38 = call i64 @mkdir(i8* %8, i32 448)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %37, %34
%41 = call i64 @strlen(i8* dereferenceable(1) %8)
%42 = icmp eq i64 %41, %19
br i1 %42, label %45, label %43
43: ; preds = %40
%44 = getelementptr inbounds i8, i8* %8, i64 %41
store i8 47, i8* %44
br label %34
45: ; preds = %40, %37
%46 = phi i32 [ 1, %37 ], [ 0, %40 ]
%47 = call i32 @free(i8* %8)
ret i32 %46
}