#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 */
int /*<<< orphan*/ F_OK;
int S_IRWXG;
int S_IRWXU;
scalar_t__ access(char *, int /*<<< orphan*/);
scalar_t__ mkdir(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
char *strchr(char *, char);
int /*<<< orphan*/ trim(char *);
int mkdir_inpath(const char *path) {
  char p[1024];
  char *b, *e;
  snprintf(p, 1024, "%s", path);
  trim(p);
  b = p;
  if (b[0] == '/')
    b++;
  while ((e = strchr(b, '/'))) {
    *e = 0;
    if (!(access(p, F_OK) == 0))
      if (mkdir(p, S_IRWXU | S_IRWXG) < 0)
        return -1;
    *e = '/';
    e++;
    b = e;
  }
  return 0;
}


x86 assembly (clang, -O0):
<mkdir_inpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x400,%esi
lea    0xe8a(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x410(%rbp),%rdi
call   1060 <trim@plt>
lea    -0x410(%rbp),%rax
mov    %rax,-0x418(%rbp)
mov    -0x418(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11ba <mkdir_inpath+0x6a>
mov    -0x418(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x418(%rbp)
jmp    11bf <mkdir_inpath+0x6f>
mov    -0x418(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
mov    %rax,-0x420(%rbp)
cmp    $0x0,%rax
je     1272 <mkdir_inpath+0x122>
mov    -0x420(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x410(%rbp),%rdi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
cmp    $0x0,%rax
je     1243 <mkdir_inpath+0xf3>
lea    -0x410(%rbp),%rdi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
mov    0x2dbf(%rip),%rax
or     (%rax),%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
jge    123e <mkdir_inpath+0xee>
movl   $0xffffffff,-0x4(%rbp)
jmp    1279 <mkdir_inpath+0x129>
jmp    1243 <mkdir_inpath+0xf3>
mov    -0x420(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x420(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x420(%rbp)
mov    -0x420(%rbp),%rax
mov    %rax,-0x418(%rbp)
jmp    11bf <mkdir_inpath+0x6f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdir_inpath>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%rcx
lea    0xe95(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1060 <trim@plt>
cmpb   $0x2f,(%rsp)
lea    0x1(%rsp),%rdi
cmovne %rbx,%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     120a <mkdir_inpath+0xba>
mov    %rax,%rbx
mov    0x2e1c(%rip),%r15
mov    %rsp,%rbp
mov    0x2e32(%rip),%r12
mov    0x2e23(%rip),%r13
jmp    11dc <mkdir_inpath+0x8c>
nop
movb   $0x2f,(%rbx)
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     120a <mkdir_inpath+0xba>
movb   $0x0,(%rbx)
mov    (%r15),%esi
mov    %rbp,%rdi
call   1070 <access@plt>
test   %rax,%rax
je     11c0 <mkdir_inpath+0x70>
mov    0x0(%r13),%esi
or     (%r12),%esi
mov    %rbp,%rdi
call   1030 <mkdir@plt>
test   %rax,%rax
jns    11c0 <mkdir_inpath+0x70>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@F_OK = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
define i32 @mkdir_inpath(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [1024 x i8]
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = load i8*, i8** %3
%9 = call i32 @snprintf(i8* %7, i32 1024, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%11 = call i32 @trim(i8* %10)
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
store i8* %12, i8** %5
%13 = load i8*, i8** %5
%14 = getelementptr inbounds i8, i8* %13, i64 0
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 47
br i1 %17, label %18, label %21
18: ; preds = %1
%19 = load i8*, i8** %5
%20 = getelementptr inbounds i8, i8* %19, i32 1
store i8* %20, i8** %5
br label %21
21: ; preds = %18, %1
br label %22
22: ; preds = %41, %21
%23 = load i8*, i8** %5
%24 = call i8* @strchr(i8* %23, i8 signext 47)
store i8* %24, i8** %6
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %46
26: ; preds = %22
%27 = load i8*, i8** %6
store i8 0, i8* %27
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%29 = load i32, i32* @F_OK
%30 = call i64 @access(i8* %28, i32 %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %41, label %32
32: ; preds = %26
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%34 = load i32, i32* @S_IRWXU
%35 = load i32, i32* @S_IRWXG
%36 = or i32 %34, %35
%37 = call i64 @mkdir(i8* %33, i32 %36)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %32
store i32 -1, i32* %2
br label %47
40: ; preds = %32
br label %41
41: ; preds = %40, %26
%42 = load i8*, i8** %6
store i8 47, i8* %42
%43 = load i8*, i8** %6
%44 = getelementptr inbounds i8, i8* %43, i32 1
store i8* %44, i8** %6
%45 = load i8*, i8** %6
store i8* %45, i8** %5
br label %22
46: ; preds = %22
store i32 0, i32* %2
br label %47
47: ; preds = %46, %39
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@F_OK = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
define i32 @mkdir_inpath(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 1024, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = call i32 @trim(i8* %3)
%6 = load i8, i8* %3
%7 = icmp eq i8 %6, 47
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 1
%9 = select i1 %7, i8* %8, i8* %3
%10 = call i8* @strchr(i8* dereferenceable(1) %9, i8 signext 47)
%11 = icmp eq i8* %10, null
br i1 %11, label %27, label %12
12: ; preds = %1, %23
%13 = phi i8* [ %25, %23 ], [ %10, %1 ]
store i8 0, i8* %13
%14 = load i32, i32* @F_OK
%15 = call i64 @access(i8* %3, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %23, label %17
17: ; preds = %12
%18 = load i32, i32* @S_IRWXU
%19 = load i32, i32* @S_IRWXG
%20 = or i32 %19, %18
%21 = call i64 @mkdir(i8* %3, i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %17, %12
store i8 47, i8* %13
%24 = getelementptr inbounds i8, i8* %13, i64 1
%25 = call i8* @strchr(i8* dereferenceable(1) %24, i8 signext 47)
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %12
27: ; preds = %17, %23, %1
%28 = phi i32 [ 0, %1 ], [ 0, %23 ], [ -1, %17 ]
ret i32 %28
}