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

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ errno;
int rmdir(char *);
char *strrchr(char *, char);
int /*<<< orphan*/ util_strscpy(char *, int, char const *);
int util_delete_path(struct udev *udev, const char *path) {
  char p[1024];
  char *pos;
  int err = 0;
  if (path[0] == '/')
    while (path[1] == '/')
      path++;
  util_strscpy(p, sizeof(p), path);
  pos = strrchr(p, '/');
  if (pos == p || pos == NULL)
    return 0;
  for (;;) {
    *pos = '\0';
    pos = strrchr(p, '/');
    if ((pos == p) || (pos == NULL))
      break;
    err = rmdir(p);
    if (err < 0) {
      if (errno == ENOENT)
        err = 0;
      break;
    }
  }
  return err;
}


x86 assembly (clang, -O0):
<util_delete_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x42c(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1189 <util_delete_path+0x59>
jmp    1162 <util_delete_path+0x32>
mov    -0x18(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2f,%eax
jne    1184 <util_delete_path+0x54>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1162 <util_delete_path+0x32>
jmp    1189 <util_delete_path+0x59>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x400,%esi
call   1030 <util_strscpy@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     11db <util_delete_path+0xab>
cmpq   $0x0,-0x428(%rbp)
jne    11e7 <util_delete_path+0xb7>
movl   $0x0,-0x4(%rbp)
jmp    128e <util_delete_path+0x15e>
jmp    11ec <util_delete_path+0xbc>
mov    -0x428(%rbp),%rax
movb   $0x0,(%rax)
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     1233 <util_delete_path+0x103>
cmpq   $0x0,-0x428(%rbp)
jne    1238 <util_delete_path+0x108>
jmp    1285 <util_delete_path+0x155>
lea    -0x420(%rbp),%rdi
call   1040 <rmdir@plt>
mov    %eax,-0x42c(%rbp)
cmpl   $0x0,-0x42c(%rbp)
jge    1280 <util_delete_path+0x150>
mov    0x2d82(%rip),%rax
mov    (%rax),%rax
mov    0x2d68(%rip),%rcx
cmp    (%rcx),%rax
jne    127b <util_delete_path+0x14b>
movl   $0x0,-0x42c(%rbp)
jmp    1285 <util_delete_path+0x155>
jmp    11ec <util_delete_path+0xbc>
mov    -0x42c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_delete_path>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%rdx
cmpb   $0x2f,(%rsi)
jne    115d <util_delete_path+0x2d>
add    $0xffffffffffffffff,%rdx
mov    %rdx,%rax
nopw   0x0(%rax,%rax,1)
lea    0x1(%rax),%rdx
cmpb   $0x2f,0x2(%rax)
mov    %rdx,%rax
je     1150 <util_delete_path+0x20>
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x400,%esi
call   1030 <util_strscpy@plt>
mov    %r14,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
xor    %ebp,%ebp
cmp    %r14,%rax
je     11d6 <util_delete_path+0xa6>
test   %rax,%rax
je     11d6 <util_delete_path+0xa6>
movb   $0x0,(%rax)
nopl   0x0(%rax)
mov    %r14,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
cmp    %r14,%rax
je     11d6 <util_delete_path+0xa6>
mov    %rax,%rbx
test   %rax,%rax
je     11d6 <util_delete_path+0xa6>
mov    %r14,%rdi
call   1040 <rmdir@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11bd <util_delete_path+0x8d>
movb   $0x0,(%rbx)
jmp    1190 <util_delete_path+0x60>
mov    0x2e1c(%rip),%rax
mov    (%rax),%rax
mov    0x2e02(%rip),%rcx
xor    %edx,%edx
cmp    (%rcx),%rax
cmove  %edx,%ebp
mov    %ebp,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.udev = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
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
store i32 0, i32* %8
%9 = load i8*, i8** %5
%10 = getelementptr inbounds i8, i8* %9, i64 0
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %12, 47
br i1 %13, label %14, label %25
14: ; preds = %2
br label %15
15: ; preds = %21, %14
%16 = load i8*, i8** %5
%17 = getelementptr inbounds i8, i8* %16, i64 1
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 47
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = load i8*, i8** %5
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %5
br label %15
24: ; preds = %15
br label %25
25: ; preds = %24, %2
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%27 = load i8*, i8** %5
%28 = call i32 @util_strscpy(i8* %26, i32 1024, i8* %27)
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%30 = call i8* @strrchr(i8* %29, i8 signext 47)
store i8* %30, i8** %7
%31 = load i8*, i8** %7
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%33 = icmp eq i8* %31, %32
br i1 %33, label %37, label %34
34: ; preds = %25
%35 = load i8*, i8** %7
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %38
37: ; preds = %34, %25
store i32 0, i32* %3
br label %64
38: ; preds = %34
br label %39
39: ; preds = %61, %38
%40 = load i8*, i8** %7
store i8 0, i8* %40
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%42 = call i8* @strrchr(i8* %41, i8 signext 47)
store i8* %42, i8** %7
%43 = load i8*, i8** %7
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%45 = icmp eq i8* %43, %44
br i1 %45, label %49, label %46
46: ; preds = %39
%47 = load i8*, i8** %7
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %50
49: ; preds = %46, %39
br label %62
50: ; preds = %46
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%52 = call i32 @rmdir(i8* %51)
store i32 %52, i32* %8
%53 = load i32, i32* %8
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %61
55: ; preds = %50
%56 = load i64, i64* @errno
%57 = load i64, i64* @ENOENT
%58 = icmp eq i64 %56, %57
br i1 %58, label %59, label %60
59: ; preds = %55
store i32 0, i32* %8
br label %60
60: ; preds = %59, %55
br label %62
61: ; preds = %50
br label %39
62: ; preds = %60, %49
%63 = load i32, i32* %8
store i32 %63, i32* %3
br label %64
64: ; preds = %62, %37
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
%struct.udev = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
define i32 @util_delete_path(%struct.udev* nocapture readnone %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = load i8, i8* %1
%6 = icmp eq i8 %5, 47
br i1 %6, label %7, label %12
7: ; preds = %2, %7
%8 = phi i8* [ %9, %7 ], [ %1, %2 ]
%9 = getelementptr inbounds i8, i8* %8, i64 1
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 47
br i1 %11, label %7, label %12
12: ; preds = %7, %2
%13 = phi i8* [ %1, %2 ], [ %8, %7 ]
%14 = call i32 @util_strscpy(i8* %4, i32 1024, i8* %13)
%15 = call i8* @strrchr(i8* %4, i8 signext 47)
%16 = icmp eq i8* %15, %4
%17 = icmp eq i8* %15, null
%18 = or i1 %16, %17
br i1 %18, label %38, label %19
19: ; preds = %12
store i8 0, i8* %15
%20 = call i8* @strrchr(i8* %4, i8 signext 47)
%21 = icmp eq i8* %20, %4
%22 = icmp eq i8* %20, null
%23 = or i1 %21, %22
br i1 %23, label %38, label %29
24: ; preds = %29
store i8 0, i8* %30
%25 = call i8* @strrchr(i8* %4, i8 signext 47)
%26 = icmp eq i8* %25, %4
%27 = icmp eq i8* %25, null
%28 = or i1 %26, %27
br i1 %28, label %38, label %29
29: ; preds = %19, %24
%30 = phi i8* [ %25, %24 ], [ %20, %19 ]
%31 = call i32 @rmdir(i8* %4)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %24
33: ; preds = %29
%34 = load i64, i64* @errno
%35 = load i64, i64* @ENOENT
%36 = icmp eq i64 %34, %35
%37 = select i1 %36, i32 0, i32 %31
br label %38
38: ; preds = %24, %19, %33, %12
%39 = phi i32 [ 0, %12 ], [ %37, %33 ], [ 0, %19 ], [ %31, %24 ]
ret i32 %39
}