#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__ EEXIST;
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
int mkdir(char *, int);
char *strdup(char const *);
int strlen(char *);
int _slog_mkdir_recursive(const char *dir) {
  char *dup_dir = strdup(dir);
  int len = strlen(dup_dir);
  int ret = 0;
  int i = dir[0] == '/' ? 1 : 0;
  for (; i <= len; ++i) {
    if (dup_dir[i] == '/' || dup_dir[i] == '\0') {
      char ch = dup_dir[i];
      dup_dir[i] = '\0';
      if (mkdir(dup_dir, 0775) == -1 && errno != EEXIST) {
        ret = -1;
        break;
      }
      dup_dir[i] = ch;
    }
  }
  free(dup_dir);
  return ret;
}


x86 assembly (clang, -O0):
<_slog_mkdir_recursive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x2f,%edx
cmove  %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     1231 <_slog_mkdir_recursive+0xf1>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     11b9 <_slog_mkdir_recursive+0x79>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    121e <_slog_mkdir_recursive+0xde>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x1d(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    $0x1fd,%esi
call   1040 <mkdir@plt>
cmp    $0xffffffff,%eax
jne    1210 <_slog_mkdir_recursive+0xd0>
mov    0x2de7(%rip),%rax
mov    (%rax),%rax
mov    0x2de5(%rip),%rcx
cmp    (%rcx),%rax
je     1210 <_slog_mkdir_recursive+0xd0>
movl   $0xffffffff,-0x18(%rbp)
jmp    1231 <_slog_mkdir_recursive+0xf1>
mov    -0x1d(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1223 <_slog_mkdir_recursive+0xe3>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1183 <_slog_mkdir_recursive+0x43>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_slog_mkdir_recursive>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1060 <strdup@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %eax,%r15d
mov    (%r14),%al
xor    %ecx,%ecx
cmp    $0x2f,%al
sete   %cl
xor    %ebp,%ebp
cmp    %ecx,%r15d
jl     11e3 <_slog_mkdir_recursive+0xa3>
xor    %ebp,%ebp
cmp    $0x2f,%al
sete   %bpl
add    $0x1,%r15d
mov    0x2e53(%rip),%r12
mov    0x2e54(%rip),%r13
jmp    11b3 <_slog_mkdir_recursive+0x73>
xchg   %ax,%ax
movb   $0x0,(%rbx,%rbp,1)
mov    %rbx,%rdi
mov    $0x1fd,%esi
call   1040 <mkdir@plt>
cmp    $0xffffffff,%eax
je     11d0 <_slog_mkdir_recursive+0x90>
mov    %r14b,(%rbx,%rbp,1)
add    $0x1,%rbp
cmp    %rbp,%r15
je     11e1 <_slog_mkdir_recursive+0xa1>
movzbl (%rbx,%rbp,1),%r14d
cmp    $0x2f,%r14b
je     1190 <_slog_mkdir_recursive+0x50>
test   %r14b,%r14b
je     1190 <_slog_mkdir_recursive+0x50>
jmp    11aa <_slog_mkdir_recursive+0x6a>
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     11a6 <_slog_mkdir_recursive+0x66>
mov    $0xffffffff,%ebp
jmp    11e3 <_slog_mkdir_recursive+0xa3>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EEXIST = global i64 0
define i32 @_slog_mkdir_recursive(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = call i8* @strdup(i8* %8)
store i8* %9, i8** %3
%10 = load i8*, i8** %3
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %4
store i32 0, i32* %5
%12 = load i8*, i8** %2
%13 = getelementptr inbounds i8, i8* %12, i64 0
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 47
%17 = zext i1 %16 to i64
%18 = select i1 %16, i32 1, i32 0
store i32 %18, i32* %6
br label %19
19: ; preds = %64, %1
%20 = load i32, i32* %6
%21 = load i32, i32* %4
%22 = icmp sle i32 %20, %21
br i1 %22, label %23, label %67
23: ; preds = %19
%24 = load i8*, i8** %3
%25 = load i32, i32* %6
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 47
br i1 %30, label %39, label %31
31: ; preds = %23
%32 = load i8*, i8** %3
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %63
39: ; preds = %31, %23
%40 = load i8*, i8** %3
%41 = load i32, i32* %6
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
%44 = load i8, i8* %43
store i8 %44, i8* %7
%45 = load i8*, i8** %3
%46 = load i32, i32* %6
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
store i8 0, i8* %48
%49 = load i8*, i8** %3
%50 = call i32 @mkdir(i8* %49, i32 509)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %57
52: ; preds = %39
%53 = load i64, i64* @errno
%54 = load i64, i64* @EEXIST
%55 = icmp ne i64 %53, %54
br i1 %55, label %56, label %57
56: ; preds = %52
store i32 -1, i32* %5
br label %67
57: ; preds = %52, %39
%58 = load i8, i8* %7
%59 = load i8*, i8** %3
%60 = load i32, i32* %6
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %59, i64 %61
store i8 %58, i8* %62
br label %63
63: ; preds = %57, %31
br label %64
64: ; preds = %63
%65 = load i32, i32* %6
%66 = add nsw i32 %65, 1
store i32 %66, i32* %6
br label %19
67: ; preds = %56, %19
%68 = load i8*, i8** %3
%69 = call i32 @free(i8* %68)
%70 = load i32, i32* %5
ret i32 %70
}

LLVM IR (-O2):
@errno = global i64 0
@EEXIST = global i64 0
define i32 @_slog_mkdir_recursive(i8* nocapture %0) {
%2 = tail call i8* @strdup(i8* %0)
%3 = tail call i32 @strlen(i8* %2)
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 47
%6 = zext i1 %5 to i32
%7 = icmp slt i32 %3, %6
br i1 %7, label %27, label %8
8: ; preds = %1
%9 = zext i1 %5 to i64
%10 = add i32 %3, 1
%11 = zext i32 %10 to i64
br label %12
12: ; preds = %8, %24
%13 = phi i64 [ %9, %8 ], [ %25, %24 ]
%14 = getelementptr inbounds i8, i8* %2, i64 %13
%15 = load i8, i8* %14
switch i8 %15, label %24 [
i8 47, label %16
i8 0, label %16
]
16: ; preds = %12, %12
store i8 0, i8* %14
%17 = tail call i32 @mkdir(i8* %2, i32 509)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i64, i64* @errno
%21 = load i64, i64* @EEXIST
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %27
23: ; preds = %16, %19
store i8 %15, i8* %14
br label %24
24: ; preds = %23, %12
%25 = add nuw nsw i64 %13, 1
%26 = icmp eq i64 %25, %11
br i1 %26, label %27, label %12
27: ; preds = %24, %19, %1
%28 = phi i32 [ 0, %1 ], [ -1, %19 ], [ 0, %24 ]
%29 = tail call i32 @free(i8* %2)
ret i32 %28
}