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

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ ENOTEMPTY;
scalar_t__ errno;
int rmdir(char *);
int /*<<< orphan*/ strcpy(char *, char const *);
char *strrchr(char *, char);
int /*<<< orphan*/ udev_log_null(struct udev *, char *, char *);
int util_delete_path(struct udev *udev, const char *path) {
  char p[1024];
  char *pos;
  int retval;
  strcpy(p, path);
  pos = strrchr(p, '/');
  if (pos == p || pos == NULL)
    return 0;
  while (1) {
    *pos = '\0';
    pos = strrchr(p, '/');
    if ((pos == p) || (pos == NULL))
      break;
    retval = rmdir(p);
    if (errno == ENOENT)
      retval = 0;
    if (retval) {
      if (errno == ENOTEMPTY)
        return 0;
      udev_log_null(udev, "rmdir(%s) failed: %m\n", p);
      break;
    }
    udev_log_null(udev, "removed '%s'\n", p);
  }
  return 0;
}


x86 assembly (clang, -O0):
<util_delete_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x420(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x428(%rbp)
mov    -0x428(%rbp),%rax
lea    -0x420(%rbp),%rcx
cmp    %rcx,%rax
je     11a0 <util_delete_path+0x60>
cmpq   $0x0,-0x428(%rbp)
jne    11ac <util_delete_path+0x6c>
movl   $0x0,-0x4(%rbp)
jmp    12a5 <util_delete_path+0x165>
jmp    11b1 <util_delete_path+0x71>
mov    -0x428(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x420(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x428(%rbp)
mov    -0x428(%rbp),%rax
lea    -0x420(%rbp),%rcx
cmp    %rcx,%rax
je     11f8 <util_delete_path+0xb8>
cmpq   $0x0,-0x428(%rbp)
jne    11fd <util_delete_path+0xbd>
jmp    129e <util_delete_path+0x15e>
lea    -0x420(%rbp),%rdi
call   1050 <rmdir@plt>
mov    %eax,-0x42c(%rbp)
mov    0x2dca(%rip),%rax
mov    (%rax),%rax
mov    0x2db0(%rip),%rcx
cmp    (%rcx),%rax
jne    1233 <util_delete_path+0xf3>
movl   $0x0,-0x42c(%rbp)
cmpl   $0x0,-0x42c(%rbp)
je     1282 <util_delete_path+0x142>
mov    0x2d99(%rip),%rax
mov    (%rax),%rax
mov    0x2d77(%rip),%rcx
cmp    (%rcx),%rax
jne    1266 <util_delete_path+0x126>
movl   $0x0,-0x4(%rbp)
jmp    12a5 <util_delete_path+0x165>
mov    -0x10(%rbp),%rdi
lea    -0x420(%rbp),%rdx
lea    0xd88(%rip),%rsi
call   1040 <udev_log_null@plt>
jmp    129e <util_delete_path+0x15e>
mov    -0x10(%rbp),%rdi
lea    -0x420(%rbp),%rdx
lea    0xd82(%rip),%rsi
call   1040 <udev_log_null@plt>
jmp    11b1 <util_delete_path+0x71>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_delete_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%r14
mov    %rsp,%r15
mov    %r15,%rdi
call   1030 <strcpy@plt>
mov    %r15,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
cmp    %r15,%rax
je     1221 <util_delete_path+0xe1>
test   %rax,%rax
je     1221 <util_delete_path+0xe1>
movb   $0x0,(%rax)
mov    %r15,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
cmp    %r15,%rax
je     1221 <util_delete_path+0xe1>
mov    %rax,%rbx
test   %rax,%rax
je     1221 <util_delete_path+0xe1>
mov    0x2e36(%rip),%r13
mov    0x2e1f(%rip),%rbp
lea    0xe5e(%rip),%r12
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1050 <rmdir@plt>
test   %eax,%eax
je     11d6 <util_delete_path+0x96>
mov    0x0(%r13),%rax
cmp    0x0(%rbp),%rax
jne    1203 <util_delete_path+0xc3>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   1040 <udev_log_null@plt>
movb   $0x0,(%rbx)
mov    %r15,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
cmp    %r15,%rax
je     1221 <util_delete_path+0xe1>
mov    %rax,%rbx
test   %rax,%rax
jne    11c0 <util_delete_path+0x80>
jmp    1221 <util_delete_path+0xe1>
mov    0x2dbe(%rip),%rcx
cmp    (%rcx),%rax
je     1221 <util_delete_path+0xe1>
lea    0xdea(%rip),%rsi
mov    %rsp,%rdx
mov    %r14,%rdi
call   1040 <udev_log_null@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.udev = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@ENOTEMPTY = global i64 0
@.str = [22 x i8] c"rmdir(%s) failed: %m\0A\00"
@.str.1 = [14 x i8] c"removed '%s'\0A\00"
define i32 @util_delete_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 i32
store %struct.udev* %0, %struct.udev** %4
store i8* %1, i8** %5
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%10 = load i8*, i8** %5
%11 = call i32 @strcpy(i8* %9, i8* %10)
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%13 = call i8* @strrchr(i8* %12, i8 signext 47)
store i8* %13, i8** %7
%14 = load i8*, i8** %7
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%16 = icmp eq i8* %14, %15
br i1 %16, label %20, label %17
17: ; preds = %2
%18 = load i8*, i8** %7
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %17, %2
store i32 0, i32* %3
br label %57
21: ; preds = %17
br label %22
22: ; preds = %21, %52
%23 = load i8*, i8** %7
store i8 0, i8* %23
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%25 = call i8* @strrchr(i8* %24, i8 signext 47)
store i8* %25, i8** %7
%26 = load i8*, i8** %7
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%28 = icmp eq i8* %26, %27
br i1 %28, label %32, label %29
29: ; preds = %22
%30 = load i8*, i8** %7
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %33
32: ; preds = %29, %22
br label %56
33: ; preds = %29
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%35 = call i32 @rmdir(i8* %34)
store i32 %35, i32* %8
%36 = load i64, i64* @errno
%37 = load i64, i64* @ENOENT
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %33
store i32 0, i32* %8
br label %40
40: ; preds = %39, %33
%41 = load i32, i32* %8
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %52
43: ; preds = %40
%44 = load i64, i64* @errno
%45 = load i64, i64* @ENOTEMPTY
%46 = icmp eq i64 %44, %45
br i1 %46, label %47, label %48
47: ; preds = %43
store i32 0, i32* %3
br label %57
48: ; preds = %43
%49 = load %struct.udev*, %struct.udev** %4
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%51 = call i32 @udev_log_null(%struct.udev* %49, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %50)
br label %56
52: ; preds = %40
%53 = load %struct.udev*, %struct.udev** %4
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%55 = call i32 @udev_log_null(%struct.udev* %53, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %54)
br label %22
56: ; preds = %48, %32
store i32 0, i32* %3
br label %57
57: ; preds = %56, %47, %20
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
%struct.udev = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@ENOTEMPTY = global i64 0
@.str = [22 x i8] c"rmdir(%s) failed: %m\0A\00"
@.str.1 = [14 x i8] c"removed '%s'\0A\00"
define i32 @util_delete_path(%struct.udev* %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = call i32 @strcpy(i8* %4, i8* %1)
%6 = call i8* @strrchr(i8* %4, i8 signext 47)
%7 = icmp eq i8* %6, %4
%8 = icmp eq i8* %6, null
%9 = or i1 %7, %8
br i1 %9, label %34, label %10
10: ; preds = %2
store i8 0, i8* %6
%11 = call i8* @strrchr(i8* %4, i8 signext 47)
%12 = icmp eq i8* %11, %4
%13 = icmp eq i8* %11, null
%14 = or i1 %12, %13
br i1 %14, label %34, label %15
15: ; preds = %10, %28
%16 = phi i8* [ %30, %28 ], [ %11, %10 ]
%17 = call i32 @rmdir(i8* %4)
%18 = load i64, i64* @errno
%19 = load i64, i64* @ENOENT
%20 = icmp eq i64 %18, %19
%21 = icmp eq i32 %17, 0
%22 = or i1 %21, %20
br i1 %22, label %28, label %23
23: ; preds = %15
%24 = load i64, i64* @ENOTEMPTY
%25 = icmp eq i64 %18, %24
br i1 %25, label %34, label %26
26: ; preds = %23
%27 = call i32 @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %4)
br label %34
28: ; preds = %15
%29 = call i32 @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %4)
store i8 0, i8* %16
%30 = call i8* @strrchr(i8* %4, i8 signext 47)
%31 = icmp eq i8* %30, %4
%32 = icmp eq i8* %30, null
%33 = or i1 %31, %32
br i1 %33, label %34, label %15
34: ; preds = %28, %10, %26, %23, %2
ret i32 0
}