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

/* Variables and functions */
scalar_t__ DT_REG;
scalar_t__ DT_UNKNOWN;
char *append(char *, char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *);
char *read_file(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
int strlen(char *);
char *__gf(char dirpath[], int *index, int *count, char **header) {
  DIR *currdir;
  char *path;
  path = malloc(strlen(dirpath) + 2);
  char *terminate = "/";
  struct dirent *d;
  strcpy(path, dirpath);
  int p = strlen(path);
  if (!path)
    path = ".";
  if (path[p - 1] != '/') {
    if (path == NULL)
      return NULL;
    strcat(path, terminate);
  }
  currdir = opendir(path);
  char *f;
  if (currdir) {
    while ((d = readdir(currdir)) != NULL)
      if ((d->d_type == DT_REG || d->d_type == DT_UNKNOWN) &&
          d->d_name[0] != '.') {
        char *curfile = append(path, d->d_name);
        if ((*count)++ >= *index) {
          ++(*index);
          f = read_file(curfile);
          if (f != NULL) {
            printf("Reading: %s\n", curfile);
            *header = strdup(curfile);
            if (curfile != NULL)
              free(curfile);
            closedir(currdir);
            if (path != NULL)
              free(path);
            return f;
          }
          if (curfile != NULL)
            free(curfile);
        } else {
          if (curfile != NULL)
            free(curfile);
        }
      }
    closedir(currdir);
  } else {
    printf("Could not open dir: %s\n", path);
    return NULL;
  }
  free(path);
  return NULL;
}


x86 assembly (clang, -O0):
<__gf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,%edi
add    $0x2,%edi
call   10c0 <malloc@plt>
mov    %rax,-0x38(%rbp)
lea    0xe0a(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x38(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x4c(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1229 <__gf+0x69>
lea    0xddd(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x4c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     1268 <__gf+0xa8>
cmpq   $0x0,-0x38(%rbp)
jne    125b <__gf+0x9b>
movq   $0x0,-0x8(%rbp)
jmp    1408 <__gf+0x248>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   10d0 <strcat@plt>
mov    -0x38(%rbp),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     13da <__gf+0x21a>
jmp    1285 <__gf+0xc5>
mov    -0x30(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
je     13cc <__gf+0x20c>
mov    -0x48(%rbp),%rax
mov    (%rax),%rax
mov    0x2d2e(%rip),%rcx
cmp    (%rcx),%rax
je     12ca <__gf+0x10a>
mov    -0x48(%rbp),%rax
mov    (%rax),%rax
mov    0x2d27(%rip),%rcx
cmp    (%rcx),%rax
jne    13c7 <__gf+0x207>
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
je     13c7 <__gf+0x207>
mov    -0x38(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rsi
call   1040 <append@plt>
mov    %rax,-0x60(%rbp)
mov    -0x20(%rbp),%rcx
mov    (%rcx),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,(%rcx)
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
jl     13a9 <__gf+0x1e9>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x60(%rbp),%rdi
call   10a0 <read_file@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     1390 <__gf+0x1d0>
mov    -0x60(%rbp),%rsi
lea    0xcca(%rip),%rdi
call   1080 <printf@plt>
mov    -0x60(%rbp),%rdi
call   10e0 <strdup@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x60(%rbp)
je     1366 <__gf+0x1a6>
mov    -0x60(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1090 <closedir@plt>
cmpq   $0x0,-0x38(%rbp)
je     1383 <__gf+0x1c3>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x58(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1408 <__gf+0x248>
cmpq   $0x0,-0x60(%rbp)
je     13a4 <__gf+0x1e4>
mov    -0x60(%rbp),%rdi
call   1030 <free@plt>
jmp    13c2 <__gf+0x202>
cmpq   $0x0,-0x60(%rbp)
je     13bd <__gf+0x1fd>
mov    -0x60(%rbp),%rdi
call   1030 <free@plt>
jmp    13c2 <__gf+0x202>
jmp    13c7 <__gf+0x207>
jmp    1285 <__gf+0xc5>
mov    -0x30(%rbp),%rdi
call   1090 <closedir@plt>
jmp    13f7 <__gf+0x237>
mov    -0x38(%rbp),%rsi
lea    0xc2c(%rip),%rdi
call   1080 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1408 <__gf+0x248>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__gf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r14
mov    %rdx,0x10(%rsp)
mov    %rsi,(%rsp)
mov    %rdi,%rbx
call   1070 <strlen@plt>
lea    0x2(%rax),%edi
call   10c0 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
call   1050 <strcpy@plt>
mov    %rbp,%rdi
call   1070 <strlen@plt>
test   %rbp,%rbp
lea    0xdf8(%rip),%r13
cmovne %rbp,%r13
cltq
cmpb   $0x2f,-0x1(%rax,%r13,1)
je     1227 <__gf+0x67>
lea    0xde1(%rip),%rsi
mov    %r13,%rdi
call   10d0 <strcat@plt>
mov    %r13,%rdi
call   1060 <opendir@plt>
test   %rax,%rax
je     12f2 <__gf+0x132>
mov    %rax,%rbx
mov    %r14,0x8(%rsp)
mov    %rax,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
mov    (%rsp),%r14
je     12e0 <__gf+0x120>
mov    0x2d7c(%rip),%r12
mov    0x2d85(%rip),%r15
jmp    128d <__gf+0xcd>
test   %rbp,%rbp
je     1280 <__gf+0xc0>
mov    %rbp,%rdi
call   1030 <free@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     12e0 <__gf+0x120>
mov    (%rax),%rcx
cmp    (%r12),%rcx
je     129b <__gf+0xdb>
cmp    (%r15),%rcx
jne    1280 <__gf+0xc0>
mov    0x8(%rax),%rsi
cmpb   $0x2e,(%rsi)
je     1280 <__gf+0xc0>
mov    %r13,%rdi
call   1040 <append@plt>
mov    %rax,%rbp
mov    0x10(%rsp),%rdx
mov    (%rdx),%ecx
lea    0x1(%rcx),%eax
mov    %eax,(%rdx)
mov    (%r14),%eax
cmp    %eax,%ecx
jl     1265 <__gf+0xa5>
add    $0x1,%eax
mov    %eax,(%r14)
mov    %rbp,%rdi
call   10a0 <read_file@plt>
test   %rax,%rax
jne    1316 <__gf+0x156>
test   %rbp,%rbp
mov    (%rsp),%r14
jne    126a <__gf+0xaa>
jmp    1280 <__gf+0xc0>
mov    %rbx,%rdi
call   1090 <closedir@plt>
mov    %r13,%rdi
call   1030 <free@plt>
jmp    1301 <__gf+0x141>
lea    0xd18(%rip),%rdi
mov    %r13,%rsi
call   1080 <printf@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rax,%r14
lea    0xce4(%rip),%rdi
mov    %rbp,%rsi
call   1080 <printf@plt>
mov    %rbp,%rdi
call   10e0 <strdup@plt>
mov    0x8(%rsp),%rcx
mov    %rax,(%rcx)
test   %rbp,%rbp
je     1345 <__gf+0x185>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1090 <closedir@plt>
mov    %r13,%rdi
call   1030 <free@plt>
jmp    1304 <__gf+0x144>

LLVM IR (-O0):
%struct.dirent = type { i64, i8* }
@.str = [2 x i8] c"/\00"
@.str.1 = [2 x i8] c".\00"
@DT_REG = global i64 0
@DT_UNKNOWN = global i64 0
@.str.2 = [13 x i8] c"Reading: %s\0A\00"
@.str.3 = [24 x i8] c"Could not open dir: %s\0A\00"
define i8* @__gf(i8* %0, i32* %1, i32* %2, i8** %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i8**
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca %struct.dirent*
%14 = alloca i32
%15 = alloca i8*
%16 = alloca i8*
store i8* %0, i8** %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i8** %3, i8*** %9
%17 = load i8*, i8** %6
%18 = call i32 @strlen(i8* %17)
%19 = add nsw i32 %18, 2
%20 = call i8* @malloc(i32 %19)
store i8* %20, i8** %11
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %12
%21 = load i8*, i8** %11
%22 = load i8*, i8** %6
%23 = call i32 @strcpy(i8* %21, i8* %22)
%24 = load i8*, i8** %11
%25 = call i32 @strlen(i8* %24)
store i32 %25, i32* %14
%26 = load i8*, i8** %11
%27 = icmp ne i8* %26, null
br i1 %27, label %29, label %28
28: ; preds = %4
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %11
br label %29
29: ; preds = %28, %4
%30 = load i8*, i8** %11
%31 = load i32, i32* %14
%32 = sub nsw i32 %31, 1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %30, i64 %33
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp ne i32 %36, 47
br i1 %37, label %38, label %46
38: ; preds = %29
%39 = load i8*, i8** %11
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %42
41: ; preds = %38
store i8* null, i8** %5
br label %142
42: ; preds = %38
%43 = load i8*, i8** %11
%44 = load i8*, i8** %12
%45 = call i32 @strcat(i8* %43, i8* %44)
br label %46
46: ; preds = %42, %29
%47 = load i8*, i8** %11
%48 = call i32* @opendir(i8* %47)
store i32* %48, i32** %10
%49 = load i32*, i32** %10
%50 = icmp ne i32* %49, null
br i1 %50, label %51, label %136
51: ; preds = %46
br label %52
52: ; preds = %132, %51
%53 = load i32*, i32** %10
%54 = call %struct.dirent* @readdir(i32* %53)
store %struct.dirent* %54, %struct.dirent** %13
%55 = icmp ne %struct.dirent* %54, null
br i1 %55, label %56, label %133
56: ; preds = %52
%57 = load %struct.dirent*, %struct.dirent** %13
%58 = getelementptr inbounds %struct.dirent, %struct.dirent* %57, i32 0, i32 0
%59 = load i64, i64* %58
%60 = load i64, i64* @DT_REG
%61 = icmp eq i64 %59, %60
br i1 %61, label %68, label %62
62: ; preds = %56
%63 = load %struct.dirent*, %struct.dirent** %13
%64 = getelementptr inbounds %struct.dirent, %struct.dirent* %63, i32 0, i32 0
%65 = load i64, i64* %64
%66 = load i64, i64* @DT_UNKNOWN
%67 = icmp eq i64 %65, %66
br i1 %67, label %68, label %132
68: ; preds = %62, %56
%69 = load %struct.dirent*, %struct.dirent** %13
%70 = getelementptr inbounds %struct.dirent, %struct.dirent* %69, i32 0, i32 1
%71 = load i8*, i8** %70
%72 = getelementptr inbounds i8, i8* %71, i64 0
%73 = load i8, i8* %72
%74 = sext i8 %73 to i32
%75 = icmp ne i32 %74, 46
br i1 %75, label %76, label %132
76: ; preds = %68
%77 = load i8*, i8** %11
%78 = load %struct.dirent*, %struct.dirent** %13
%79 = getelementptr inbounds %struct.dirent, %struct.dirent* %78, i32 0, i32 1
%80 = load i8*, i8** %79
%81 = call i8* @append(i8* %77, i8* %80)
store i8* %81, i8** %16
%82 = load i32*, i32** %8
%83 = load i32, i32* %82
%84 = add nsw i32 %83, 1
store i32 %84, i32* %82
%85 = load i32*, i32** %7
%86 = load i32, i32* %85
%87 = icmp sge i32 %83, %86
br i1 %87, label %88, label %124
88: ; preds = %76
%89 = load i32*, i32** %7
%90 = load i32, i32* %89
%91 = add nsw i32 %90, 1
store i32 %91, i32* %89
%92 = load i8*, i8** %16
%93 = call i8* @read_file(i8* %92)
store i8* %93, i8** %15
%94 = load i8*, i8** %15
%95 = icmp ne i8* %94, null
br i1 %95, label %96, label %117
96: ; preds = %88
%97 = load i8*, i8** %16
%98 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %97)
%99 = load i8*, i8** %16
%100 = call i8* @strdup(i8* %99)
%101 = load i8**, i8*** %9
store i8* %100, i8** %101
%102 = load i8*, i8** %16
%103 = icmp ne i8* %102, null
br i1 %103, label %104, label %107
104: ; preds = %96
%105 = load i8*, i8** %16
%106 = call i32 @free(i8* %105)
br label %107
107: ; preds = %104, %96
%108 = load i32*, i32** %10
%109 = call i32 @closedir(i32* %108)
%110 = load i8*, i8** %11
%111 = icmp ne i8* %110, null
br i1 %111, label %112, label %115
112: ; preds = %107
%113 = load i8*, i8** %11
%114 = call i32 @free(i8* %113)
br label %115
115: ; preds = %112, %107
%116 = load i8*, i8** %15
store i8* %116, i8** %5
br label %142
117: ; preds = %88
%118 = load i8*, i8** %16
%119 = icmp ne i8* %118, null
br i1 %119, label %120, label %123
120: ; preds = %117
%121 = load i8*, i8** %16
%122 = call i32 @free(i8* %121)
br label %123
123: ; preds = %120, %117
br label %131
124: ; preds = %76
%125 = load i8*, i8** %16
%126 = icmp ne i8* %125, null
br i1 %126, label %127, label %130
127: ; preds = %124
%128 = load i8*, i8** %16
%129 = call i32 @free(i8* %128)
br label %130
130: ; preds = %127, %124
br label %131
131: ; preds = %130, %123
br label %132
132: ; preds = %131, %68, %62
br label %52
133: ; preds = %52
%134 = load i32*, i32** %10
%135 = call i32 @closedir(i32* %134)
br label %139
136: ; preds = %46
%137 = load i8*, i8** %11
%138 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %137)
store i8* null, i8** %5
br label %142
139: ; preds = %133
%140 = load i8*, i8** %11
%141 = call i32 @free(i8* %140)
store i8* null, i8** %5
br label %142
142: ; preds = %139, %136, %115, %41
%143 = load i8*, i8** %5
ret i8* %143
}

LLVM IR (-O2):
%struct.dirent = type { i64, i8* }
@.str = [2 x i8] c"/\00"
@.str.1 = [2 x i8] c".\00"
@DT_REG = global i64 0
@DT_UNKNOWN = global i64 0
@.str.2 = [13 x i8] c"Reading: %s\0A\00"
@.str.3 = [24 x i8] c"Could not open dir: %s\0A\00"
define i8* @__gf(i8* %0, i32* nocapture %1, i32* nocapture %2, i8** nocapture writeonly %3) {
%5 = tail call i32 @strlen(i8* %0)
%6 = add nsw i32 %5, 2
%7 = tail call i8* @malloc(i32 %6)
%8 = tail call i32 @strcpy(i8* %7, i8* %0)
%9 = tail call i32 @strlen(i8* %7)
%10 = icmp eq i8* %7, null
%11 = select i1 %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %7
%12 = add nsw i32 %9, -1
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 47
br i1 %16, label %19, label %17
17: ; preds = %4
%18 = tail call i32 @strcat(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %4
%20 = tail call i32* @opendir(i8* %11)
%21 = icmp eq i32* %20, null
br i1 %21, label %70, label %22
22: ; preds = %19
%23 = tail call %struct.dirent* @readdir(i32* %20)
%24 = icmp eq %struct.dirent* %23, null
br i1 %24, label %67, label %25
25: ; preds = %22, %64
%26 = phi %struct.dirent* [ %65, %64 ], [ %23, %22 ]
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i64 0, i32 0
%28 = load i64, i64* %27
%29 = load i64, i64* @DT_REG
%30 = icmp eq i64 %28, %29
%31 = load i64, i64* @DT_UNKNOWN
%32 = icmp eq i64 %28, %31
%33 = select i1 %30, i1 true, i1 %32
br i1 %33, label %34, label %64
34: ; preds = %25
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i64 0, i32 1
%36 = load i8*, i8** %35
%37 = load i8, i8* %36
%38 = icmp eq i8 %37, 46
br i1 %38, label %64, label %39
39: ; preds = %34
%40 = tail call i8* @append(i8* %11, i8* %36)
%41 = load i32, i32* %2
%42 = add nsw i32 %41, 1
store i32 %42, i32* %2
%43 = load i32, i32* %1
%44 = icmp slt i32 %41, %43
br i1 %44, label %57, label %45
45: ; preds = %39
%46 = add nsw i32 %43, 1
store i32 %46, i32* %1
%47 = tail call i8* @read_file(i8* %40)
%48 = icmp eq i8* %47, null
br i1 %48, label %55, label %49
49: ; preds = %45
%50 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %40)
%51 = tail call i8* @strdup(i8* %40)
store i8* %51, i8** %3
%52 = icmp eq i8* %40, null
br i1 %52, label %59, label %53
53: ; preds = %49
%54 = tail call i32 @free(i8* %40)
br label %59
55: ; preds = %45
%56 = icmp eq i8* %40, null
br i1 %56, label %64, label %62
57: ; preds = %39
%58 = icmp eq i8* %40, null
br i1 %58, label %64, label %62
59: ; preds = %49, %53
%60 = tail call i32 @closedir(i32* %20)
%61 = tail call i32 @free(i8* %11)
br label %72
62: ; preds = %57, %55
%63 = tail call i32 @free(i8* %40)
br label %64
64: ; preds = %62, %55, %57, %25, %34
%65 = tail call %struct.dirent* @readdir(i32* %20)
%66 = icmp eq %struct.dirent* %65, null
br i1 %66, label %67, label %25
67: ; preds = %64, %22
%68 = tail call i32 @closedir(i32* %20)
%69 = tail call i32 @free(i8* %11)
br label %72
70: ; preds = %19
%71 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %11)
br label %72
72: ; preds = %59, %70, %67
%73 = phi i8* [ %47, %59 ], [ null, %67 ], [ null, %70 ]
ret i8* %73
}