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

/* Variables and functions */
int S_IFDIR;
int S_IFMT;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ *opendir(char *);
int parseadminlevel(int /*<<< orphan*/ *, int);
long parseid(int /*<<< orphan*/ *);
long parselast(int /*<<< orphan*/ *);
int parselevel(int /*<<< orphan*/ *);
char *parsename(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcmp(char *, char *);
void walkdir(FILE *index_file, char *dir) {
  char filename_qfd[1000];
  struct dirent *dp;
  DIR *dfd;
  if ((dfd = opendir(dir)) == NULL) {
    fprintf(stderr, "Can't open %s\n", dir);
    return;
  }
  while ((dp = readdir(dfd)) != NULL) {
    struct stat stbuf;
    sprintf(filename_qfd, "%s/%s", dir, dp->d_name);
    if (stat(filename_qfd, &stbuf) == -1) {
      fprintf(stdout, "Unable to stat file: %s\n", filename_qfd);
      continue;
    }
    if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
      if (!strcmp(".", dp->d_name) || !strcmp("..", dp->d_name))
        continue;
      walkdir(index_file, filename_qfd);
    } else {
      char *name = parsename(dp->d_name);
      if (name != NULL) {
        FILE *plr_file = fopen(filename_qfd, "r");
        long id = parseid(plr_file);
        int level = parselevel(plr_file);
        int adminlevel = parseadminlevel(plr_file, level);
        if (level > 30)
          level = 30;
        long last = parselast(plr_file);
        fprintf(index_file, "%ld %s %d %d 0 %ld\n", id, name, level, adminlevel,
                last);
        fclose(plr_file);
      }
    }
  }
}


x86 assembly (clang, -O0):
<walkdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x410(%rbp)
cmp    $0x0,%rax
jne    122e <walkdir+0x4e>
mov    0x2dc4(%rip),%rax
movslq (%rax),%rdi
mov    -0x10(%rbp),%rdx
lea    0xdde(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
jmp    141b <walkdir+0x23b>
jmp    1233 <walkdir+0x53>
mov    -0x410(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x408(%rbp)
cmp    $0x0,%rax
je     141b <walkdir+0x23b>
lea    -0x400(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x408(%rbp),%rax
mov    (%rax),%rcx
lea    0xda3(%rip),%rsi
call   1100 <sprintf@plt>
lea    -0x400(%rbp),%rdi
lea    -0x418(%rbp),%rsi
call   10b0 <stat@plt>
cmp    $0xffffffff,%eax
jne    12b1 <walkdir+0xd1>
mov    0x2d3c(%rip),%rax
movslq (%rax),%rdi
lea    -0x400(%rbp),%rdx
lea    0xd70(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
jmp    1233 <walkdir+0x53>
mov    -0x418(%rbp),%eax
mov    0x2d22(%rip),%rcx
and    (%rcx),%eax
mov    0x2cf9(%rip),%rcx
cmp    (%rcx),%eax
jne    1327 <walkdir+0x147>
mov    -0x408(%rbp),%rax
mov    (%rax),%rsi
lea    0xd4f(%rip),%rdi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
je     130d <walkdir+0x12d>
mov    -0x408(%rbp),%rax
mov    (%rax),%rsi
lea    0xd2f(%rip),%rdi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    1312 <walkdir+0x132>
jmp    1233 <walkdir+0x53>
mov    -0x8(%rbp),%rdi
lea    -0x400(%rbp),%rsi
call   10e0 <walkdir@plt>
jmp    1416 <walkdir+0x236>
mov    -0x408(%rbp),%rax
mov    (%rax),%rdi
call   1030 <parsename@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
je     1411 <walkdir+0x231>
lea    -0x400(%rbp),%rdi
lea    0xcd8(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,-0x428(%rbp)
mov    -0x428(%rbp),%rdi
call   1070 <parseid@plt>
mov    %rax,-0x430(%rbp)
mov    -0x428(%rbp),%rdi
call   10d0 <parselevel@plt>
mov    %eax,-0x434(%rbp)
mov    -0x428(%rbp),%rdi
mov    -0x434(%rbp),%esi
call   1080 <parseadminlevel@plt>
mov    %eax,-0x438(%rbp)
cmpl   $0x1e,-0x434(%rbp)
jle    13b9 <walkdir+0x1d9>
movl   $0x1e,-0x434(%rbp)
mov    -0x428(%rbp),%rdi
call   1040 <parselast@plt>
mov    %rax,-0x440(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x430(%rbp),%rdx
mov    -0x420(%rbp),%rcx
mov    -0x434(%rbp),%r8d
mov    -0x438(%rbp),%r9d
mov    -0x440(%rbp),%rax
lea    0xc39(%rip),%rsi
mov    %rax,(%rsp)
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x428(%rbp),%rdi
call   1050 <fclose@plt>
jmp    1416 <walkdir+0x236>
jmp    1233 <walkdir+0x53>
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<walkdir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rsi,%r15
mov    %rdi,0x18(%rsp)
mov    %rsi,%rdi
call   1060 <opendir@plt>
test   %rax,%rax
je     13a7 <walkdir+0x1c7>
mov    %rax,%r13
mov    %rax,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     1395 <walkdir+0x1b5>
mov    %rax,%r14
lea    0xde7(%rip),%r12
lea    0x30(%rsp),%rbx
mov    %r15,0x20(%rsp)
jmp    1274 <walkdir+0x94>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x2d89(%rip),%rax
movslq (%rax),%rdi
lea    0xdc4(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    %r13,%rdi
call   10c0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
lea    0x30(%rsp),%rbx
je     1395 <walkdir+0x1b5>
mov    (%r14),%rcx
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   1100 <sprintf@plt>
mov    %rbx,%rdi
lea    0x10(%rsp),%rsi
call   10b0 <stat@plt>
cmp    $0xffffffff,%eax
je     1240 <walkdir+0x60>
mov    0x2d42(%rip),%rax
mov    (%rax),%eax
and    0x10(%rsp),%eax
mov    (%r14),%rbx
mov    0x2d12(%rip),%rcx
cmp    (%rcx),%eax
jne    12f0 <walkdir+0x110>
lea    0xd76(%rip),%rdi
mov    %rbx,%rsi
call   1090 <strcmp@plt>
test   %eax,%eax
je     125b <walkdir+0x7b>
lea    0xd62(%rip),%rdi
mov    %rbx,%rsi
call   1090 <strcmp@plt>
test   %eax,%eax
je     125b <walkdir+0x7b>
mov    0x18(%rsp),%rdi
lea    0x30(%rsp),%rsi
call   10e0 <walkdir@plt>
jmp    125b <walkdir+0x7b>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1030 <parsename@plt>
test   %rax,%rax
je     125b <walkdir+0x7b>
mov    %rax,%rbp
lea    0x30(%rsp),%rdi
lea    0xd21(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <parseid@plt>
mov    %rax,0x28(%rsp)
mov    %rbx,%rdi
call   10d0 <parselevel@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
mov    %eax,%esi
call   1080 <parseadminlevel@plt>
mov    %r12,%r14
mov    %r13,%r12
mov    %eax,%r13d
cmp    $0x1e,%r15d
mov    $0x1e,%eax
cmovge %eax,%r15d
mov    %rbx,%rdi
call   1040 <parselast@plt>
mov    %rax,(%rsp)
mov    0x18(%rsp),%rdi
lea    0xccb(%rip),%rsi
mov    0x28(%rsp),%rdx
mov    %rbp,%rcx
mov    %r15d,%r8d
mov    0x20(%rsp),%r15
mov    %r13d,%r9d
mov    %r12,%r13
mov    %r14,%r12
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    125b <walkdir+0x7b>
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c2a(%rip),%rax
movslq (%rax),%rdi
lea    0xc48(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10a0 <fprintf@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [15 x i8] c"Can't open %s\0A\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@stdout = global i32 0
@.str.2 = [25 x i8] c"Unable to stat file: %s\0A\00"
@S_IFMT = global i32 0
@S_IFDIR = global i32 0
@.str.3 = [2 x i8] c".\00"
@.str.4 = [3 x i8] c"..\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [20 x i8] c"%ld %s %d %d 0 %ld\0A\00"
define void @walkdir(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca [1000 x i8]
%6 = alloca %struct.dirent*
%7 = alloca i32*
%8 = alloca %struct.stat
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
%14 = alloca i64
store i32* %0, i32** %3
store i8* %1, i8** %4
%15 = load i8*, i8** %4
%16 = call i32* @opendir(i8* %15)
store i32* %16, i32** %7
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %24
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = sext i32 %19 to i64
%21 = inttoptr i64 %20 to i32*
%22 = load i8*, i8** %4
%23 = call i32 (i32*, i8*, ...) @fprintf(i32* %21, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %22)
br label %102
24: ; preds = %2
br label %25
25: ; preds = %101, %64, %39, %24
%26 = load i32*, i32** %7
%27 = call %struct.dirent* @readdir(i32* %26)
store %struct.dirent* %27, %struct.dirent** %6
%28 = icmp ne %struct.dirent* %27, null
br i1 %28, label %29, label %102
29: ; preds = %25
%30 = getelementptr inbounds [1000 x i8], [1000 x i8]* %5, i64 0, i64 0
%31 = load i8*, i8** %4
%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 = call i32 @sprintf(i8* %30, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %31, i8* %34)
%36 = getelementptr inbounds [1000 x i8], [1000 x i8]* %5, i64 0, i64 0
%37 = call i32 @stat(i8* %36, %struct.stat* %8)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %45
39: ; preds = %29
%40 = load i32, i32* @stdout
%41 = sext i32 %40 to i64
%42 = inttoptr i64 %41 to i32*
%43 = getelementptr inbounds [1000 x i8], [1000 x i8]* %5, i64 0, i64 0
%44 = call i32 (i32*, i8*, ...) @fprintf(i32* %42, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %43)
br label %25
45: ; preds = %29
%46 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%47 = load i32, i32* %46
%48 = load i32, i32* @S_IFMT
%49 = and i32 %47, %48
%50 = load i32, i32* @S_IFDIR
%51 = icmp eq i32 %49, %50
br i1 %51, label %52, label %68
52: ; preds = %45
%53 = load %struct.dirent*, %struct.dirent** %6
%54 = getelementptr inbounds %struct.dirent, %struct.dirent* %53, i32 0, i32 0
%55 = load i8*, i8** %54
%56 = call i32 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8* %55)
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %64
58: ; preds = %52
%59 = load %struct.dirent*, %struct.dirent** %6
%60 = getelementptr inbounds %struct.dirent, %struct.dirent* %59, i32 0, i32 0
%61 = load i8*, i8** %60
%62 = call i32 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %61)
%63 = icmp ne i32 %62, 0
br i1 %63, label %65, label %64
64: ; preds = %58, %52
br label %25
65: ; preds = %58
%66 = load i32*, i32** %3
%67 = getelementptr inbounds [1000 x i8], [1000 x i8]* %5, i64 0, i64 0
br label %101
68: ; preds = %45
%69 = load %struct.dirent*, %struct.dirent** %6
%70 = getelementptr inbounds %struct.dirent, %struct.dirent* %69, i32 0, i32 0
%71 = load i8*, i8** %70
%72 = call i8* @parsename(i8* %71)
store i8* %72, i8** %9
%73 = load i8*, i8** %9
%74 = icmp ne i8* %73, null
br i1 %74, label %75, label %100
75: ; preds = %68
%76 = getelementptr inbounds [1000 x i8], [1000 x i8]* %5, i64 0, i64 0
%77 = call i32* @fopen(i8* %76, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %77, i32** %10
%78 = load i32*, i32** %10
%79 = call i64 @parseid(i32* %78)
store i64 %79, i64* %11
%80 = load i32*, i32** %10
%81 = call i32 @parselevel(i32* %80)
store i32 %81, i32* %12
%82 = load i32*, i32** %10
%83 = load i32, i32* %12
%84 = call i32 @parseadminlevel(i32* %82, i32 %83)
store i32 %84, i32* %13
%85 = load i32, i32* %12
%86 = icmp sgt i32 %85, 30
br i1 %86, label %87, label %88
87: ; preds = %75
store i32 30, i32* %12
br label %88
88: ; preds = %87, %75
%89 = load i32*, i32** %10
%90 = call i64 @parselast(i32* %89)
store i64 %90, i64* %14
%91 = load i32*, i32** %3
%92 = load i64, i64* %11
%93 = load i8*, i8** %9
%94 = load i32, i32* %12
%95 = load i32, i32* %13
%96 = load i64, i64* %14
%97 = call i32 (i32*, i8*, ...) @fprintf(i32* %91, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i64 %92, i8* %93, i32 %94, i32 %95, i64 %96)
%98 = load i32*, i32** %10
%99 = call i32 @fclose(i32* %98)
br label %100
100: ; preds = %88, %68
br label %101
101: ; preds = %100, %65
br label %25
102: ; preds = %18, %25
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [15 x i8] c"Can't open %s\0A\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@stdout = global i32 0
@.str.2 = [25 x i8] c"Unable to stat file: %s\0A\00"
@S_IFMT = global i32 0
@S_IFDIR = global i32 0
@.str.3 = [2 x i8] c".\00"
@.str.4 = [3 x i8] c"..\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [20 x i8] c"%ld %s %d %d 0 %ld\0A\00"
define void @walkdir(i32* nocapture %0, i8* %1) {
%3 = alloca [1000 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
%6 = tail call i32* @opendir(i8* %1)
%7 = icmp eq i32* %6, null
br i1 %7, label %14, label %8
8: ; preds = %2
%9 = call %struct.dirent* @readdir(i32* %6)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %61, label %11
11: ; preds = %8
%12 = bitcast %struct.stat* %4 to i8*
%13 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
br label %19
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = sext i32 %15 to i64
%17 = inttoptr i64 %16 to i32*
%18 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %17, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %61
19: ; preds = %11, %58
%20 = phi %struct.dirent* [ %9, %11 ], [ %59, %58 ]
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i64 0, i32 0
%22 = load i8*, i8** %21
%23 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %22)
%24 = call i32 @stat(i8* %5, %struct.stat* %4)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %31
26: ; preds = %19
%27 = load i32, i32* @stdout
%28 = sext i32 %27 to i64
%29 = inttoptr i64 %28 to i32*
%30 = call i32 (i32*, i8*, ...) @fprintf(i32* %29, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %5)
br label %58
31: ; preds = %19
%32 = load i32, i32* %13
%33 = load i32, i32* @S_IFMT
%34 = and i32 %33, %32
%35 = load i32, i32* @S_IFDIR
%36 = icmp eq i32 %34, %35
%37 = load i8*, i8** %21
br i1 %36, label %38, label %45
38: ; preds = %31
%39 = call i32 @strcmp(i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8* dereferenceable(1) %37)
%40 = icmp eq i32 %39, 0
br i1 %40, label %58, label %41
41: ; preds = %38
%42 = call i32 @strcmp(i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* dereferenceable(1) %37)
%43 = icmp eq i32 %42, 0
br i1 %43, label %58, label %44
44: ; preds = %41
br label %58
45: ; preds = %31
%46 = call i8* @parsename(i8* %37)
%47 = icmp eq i8* %46, null
br i1 %47, label %58, label %48
48: ; preds = %45
%49 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%50 = call i64 @parseid(i32* %49)
%51 = call i32 @parselevel(i32* %49)
%52 = call i32 @parseadminlevel(i32* %49, i32 %51)
%53 = icmp slt i32 %51, 30
%54 = select i1 %53, i32 %51, i32 30
%55 = call i64 @parselast(i32* %49)
%56 = call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i64 %50, i8* %46, i32 %54, i32 %52, i64 %55)
%57 = call i32 @fclose(i32* %49)
br label %58
58: ; preds = %44, %48, %45, %38, %41, %26
%59 = call %struct.dirent* @readdir(i32* %6)
%60 = icmp eq %struct.dirent* %59, null
br i1 %60, label %61, label %19
61: ; preds = %58, %8, %14
ret void
}