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

/* Variables and functions */
char *Config(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
char *getfileseparator();
scalar_t__ is_directory(char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, ...);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *getnextfile(char *path) {
  static int cfg_pos = 0;
  static DIR *curr_dir = NULL;
  static char *curr_dir_path = NULL;
  for (;;) {
    if (curr_dir) {
      struct dirent *dir_ent = readdir(curr_dir);
      if (dir_ent) {
        if (strcmp(dir_ent->d_name, ".") == 0)
          continue;
        if (strcmp(dir_ent->d_name, "..") == 0)
          continue;
        sprintf(path, "%s%s%s", curr_dir_path, getfileseparator(),
                dir_ent->d_name);
        return path;
      }
      closedir(curr_dir);
      curr_dir = NULL;
    }
    cfg_pos++;
    char cfg_opt[128];
    if (cfg_pos == 1) {
      sprintf(cfg_opt, "TARGET-PATH");
    } else {
      sprintf(cfg_opt, "TARGET-PATH-%d", cfg_pos);
    }
    char *fpath = Config(cfg_opt);
    if (fpath == NULL)
      return NULL;
    if (fpath[0] != '\0') {
      if (is_directory(fpath)) {
        curr_dir_path = fpath;
        curr_dir = opendir(fpath);
      } else {
        strcpy(path, fpath);
        return path;
      }
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<getnextfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,0x2ed1(%rip)
je     1274 <getnextfile+0xe4>
mov    0x2ec4(%rip),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     125d <getnextfile+0xcd>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    0xe2c(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11e9 <getnextfile+0x59>
jmp    119f <getnextfile+0xf>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    120a <getnextfile+0x7a>
jmp    119f <getnextfile+0xf>
mov    -0x10(%rbp),%rax
mov    %rax,-0xb8(%rbp)
mov    0x2e64(%rip),%rax
mov    %rax,-0xb0(%rbp)
mov    $0x0,%al
call   1080 <getfileseparator@plt>
mov    -0xb8(%rbp),%rdi
mov    -0xb0(%rbp),%rdx
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    (%rax),%r8d
lea    0xdba(%rip),%rsi
mov    $0x0,%al
call   10b0 <sprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1369 <getnextfile+0x1d9>
mov    0x2e14(%rip),%rdi
call   1050 <closedir@plt>
movq   $0x0,0x2e04(%rip)
mov    0x2df6(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2ded(%rip)
cmpl   $0x1,0x2de6(%rip)
jne    12aa <getnextfile+0x11a>
lea    -0xa0(%rbp),%rdi
lea    0xd6c(%rip),%rsi
mov    $0x0,%al
call   10b0 <sprintf@plt>
jmp    12c5 <getnextfile+0x135>
lea    -0xa0(%rbp),%rdi
mov    0x2db9(%rip),%edx
lea    0xd58(%rip),%rsi
mov    $0x0,%al
call   10b0 <sprintf@plt>
lea    -0xa0(%rbp),%rdi
call   10a0 <Config@plt>
mov    %rax,-0xa8(%rbp)
cmpq   $0x0,-0xa8(%rbp)
jne    12f3 <getnextfile+0x163>
movq   $0x0,-0x8(%rbp)
jmp    1369 <getnextfile+0x1d9>
mov    -0xa8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1364 <getnextfile+0x1d4>
mov    -0xa8(%rbp),%rdi
call   1090 <is_directory@plt>
cmp    $0x0,%rax
je     1342 <getnextfile+0x1b2>
mov    -0xa8(%rbp),%rax
mov    %rax,0x2d56(%rip)
mov    -0xa8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,0x2d3b(%rip)
jmp    135f <getnextfile+0x1cf>
mov    -0x10(%rbp),%rdi
mov    -0xa8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1369 <getnextfile+0x1d9>
jmp    1364 <getnextfile+0x1d4>
jmp    119f <getnextfile+0xf>
mov    -0x8(%rbp),%rax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getnextfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %rdi,0x8(%rsp)
movabs $0x502d544547524154,%r14
54 2d 50
lea    0x10(%rsp),%rbx
lea    0xe5a(%rip),%r15
lea    0xe3e(%rip),%r12
lea    0xe36(%rip),%r13
nopw   0x0(%rax,%rax,1)
mov    0x2ea1(%rip),%rdi
test   %rdi,%rdi
je     1227 <getnextfile+0x97>
call   1070 <readdir@plt>
test   %rax,%rax
je     1210 <getnextfile+0x80>
mov    %rax,%rbp
mov    (%rax),%edi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11d0 <getnextfile+0x40>
mov    0x0(%rbp),%edi
mov    %r13,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11d0 <getnextfile+0x40>
jmp    12b4 <getnextfile+0x124>
nopl   (%rax)
mov    0x2e61(%rip),%rdi
call   1050 <closedir@plt>
movq   $0x0,0x2e51(%rip)
mov    0x2e43(%rip),%eax
lea    0x1(%rax),%edx
mov    %edx,0x2e3a(%rip)
test   %eax,%eax
je     1250 <getnextfile+0xc0>
mov    %rbx,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   10b0 <sprintf@plt>
jmp    125d <getnextfile+0xcd>
nopl   0x0(%rax)
mov    %r14,0x10(%rsp)
movl   $0x485441,0x18(%rsp)
mov    %rbx,%rdi
call   10a0 <Config@plt>
test   %rax,%rax
je     129e <getnextfile+0x10e>
mov    %rax,%rbp
cmpb   $0x0,(%rax)
je     11d0 <getnextfile+0x40>
mov    %rbp,%rdi
call   1090 <is_directory@plt>
test   %rax,%rax
je     12a2 <getnextfile+0x112>
mov    %rbp,0x2df6(%rip)
mov    %rbp,%rdi
call   1040 <opendir@plt>
mov    %rax,0x2ddf(%rip)
jmp    11d0 <getnextfile+0x40>
xor    %ebx,%ebx
jmp    12e2 <getnextfile+0x152>
mov    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
jmp    12e2 <getnextfile+0x152>
mov    0x2dc5(%rip),%r14
xor    %eax,%eax
call   1080 <getfileseparator@plt>
mov    0x0(%rbp),%r8d
lea    0xd36(%rip),%rsi
mov    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   10b0 <sprintf@plt>
mov    %rbx,%rax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@getnextfile.cfg_pos = internal global i32 0
@getnextfile.curr_dir = internal global i32* null
@getnextfile.curr_dir_path = internal global i8* null
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [7 x i8] c"%s%s%s\00"
@.str.3 = [12 x i8] c"TARGET-PATH\00"
@.str.4 = [15 x i8] c"TARGET-PATH-%d\00"
define i8* @getnextfile(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.dirent*
%5 = alloca [128 x i8]
%6 = alloca i8*
store i8* %0, i8** %3
br label %7
7: ; preds = %79, %28, %21, %1
%8 = load i32*, i32** @getnextfile.curr_dir
%9 = icmp ne i32* %8, null
br i1 %9, label %10, label %41
10: ; preds = %7
%11 = load i32*, i32** @getnextfile.curr_dir
%12 = call %struct.dirent* @readdir(i32* %11)
store %struct.dirent* %12, %struct.dirent** %4
%13 = load %struct.dirent*, %struct.dirent** %4
%14 = icmp ne %struct.dirent* %13, null
br i1 %14, label %15, label %38
15: ; preds = %10
%16 = load %struct.dirent*, %struct.dirent** %4
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i64 @strcmp(i32 %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %15
br label %7
22: ; preds = %15
%23 = load %struct.dirent*, %struct.dirent** %4
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i64 @strcmp(i32 %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %22
br label %7
29: ; preds = %22
%30 = load i8*, i8** %3
%31 = load i8*, i8** @getnextfile.curr_dir_path
%32 = call i8* (...) @getfileseparator()
%33 = load %struct.dirent*, %struct.dirent** %4
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 (i8*, i8*, ...) @sprintf(i8* %30, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %31, i8* %32, i32 %35)
%37 = load i8*, i8** %3
store i8* %37, i8** %2
br label %80
38: ; preds = %10
%39 = load i32*, i32** @getnextfile.curr_dir
%40 = call i32 @closedir(i32* %39)
store i32* null, i32** @getnextfile.curr_dir
br label %41
41: ; preds = %38, %7
%42 = load i32, i32* @getnextfile.cfg_pos
%43 = add nsw i32 %42, 1
store i32 %43, i32* @getnextfile.cfg_pos
%44 = load i32, i32* @getnextfile.cfg_pos
%45 = icmp eq i32 %44, 1
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%48 = call i32 (i8*, i8*, ...) @sprintf(i8* %47, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %53
49: ; preds = %41
%50 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%51 = load i32, i32* @getnextfile.cfg_pos
%52 = call i32 (i8*, i8*, ...) @sprintf(i8* %50, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %51)
br label %53
53: ; preds = %49, %46
%54 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%55 = call i8* @Config(i8* %54)
store i8* %55, i8** %6
%56 = load i8*, i8** %6
%57 = icmp eq i8* %56, null
br i1 %57, label %58, label %59
58: ; preds = %53
store i8* null, i8** %2
br label %80
59: ; preds = %53
%60 = load i8*, i8** %6
%61 = getelementptr inbounds i8, i8* %60, i64 0
%62 = load i8, i8* %61
%63 = sext i8 %62 to i32
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %79
65: ; preds = %59
%66 = load i8*, i8** %6
%67 = call i64 @is_directory(i8* %66)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %73
69: ; preds = %65
%70 = load i8*, i8** %6
store i8* %70, i8** @getnextfile.curr_dir_path
%71 = load i8*, i8** %6
%72 = call i32* @opendir(i8* %71)
store i32* %72, i32** @getnextfile.curr_dir
br label %78
73: ; preds = %65
%74 = load i8*, i8** %3
%75 = load i8*, i8** %6
%76 = call i32 @strcpy(i8* %74, i8* %75)
%77 = load i8*, i8** %3
store i8* %77, i8** %2
br label %80
78: ; preds = %69
br label %79
79: ; preds = %78, %59
br label %7
80: ; preds = %73, %58, %29
%81 = load i8*, i8** %2
ret i8* %81
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@getnextfile.cfg_pos = internal unnamed_addr global i32 0
@getnextfile.curr_dir = internal unnamed_addr global i32* null
@getnextfile.curr_dir_path = internal unnamed_addr global i8* null
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [7 x i8] c"%s%s%s\00"
@.str.3 = [12 x i8] c"TARGET-PATH\00"
@.str.4 = [15 x i8] c"TARGET-PATH-%d\00"
define i8* @getnextfile(i8* %0) {
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %15, %1
%5 = load i32*, i32** @getnextfile.curr_dir
%6 = icmp eq i32* %5, null
br i1 %6, label %28, label %7
7: ; preds = %4
%8 = call %struct.dirent* @readdir(i32* %5)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %25, label %10
10: ; preds = %7
%11 = getelementptr inbounds %struct.dirent, %struct.dirent* %8, i64 0, i32 0
%12 = load i32, i32* %11
%13 = call i64 @strcmp(i32 %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %10, %16, %50
br label %4
16: ; preds = %10
%17 = load i32, i32* %11
%18 = call i64 @strcmp(i32 %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %15, label %20
20: ; preds = %16
%21 = load i8*, i8** @getnextfile.curr_dir_path
%22 = call i8* (...) @getfileseparator()
%23 = load i32, i32* %11
%24 = call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %21, i8* %22, i32 %23)
br label %51
25: ; preds = %7
%26 = load i32*, i32** @getnextfile.curr_dir
%27 = call i32 @closedir(i32* %26)
store i32* null, i32** @getnextfile.curr_dir
br label %28
28: ; preds = %25, %4
%29 = load i32, i32* @getnextfile.cfg_pos
%30 = add nsw i32 %29, 1
store i32 %30, i32* @getnextfile.cfg_pos
%31 = icmp eq i32 %29, 0
br i1 %31, label %32, label %33
32: ; preds = %28
br label %35
33: ; preds = %28
%34 = call i32 (i8*, i8*, ...) @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %30)
br label %35
35: ; preds = %33, %32
%36 = call i8* @Config(i8* %3)
%37 = icmp eq i8* %36, null
br i1 %37, label %48, label %38
38: ; preds = %35
%39 = load i8, i8* %36
%40 = icmp eq i8 %39, 0
br i1 %40, label %50, label %41
41: ; preds = %38
%42 = call i64 @is_directory(i8* %36)
%43 = icmp eq i64 %42, 0
br i1 %43, label %46, label %44
44: ; preds = %41
store i8* %36, i8** @getnextfile.curr_dir_path
%45 = call i32* @opendir(i8* %36)
store i32* %45, i32** @getnextfile.curr_dir
br label %50
46: ; preds = %41
%47 = call i32 @strcpy(i8* %0, i8* %36)
br label %48
48: ; preds = %35, %46
%49 = phi i8* [ %0, %46 ], [ null, %35 ]
br label %51
50: ; preds = %38, %44
br label %15
51: ; preds = %48, %20
%52 = phi i8* [ %0, %20 ], [ %49, %48 ]
ret i8* %52
}