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

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ add_watch(char *, int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char *, char *);
scalar_t__ strlen(char *);
void watch_dirs(char *dir, int inotify_fd) {
  DIR *dp;
  struct dirent *ep;
  struct stat stat_struct;
  dp = opendir(dir);
  if (dp == NULL)
    return;
  add_watch(dir, inotify_fd);
  while (ep = readdir(dp)) {
    if (strcmp(".", ep->d_name) == 0)
      continue;
    if (strcmp("..", ep->d_name) == 0)
      continue;
    if (stat(dir, &stat_struct))
      continue;
    if (S_ISDIR(stat_struct.st_mode)) {
      char *npath = malloc(strlen(dir) + strlen(ep->d_name) + 2);
      npath[0] = 0;
      strcat(npath, dir);
      strcat(npath, "/");
      strcat(npath, ep->d_name);
      watch_dirs(npath, inotify_fd);
      free(npath);
    }
  }
  closedir(dp);
}


x86 assembly (clang, -O0):
<watch_dirs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ec <watch_dirs+0x2c>
jmp    1305 <watch_dirs+0x145>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1060 <add_watch@plt>
mov    -0x18(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12fc <watch_dirs+0x13c>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xde4(%rip),%rdi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    1231 <watch_dirs+0x71>
jmp    11f8 <watch_dirs+0x38>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xdc1(%rip),%rdi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    1253 <watch_dirs+0x93>
jmp    11f8 <watch_dirs+0x38>
mov    -0x8(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10a0 <stat@plt>
cmp    $0x0,%rax
je     126f <watch_dirs+0xaf>
jmp    11f8 <watch_dirs+0x38>
mov    -0x28(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%rax
je     12f7 <watch_dirs+0x137>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    -0x38(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   10c0 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   10e0 <strcat@plt>
mov    -0x30(%rbp),%rdi
lea    0xd36(%rip),%rsi
call   10e0 <strcat@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
call   10e0 <strcat@plt>
mov    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   10d0 <watch_dirs@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    11f8 <watch_dirs+0x38>
mov    -0x18(%rbp),%rdi
call   1080 <closedir@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<watch_dirs>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r13d
mov    %rdi,%r12
call   1040 <opendir@plt>
test   %rax,%rax
je     12e9 <watch_dirs+0x129>
mov    %rax,%rbp
mov    %r12,%rdi
mov    %r13d,%esi
call   1060 <add_watch@plt>
mov    %rbp,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     12e1 <watch_dirs+0x121>
mov    %rax,%r14
lea    0xdf9(%rip),%r15
lea    0xdf1(%rip),%rbx
jmp    1234 <watch_dirs+0x74>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   10b0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     12e1 <watch_dirs+0x121>
mov    (%r14),%rsi
mov    %r15,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1220 <watch_dirs+0x60>
mov    (%r14),%rsi
mov    %rbx,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1220 <watch_dirs+0x60>
mov    %r12,%rdi
mov    %rsp,%rsi
call   10a0 <stat@plt>
test   %rax,%rax
jne    1220 <watch_dirs+0x60>
mov    (%rsp),%edi
call   1070 <S_ISDIR@plt>
test   %rax,%rax
je     1220 <watch_dirs+0x60>
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %r13d,%ebx
mov    %rax,%r13
mov    (%r14),%rdi
call   1050 <strlen@plt>
lea    (%rax,%r13,1),%rdi
add    $0x2,%rdi
mov    %ebx,%r13d
call   10c0 <malloc@plt>
mov    %rax,%rbx
movb   $0x0,(%rax)
mov    %rax,%rdi
mov    %r12,%rsi
call   10e0 <strcat@plt>
mov    %rbx,%rdi
lea    0xd51(%rip),%rsi
call   10e0 <strcat@plt>
mov    (%r14),%rsi
mov    %rbx,%rdi
call   10e0 <strcat@plt>
mov    %rbx,%rdi
mov    %r13d,%esi
call   10d0 <watch_dirs@plt>
mov    %rbx,%rdi
lea    0xd29(%rip),%rbx
call   1030 <free@plt>
jmp    1220 <watch_dirs+0x60>
mov    %rbp,%rdi
call   1080 <closedir@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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