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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int remove(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char *);
scalar_t__ strstr(char *, char *);
int /*<<< orphan*/ zrpc_log(char *, char const *, ...);
void zrpc_clean_tmp_files_for_bgpd_bfdd(void) {
  DIR *dir;
  struct dirent *ent;
  char buf[512] = {0};
  const char *tmp = "/tmp";
  int ret;
  if (!(dir = opendir(tmp))) {
    zrpc_log("can't open %s", tmp);
    return;
  }
  while ((ent = readdir(dir)) != NULL) {
    if (strstr(ent->d_name, "qzc-")) {
      snprintf(buf, sizeof(buf), "%s/%s", tmp, ent->d_name);
      ret = remove(buf);
      zrpc_log("remove(%s), ret=%d\n", buf, ret);
    }
  }
  closedir(dir);
}


x86 assembly (clang, -O0):
<zrpc_clean_tmp_files_for_bgpd_bfdd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
lea    0xe5b(%rip),%rax
mov    %rax,-0x218(%rbp)
mov    -0x218(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jne    11e0 <zrpc_clean_tmp_files_for_bgpd_bfdd+0x60>
mov    -0x218(%rbp),%rsi
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1040 <zrpc_log@plt>
jmp    127a <zrpc_clean_tmp_files_for_bgpd_bfdd+0xfa>
jmp    11e5 <zrpc_clean_tmp_files_for_bgpd_bfdd+0x65>
mov    -0x8(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1271 <zrpc_clean_tmp_files_for_bgpd_bfdd+0xf1>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe09(%rip),%rsi
call   10a0 <strstr@plt>
cmp    $0x0,%rax
je     126c <zrpc_clean_tmp_files_for_bgpd_bfdd+0xec>
lea    -0x210(%rbp),%rdi
mov    -0x218(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    (%rax),%r8
mov    $0x200,%esi
lea    0xdde(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x210(%rbp),%rdi
call   1030 <remove@plt>
mov    %eax,-0x21c(%rbp)
lea    -0x210(%rbp),%rsi
mov    -0x21c(%rbp),%edx
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1040 <zrpc_log@plt>
jmp    11e5 <zrpc_clean_tmp_files_for_bgpd_bfdd+0x65>
mov    -0x8(%rbp),%rdi
call   1080 <closedir@plt>
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zrpc_clean_tmp_files_for_bgpd_bfdd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rsp,%rdi
mov    $0x200,%edx
xor    %esi,%esi
call   1070 <memset@plt>
lea    0xe59(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     125d <zrpc_clean_tmp_files_for_bgpd_bfdd+0xdd>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1243 <zrpc_clean_tmp_files_for_bgpd_bfdd+0xc3>
mov    %rax,%r15
lea    0xe44(%rip),%r14
lea    0xe2a(%rip),%r12
mov    %rsp,%r13
lea    0xe3e(%rip),%rbp
jmp    1200 <zrpc_clean_tmp_files_for_bgpd_bfdd+0x80>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1090 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
je     1243 <zrpc_clean_tmp_files_for_bgpd_bfdd+0xc3>
mov    (%r15),%rdi
mov    %r14,%rsi
call   10a0 <strstr@plt>
test   %rax,%rax
je     11f0 <zrpc_clean_tmp_files_for_bgpd_bfdd+0x70>
mov    (%r15),%r8
mov    %r13,%rdi
mov    $0x200,%esi
lea    0xdf6(%rip),%rdx
mov    %r12,%rcx
call   1060 <snprintf@plt>
mov    %r13,%rdi
call   1030 <remove@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <zrpc_log@plt>
jmp    11f0 <zrpc_clean_tmp_files_for_bgpd_bfdd+0x70>
mov    %rbx,%rdi
call   1080 <closedir@plt>
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda1(%rip),%rdi
lea    0xd95(%rip),%rsi
xor    %eax,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <zrpc_log@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [5 x i8] c"/tmp\00"
@.str.1 = [14 x i8] c"can't open %s\00"
@.str.2 = [5 x i8] c"qzc-\00"
@.str.3 = [6 x i8] c"%s/%s\00"
@.str.4 = [20 x i8] c"remove(%s), ret=%d\0A\00"
define void @zrpc_clean_tmp_files_for_bgpd_bfdd() {
%1 = alloca i32*
%2 = alloca %struct.dirent*
%3 = alloca [512 x i8]
%4 = alloca i8*
%5 = alloca i32
%6 = bitcast [512 x i8]* %3 to i8*
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %4
%7 = load i8*, i8** %4
%8 = call i32* @opendir(i8* %7)
store i32* %8, i32** %1
%9 = icmp ne i32* %8, null
br i1 %9, label %13, label %10
10: ; preds = %0
%11 = load i8*, i8** %4
%12 = call i32 (i8*, i8*, ...) @zrpc_log(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %11)
br label %40
13: ; preds = %0
br label %14
14: ; preds = %36, %13
%15 = load i32*, i32** %1
%16 = call %struct.dirent* @readdir(i32* %15)
store %struct.dirent* %16, %struct.dirent** %2
%17 = icmp ne %struct.dirent* %16, null
br i1 %17, label %18, label %37
18: ; preds = %14
%19 = load %struct.dirent*, %struct.dirent** %2
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i32 0, i32 0
%21 = load i8*, i8** %20
%22 = call i64 @strstr(i8* %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %36
24: ; preds = %18
%25 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%26 = load i8*, i8** %4
%27 = load %struct.dirent*, %struct.dirent** %2
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i32 0, i32 0
%29 = load i8*, i8** %28
%30 = call i32 @snprintf(i8* %25, i32 512, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %26, i8* %29)
%31 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%32 = call i32 @remove(i8* %31)
store i32 %32, i32* %5
%33 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%34 = load i32, i32* %5
%35 = call i32 (i8*, i8*, ...) @zrpc_log(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %33, i32 %34)
br label %36
36: ; preds = %24, %18
br label %14
37: ; preds = %14
%38 = load i32*, i32** %1
%39 = call i32 @closedir(i32* %38)
br label %40
40: ; preds = %37, %10
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [5 x i8] c"/tmp\00"
@.str.1 = [14 x i8] c"can't open %s\00"
@.str.2 = [5 x i8] c"qzc-\00"
@.str.3 = [6 x i8] c"%s/%s\00"
@.str.4 = [20 x i8] c"remove(%s), ret=%d\0A\00"
define void @zrpc_clean_tmp_files_for_bgpd_bfdd() {
%1 = alloca [512 x i8]
%2 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %8, label %5
5: ; preds = %0
%6 = call %struct.dirent* @readdir(i32* %3)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %24, label %10
8: ; preds = %0
%9 = tail call i32 (i8*, i8*, ...) @zrpc_log(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %26
10: ; preds = %5, %21
%11 = phi %struct.dirent* [ %22, %21 ], [ %6, %5 ]
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%13 = load i8*, i8** %12
%14 = call i64 @strstr(i8* %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %10
%17 = load i8*, i8** %12
%18 = call i32 @snprintf(i8* %2, i32 512, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @remove(i8* %2)
%20 = call i32 (i8*, i8*, ...) @zrpc_log(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %2, i32 %19)
br label %21
21: ; preds = %16, %10
%22 = call %struct.dirent* @readdir(i32* %3)
%23 = icmp eq %struct.dirent* %22, null
br i1 %23, label %24, label %10
24: ; preds = %21, %5
%25 = call i32 @closedir(i32* %3)
br label %26
26: ; preds = %24, %8
ret void
}