#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 */
scalar_t__ EACCES;
scalar_t__ EEXIST;
scalar_t__ EROFS;
int /*<<< orphan*/ S_IRWXU;
scalar_t__ errno;
int mkdir(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
char *strrchr(char *, char);
int mk_dirs(char *path) {
  int ret;
  int len;
  char subpath[256] = "";
  char *pos = NULL;
  char pth[(1 * 1024 * 1024)];
  if (path == NULL) {
    return -1;
  }
  strcpy(pth, path);
  if ((len = strlen(pth)) > 0 && pth[len - 1] == '/') {
    pth[len - 1] = '\0';
  }
  while ((ret = mkdir(pth, S_IRWXU)) != 0) {
    if (errno == EEXIST) {
      return 0;
    } else if (errno == EACCES || errno == EROFS) {
      return ret;
    }
    if ((pos = strrchr(pth, '/')) == NULL) {
      return ret;
    }
    strncat(subpath, pth, pos - pth);
    mk_dirs(subpath);
  }
  return ret;
}


x86 assembly (clang, -O0):
<mk_dirs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100130,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
movq   $0x0,-0x128(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b4 <mk_dirs+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ff <mk_dirs+0x18f>
lea    -0x100130(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x100130(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    1205 <mk_dirs+0x95>
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x100130(%rbp,%rax,1),%eax
ff
cmp    $0x2f,%eax
jne    1205 <mk_dirs+0x95>
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x100130(%rbp,%rax,1)
jmp    120a <mk_dirs+0x9a>
lea    -0x100130(%rbp),%rdi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
call   1040 <mkdir@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     12f9 <mk_dirs+0x189>
mov    0x2d8e(%rip),%rax
mov    (%rax),%rax
mov    0x2d8c(%rip),%rcx
cmp    (%rcx),%rax
jne    1251 <mk_dirs+0xe1>
movl   $0x0,-0x4(%rbp)
jmp    12ff <mk_dirs+0x18f>
mov    0x2d68(%rip),%rax
mov    (%rax),%rax
mov    0x2d76(%rip),%rcx
cmp    (%rcx),%rax
je     1285 <mk_dirs+0x115>
mov    0x2d4e(%rip),%rax
mov    (%rax),%rax
mov    0x2d64(%rip),%rcx
cmp    (%rcx),%rax
jne    1290 <mk_dirs+0x120>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ff <mk_dirs+0x18f>
jmp    1295 <mk_dirs+0x125>
lea    -0x100130(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x128(%rbp)
cmp    $0x0,%rax
jne    12c2 <mk_dirs+0x152>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ff <mk_dirs+0x18f>
lea    -0x120(%rbp),%rdi
lea    -0x100130(%rbp),%rsi
mov    -0x128(%rbp),%rax
lea    -0x100130(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1080 <strncat@plt>
lea    -0x120(%rbp),%rdi
call   1090 <mk_dirs@plt>
jmp    120a <mk_dirs+0x9a>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x100130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mk_dirs>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x100108,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
test   %rdi,%rdi
je     124e <mk_dirs+0xee>
mov    %rdi,%rsi
lea    0x100(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
jle    1214 <mk_dirs+0xb4>
add    $0xffffffff,%eax
cmpb   $0x2f,0x100(%rsp,%rax,1)
2f
jne    1214 <mk_dirs+0xb4>
movb   $0x0,0x100(%rsp,%rax,1)
mov    0x2dcd(%rip),%r12
mov    (%r12),%esi
lea    0x100(%rsp),%rdi
call   1040 <mkdir@plt>
test   %eax,%eax
je     124a <mk_dirs+0xea>
mov    %eax,%ebx
mov    0x2d87(%rip),%r13
mov    0x0(%r13),%rax
mov    0x2d84(%rip),%rbp
cmp    0x0(%rbp),%rax
jne    1267 <mk_dirs+0x107>
xor    %ebx,%ebx
jmp    1253 <mk_dirs+0xf3>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x100108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x100(%rsp),%r14
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x2d51(%rip),%rcx
cmp    (%rcx),%rax
je     1253 <mk_dirs+0xf3>
mov    0x2d4d(%rip),%rcx
cmp    (%rcx),%rax
je     1253 <mk_dirs+0xf3>
mov    %r14,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     1253 <mk_dirs+0xf3>
sub    %r14d,%eax
mov    %r15,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1070 <strncat@plt>
mov    %r15,%rdi
call   1080 <mk_dirs@plt>
mov    (%r12),%esi
mov    %r14,%rdi
call   1040 <mkdir@plt>
test   %eax,%eax
je     124a <mk_dirs+0xea>
mov    %eax,%ebx
mov    0x0(%r13),%rax
cmp    0x0(%rbp),%rax
jne    1280 <mk_dirs+0x120>
jmp    124a <mk_dirs+0xea>

LLVM IR (-O0):
@S_IRWXU = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@EACCES = global i64 0
@EROFS = global i64 0
define i32 @mk_dirs(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [256 x i8]
%7 = alloca i8*
%8 = alloca [1048576 x i8]
store i8* %0, i8** %3
%9 = bitcast [256 x i8]* %6 to i8*
store i8* null, i8** %7
%10 = load i8*, i8** %3
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %75
13: ; preds = %1
%14 = getelementptr inbounds [1048576 x i8], [1048576 x i8]* %8, i64 0, i64 0
%15 = load i8*, i8** %3
%16 = call i32 @strcpy(i8* %14, i8* %15)
%17 = getelementptr inbounds [1048576 x i8], [1048576 x i8]* %8, i64 0, i64 0
%18 = call i32 @strlen(i8* %17)
store i32 %18, i32* %5
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %33
20: ; preds = %13
%21 = load i32, i32* %5
%22 = sub nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [1048576 x i8], [1048576 x i8]* %8, i64 0, i64 %23
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 47
br i1 %27, label %28, label %33
28: ; preds = %20
%29 = load i32, i32* %5
%30 = sub nsw i32 %29, 1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [1048576 x i8], [1048576 x i8]* %8, i64 0, i64 %31
store i8 0, i8* %32
br label %33
33: ; preds = %28, %20, %13
br label %34
34: ; preds = %61, %33
%35 = getelementptr inbounds [1048576 x i8], [1048576 x i8]* %8, i64 0, i64 0
%36 = load i32, i32* @S_IRWXU
%37 = call i32 @mkdir(i8* %35, i32 %36)
store i32 %37, i32* %4
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %73
39: ; preds = %34
%40 = load i64, i64* @errno
%41 = load i64, i64* @EEXIST
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %44
43: ; preds = %39
store i32 0, i32* %2
br label %75
44: ; preds = %39
%45 = load i64, i64* @errno
%46 = load i64, i64* @EACCES
%47 = icmp eq i64 %45, %46
br i1 %47, label %52, label %48
48: ; preds = %44
%49 = load i64, i64* @errno
%50 = load i64, i64* @EROFS
%51 = icmp eq i64 %49, %50
br i1 %51, label %52, label %54
52: ; preds = %48, %44
%53 = load i32, i32* %4
store i32 %53, i32* %2
br label %75
54: ; preds = %48
br label %55
55: ; preds = %54
%56 = getelementptr inbounds [1048576 x i8], [1048576 x i8]* %8, i64 0, i64 0
%57 = call i8* @strrchr(i8* %56, i8 signext 47)
store i8* %57, i8** %7
%58 = icmp eq i8* %57, null
br i1 %58, label %59, label %61
59: ; preds = %55
%60 = load i32, i32* %4
store i32 %60, i32* %2
br label %75
61: ; preds = %55
%62 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%63 = getelementptr inbounds [1048576 x i8], [1048576 x i8]* %8, i64 0, i64 0
%64 = load i8*, i8** %7
%65 = getelementptr inbounds [1048576 x i8], [1048576 x i8]* %8, i64 0, i64 0
%66 = ptrtoint i8* %64 to i64
%67 = ptrtoint i8* %65 to i64
%68 = sub i64 %66, %67
%69 = trunc i64 %68 to i32
%70 = call i32 @strncat(i8* %62, i8* %63, i32 %69)
%71 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%72 = call i32 @mk_dirs(i8* %71)
br label %34
73: ; preds = %34
%74 = load i32, i32* %4
store i32 %74, i32* %2
br label %75
75: ; preds = %73, %59, %52, %43, %12
%76 = load i32, i32* %2
ret i32 %76
}

LLVM IR (-O2):
@S_IRWXU = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@EACCES = global i64 0
@EROFS = global i64 0
define i32 @mk_dirs(i8* %0) {
%2 = alloca [256 x i8]
%3 = alloca [1048576 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [1048576 x i8], [1048576 x i8]* %3, i64 0, i64 0
%6 = icmp eq i8* %0, null
br i1 %6, label %51, label %7
7: ; preds = %1
%8 = call i32 @strcpy(i8* %5, i8* %0)
%9 = call i32 @strlen(i8* %5)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %7
%12 = add nsw i32 %9, -1
%13 = zext i32 %12 to i64
%14 = getelementptr inbounds [1048576 x i8], [1048576 x i8]* %3, i64 0, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 47
br i1 %16, label %17, label %18
17: ; preds = %11
store i8 0, i8* %14
br label %18
18: ; preds = %17, %11, %7
%19 = load i32, i32* @S_IRWXU
%20 = call i32 @mkdir(i8* %5, i32 %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %51, label %22
22: ; preds = %18
%23 = ptrtoint [1048576 x i8]* %3 to i64
%24 = load i64, i64* @errno
%25 = load i64, i64* @EEXIST
%26 = icmp eq i64 %24, %25
br i1 %26, label %51, label %31
27: ; preds = %42
%28 = load i64, i64* @errno
%29 = load i64, i64* @EEXIST
%30 = icmp eq i64 %28, %29
br i1 %30, label %51, label %31
31: ; preds = %22, %27
%32 = phi i64 [ %28, %27 ], [ %24, %22 ]
%33 = phi i32 [ %49, %27 ], [ %20, %22 ]
%34 = load i64, i64* @EACCES
%35 = icmp eq i64 %32, %34
%36 = load i64, i64* @EROFS
%37 = icmp eq i64 %32, %36
%38 = select i1 %35, i1 true, i1 %37
br i1 %38, label %51, label %39
39: ; preds = %31
%40 = call i8* @strrchr(i8* %5, i8 signext 47)
%41 = icmp eq i8* %40, null
br i1 %41, label %51, label %42
42: ; preds = %39
%43 = ptrtoint i8* %40 to i64
%44 = sub i64 %43, %23
%45 = trunc i64 %44 to i32
%46 = call i32 @strncat(i8* %4, i8* %5, i32 %45)
%47 = call i32 @mk_dirs(i8* %4)
%48 = load i32, i32* @S_IRWXU
%49 = call i32 @mkdir(i8* %5, i32 %48)
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %27
51: ; preds = %42, %39, %31, %27, %22, %18, %1
%52 = phi i32 [ -1, %1 ], [ 0, %18 ], [ 0, %22 ], [ 0, %27 ], [ %33, %31 ], [ %33, %39 ], [ 0, %42 ]
ret i32 %52
}