#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 udev {
  int dummy;
};
struct stat {
  int st_mode;
};
typedef int /*<<< orphan*/ p;

/* Variables and functions */
scalar_t__ EEXIST;
int S_IFDIR;
int S_IFMT;
scalar_t__ errno;
int mkdir(char *, int);
scalar_t__ stat(char *, struct stat *);
char *strrchr(char *, char);
int /*<<< orphan*/ udev_log_null(struct udev *, char *, char *);
int /*<<< orphan*/ udev_selinux_resetfscreatecon(struct udev *);
int /*<<< orphan*/ udev_selinux_setfscreatecon(struct udev *, char *, int);
int /*<<< orphan*/ util_strlcpy(char *, char const *, int);
int util_create_path(struct udev *udev, const char *path) {
  char p[1024];
  char *pos;
  struct stat stats;
  int ret;
  util_strlcpy(p, path, sizeof(p));
  pos = strrchr(p, '/');
  if (pos == p || pos == NULL)
    return 0;
  while (pos[-1] == '/')
    pos--;
  pos[0] = '\0';
  udev_log_null(udev, "stat '%s'\n", p);
  if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
    return 0;
  if (util_create_path(udev, p) != 0)
    return -1;
  udev_log_null(udev, "mkdir '%s'\n", p);
  udev_selinux_setfscreatecon(udev, p, S_IFDIR | 0755);
  ret = mkdir(p, 0755);
  udev_selinux_resetfscreatecon(udev);
  if (ret == 0)
    return 0;
  if (errno == EEXIST)
    if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
      return 0;
  return -1;
}


x86 assembly (clang, -O0):
<util_create_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x400,%edx
call   10a0 <util_strlcpy@plt>
lea    -0x420(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x428(%rbp)
mov    -0x428(%rbp),%rax
lea    -0x420(%rbp),%rcx
cmp    %rcx,%rax
je     11e5 <util_create_path+0x65>
cmpq   $0x0,-0x428(%rbp)
jne    11f1 <util_create_path+0x71>
movl   $0x0,-0x4(%rbp)
jmp    138a <util_create_path+0x20a>
jmp    11f6 <util_create_path+0x76>
mov    -0x428(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0x2f,%eax
jne    1221 <util_create_path+0xa1>
mov    -0x428(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x428(%rbp)
jmp    11f6 <util_create_path+0x76>
mov    -0x428(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x420(%rbp),%rdx
lea    0xdc3(%rip),%rsi
call   1040 <udev_log_null@plt>
lea    -0x420(%rbp),%rdi
lea    -0x430(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jne    1289 <util_create_path+0x109>
mov    -0x430(%rbp),%eax
mov    0x2d74(%rip),%rcx
and    (%rcx),%eax
mov    0x2d4b(%rip),%rcx
cmp    (%rcx),%eax
jne    1289 <util_create_path+0x109>
movl   $0x0,-0x4(%rbp)
jmp    138a <util_create_path+0x20a>
mov    -0x10(%rbp),%rdi
lea    -0x420(%rbp),%rsi
call   1060 <util_create_path@plt>
cmp    $0x0,%eax
je     12ae <util_create_path+0x12e>
movl   $0xffffffff,-0x4(%rbp)
jmp    138a <util_create_path+0x20a>
mov    -0x10(%rbp),%rdi
lea    -0x420(%rbp),%rdx
lea    0xd4b(%rip),%rsi
call   1040 <udev_log_null@plt>
mov    -0x10(%rbp),%rdi
lea    -0x420(%rbp),%rsi
mov    0x2ce9(%rip),%rax
mov    (%rax),%edx
or     $0x1ed,%edx
call   1090 <udev_selinux_setfscreatecon@plt>
lea    -0x420(%rbp),%rdi
mov    $0x1ed,%esi
call   1030 <mkdir@plt>
mov    %eax,-0x434(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <udev_selinux_resetfscreatecon@plt>
cmpl   $0x0,-0x434(%rbp)
jne    131d <util_create_path+0x19d>
movl   $0x0,-0x4(%rbp)
jmp    138a <util_create_path+0x20a>
mov    0x2cac(%rip),%rax
mov    (%rax),%rax
mov    0x2caa(%rip),%rcx
cmp    (%rcx),%rax
jne    1383 <util_create_path+0x203>
lea    -0x420(%rbp),%rdi
lea    -0x430(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jne    137e <util_create_path+0x1fe>
mov    -0x430(%rbp),%eax
mov    0x2c7f(%rip),%rcx
and    (%rcx),%eax
mov    0x2c56(%rip),%rcx
cmp    (%rcx),%eax
jne    137e <util_create_path+0x1fe>
movl   $0x0,-0x4(%rbp)
jmp    138a <util_create_path+0x20a>
jmp    1383 <util_create_path+0x203>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_create_path>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    %rdi,%rbx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x400,%edx
call   10a0 <util_strlcpy@plt>
mov    %rbp,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
xor    %r14d,%r14d
cmp    %rbp,%rax
je     1225 <util_create_path+0xa5>
test   %rax,%rax
je     1225 <util_create_path+0xa5>
add    $0x1,%rax
cmpb   $0x2f,-0x2(%rax)
lea    -0x1(%rax),%rax
je     11c0 <util_create_path+0x40>
movb   $0x0,(%rax)
lea    0xe2c(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbx,%rdi
mov    %rbp,%rdx
call   1040 <udev_log_null@plt>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1070 <stat@plt>
test   %rax,%rax
jne    120e <util_create_path+0x8e>
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
and    0x8(%rsp),%eax
mov    0x2db6(%rip),%rcx
cmp    (%rcx),%eax
je     1225 <util_create_path+0xa5>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <util_create_path@plt>
test   %eax,%eax
je     1236 <util_create_path+0xb6>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdce(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbx,%rdi
mov    %rbp,%rdx
call   1040 <udev_log_null@plt>
mov    0x2d6c(%rip),%r15
mov    $0x1ed,%eax
mov    (%r15),%edx
or     %eax,%edx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1090 <udev_selinux_setfscreatecon@plt>
mov    %rbp,%rdi
mov    $0x1ed,%esi
call   1030 <mkdir@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1080 <udev_selinux_resetfscreatecon@plt>
test   %ebp,%ebp
je     1225 <util_create_path+0xa5>
mov    0x2d45(%rip),%rax
mov    (%rax),%rax
mov    0x2d43(%rip),%rcx
cmp    (%rcx),%rax
jne    121f <util_create_path+0x9f>
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1070 <stat@plt>
test   %rax,%rax
jne    121f <util_create_path+0x9f>
mov    0x2d27(%rip),%rax
mov    (%rax),%eax
and    0x8(%rsp),%eax
cmp    (%r15),%eax
jne    121f <util_create_path+0x9f>
jmp    1225 <util_create_path+0xa5>

LLVM IR (-O0):
%struct.udev = type { i32 }
%struct.stat = type { i32 }
@.str = [11 x i8] c"stat '%s'\0A\00"
@S_IFMT = global i32 0
@S_IFDIR = global i32 0
@.str.1 = [12 x i8] c"mkdir '%s'\0A\00"
@errno = global i64 0
@EEXIST = global i64 0
define i32 @util_create_path(%struct.udev* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.udev*
%5 = alloca i8*
%6 = alloca [1024 x i8]
%7 = alloca i8*
%8 = alloca %struct.stat
%9 = alloca i32
store %struct.udev* %0, %struct.udev** %4
store i8* %1, i8** %5
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%11 = load i8*, i8** %5
%12 = call i32 @util_strlcpy(i8* %10, i8* %11, i32 1024)
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%14 = call i8* @strrchr(i8* %13, i8 signext 47)
store i8* %14, i8** %7
%15 = load i8*, i8** %7
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%17 = icmp eq i8* %15, %16
br i1 %17, label %21, label %18
18: ; preds = %2
%19 = load i8*, i8** %7
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %22
21: ; preds = %18, %2
store i32 0, i32* %3
br label %89
22: ; preds = %18
br label %23
23: ; preds = %29, %22
%24 = load i8*, i8** %7
%25 = getelementptr inbounds i8, i8* %24, i64 -1
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 47
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = load i8*, i8** %7
%31 = getelementptr inbounds i8, i8* %30, i32 -1
store i8* %31, i8** %7
br label %23
32: ; preds = %23
%33 = load i8*, i8** %7
%34 = getelementptr inbounds i8, i8* %33, i64 0
store i8 0, i8* %34
%35 = load %struct.udev*, %struct.udev** %4
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%37 = call i32 @udev_log_null(%struct.udev* %35, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %36)
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%39 = call i64 @stat(i8* %38, %struct.stat* %8)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %49
41: ; preds = %32
%42 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%43 = load i32, i32* %42
%44 = load i32, i32* @S_IFMT
%45 = and i32 %43, %44
%46 = load i32, i32* @S_IFDIR
%47 = icmp eq i32 %45, %46
br i1 %47, label %48, label %49
48: ; preds = %41
store i32 0, i32* %3
br label %89
49: ; preds = %41, %32
%50 = load %struct.udev*, %struct.udev** %4
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%52 = call i32 @util_create_path(%struct.udev* %50, i8* %51)
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %49
store i32 -1, i32* %3
br label %89
55: ; preds = %49
%56 = load %struct.udev*, %struct.udev** %4
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%58 = call i32 @udev_log_null(%struct.udev* %56, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %57)
%59 = load %struct.udev*, %struct.udev** %4
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%61 = load i32, i32* @S_IFDIR
%62 = or i32 %61, 493
%63 = call i32 @udev_selinux_setfscreatecon(%struct.udev* %59, i8* %60, i32 %62)
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%65 = call i32 @mkdir(i8* %64, i32 493)
store i32 %65, i32* %9
%66 = load %struct.udev*, %struct.udev** %4
%67 = call i32 @udev_selinux_resetfscreatecon(%struct.udev* %66)
%68 = load i32, i32* %9
%69 = icmp eq i32 %68, 0
br i1 %69, label %70, label %71
70: ; preds = %55
store i32 0, i32* %3
br label %89
71: ; preds = %55
%72 = load i64, i64* @errno
%73 = load i64, i64* @EEXIST
%74 = icmp eq i64 %72, %73
br i1 %74, label %75, label %88
75: ; preds = %71
%76 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%77 = call i64 @stat(i8* %76, %struct.stat* %8)
%78 = icmp eq i64 %77, 0
br i1 %78, label %79, label %87
79: ; preds = %75
%80 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%81 = load i32, i32* %80
%82 = load i32, i32* @S_IFMT
%83 = and i32 %81, %82
%84 = load i32, i32* @S_IFDIR
%85 = icmp eq i32 %83, %84
br i1 %85, label %86, label %87
86: ; preds = %79
store i32 0, i32* %3
br label %89
87: ; preds = %79, %75
br label %88
88: ; preds = %87, %71
store i32 -1, i32* %3
br label %89
89: ; preds = %88, %86, %70, %54, %48, %21
%90 = load i32, i32* %3
ret i32 %90
}

LLVM IR (-O2):
%struct.udev = type { i32 }
%struct.stat = type { i32 }
@.str = [11 x i8] c"stat '%s'\0A\00"
@S_IFMT = global i32 0
@S_IFDIR = global i32 0
@.str.1 = [12 x i8] c"mkdir '%s'\0A\00"
@errno = global i64 0
@EEXIST = global i64 0
define i32 @util_create_path(%struct.udev* %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.stat* %4 to i8*
%7 = call i32 @util_strlcpy(i8* %5, i8* %1, i32 1024)
%8 = call i8* @strrchr(i8* %5, i8 signext 47)
%9 = icmp eq i8* %8, %5
%10 = icmp eq i8* %8, null
%11 = or i1 %9, %10
br i1 %11, label %54, label %12
12: ; preds = %2, %12
%13 = phi i8* [ %14, %12 ], [ %8, %2 ]
%14 = getelementptr inbounds i8, i8* %13, i64 -1
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 47
br i1 %16, label %12, label %17
17: ; preds = %12
store i8 0, i8* %13
%18 = call i32 @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %5)
%19 = call i64 @stat(i8* %5, %struct.stat* %4)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %17
%22 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%23 = load i32, i32* %22
%24 = load i32, i32* @S_IFMT
%25 = and i32 %24, %23
%26 = load i32, i32* @S_IFDIR
%27 = icmp eq i32 %25, %26
br i1 %27, label %54, label %28
28: ; preds = %21, %17
%29 = call i32 @util_create_path(%struct.udev* %0, i8* %5)
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %54
31: ; preds = %28
%32 = call i32 @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%33 = load i32, i32* @S_IFDIR
%34 = or i32 %33, 493
%35 = call i32 @udev_selinux_setfscreatecon(%struct.udev* %0, i8* %5, i32 %34)
%36 = call i32 @mkdir(i8* %5, i32 493)
%37 = call i32 @udev_selinux_resetfscreatecon(%struct.udev* %0)
%38 = icmp eq i32 %36, 0
br i1 %38, label %54, label %39
39: ; preds = %31
%40 = load i64, i64* @errno
%41 = load i64, i64* @EEXIST
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %53
43: ; preds = %39
%44 = call i64 @stat(i8* %5, %struct.stat* %4)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %53
46: ; preds = %43
%47 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%48 = load i32, i32* %47
%49 = load i32, i32* @S_IFMT
%50 = and i32 %49, %48
%51 = load i32, i32* @S_IFDIR
%52 = icmp eq i32 %50, %51
br i1 %52, label %54, label %53
53: ; preds = %43, %46, %39
br label %54
54: ; preds = %46, %31, %28, %21, %2, %53
%55 = phi i32 [ -1, %53 ], [ 0, %2 ], [ 0, %21 ], [ -1, %28 ], [ 0, %31 ], [ 0, %46 ]
ret i32 %55
}