#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 stat {
  int /*<<< orphan*/ st_mode;
};
struct dirent {
  int /*<<< orphan*/ *d_name;
};
struct cfg_file {
  int dummy;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ LOG_DEBUG;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ _log(int /*<<< orphan*/, char *, char const *, ...);
scalar_t__ cfg_files_filter_out(int /*<<< orphan*/ *, char const *,
                                int /*<<< orphan*/ *);
int /*<<< orphan*/ cfg_files_insert_sorted(struct cfg_file ***, size_t *,
                                           char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int errno;
int /*<<< orphan*/ *opendir(char const *);
int readdir_r(int /*<<< orphan*/ *, struct dirent *, struct dirent **);
scalar_t__ stat(char const *, struct stat *);
char const *strerror(int);
int cfg_files_list(struct cfg_file ***p_files, size_t *p_n_files,
                   const char *path) {
  DIR *d;
  int err = 0;
  struct stat st;
  if (stat(path, &st) != 0) {
    err = -errno;
    _log(LOG_DEBUG, "could not stat '%s': %m\n", path);
    return err;
  }
  if (S_ISREG(st.st_mode)) {
    cfg_files_insert_sorted(p_files, p_n_files, path, NULL);
    return 0;
  }
  if (!S_ISDIR(st.st_mode)) {
    _log(LOG_ERR, "unsupported file mode %s: %#x\n", path, st.st_mode);
    return -EINVAL;
  }
  d = opendir(path);
  if (d == NULL) {
    _log(LOG_ERR, "files list %s: %m\n", path);
    return -EINVAL;
  }
  for (;;) {
    struct dirent ent, *entp;
    err = readdir_r(d, &ent, &entp);
    if (err != 0) {
      _log(LOG_ERR, "reading entry %s\n", strerror(-err));
      break;
    }
    if (entp == NULL)
      break;
    if (cfg_files_filter_out(d, path, entp->d_name))
      continue;
    cfg_files_insert_sorted(p_files, p_n_files, path, entp->d_name);
  }
  closedir(d);
  _log(LOG_DEBUG, "parsed configuration files from %s\n", path);
  return err;
}


x86 assembly (clang, -O0):
<cfg_files_list>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     1206 <cfg_files_list+0x66>
xor    %eax,%eax
mov    0x2e05(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe0c(%rip),%rsi
mov    $0x0,%al
call   1030 <_log@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    138d <cfg_files_list+0x1ed>
mov    -0x30(%rbp),%edi
call   1090 <S_ISREG@plt>
cmp    $0x0,%rax
je     1239 <cfg_files_list+0x99>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   10a0 <cfg_files_insert_sorted@plt>
movl   $0x0,-0x4(%rbp)
jmp    138d <cfg_files_list+0x1ed>
mov    -0x30(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    127b <cfg_files_list+0xdb>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%ecx
lea    0xdb8(%rip),%rsi
mov    $0x0,%al
call   1030 <_log@plt>
xor    %eax,%eax
mov    0x2d67(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    138d <cfg_files_list+0x1ed>
mov    -0x20(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12c1 <cfg_files_list+0x121>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xd91(%rip),%rsi
mov    $0x0,%al
call   1030 <_log@plt>
xor    %eax,%eax
mov    0x2d21(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    138d <cfg_files_list+0x1ed>
jmp    12c6 <cfg_files_list+0x126>
mov    -0x28(%rbp),%rdi
lea    -0x38(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1040 <readdir_r@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1313 <cfg_files_list+0x173>
mov    0x2cd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
xor    %edi,%edi
sub    -0x2c(%rbp),%edi
call   10b0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%rdx
lea    0xd44(%rip),%rsi
mov    $0x0,%al
call   1030 <_log@plt>
jmp    1363 <cfg_files_list+0x1c3>
cmpq   $0x0,-0x40(%rbp)
jne    1323 <cfg_files_list+0x183>
jmp    1363 <cfg_files_list+0x1c3>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x40(%rbp),%rax
mov    (%rax),%rdx
call   10c0 <cfg_files_filter_out@plt>
cmp    $0x0,%rax
je     1346 <cfg_files_list+0x1a6>
jmp    12c6 <cfg_files_list+0x126>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x40(%rbp),%rax
mov    (%rax),%rcx
call   10a0 <cfg_files_insert_sorted@plt>
jmp    12c6 <cfg_files_list+0x126>
mov    -0x28(%rbp),%rdi
call   1070 <closedir@plt>
mov    0x2c5d(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xcdd(%rip),%rsi
mov    $0x0,%al
call   1030 <_log@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cfg_files_list>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rsp,%rsi
mov    %rdx,%rdi
call   1080 <stat@plt>
test   %rax,%rax
je     11f1 <cfg_files_list+0x51>
mov    0x2e12(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
lea    0xe1e(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <_log@plt>
jmp    12f7 <cfg_files_list+0x157>
mov    (%rsp),%edi
call   1090 <S_ISREG@plt>
test   %rax,%rax
je     1215 <cfg_files_list+0x75>
xor    %ebx,%ebx
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
call   10a0 <cfg_files_insert_sorted@plt>
jmp    12f7 <cfg_files_list+0x157>
mov    (%rsp),%edi
call   1060 <S_ISDIR@plt>
test   %eax,%eax
je     12b1 <cfg_files_list+0x111>
mov    %r14,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     12d2 <cfg_files_list+0x132>
mov    %rax,%r13
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1040 <readdir_r@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    130a <cfg_files_list+0x16a>
lea    0x8(%rsp),%rbp
jmp    1278 <cfg_files_list+0xd8>
nopl   0x0(%rax)
mov    %r13,%rdi
lea    0x10(%rsp),%rsi
mov    %rbp,%rdx
call   1040 <readdir_r@plt>
test   %eax,%eax
jne    1308 <cfg_files_list+0x168>
mov    0x8(%rsp),%rax
test   %rax,%rax
je     134c <cfg_files_list+0x1ac>
mov    (%rax),%rdx
mov    %r13,%rdi
mov    %r14,%rsi
call   10c0 <cfg_files_filter_out@plt>
test   %rax,%rax
jne    1260 <cfg_files_list+0xc0>
mov    0x8(%rsp),%rax
mov    (%rax),%rcx
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   10a0 <cfg_files_insert_sorted@plt>
jmp    1260 <cfg_files_list+0xc0>
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
mov    (%rsp),%ecx
lea    0xd55(%rip),%rsi
xor    %ebx,%ebx
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <_log@plt>
jmp    12ee <cfg_files_list+0x14e>
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
lea    0xd56(%rip),%rsi
xor    %ebx,%ebx
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <_log@plt>
mov    0x2ce3(%rip),%rax
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebx
mov    0x2caf(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
neg    %edi
call   10b0 <strerror@plt>
lea    0xd28(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <_log@plt>
mov    %r13,%rdi
call   1070 <closedir@plt>
mov    0x2c92(%rip),%rax
mov    (%rax),%edi
lea    0xd16(%rip),%rsi
jmp    11e2 <cfg_files_list+0x42>
xor    %ebx,%ebx
jmp    132f <cfg_files_list+0x18f>

LLVM IR (-O0):
%struct.cfg_file = type { i32 }
%struct.stat = type { i32 }
%struct.dirent = type { i32* }
@errno = global i32 0
@LOG_DEBUG = global i32 0
@.str = [25 x i8] c"could not stat '%s': %m\0A\00"
@LOG_ERR = global i32 0
@.str.1 = [31 x i8] c"unsupported file mode %s: %#x\0A\00"
@EINVAL = global i32 0
@.str.2 = [19 x i8] c"files list %s: %m\0A\00"
@.str.3 = [18 x i8] c"reading entry %s\0A\00"
@.str.4 = [36 x i8] c"parsed configuration files from %s\0A\00"
define i32 @cfg_files_list(%struct.cfg_file*** %0, i64* %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.cfg_file***
%6 = alloca i64*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca %struct.stat
%11 = alloca %struct.dirent
%12 = alloca %struct.dirent*
store %struct.cfg_file*** %0, %struct.cfg_file**** %5
store i64* %1, i64** %6
store i8* %2, i8** %7
store i32 0, i32* %9
%13 = load i8*, i8** %7
%14 = call i64 @stat(i8* %13, %struct.stat* %10)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %3
%17 = load i32, i32* @errno
%18 = sub nsw i32 0, %17
store i32 %18, i32* %9
%19 = load i32, i32* @LOG_DEBUG
%20 = load i8*, i8** %7
%21 = call i32 (i32, i8*, i8*, ...) @_log(i32 %19, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = load i32, i32* %9
store i32 %22, i32* %4
br label %97
23: ; preds = %3
%24 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i64 @S_ISREG(i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %23
%29 = load %struct.cfg_file***, %struct.cfg_file**** %5
%30 = load i64*, i64** %6
%31 = load i8*, i8** %7
%32 = call i32 @cfg_files_insert_sorted(%struct.cfg_file*** %29, i64* %30, i8* %31, i32* null)
store i32 0, i32* %4
br label %97
33: ; preds = %23
%34 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @S_ISDIR(i32 %35)
%37 = icmp ne i32 %36, 0
br i1 %37, label %46, label %38
38: ; preds = %33
%39 = load i32, i32* @LOG_ERR
%40 = load i8*, i8** %7
%41 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 (i32, i8*, i8*, ...) @_log(i32 %39, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %40, i32 %42)
%44 = load i32, i32* @EINVAL
%45 = sub nsw i32 0, %44
store i32 %45, i32* %4
br label %97
46: ; preds = %33
%47 = load i8*, i8** %7
%48 = call i32* @opendir(i8* %47)
store i32* %48, i32** %8
%49 = load i32*, i32** %8
%50 = icmp eq i32* %49, null
br i1 %50, label %51, label %57
51: ; preds = %46
%52 = load i32, i32* @LOG_ERR
%53 = load i8*, i8** %7
%54 = call i32 (i32, i8*, i8*, ...) @_log(i32 %52, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %53)
%55 = load i32, i32* @EINVAL
%56 = sub nsw i32 0, %55
store i32 %56, i32* %4
br label %97
57: ; preds = %46
br label %58
58: ; preds = %82, %81, %57
%59 = load i32*, i32** %8
%60 = call i32 @readdir_r(i32* %59, %struct.dirent* %11, %struct.dirent** %12)
store i32 %60, i32* %9
%61 = load i32, i32* %9
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %69
63: ; preds = %58
%64 = load i32, i32* @LOG_ERR
%65 = load i32, i32* %9
%66 = sub nsw i32 0, %65
%67 = call i8* @strerror(i32 %66)
%68 = call i32 (i32, i8*, i8*, ...) @_log(i32 %64, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %67)
br label %90
69: ; preds = %58
%70 = load %struct.dirent*, %struct.dirent** %12
%71 = icmp eq %struct.dirent* %70, null
br i1 %71, label %72, label %73
72: ; preds = %69
br label %90
73: ; preds = %69
%74 = load i32*, i32** %8
%75 = load i8*, i8** %7
%76 = load %struct.dirent*, %struct.dirent** %12
%77 = getelementptr inbounds %struct.dirent, %struct.dirent* %76, i32 0, i32 0
%78 = load i32*, i32** %77
%79 = call i64 @cfg_files_filter_out(i32* %74, i8* %75, i32* %78)
%80 = icmp ne i64 %79, 0
br i1 %80, label %81, label %82
81: ; preds = %73
br label %58
82: ; preds = %73
%83 = load %struct.cfg_file***, %struct.cfg_file**** %5
%84 = load i64*, i64** %6
%85 = load i8*, i8** %7
%86 = load %struct.dirent*, %struct.dirent** %12
%87 = getelementptr inbounds %struct.dirent, %struct.dirent* %86, i32 0, i32 0
%88 = load i32*, i32** %87
%89 = call i32 @cfg_files_insert_sorted(%struct.cfg_file*** %83, i64* %84, i8* %85, i32* %88)
br label %58
90: ; preds = %72, %63
%91 = load i32*, i32** %8
%92 = call i32 @closedir(i32* %91)
%93 = load i32, i32* @LOG_DEBUG
%94 = load i8*, i8** %7
%95 = call i32 (i32, i8*, i8*, ...) @_log(i32 %93, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %94)
%96 = load i32, i32* %9
store i32 %96, i32* %4
br label %97
97: ; preds = %90, %51, %38, %28, %16
%98 = load i32, i32* %4
ret i32 %98
}

LLVM IR (-O2):
%struct.cfg_file = type { i32 }
%struct.stat = type { i32 }
%struct.dirent = type { i32* }
@errno = global i32 0
@LOG_DEBUG = global i32 0
@.str = [25 x i8] c"could not stat '%s': %m\0A\00"
@LOG_ERR = global i32 0
@.str.1 = [31 x i8] c"unsupported file mode %s: %#x\0A\00"
@EINVAL = global i32 0
@.str.2 = [19 x i8] c"files list %s: %m\0A\00"
@.str.3 = [18 x i8] c"reading entry %s\0A\00"
@.str.4 = [36 x i8] c"parsed configuration files from %s\0A\00"
define i32 @cfg_files_list(%struct.cfg_file*** %0, i64* %1, i8* %2) {
%4 = alloca %struct.stat
%5 = alloca %struct.dirent
%6 = alloca %struct.dirent*
%7 = bitcast %struct.stat* %4 to i8*
%8 = call i64 @stat(i8* %2, %struct.stat* %4)
%9 = icmp eq i64 %8, 0
br i1 %9, label %15, label %10
10: ; preds = %3
%11 = load i32, i32* @errno
%12 = sub nsw i32 0, %11
%13 = load i32, i32* @LOG_DEBUG
%14 = call i32 (i32, i8*, i8*, ...) @_log(i32 %13, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %2)
br label %72
15: ; preds = %3
%16 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%17 = load i32, i32* %16
%18 = call i64 @S_ISREG(i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %15
%21 = call i32 @cfg_files_insert_sorted(%struct.cfg_file*** %0, i64* %1, i8* %2, i32* null)
br label %72
22: ; preds = %15
%23 = load i32, i32* %16
%24 = call i32 @S_ISDIR(i32 %23)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %22
%27 = load i32, i32* @LOG_ERR
%28 = load i32, i32* %16
%29 = call i32 (i32, i8*, i8*, ...) @_log(i32 %27, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %2, i32 %28)
%30 = load i32, i32* @EINVAL
%31 = sub nsw i32 0, %30
br label %72
32: ; preds = %22
%33 = call i32* @opendir(i8* %2)
%34 = icmp eq i32* %33, null
br i1 %34, label %40, label %35
35: ; preds = %32
%36 = bitcast %struct.dirent* %5 to i8*
%37 = bitcast %struct.dirent** %6 to i8*
%38 = call i32 @readdir_r(i32* %33, %struct.dirent* %5, %struct.dirent** %6)
%39 = icmp eq i32 %38, 0
br i1 %39, label %51, label %45
40: ; preds = %32
%41 = load i32, i32* @LOG_ERR
%42 = call i32 (i32, i8*, i8*, ...) @_log(i32 %41, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%43 = load i32, i32* @EINVAL
%44 = sub nsw i32 0, %43
br label %72
45: ; preds = %64, %35
%46 = phi i32 [ %38, %35 ], [ %65, %64 ]
%47 = load i32, i32* @LOG_ERR
%48 = sub nsw i32 0, %46
%49 = call i8* @strerror(i32 %48)
%50 = call i32 (i32, i8*, i8*, ...) @_log(i32 %47, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %49)
br label %67
51: ; preds = %35, %64
%52 = load %struct.dirent*, %struct.dirent** %6
%53 = icmp eq %struct.dirent* %52, null
br i1 %53, label %67, label %54
54: ; preds = %51
%55 = getelementptr inbounds %struct.dirent, %struct.dirent* %52, i64 0, i32 0
%56 = load i32*, i32** %55
%57 = call i64 @cfg_files_filter_out(i32* %33, i8* %2, i32* %56)
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %64
59: ; preds = %54
%60 = load %struct.dirent*, %struct.dirent** %6
%61 = getelementptr inbounds %struct.dirent, %struct.dirent* %60, i64 0, i32 0
%62 = load i32*, i32** %61
%63 = call i32 @cfg_files_insert_sorted(%struct.cfg_file*** %0, i64* %1, i8* %2, i32* %62)
br label %64
64: ; preds = %54, %59
%65 = call i32 @readdir_r(i32* %33, %struct.dirent* %5, %struct.dirent** %6)
%66 = icmp eq i32 %65, 0
br i1 %66, label %51, label %45
67: ; preds = %51, %45
%68 = phi i32 [ %46, %45 ], [ 0, %51 ]
%69 = call i32 @closedir(i32* %33)
%70 = load i32, i32* @LOG_DEBUG
%71 = call i32 (i32, i8*, i8*, ...) @_log(i32 %70, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %2)
br label %72
72: ; preds = %67, %40, %26, %20, %10
%73 = phi i32 [ %12, %10 ], [ 0, %20 ], [ %44, %40 ], [ %68, %67 ], [ %31, %26 ]
ret i32 %73
}