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

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
char *dirname(char *);
int /*<<< orphan*/ free(char *);
scalar_t__ mkdir(char *, int /*<<< orphan*/);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strdup(char *);
char *stripslash(int /*<<< orphan*/);
int make_valid_path(char *dir, mode_t mode) {
  struct stat st;
  char *tmp = NULL, *path = stripslash(strdup(dir));
  int retval;
  if (stat(path, &st) == 0) {
    if (S_ISDIR(st.st_mode)) {
      retval = 1;
      goto end;
    } else {
      retval = 0;
      goto end;
    }
  }
  if (!make_valid_path(tmp = dirname(path), mode)) {
    retval = 0;
    goto end;
  }
  if (mkdir(path, mode) < 0) {
    retval = 0;
    goto end;
  }
  retval = 1;
end:
  if (tmp != NULL)
    free(tmp);
  if (path != NULL)
    free(path);
  return retval;
}


x86 assembly (clang, -O0):
<make_valid_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %eax,%edi
call   1040 <stripslash@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jne    11ec <make_valid_path+0x6c>
mov    -0x10(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%rax
je     11e0 <make_valid_path+0x60>
movl   $0x1,-0x24(%rbp)
jmp    1242 <make_valid_path+0xc2>
movl   $0x0,-0x24(%rbp)
jmp    1242 <make_valid_path+0xc2>
mov    -0x20(%rbp),%rdi
call   1070 <dirname@plt>
mov    %rax,%rdi
mov    %rdi,-0x18(%rbp)
mov    -0xc(%rbp),%esi
call   1090 <make_valid_path@plt>
cmp    $0x0,%eax
jne    1219 <make_valid_path+0x99>
movl   $0x0,-0x24(%rbp)
jmp    1242 <make_valid_path+0xc2>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <mkdir@plt>
cmp    $0x0,%rax
jge    123b <make_valid_path+0xbb>
movl   $0x0,-0x24(%rbp)
jmp    1242 <make_valid_path+0xc2>
movl   $0x1,-0x24(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1256 <make_valid_path+0xd6>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x20(%rbp)
je     126a <make_valid_path+0xea>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_valid_path>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
call   10a0 <strdup@plt>
mov    %eax,%edi
call   1040 <stripslash@plt>
mov    %rax,%r14
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
call   1080 <stat@plt>
test   %rax,%rax
je     11e0 <make_valid_path+0x60>
mov    %r14,%rdi
call   1070 <dirname@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %ebx,%esi
call   1090 <make_valid_path@plt>
test   %eax,%eax
je     11f3 <make_valid_path+0x73>
mov    %r14,%rdi
mov    %ebx,%esi
call   1050 <mkdir@plt>
mov    %rax,%rbx
shr    $0x3f,%rbx
xor    $0x1,%ebx
test   %r15,%r15
jne    11fa <make_valid_path+0x7a>
jmp    1202 <make_valid_path+0x82>
mov    0x8(%rsp),%edi
call   1060 <S_ISDIR@plt>
xor    %ebx,%ebx
test   %rax,%rax
setne  %bl
jmp    1202 <make_valid_path+0x82>
xor    %ebx,%ebx
test   %r15,%r15
je     1202 <make_valid_path+0x82>
mov    %r15,%rdi
call   1030 <free@plt>
test   %r14,%r14
je     120f <make_valid_path+0x8f>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
define i32 @make_valid_path(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i8* null, i8** %6
%9 = load i8*, i8** %3
%10 = call i32 @strdup(i8* %9)
%11 = call i8* @stripslash(i32 %10)
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = call i64 @stat(i8* %12, %struct.stat* %5)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %2
%16 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i64 @S_ISDIR(i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %15
store i32 1, i32* %8
br label %36
21: ; preds = %15
store i32 0, i32* %8
br label %36
22: ; preds = %2
%23 = load i8*, i8** %7
%24 = call i8* @dirname(i8* %23)
store i8* %24, i8** %6
%25 = load i32, i32* %4
%26 = call i32 @make_valid_path(i8* %24, i32 %25)
%27 = icmp ne i32 %26, 0
br i1 %27, label %29, label %28
28: ; preds = %22
store i32 0, i32* %8
br label %36
29: ; preds = %22
%30 = load i8*, i8** %7
%31 = load i32, i32* %4
%32 = call i64 @mkdir(i8* %30, i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %29
store i32 0, i32* %8
br label %36
35: ; preds = %29
store i32 1, i32* %8
br label %36
36: ; preds = %35, %34, %28, %21, %20
%37 = load i8*, i8** %6
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = load i8*, i8** %6
%41 = call i32 @free(i8* %40)
br label %42
42: ; preds = %39, %36
%43 = load i8*, i8** %7
%44 = icmp ne i8* %43, null
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i8*, i8** %7
%47 = call i32 @free(i8* %46)
br label %48
48: ; preds = %45, %42
%49 = load i32, i32* %8
ret i32 %49
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define i32 @make_valid_path(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = tail call i32 @strdup(i8* %0)
%6 = tail call i8* @stripslash(i32 %5)
%7 = call i64 @stat(i8* %6, %struct.stat* %3)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %2
%10 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%11 = load i32, i32* %10
%12 = call i64 @S_ISDIR(i32 %11)
%13 = icmp ne i64 %12, 0
%14 = zext i1 %13 to i32
br label %29
15: ; preds = %2
%16 = call i8* @dirname(i8* %6)
%17 = call i32 @make_valid_path(i8* %16, i32 %1)
%18 = icmp eq i32 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %15
%20 = call i64 @mkdir(i8* %6, i32 %1)
%21 = lshr i64 %20, 63
%22 = trunc i64 %21 to i32
%23 = xor i32 %22, 1
br label %24
24: ; preds = %19, %15
%25 = phi i32 [ 0, %15 ], [ %23, %19 ]
%26 = icmp eq i8* %16, null
br i1 %26, label %29, label %27
27: ; preds = %24
%28 = call i32 @free(i8* %16)
br label %29
29: ; preds = %9, %27, %24
%30 = phi i32 [ %14, %9 ], [ %25, %27 ], [ %25, %24 ]
%31 = icmp eq i8* %6, null
br i1 %31, label %34, label %32
32: ; preds = %29
%33 = call i32 @free(i8* %6)
br label %34
34: ; preds = %32, %29
ret i32 %30
}