#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 dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ filename;
typedef int /*<<< orphan*/ dirname;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ rmdir(char *);
char *udev_get_dev_path(struct udev *);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ util_strlcat(char *, char *, int);
int /*<<< orphan*/ util_strlcpy(char *, char *, int);
void cleanup_queue_dir(struct udev *udev) {
  char dirname[1024];
  char filename[1024];
  DIR *dir;
  util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
  util_strlcat(filename, "/.udev/uevent_seqnum", sizeof(filename));
  unlink(filename);
  util_strlcpy(dirname, udev_get_dev_path(udev), sizeof(dirname));
  util_strlcat(dirname, "/.udev/queue", sizeof(dirname));
  dir = opendir(dirname);
  if (dir != NULL) {
    while (1) {
      struct dirent *dent;
      dent = readdir(dir);
      if (dent == NULL || dent->d_name[0] == '\0')
        break;
      if (dent->d_name[0] == '.')
        continue;
      util_strlcpy(filename, dirname, sizeof(filename));
      util_strlcat(filename, "/", sizeof(filename));
      util_strlcat(filename, dent->d_name, sizeof(filename));
      unlink(filename);
    }
    closedir(dir);
    rmdir(dirname);
  }
}


x86 assembly (clang, -O0):
<cleanup_queue_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x810(%rbp),%rax
mov    %rax,-0x830(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <udev_get_dev_path@plt>
mov    -0x830(%rbp),%rdi
mov    %rax,%rsi
mov    $0x400,%edx
call   1090 <util_strlcpy@plt>
lea    -0x810(%rbp),%rdi
lea    0xe38(%rip),%rsi
mov    $0x400,%edx
call   10a0 <util_strlcat@plt>
lea    -0x810(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x410(%rbp),%rax
mov    %rax,-0x828(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <udev_get_dev_path@plt>
mov    -0x828(%rbp),%rdi
mov    %rax,%rsi
mov    $0x400,%edx
call   1090 <util_strlcpy@plt>
lea    -0x410(%rbp),%rdi
lea    0xdfe(%rip),%rsi
mov    $0x400,%edx
call   10a0 <util_strlcat@plt>
lea    -0x410(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x818(%rbp)
cmpq   $0x0,-0x818(%rbp)
je     1312 <cleanup_queue_dir+0x192>
jmp    1247 <cleanup_queue_dir+0xc7>
mov    -0x818(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x820(%rbp)
cmpq   $0x0,-0x820(%rbp)
je     127e <cleanup_queue_dir+0xfe>
mov    -0x820(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1283 <cleanup_queue_dir+0x103>
jmp    12fa <cleanup_queue_dir+0x17a>
mov    -0x820(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    129e <cleanup_queue_dir+0x11e>
jmp    1247 <cleanup_queue_dir+0xc7>
lea    -0x810(%rbp),%rdi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1090 <util_strlcpy@plt>
lea    -0x810(%rbp),%rdi
lea    0xd5e(%rip),%rsi
mov    $0x400,%edx
call   10a0 <util_strlcat@plt>
lea    -0x810(%rbp),%rdi
mov    -0x820(%rbp),%rax
mov    (%rax),%rsi
mov    $0x400,%edx
call   10a0 <util_strlcat@plt>
lea    -0x810(%rbp),%rdi
call   1030 <unlink@plt>
jmp    1247 <cleanup_queue_dir+0xc7>
mov    -0x818(%rbp),%rdi
call   1060 <closedir@plt>
lea    -0x410(%rbp),%rdi
call   1050 <rmdir@plt>
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cleanup_queue_dir>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x800,%rsp
mov    %rdi,%rbx
call   1070 <udev_get_dev_path@plt>
lea    0x400(%rsp),%r14
mov    %r14,%rdi
mov    %rax,%rsi
mov    $0x400,%edx
call   1090 <util_strlcpy@plt>
lea    0xe49(%rip),%rsi
mov    %r14,%rdi
mov    $0x400,%edx
call   10a0 <util_strlcat@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    %rbx,%rdi
call   1070 <udev_get_dev_path@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %rax,%rsi
mov    $0x400,%edx
call   1090 <util_strlcpy@plt>
lea    0xe27(%rip),%rsi
mov    %rbx,%rdi
mov    $0x400,%edx
call   10a0 <util_strlcat@plt>
mov    %rbx,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12a8 <cleanup_queue_dir+0x128>
mov    %rax,%r13
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1298 <cleanup_queue_dir+0x118>
mov    %rax,%rbx
lea    0x400(%rsp),%r14
mov    %rsp,%r15
lea    0xdf1(%rip),%r12
jmp    1250 <cleanup_queue_dir+0xd0>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r13,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1298 <cleanup_queue_dir+0x118>
mov    (%rbx),%rax
movzbl (%rax),%eax
cmp    $0x2e,%al
je     1240 <cleanup_queue_dir+0xc0>
test   %al,%al
je     1298 <cleanup_queue_dir+0x118>
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x400,%edx
call   1090 <util_strlcpy@plt>
mov    %r14,%rdi
mov    %r12,%rsi
mov    $0x400,%edx
call   10a0 <util_strlcat@plt>
mov    (%rbx),%rsi
mov    %r14,%rdi
mov    $0x400,%edx
call   10a0 <util_strlcat@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
jmp    1240 <cleanup_queue_dir+0xc0>
mov    %r13,%rdi
call   1060 <closedir@plt>
mov    %rsp,%rdi
call   1050 <rmdir@plt>
add    $0x800,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.udev = type { i32 }
%struct.dirent = type { i8* }
@.str = [21 x i8] c"/.udev/uevent_seqnum\00"
@.str.1 = [13 x i8] c"/.udev/queue\00"
@.str.2 = [2 x i8] c"/\00"
define void @cleanup_queue_dir(%struct.udev* %0) {
%2 = alloca %struct.udev*
%3 = alloca [1024 x i8]
%4 = alloca [1024 x i8]
%5 = alloca i32*
%6 = alloca %struct.dirent*
store %struct.udev* %0, %struct.udev** %2
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = load %struct.udev*, %struct.udev** %2
%9 = call i8* @udev_get_dev_path(%struct.udev* %8)
%10 = call i32 @util_strlcpy(i8* %7, i8* %9, i32 1024)
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%12 = call i32 @util_strlcat(i8* %11, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 1024)
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%14 = call i32 @unlink(i8* %13)
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%16 = load %struct.udev*, %struct.udev** %2
%17 = call i8* @udev_get_dev_path(%struct.udev* %16)
%18 = call i32 @util_strlcpy(i8* %15, i8* %17, i32 1024)
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%20 = call i32 @util_strlcat(i8* %19, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 1024)
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%22 = call i32* @opendir(i8* %21)
store i32* %22, i32** %5
%23 = load i32*, i32** %5
%24 = icmp ne i32* %23, null
br i1 %24, label %25, label %67
25: ; preds = %1
br label %26
26: ; preds = %25, %48, %49
%27 = load i32*, i32** %5
%28 = call %struct.dirent* @readdir(i32* %27)
store %struct.dirent* %28, %struct.dirent** %6
%29 = load %struct.dirent*, %struct.dirent** %6
%30 = icmp eq %struct.dirent* %29, null
br i1 %30, label %39, label %31
31: ; preds = %26
%32 = load %struct.dirent*, %struct.dirent** %6
%33 = getelementptr inbounds %struct.dirent, %struct.dirent* %32, i32 0, i32 0
%34 = load i8*, i8** %33
%35 = getelementptr inbounds i8, i8* %34, i64 0
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %31, %26
br label %62
40: ; preds = %31
%41 = load %struct.dirent*, %struct.dirent** %6
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i32 0, i32 0
%43 = load i8*, i8** %42
%44 = getelementptr inbounds i8, i8* %43, i64 0
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 46
br i1 %47, label %48, label %49
48: ; preds = %40
br label %26
49: ; preds = %40
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%52 = call i32 @util_strlcpy(i8* %50, i8* %51, i32 1024)
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%54 = call i32 @util_strlcat(i8* %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1024)
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%56 = load %struct.dirent*, %struct.dirent** %6
%57 = getelementptr inbounds %struct.dirent, %struct.dirent* %56, i32 0, i32 0
%58 = load i8*, i8** %57
%59 = call i32 @util_strlcat(i8* %55, i8* %58, i32 1024)
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%61 = call i32 @unlink(i8* %60)
br label %26
62: ; preds = %39
%63 = load i32*, i32** %5
%64 = call i32 @closedir(i32* %63)
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%66 = call i32 @rmdir(i8* %65)
br label %67
67: ; preds = %62, %1
ret void
}

LLVM IR (-O2):
%struct.udev = type { i32 }
%struct.dirent = type { i8* }
@.str = [21 x i8] c"/.udev/uevent_seqnum\00"
@.str.1 = [13 x i8] c"/.udev/queue\00"
@.str.2 = [2 x i8] c"/\00"
define void @cleanup_queue_dir(%struct.udev* %0) {
%2 = alloca [1024 x i8]
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = tail call i8* @udev_get_dev_path(%struct.udev* %0)
%7 = call i32 @util_strlcpy(i8* %5, i8* %6, i32 1024)
%8 = call i32 @util_strlcat(i8* %5, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 1024)
%9 = call i32 @unlink(i8* %5)
%10 = call i8* @udev_get_dev_path(%struct.udev* %0)
%11 = call i32 @util_strlcpy(i8* %4, i8* %10, i32 1024)
%12 = call i32 @util_strlcat(i8* %4, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 1024)
%13 = call i32* @opendir(i8* %4)
%14 = icmp eq i32* %13, null
br i1 %14, label %35, label %15
15: ; preds = %1
%16 = call %struct.dirent* @readdir(i32* %13)
%17 = icmp eq %struct.dirent* %16, null
br i1 %17, label %32, label %18
18: ; preds = %15, %29
%19 = phi %struct.dirent* [ %30, %29 ], [ %16, %15 ]
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i64 0, i32 0
%21 = load i8*, i8** %20
%22 = load i8, i8* %21
switch i8 %22, label %23 [
i8 0, label %32
i8 46, label %29
]
23: ; preds = %18
%24 = call i32 @util_strlcpy(i8* %5, i8* %4, i32 1024)
%25 = call i32 @util_strlcat(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1024)
%26 = load i8*, i8** %20
%27 = call i32 @util_strlcat(i8* %5, i8* %26, i32 1024)
%28 = call i32 @unlink(i8* %5)
br label %29
29: ; preds = %18, %23
%30 = call %struct.dirent* @readdir(i32* %13)
%31 = icmp eq %struct.dirent* %30, null
br i1 %31, label %32, label %18
32: ; preds = %29, %18, %15
%33 = call i32 @closedir(i32* %13)
%34 = call i32 @rmdir(i8* %4)
br label %35
35: ; preds = %32, %1
ret void
}