#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*/ DIR;

/* Variables and functions */
int S_ISDIR(int /*<<< orphan*/);
int 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 *);
scalar_t__ stat(char const *, struct stat *);
scalar_t__ strcmp(char *, char *);
int imv_navigator_add(struct imv_navigator *nav, const char *path,
                      int recursive) {
  char path_buf[4096 + 1];
  struct stat path_info;
  if ((stat(path, &path_info) == 0) && S_ISDIR(path_info.st_mode)) {
    int result = 0;
    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);
        struct stat new_path_info;
        if (stat(path_buf, &new_path_info)) {
          result = 1;
          break;
        }
        int is_dir = S_ISDIR(new_path_info.st_mode);
        if (is_dir && recursive) {
          if (imv_navigator_add(nav, path_buf, recursive) != 0) {
            result = 1;
            break;
          }
        } else if (!is_dir) {
          if (add_item(nav, path_buf) != 0) {
            result = 1;
            break;
          }
        }
      }
      closedir(d);
    }
    return result;
  } else {
    return add_item(nav, path);
  }
  return 0;
}


x86 assembly (clang, -O0):
<imv_navigator_add>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x1028(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
jne    136b <imv_navigator_add+0x1db>
mov    -0x1028(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
je     136b <imv_navigator_add+0x1db>
movl   $0x0,-0x102c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x1038(%rbp)
cmpq   $0x0,-0x1038(%rbp)
je     135d <imv_navigator_add+0x1cd>
jmp    1201 <imv_navigator_add+0x71>
mov    -0x1038(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x1040(%rbp)
cmp    $0x0,%rax
je     1351 <imv_navigator_add+0x1c1>
mov    -0x1040(%rbp),%rax
mov    (%rax),%rdi
lea    0xdd1(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     125e <imv_navigator_add+0xce>
mov    -0x1040(%rbp),%rax
mov    (%rax),%rdi
lea    0xdb2(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1263 <imv_navigator_add+0xd3>
jmp    1201 <imv_navigator_add+0x71>
lea    -0x1020(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    -0x1040(%rbp),%rax
mov    (%rax),%r8
mov    $0x1001,%esi
lea    0xd7f(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x1020(%rbp),%rdi
lea    -0x1048(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
je     12b5 <imv_navigator_add+0x125>
movl   $0x1,-0x102c(%rbp)
jmp    1351 <imv_navigator_add+0x1c1>
mov    -0x1048(%rbp),%edi
call   1050 <S_ISDIR@plt>
mov    %eax,-0x104c(%rbp)
cmpl   $0x0,-0x104c(%rbp)
je     130d <imv_navigator_add+0x17d>
cmpl   $0x0,-0x1c(%rbp)
je     130d <imv_navigator_add+0x17d>
mov    -0x10(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   10b0 <imv_navigator_add@plt>
cmp    $0x0,%eax
je     1308 <imv_navigator_add+0x178>
movl   $0x1,-0x102c(%rbp)
jmp    1351 <imv_navigator_add+0x1c1>
jmp    134c <imv_navigator_add+0x1bc>
cmpl   $0x0,-0x104c(%rbp)
jne    1347 <imv_navigator_add+0x1b7>
mov    -0x10(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
call   1040 <add_item@plt>
cmp    $0x0,%eax
je     1342 <imv_navigator_add+0x1b2>
movl   $0x1,-0x102c(%rbp)
jmp    1351 <imv_navigator_add+0x1c1>
jmp    1347 <imv_navigator_add+0x1b7>
jmp    134c <imv_navigator_add+0x1bc>
jmp    1201 <imv_navigator_add+0x71>
mov    -0x1038(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x102c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    137b <imv_navigator_add+0x1eb>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <add_item@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<imv_navigator_add>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %edx,%ebp
mov    %rsi,%r12
mov    %rdi,%r14
lea    0x8(%rsp),%rsi
mov    %r12,%rdi
call   1090 <stat@plt>
test   %rax,%rax
je     11dc <imv_navigator_add+0x4c>
mov    %r14,%rdi
mov    %r12,%rsi
call   1040 <add_item@plt>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %eax,%eax
je     11bb <imv_navigator_add+0x2b>
mov    %r12,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     12e9 <imv_navigator_add+0x159>
mov    %rax,%r15
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12da <imv_navigator_add+0x14a>
mov    %rax,%r13
jmp    1244 <imv_navigator_add+0xb4>
mov    %r14,%rdi
lea    0x10(%rsp),%rsi
mov    %ebp,%edx
call   10b0 <imv_navigator_add@plt>
test   %eax,%eax
jne    12d3 <imv_navigator_add+0x143>
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     12da <imv_navigator_add+0x14a>
mov    0x0(%r13),%rdi
lea    0xdb1(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1230 <imv_navigator_add+0xa0>
mov    0x0(%r13),%rdi
lea    0xd9d(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1230 <imv_navigator_add+0xa0>
mov    0x0(%r13),%r8
mov    %ebp,%ebx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x1001,%esi
lea    0xd7b(%rip),%rdx
mov    %r12,%rcx
call   1060 <snprintf@plt>
mov    %rbp,%rdi
mov    %ebx,%ebp
mov    %rsp,%rsi
call   1090 <stat@plt>
test   %rax,%rax
jne    12d3 <imv_navigator_add+0x143>
mov    (%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %ebp,%ebp
je     12b6 <imv_navigator_add+0x126>
test   %eax,%eax
jne    1213 <imv_navigator_add+0x83>
test   %eax,%eax
jne    1230 <imv_navigator_add+0xa0>
mov    %r14,%rdi
lea    0x10(%rsp),%rsi
call   1040 <add_item@plt>
test   %eax,%eax
je     1230 <imv_navigator_add+0xa0>
mov    $0x1,%ebx
jmp    12dc <imv_navigator_add+0x14c>
xor    %ebx,%ebx
mov    %r15,%rdi
call   1070 <closedir@plt>
jmp    11c8 <imv_navigator_add+0x38>
xor    %ebx,%ebx
jmp    11c8 <imv_navigator_add+0x38>

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 i32 @imv_navigator_add(%struct.imv_navigator* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.imv_navigator*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [4097 x i8]
%9 = alloca %struct.stat
%10 = alloca i32
%11 = alloca i32*
%12 = alloca %struct.dirent*
%13 = alloca %struct.stat
%14 = alloca i32
store %struct.imv_navigator* %0, %struct.imv_navigator** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%15 = load i8*, i8** %6
%16 = call i64 @stat(i8* %15, %struct.stat* %9)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %91
18: ; preds = %3
%19 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @S_ISDIR(i32 %20)
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %91
23: ; preds = %18
store i32 0, i32* %10
%24 = load i8*, i8** %6
%25 = call i32* @opendir(i8* %24)
store i32* %25, i32** %11
%26 = load i32*, i32** %11
%27 = icmp ne i32* %26, null
br i1 %27, label %28, label %89
28: ; preds = %23
br label %29
29: ; preds = %85, %45, %28
%30 = load i32*, i32** %11
%31 = call %struct.dirent* @readdir(i32* %30)
store %struct.dirent* %31, %struct.dirent** %12
%32 = icmp ne %struct.dirent* %31, null
br i1 %32, label %33, label %86
33: ; preds = %29
%34 = load %struct.dirent*, %struct.dirent** %12
%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 ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%38 = icmp eq i64 %37, 0
br i1 %38, label %45, label %39
39: ; preds = %33
%40 = load %struct.dirent*, %struct.dirent** %12
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i32 0, i32 0
%42 = load i8*, i8** %41
%43 = call i64 @strcmp(i8* %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %39, %33
br label %29
46: ; preds = %39
%47 = getelementptr inbounds [4097 x i8], [4097 x i8]* %8, i64 0, i64 0
%48 = load i8*, i8** %6
%49 = load %struct.dirent*, %struct.dirent** %12
%50 = getelementptr inbounds %struct.dirent, %struct.dirent* %49, i32 0, i32 0
%51 = load i8*, i8** %50
%52 = call i32 @snprintf(i8* %47, i32 4097, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %48, i8* %51)
%53 = getelementptr inbounds [4097 x i8], [4097 x i8]* %8, i64 0, i64 0
%54 = call i64 @stat(i8* %53, %struct.stat* %13)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %46
store i32 1, i32* %10
br label %86
57: ; preds = %46
%58 = getelementptr inbounds %struct.stat, %struct.stat* %13, i32 0, i32 0
%59 = load i32, i32* %58
%60 = call i32 @S_ISDIR(i32 %59)
store i32 %60, i32* %14
%61 = load i32, i32* %14
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %74
63: ; preds = %57
%64 = load i32, i32* %7
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %74
66: ; preds = %63
%67 = load %struct.imv_navigator*, %struct.imv_navigator** %5
%68 = getelementptr inbounds [4097 x i8], [4097 x i8]* %8, i64 0, i64 0
%69 = load i32, i32* %7
%70 = call i32 @imv_navigator_add(%struct.imv_navigator* %67, i8* %68, i32 %69)
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %73
72: ; preds = %66
store i32 1, i32* %10
br label %86
73: ; preds = %66
br label %85
74: ; preds = %63, %57
%75 = load i32, i32* %14
%76 = icmp ne i32 %75, 0
br i1 %76, label %84, label %77
77: ; preds = %74
%78 = load %struct.imv_navigator*, %struct.imv_navigator** %5
%79 = getelementptr inbounds [4097 x i8], [4097 x i8]* %8, i64 0, i64 0
%80 = call i32 @add_item(%struct.imv_navigator* %78, i8* %79)
%81 = icmp ne i32 %80, 0
br i1 %81, label %82, label %83
82: ; preds = %77
store i32 1, i32* %10
br label %86
83: ; preds = %77
br label %84
84: ; preds = %83, %74
br label %85
85: ; preds = %84, %73
br label %29
86: ; preds = %82, %72, %56, %29
%87 = load i32*, i32** %11
%88 = call i32 @closedir(i32* %87)
br label %89
89: ; preds = %86, %23
%90 = load i32, i32* %10
store i32 %90, i32* %4
br label %95
91: ; preds = %18, %3
%92 = load %struct.imv_navigator*, %struct.imv_navigator** %5
%93 = load i8*, i8** %6
%94 = call i32 @add_item(%struct.imv_navigator* %92, i8* %93)
store i32 %94, i32* %4
br label %95
95: ; preds = %91, %89
%96 = load i32, i32* %4
ret i32 %96
}

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