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

/* Variables and functions */
scalar_t__ DT_DIR;
int IN_CREATE;
int IN_DELETE;
int IN_MODIFY;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int inotify_add_watch(int, char *, int);
int /*<<< orphan*/ log_data(char *, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcat(char *, int /*<<< orphan*/);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ write_watch_file(char *, int);
void add_watches(int fd, char *root, char *watch_logs) {
  int wd;
  char *abs_dir;
  struct dirent *entry;
  DIR *dp;
  dp = opendir(root);
  if (dp == NULL) {
    perror("Error opening the starting directory");
    exit(0);
  }
  wd = inotify_add_watch(fd, root, IN_CREATE | IN_MODIFY | IN_DELETE);
  write_watch_file(root, wd);
  abs_dir = (char *)malloc(1024);
  memset(abs_dir, 1024, 0);
  while ((entry = readdir(dp))) {
    log_data(watch_logs, "Adding watches");
    if (entry->d_type == DT_DIR) {
      if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
        continue;
      strcpy(abs_dir, root);
      strcat(abs_dir, entry->d_name);
      wd = inotify_add_watch(fd, abs_dir, IN_CREATE | IN_MODIFY | IN_DELETE);
      if (wd == -1) {
        log_data(watch_logs, "Could not add watch");
      } else {
        write_watch_file(abs_dir, wd);
        log_data(watch_logs, "Add watch");
      }
    }
  }
  closedir(dp);
  free(abs_dir);
}


x86 assembly (clang, -O0):
<add_watches>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    121e <add_watches+0x3e>
lea    0xdee(%rip),%rdi
call   10e0 <perror@plt>
xor    %edi,%edi
call   1100 <exit@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2d9c(%rip),%rax
mov    (%rax),%edx
mov    0x2dbb(%rip),%rax
or     (%rax),%edx
mov    0x2da2(%rip),%rax
or     (%rax),%edx
call   10c0 <inotify_add_watch@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1090 <write_watch_file@plt>
mov    $0x400,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x400,%esi
xor    %edx,%edx
call   1060 <memset@plt>
mov    -0x38(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     137b <add_watches+0x19b>
mov    -0x18(%rbp),%rdi
lea    0xd91(%rip),%rsi
call   10d0 <log_data@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    0x2d29(%rip),%rcx
cmp    (%rcx),%rax
jne    1376 <add_watches+0x196>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%edi
lea    0xd77(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     12ea <add_watches+0x10a>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%edi
lea    0xd59(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    12ef <add_watches+0x10f>
jmp    1272 <add_watches+0x92>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%esi
call   10f0 <strcat@plt>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    0x2cae(%rip),%rax
mov    (%rax),%edx
mov    0x2ccd(%rip),%rax
or     (%rax),%edx
mov    0x2cb4(%rip),%rax
or     (%rax),%edx
call   10c0 <inotify_add_watch@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1355 <add_watches+0x175>
mov    -0x18(%rbp),%rdi
lea    0xcec(%rip),%rsi
call   10d0 <log_data@plt>
jmp    1371 <add_watches+0x191>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1090 <write_watch_file@plt>
mov    -0x18(%rbp),%rdi
lea    0xcdf(%rip),%rsi
call   10d0 <log_data@plt>
jmp    1376 <add_watches+0x196>
jmp    1272 <add_watches+0x92>
mov    -0x38(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_watches>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
mov    %rsi,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1365 <add_watches+0x185>
mov    %rax,%rbp
mov    0x2db6(%rip),%rax
mov    0x2dd7(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dbc(%rip),%rax
or     (%rax),%edx
mov    %r15d,0xc(%rsp)
mov    %r15d,%edi
mov    %r14,%rsi
call   10c0 <inotify_add_watch@plt>
mov    %r14,0x10(%rsp)
mov    %r14,%rdi
mov    %eax,%esi
call   1090 <write_watch_file@plt>
mov    $0x400,%edi
call   10b0 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x400,%esi
xor    %edx,%edx
call   1060 <memset@plt>
mov    %rbp,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     1347 <add_watches+0x167>
mov    %rax,%r14
lea    0xda9(%rip),%r13
mov    0x2d4d(%rip),%r15
jmp    12a4 <add_watches+0xc4>
mov    %rbx,%rdi
call   10d0 <log_data@plt>
nopl   (%rax)
mov    %rbp,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     1347 <add_watches+0x167>
mov    %rbx,%rdi
mov    %r13,%rsi
call   10d0 <log_data@plt>
mov    (%r14),%rax
cmp    (%r15),%rax
jne    1290 <add_watches+0xb0>
mov    0x8(%r14),%edi
lea    0xd73(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1290 <add_watches+0xb0>
mov    0x8(%r14),%edi
lea    0xd5d(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1290 <add_watches+0xb0>
mov    %r12,%rdi
mov    0x10(%rsp),%rsi
call   1040 <strcpy@plt>
mov    0x8(%r14),%esi
mov    %r12,%rdi
call   10f0 <strcat@plt>
mov    0x2cef(%rip),%rax
mov    (%rax),%edx
mov    0x2cbe(%rip),%rax
or     (%rax),%edx
mov    0x2ccd(%rip),%rax
or     (%rax),%edx
mov    0xc(%rsp),%edi
mov    %r12,%rsi
call   10c0 <inotify_add_watch@plt>
lea    0xd0f(%rip),%rsi
cmp    $0xffffffff,%eax
je     1285 <add_watches+0xa5>
mov    %r12,%rdi
mov    %eax,%esi
call   1090 <write_watch_file@plt>
lea    0xd09(%rip),%rsi
jmp    1285 <add_watches+0xa5>
mov    %rbp,%rdi
call   1070 <closedir@plt>
mov    %r12,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
lea    0xc94(%rip),%rdi
call   10e0 <perror@plt>
xor    %edi,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i64, i32 }
@.str = [37 x i8] c"Error opening the starting directory\00"
@IN_CREATE = global i32 0
@IN_MODIFY = global i32 0
@IN_DELETE = global i32 0
@.str.1 = [15 x i8] c"Adding watches\00"
@DT_DIR = global i64 0
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [20 x i8] c"Could not add watch\00"
@.str.5 = [10 x i8] c"Add watch\00"
define void @add_watches(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca %struct.dirent*
%10 = alloca i32*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%11 = load i8*, i8** %5
%12 = call i32* @opendir(i8* %11)
store i32* %12, i32** %10
%13 = load i32*, i32** %10
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 0)
unreachable
18: ; preds = %3
%19 = load i32, i32* %4
%20 = load i8*, i8** %5
%21 = load i32, i32* @IN_CREATE
%22 = load i32, i32* @IN_MODIFY
%23 = or i32 %21, %22
%24 = load i32, i32* @IN_DELETE
%25 = or i32 %23, %24
%26 = call i32 @inotify_add_watch(i32 %19, i8* %20, i32 %25)
store i32 %26, i32* %7
%27 = load i8*, i8** %5
%28 = load i32, i32* %7
%29 = call i32 @write_watch_file(i8* %27, i32 %28)
%30 = call i64 @malloc(i32 1024)
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %8
%32 = load i8*, i8** %8
%33 = call i32 @memset(i8* %32, i32 1024, i32 0)
br label %34
34: ; preds = %88, %58, %18
%35 = load i32*, i32** %10
%36 = call %struct.dirent* @readdir(i32* %35)
store %struct.dirent* %36, %struct.dirent** %9
%37 = icmp ne %struct.dirent* %36, null
br i1 %37, label %38, label %89
38: ; preds = %34
%39 = load i8*, i8** %6
%40 = call i32 @log_data(i8* %39, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%41 = load %struct.dirent*, %struct.dirent** %9
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i32 0, i32 0
%43 = load i64, i64* %42
%44 = load i64, i64* @DT_DIR
%45 = icmp eq i64 %43, %44
br i1 %45, label %46, label %88
46: ; preds = %38
%47 = load %struct.dirent*, %struct.dirent** %9
%48 = getelementptr inbounds %struct.dirent, %struct.dirent* %47, i32 0, i32 1
%49 = load i32, i32* %48
%50 = call i64 @strcmp(i32 %49, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%51 = icmp eq i64 %50, 0
br i1 %51, label %58, label %52
52: ; preds = %46
%53 = load %struct.dirent*, %struct.dirent** %9
%54 = getelementptr inbounds %struct.dirent, %struct.dirent* %53, i32 0, i32 1
%55 = load i32, i32* %54
%56 = call i64 @strcmp(i32 %55, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%57 = icmp eq i64 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %52, %46
br label %34
59: ; preds = %52
%60 = load i8*, i8** %8
%61 = load i8*, i8** %5
%62 = call i32 @strcpy(i8* %60, i8* %61)
%63 = load i8*, i8** %8
%64 = load %struct.dirent*, %struct.dirent** %9
%65 = getelementptr inbounds %struct.dirent, %struct.dirent* %64, i32 0, i32 1
%66 = load i32, i32* %65
%67 = call i32 @strcat(i8* %63, i32 %66)
%68 = load i32, i32* %4
%69 = load i8*, i8** %8
%70 = load i32, i32* @IN_CREATE
%71 = load i32, i32* @IN_MODIFY
%72 = or i32 %70, %71
%73 = load i32, i32* @IN_DELETE
%74 = or i32 %72, %73
%75 = call i32 @inotify_add_watch(i32 %68, i8* %69, i32 %74)
store i32 %75, i32* %7
%76 = load i32, i32* %7
%77 = icmp eq i32 %76, -1
br i1 %77, label %78, label %81
78: ; preds = %59
%79 = load i8*, i8** %6
%80 = call i32 @log_data(i8* %79, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %87
81: ; preds = %59
%82 = load i8*, i8** %8
%83 = load i32, i32* %7
%84 = call i32 @write_watch_file(i8* %82, i32 %83)
%85 = load i8*, i8** %6
%86 = call i32 @log_data(i8* %85, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
br label %87
87: ; preds = %81, %78
br label %88
88: ; preds = %87, %38
br label %34
89: ; preds = %34
%90 = load i32*, i32** %10
%91 = call i32 @closedir(i32* %90)
%92 = load i8*, i8** %8
%93 = call i32 @free(i8* %92)
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i64, i32 }
@.str = [37 x i8] c"Error opening the starting directory\00"
@IN_CREATE = global i32 0
@IN_MODIFY = global i32 0
@IN_DELETE = global i32 0
@.str.1 = [15 x i8] c"Adding watches\00"
@DT_DIR = global i64 0
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [20 x i8] c"Could not add watch\00"
@.str.5 = [10 x i8] c"Add watch\00"
define void @add_watches(i32 %0, i8* %1, i8* %2) {
%4 = tail call i32* @opendir(i8* %1)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = tail call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 0)
unreachable
9: ; preds = %3
%10 = load i32, i32* @IN_CREATE
%11 = load i32, i32* @IN_MODIFY
%12 = or i32 %11, %10
%13 = load i32, i32* @IN_DELETE
%14 = or i32 %12, %13
%15 = tail call i32 @inotify_add_watch(i32 %0, i8* %1, i32 %14)
%16 = tail call i32 @write_watch_file(i8* %1, i32 %15)
%17 = tail call i64 @malloc(i32 1024)
%18 = inttoptr i64 %17 to i8*
%19 = tail call i32 @memset(i8* %18, i32 1024, i32 0)
%20 = tail call %struct.dirent* @readdir(i32* %4)
%21 = icmp eq %struct.dirent* %20, null
br i1 %21, label %57, label %22
22: ; preds = %9, %41
%23 = phi %struct.dirent* [ %42, %41 ], [ %20, %9 ]
%24 = tail call i32 @log_data(i8* %2, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i64 0, i32 0
%26 = load i64, i64* %25
%27 = load i64, i64* @DT_DIR
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %41
29: ; preds = %22
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i64 0, i32 1
%31 = load i32, i32* %30
%32 = tail call i64 @strcmp(i32 %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
br i1 %33, label %41, label %34
34: ; preds = %29
%35 = load i32, i32* %30
%36 = tail call i64 @strcmp(i32 %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%37 = icmp eq i64 %36, 0
br i1 %37, label %41, label %44
38: ; preds = %44, %55
%39 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), %55 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), %44 ]
%40 = tail call i32 @log_data(i8* %2, i8* %39)
br label %41
41: ; preds = %38, %22, %29, %34
%42 = tail call %struct.dirent* @readdir(i32* %4)
%43 = icmp eq %struct.dirent* %42, null
br i1 %43, label %57, label %22
44: ; preds = %34
%45 = tail call i32 @strcpy(i8* %18, i8* %1)
%46 = load i32, i32* %30
%47 = tail call i32 @strcat(i8* %18, i32 %46)
%48 = load i32, i32* @IN_CREATE
%49 = load i32, i32* @IN_MODIFY
%50 = or i32 %49, %48
%51 = load i32, i32* @IN_DELETE
%52 = or i32 %50, %51
%53 = tail call i32 @inotify_add_watch(i32 %0, i8* %18, i32 %52)
%54 = icmp eq i32 %53, -1
br i1 %54, label %38, label %55
55: ; preds = %44
%56 = tail call i32 @write_watch_file(i8* %18, i32 %53)
br label %38
57: ; preds = %41, %9
%58 = tail call i32 @closedir(i32* %4)
%59 = tail call i32 @free(i8* %18)
ret void
}