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

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ add_item(struct imv_navigator *, char const *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char *);
int /*<<< orphan*/ stat(char const *, struct stat *);
scalar_t__ strcmp(char *, char *);
void imv_navigator_add(struct imv_navigator *nav, const char *path,
                       int recursive) {
  char path_buf[512];
  struct stat path_info;
  stat(path, &path_info);
  if (S_ISDIR(path_info.st_mode)) {
    DIR *d = opendir(path);
    if (d) {
      struct dirent *dir;
      while ((dir = readdir(d)) != NULL) {
        if (strcmp(dir->d_name, "..") == 0 || strcmp(dir->d_name, ".") == 0) {
          continue;
        }
        snprintf(path_buf, sizeof(path_buf), "%s/%s", path, dir->d_name);
        if (recursive) {
          imv_navigator_add(nav, path_buf, recursive);
        } else {
          add_item(nav, path_buf);
        }
      }
      closedir(d);
    }
  } else {
    add_item(nav, path);
  }
}


x86 assembly (clang, -O0):
<imv_navigator_add>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x228(%rbp),%rsi
call   1090 <stat@plt>
mov    -0x228(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%rax
je     12be <imv_navigator_add+0x12e>
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x230(%rbp)
cmpq   $0x0,-0x230(%rbp)
je     12b9 <imv_navigator_add+0x129>
jmp    11ee <imv_navigator_add+0x5e>
mov    -0x230(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x238(%rbp)
cmp    $0x0,%rax
je     12ad <imv_navigator_add+0x11d>
mov    -0x238(%rbp),%rax
mov    (%rax),%rdi
lea    0xde4(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     124b <imv_navigator_add+0xbb>
mov    -0x238(%rbp),%rax
mov    (%rax),%rdi
lea    0xdc5(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1250 <imv_navigator_add+0xc0>
jmp    11ee <imv_navigator_add+0x5e>
lea    -0x220(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x238(%rbp),%rax
mov    (%rax),%r8
mov    $0x200,%esi
lea    0xd92(%rip),%rdx
call   1060 <snprintf@plt>
cmpl   $0x0,-0x14(%rbp)
je     1298 <imv_navigator_add+0x108>
mov    -0x8(%rbp),%rdi
lea    -0x220(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   10b0 <imv_navigator_add@plt>
jmp    12a8 <imv_navigator_add+0x118>
mov    -0x8(%rbp),%rdi
lea    -0x220(%rbp),%rsi
call   1040 <add_item@plt>
jmp    11ee <imv_navigator_add+0x5e>
mov    -0x230(%rbp),%rdi
call   1070 <closedir@plt>
jmp    12cb <imv_navigator_add+0x13b>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <add_item@plt>
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<imv_navigator_add>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1090 <stat@plt>
mov    (%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %rax,%rax
je     1282 <imv_navigator_add+0xf2>
mov    %rbx,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     12a6 <imv_navigator_add+0x116>
mov    %rax,%rbp
mov    %r14,0x8(%rsp)
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     129e <imv_navigator_add+0x10e>
mov    %rax,%r14
lea    0xe06(%rip),%r12
lea    0x10(%rsp),%r13
jmp    1230 <imv_navigator_add+0xa0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x8(%rsp),%rdi
mov    %r13,%rsi
mov    %r15d,%edx
call   10b0 <imv_navigator_add@plt>
mov    %rbp,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     129e <imv_navigator_add+0x10e>
mov    (%r14),%rdi
mov    %r12,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1220 <imv_navigator_add+0x90>
mov    (%r14),%rdi
lea    0xdb7(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1220 <imv_navigator_add+0x90>
mov    (%r14),%r8
mov    %r13,%rdi
mov    $0x200,%esi
lea    0xd9d(%rip),%rdx
mov    %rbx,%rcx
call   1060 <snprintf@plt>
test   %r15d,%r15d
jne    1210 <imv_navigator_add+0x80>
mov    0x8(%rsp),%rdi
mov    %r13,%rsi
call   1040 <add_item@plt>
jmp    1220 <imv_navigator_add+0x90>
mov    %r14,%rdi
mov    %rbx,%rsi
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <add_item@plt>
mov    %rbp,%rdi
call   1070 <closedir@plt>
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.imv_navigator = type { i32 }
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [3 x i8] c"..\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define void @imv_navigator_add(%struct.imv_navigator* %0, i8* %1, i32 %2) {
%4 = alloca %struct.imv_navigator*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [512 x i8]
%8 = alloca %struct.stat
%9 = alloca i32*
%10 = alloca %struct.dirent*
store %struct.imv_navigator* %0, %struct.imv_navigator** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%11 = load i8*, i8** %5
%12 = call i32 @stat(i8* %11, %struct.stat* %8)
%13 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%14 = load i32, i32* %13
%15 = call i64 @S_ISDIR(i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %62
17: ; preds = %3
%18 = load i8*, i8** %5
%19 = call i32* @opendir(i8* %18)
store i32* %19, i32** %9
%20 = load i32*, i32** %9
%21 = icmp ne i32* %20, null
br i1 %21, label %22, label %61
22: ; preds = %17
br label %23
23: ; preds = %57, %39, %22
%24 = load i32*, i32** %9
%25 = call %struct.dirent* @readdir(i32* %24)
store %struct.dirent* %25, %struct.dirent** %10
%26 = icmp ne %struct.dirent* %25, null
br i1 %26, label %27, label %58
27: ; preds = %23
%28 = load %struct.dirent*, %struct.dirent** %10
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i32 0, i32 0
%30 = load i8*, i8** %29
%31 = call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %39, label %33
33: ; preds = %27
%34 = load %struct.dirent*, %struct.dirent** %10
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i8*, i8** %35
%37 = call i64 @strcmp(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %33, %27
br label %23
40: ; preds = %33
%41 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%42 = load i8*, i8** %5
%43 = load %struct.dirent*, %struct.dirent** %10
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 0
%45 = load i8*, i8** %44
%46 = call i32 @snprintf(i8* %41, i32 512, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %42, i8* %45)
%47 = load i32, i32* %6
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %40
%50 = load %struct.imv_navigator*, %struct.imv_navigator** %4
%51 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%52 = load i32, i32* %6
br label %57
53: ; preds = %40
%54 = load %struct.imv_navigator*, %struct.imv_navigator** %4
%55 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%56 = call i32 @add_item(%struct.imv_navigator* %54, i8* %55)
br label %57
57: ; preds = %53, %49
br label %23
58: ; preds = %23
%59 = load i32*, i32** %9
%60 = call i32 @closedir(i32* %59)
br label %61
61: ; preds = %58, %17
br label %66
62: ; preds = %3
%63 = load %struct.imv_navigator*, %struct.imv_navigator** %4
%64 = load i8*, i8** %5
%65 = call i32 @add_item(%struct.imv_navigator* %63, i8* %64)
br label %66
66: ; preds = %62, %61
ret void
}

LLVM IR (-O2):
%struct.imv_navigator = type { i32 }
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [3 x i8] c"..\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define void @imv_navigator_add(%struct.imv_navigator* %0, i8* %1, i32 %2) {
%4 = alloca [512 x i8]
%5 = alloca %struct.stat
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%7 = bitcast %struct.stat* %5 to i8*
%8 = call i32 @stat(i8* %1, %struct.stat* %5)
%9 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%10 = load i32, i32* %9
%11 = tail call i64 @S_ISDIR(i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %42, label %13
13: ; preds = %3
%14 = tail call i32* @opendir(i8* %1)
%15 = icmp eq i32* %14, null
br i1 %15, label %44, label %16
16: ; preds = %13
%17 = call %struct.dirent* @readdir(i32* %14)
%18 = icmp eq %struct.dirent* %17, null
br i1 %18, label %40, label %19
19: ; preds = %16
%20 = icmp eq i32 %2, 0
br label %21
21: ; preds = %19, %31
%22 = phi %struct.dirent* [ %17, %19 ], [ %32, %31 ]
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i64 0, i32 0
%24 = load i8*, i8** %23
%25 = call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %21
%28 = load i8*, i8** %23
%29 = call i64 @strcmp(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %37, %38, %21, %27
%32 = call %struct.dirent* @readdir(i32* %14)
%33 = icmp eq %struct.dirent* %32, null
br i1 %33, label %40, label %21
34: ; preds = %27
%35 = load i8*, i8** %23
%36 = call i32 @snprintf(i8* %6, i32 512, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8* %35)
br i1 %20, label %38, label %37
37: ; preds = %34
br label %31
38: ; preds = %34
%39 = call i32 @add_item(%struct.imv_navigator* %0, i8* %6)
br label %31
40: ; preds = %31, %16
%41 = call i32 @closedir(i32* %14)
br label %44
42: ; preds = %3
%43 = tail call i32 @add_item(%struct.imv_navigator* %0, i8* %1)
br label %44
44: ; preds = %13, %40, %42
ret void
}