#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;
};

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ create_directory(char *, int);
char *dirname(char *);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
int rmdir(char const *);
int stat(char const *, struct stat *);
char *strdup(char const *);
int unlink(char const *);
bool prepare_path(const char *path) {
  char *d = strdup(path);
  char *parent = dirname(d);
  if (!create_directory(parent, 0700)) {
    free(d);
    return false;
  }
  free(d);
  struct stat s;
  if (-1 == stat(path, &s)) {
    if (errno == ENOENT)
      return true;
    return false;
  }
  if (S_ISDIR(s.st_mode)) {
    if (-1 == rmdir(path))
      return errno == ENOENT;
    return true;
  } else {
    if (-1 == unlink(path))
      return errno == ENOENT;
    return true;
  }
}


x86 assembly (clang, -O0):
<prepare_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <dirname@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x1c0,%esi
call   10a0 <create_directory@plt>
cmp    $0x0,%eax
jne    11d2 <prepare_path+0x52>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c6 <prepare_path+0x146>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <stat@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1229 <prepare_path+0xa9>
mov    0x2de2(%rip),%rax
mov    (%rax),%rax
mov    0x2dc8(%rip),%rcx
cmp    (%rcx),%rax
jne    121d <prepare_path+0x9d>
movl   $0x1,-0x4(%rbp)
jmp    12c6 <prepare_path+0x146>
movl   $0x0,-0x4(%rbp)
jmp    12c6 <prepare_path+0x146>
mov    -0x28(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1283 <prepare_path+0x103>
mov    -0x10(%rbp),%rdi
call   1050 <rmdir@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1277 <prepare_path+0xf7>
mov    0x2d86(%rip),%rax
mov    (%rax),%rax
mov    0x2d6c(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
jmp    12c6 <prepare_path+0x146>
movl   $0x1,-0x4(%rbp)
jmp    12c6 <prepare_path+0x146>
mov    -0x10(%rbp),%rdi
call   1040 <unlink@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12bf <prepare_path+0x13f>
mov    0x2d3e(%rip),%rax
mov    (%rax),%rax
mov    0x2d24(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
jmp    12c6 <prepare_path+0x146>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prepare_path>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
call   1090 <strdup@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <dirname@plt>
mov    %rax,%rdi
mov    $0x1c0,%esi
call   10a0 <create_directory@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
test   %ebp,%ebp
je     11e0 <prepare_path+0x60>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
je     11f0 <prepare_path+0x70>
mov    0x8(%rsp),%edi
call   1060 <S_ISDIR@plt>
mov    %r14,%rdi
test   %rax,%rax
je     11e4 <prepare_path+0x64>
call   1050 <rmdir@plt>
jmp    11e9 <prepare_path+0x69>
xor    %eax,%eax
jmp    120a <prepare_path+0x8a>
call   1040 <unlink@plt>
mov    $0x1,%cl
cmp    $0xffffffff,%eax
jne    1207 <prepare_path+0x87>
mov    0x2de9(%rip),%rax
mov    (%rax),%rax
mov    0x2dcf(%rip),%rcx
cmp    (%rcx),%rax
sete   %cl
movzbl %cl,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
define i32 @prepare_path(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = tail call i8* @strdup(i8* %0)
%4 = tail call i8* @dirname(i8* %3)
%5 = tail call i32 @create_directory(i8* %4, i32 448)
%6 = icmp eq i32 %5, 0
%7 = tail call i32 @free(i8* %3)
br i1 %6, label %38, label %8
8: ; preds = %1
%9 = bitcast %struct.stat* %2 to i8*
%10 = call i32 @stat(i8* %0, %struct.stat* %2)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = load i64, i64* @errno
%14 = load i64, i64* @ENOENT
%15 = icmp eq i64 %13, %14
br label %35
16: ; preds = %8
%17 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%18 = load i32, i32* %17
%19 = tail call i64 @S_ISDIR(i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %28, label %21
21: ; preds = %16
%22 = tail call i32 @rmdir(i8* %0)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %35
24: ; preds = %21
%25 = load i64, i64* @errno
%26 = load i64, i64* @ENOENT
%27 = icmp eq i64 %25, %26
br label %35
28: ; preds = %16
%29 = tail call i32 @unlink(i8* %0)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = load i64, i64* @errno
%33 = load i64, i64* @ENOENT
%34 = icmp eq i64 %32, %33
br label %35
35: ; preds = %28, %21, %12, %31, %24
%36 = phi i1 [ %27, %24 ], [ %34, %31 ], [ %15, %12 ], [ true, %21 ], [ true, %28 ]
%37 = zext i1 %36 to i32
br label %38
38: ; preds = %1, %35
%39 = phi i32 [ %37, %35 ], [ 0, %1 ]
ret i32 %39
}