#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 */
int IN_CREATE;
int IN_DELETE;
int IN_DELETE_SELF;
int IN_ISDIR;
int IN_MODIFY;
int IN_MOVED_FROM;
int IN_MOVED_TO;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int inotify_add_watch(int, char *, int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printerr_ex(int, char *, int);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ save_pair(int, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int /*<<< orphan*/ stat(char *, struct stat *);
scalar_t__ strcmp(char *, char *);
void watch_dirs(int ifd, char *path) {
  DIR *p_dir;
  struct dirent *p_dirent;
  struct stat st;
  char full_path[256];
  int wd;
  p_dir = opendir(path);
  if (p_dir == NULL)
    return;
  wd = inotify_add_watch(ifd, path,
                         IN_MODIFY | IN_CREATE | IN_DELETE | IN_MOVED_FROM |
                             IN_MOVED_TO | IN_ISDIR | IN_DELETE_SELF);
  printerr_ex((wd == -1), "inotify_add_watch", 1);
  save_pair(wd, path);
  while ((p_dirent = readdir(p_dir)) != NULL) {
    snprintf(full_path, 256 - 1, "%s/%s", path, p_dirent->d_name);
    stat(full_path, &st);
    if (S_ISDIR(st.st_mode)) {
      if (strcmp(p_dirent->d_name, ".") && strcmp(p_dirent->d_name, ".."))
        watch_dirs(ifd, full_path);
    }
  }
  closedir(p_dir);
}


x86 assembly (clang, -O0):
<watch_dirs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11df <watch_dirs+0x2f>
jmp    1317 <watch_dirs+0x167>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
mov    0x2dc2(%rip),%rax
or     (%rax),%edx
mov    0x2de1(%rip),%rax
or     (%rax),%edx
mov    0x2db8(%rip),%rax
or     (%rax),%edx
mov    0x2db7(%rip),%rax
or     (%rax),%edx
mov    0x2dbe(%rip),%rax
or     (%rax),%edx
mov    0x2d85(%rip),%rax
or     (%rax),%edx
call   10a0 <inotify_add_watch@plt>
mov    %eax,-0x134(%rbp)
cmpl   $0xffffffff,-0x134(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
lea    0xdba(%rip),%rsi
mov    $0x1,%edx
call   10c0 <printerr_ex@plt>
mov    -0x134(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   10d0 <save_pair@plt>
mov    -0x18(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     130e <watch_dirs+0x15e>
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    (%rax),%r8
mov    $0xff,%esi
lea    0xd7e(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x130(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <stat@plt>
mov    -0x28(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1309 <watch_dirs+0x159>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xd50(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1304 <watch_dirs+0x154>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xd32(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1304 <watch_dirs+0x154>
mov    -0x4(%rbp),%edi
lea    -0x130(%rbp),%rsi
call   10b0 <watch_dirs@plt>
jmp    1309 <watch_dirs+0x159>
jmp    125f <watch_dirs+0xaf>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<watch_dirs>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rsi,%r12
mov    %edi,%ebx
mov    %rsi,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     12fd <watch_dirs+0x14d>
mov    %rax,%rbp
mov    0x2e0f(%rip),%rax
mov    0x2dd0(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2ded(%rip),%rax
or     (%rax),%edx
mov    0x2dc4(%rip),%rax
or     (%rax),%edx
mov    0x2dc3(%rip),%rax
or     (%rax),%edx
mov    0x2dca(%rip),%rax
or     (%rax),%edx
mov    0x2d91(%rip),%rax
or     (%rax),%edx
mov    %ebx,0xc(%rsp)
mov    %ebx,%edi
mov    %r12,%rsi
call   10a0 <inotify_add_watch@plt>
mov    %eax,%ebx
xor    %edi,%edi
cmp    $0xffffffff,%eax
sete   %dil
lea    0xdc7(%rip),%rsi
mov    $0x1,%edx
call   10c0 <printerr_ex@plt>
mov    %ebx,%edi
mov    %r12,%rsi
call   10d0 <save_pair@plt>
mov    %rbp,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     12f5 <watch_dirs+0x145>
mov    %rax,%r14
lea    0xdaa(%rip),%r15
lea    0x10(%rsp),%rbx
lea    0x8(%rsp),%r13
jmp    1290 <watch_dirs+0xe0>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbp,%rdi
call   1090 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     12f5 <watch_dirs+0x145>
mov    (%r14),%r8
mov    %rbx,%rdi
mov    $0xff,%esi
mov    %r15,%rdx
mov    %r12,%rcx
call   1050 <snprintf@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1080 <stat@plt>
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
test   %rax,%rax
je     1280 <watch_dirs+0xd0>
mov    (%r14),%rdi
lea    0xd50(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1280 <watch_dirs+0xd0>
mov    (%r14),%rdi
lea    0xd3b(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1280 <watch_dirs+0xd0>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
call   10b0 <watch_dirs@plt>
jmp    1280 <watch_dirs+0xd0>
mov    %rbp,%rdi
call   1060 <closedir@plt>
add    $0x118,%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 }
@IN_MODIFY = global i32 0
@IN_CREATE = global i32 0
@IN_DELETE = global i32 0
@IN_MOVED_FROM = global i32 0
@IN_MOVED_TO = global i32 0
@IN_ISDIR = global i32 0
@IN_DELETE_SELF = global i32 0
@.str = [18 x i8] c"inotify_add_watch\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
define void @watch_dirs(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca %struct.stat
%8 = alloca [256 x i8]
%9 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%10 = load i8*, i8** %4
%11 = call i32* @opendir(i8* %10)
store i32* %11, i32** %5
%12 = load i32*, i32** %5
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %15
14: ; preds = %2
br label %76
15: ; preds = %2
%16 = load i32, i32* %3
%17 = load i8*, i8** %4
%18 = load i32, i32* @IN_MODIFY
%19 = load i32, i32* @IN_CREATE
%20 = or i32 %18, %19
%21 = load i32, i32* @IN_DELETE
%22 = or i32 %20, %21
%23 = load i32, i32* @IN_MOVED_FROM
%24 = or i32 %22, %23
%25 = load i32, i32* @IN_MOVED_TO
%26 = or i32 %24, %25
%27 = load i32, i32* @IN_ISDIR
%28 = or i32 %26, %27
%29 = load i32, i32* @IN_DELETE_SELF
%30 = or i32 %28, %29
%31 = call i32 @inotify_add_watch(i32 %16, i8* %17, i32 %30)
store i32 %31, i32* %9
%32 = load i32, i32* %9
%33 = icmp eq i32 %32, -1
%34 = zext i1 %33 to i32
%35 = call i32 @printerr_ex(i32 %34, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 1)
%36 = load i32, i32* %9
%37 = load i8*, i8** %4
%38 = call i32 @save_pair(i32 %36, i8* %37)
br label %39
39: ; preds = %72, %15
%40 = load i32*, i32** %5
%41 = call %struct.dirent* @readdir(i32* %40)
store %struct.dirent* %41, %struct.dirent** %6
%42 = icmp ne %struct.dirent* %41, null
br i1 %42, label %43, label %73
43: ; preds = %39
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%45 = load i8*, i8** %4
%46 = load %struct.dirent*, %struct.dirent** %6
%47 = getelementptr inbounds %struct.dirent, %struct.dirent* %46, i32 0, i32 0
%48 = load i8*, i8** %47
%49 = call i32 @snprintf(i8* %44, i32 255, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %45, i8* %48)
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%51 = call i32 @stat(i8* %50, %struct.stat* %7)
%52 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i64 @S_ISDIR(i32 %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %72
56: ; preds = %43
%57 = load %struct.dirent*, %struct.dirent** %6
%58 = getelementptr inbounds %struct.dirent, %struct.dirent* %57, i32 0, i32 0
%59 = load i8*, i8** %58
%60 = call i64 @strcmp(i8* %59, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %71
62: ; preds = %56
%63 = load %struct.dirent*, %struct.dirent** %6
%64 = getelementptr inbounds %struct.dirent, %struct.dirent* %63, i32 0, i32 0
%65 = load i8*, i8** %64
%66 = call i64 @strcmp(i8* %65, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %62
%69 = load i32, i32* %3
%70 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
br label %71
71: ; preds = %68, %62, %56
br label %72
72: ; preds = %71, %43
br label %39
73: ; preds = %39
%74 = load i32*, i32** %5
%75 = call i32 @closedir(i32* %74)
br label %76
76: ; preds = %73, %14
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@IN_MODIFY = global i32 0
@IN_CREATE = global i32 0
@IN_DELETE = global i32 0
@IN_MOVED_FROM = global i32 0
@IN_MOVED_TO = global i32 0
@IN_ISDIR = global i32 0
@IN_DELETE_SELF = global i32 0
@.str = [18 x i8] c"inotify_add_watch\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
define void @watch_dirs(i32 %0, i8* %1) {
%3 = alloca %struct.stat
%4 = alloca [256 x i8]
%5 = bitcast %struct.stat* %3 to i8*
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = tail call i32* @opendir(i8* %1)
%8 = icmp eq i32* %7, null
br i1 %8, label %55, label %9
9: ; preds = %2
%10 = load i32, i32* @IN_MODIFY
%11 = load i32, i32* @IN_CREATE
%12 = or i32 %11, %10
%13 = load i32, i32* @IN_DELETE
%14 = or i32 %12, %13
%15 = load i32, i32* @IN_MOVED_FROM
%16 = or i32 %14, %15
%17 = load i32, i32* @IN_MOVED_TO
%18 = or i32 %16, %17
%19 = load i32, i32* @IN_ISDIR
%20 = or i32 %18, %19
%21 = load i32, i32* @IN_DELETE_SELF
%22 = or i32 %20, %21
%23 = tail call i32 @inotify_add_watch(i32 %0, i8* %1, i32 %22)
%24 = icmp eq i32 %23, -1
%25 = zext i1 %24 to i32
%26 = tail call i32 @printerr_ex(i32 %25, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 1)
%27 = tail call i32 @save_pair(i32 %23, i8* %1)
%28 = call %struct.dirent* @readdir(i32* %7)
%29 = icmp eq %struct.dirent* %28, null
br i1 %29, label %53, label %30
30: ; preds = %9
%31 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %32
32: ; preds = %30, %50
%33 = phi %struct.dirent* [ %28, %30 ], [ %51, %50 ]
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i64 0, i32 0
%35 = load i8*, i8** %34
%36 = call i32 @snprintf(i8* %6, i32 255, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %35)
%37 = call i32 @stat(i8* %6, %struct.stat* %3)
%38 = load i32, i32* %31
%39 = call i64 @S_ISDIR(i32 %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %50, label %41
41: ; preds = %32
%42 = load i8*, i8** %34
%43 = call i64 @strcmp(i8* %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%44 = icmp eq i64 %43, 0
br i1 %44, label %50, label %45
45: ; preds = %41
%46 = load i8*, i8** %34
%47 = call i64 @strcmp(i8* %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%48 = icmp eq i64 %47, 0
br i1 %48, label %50, label %49
49: ; preds = %45
br label %50
50: ; preds = %41, %45, %49, %32
%51 = call %struct.dirent* @readdir(i32* %7)
%52 = icmp eq %struct.dirent* %51, null
br i1 %52, label %53, label %32
53: ; preds = %50, %9
%54 = call i32 @closedir(i32* %7)
br label %55
55: ; preds = %2, %53
ret void
}