#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ logErrorMessages(char *, char *);
int /*<<< orphan*/ read_from_line(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ strstr(char *, char *);
void read_watcher_file(char *directory, int watch_id, char *result) {
  FILE *fp;
  char buf[100];
  char watch_file_name[50] = "/root/logs/watcher.txt";
  if ((fp = fopen(watch_file_name, "r")) == NULL) {
    logErrorMessages("Failed to open watch file", watch_file_name);
  }
  char str[10];
  sprintf(str, "%d", watch_id);
  while (!feof(fp)) {
    fgets(buf, 100, fp);
    if (strstr(buf, str)) {
      read_from_line(buf, result);
    }
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<read_watcher_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0xd0(%rbp),%rdi
lea    0xe4c(%rip),%rsi
mov    $0x32,%edx
call   1070 <memcpy@plt>
lea    -0xd0(%rbp),%rdi
lea    0xe66(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11f2 <read_watcher_file+0x62>
lea    -0xd0(%rbp),%rsi
lea    0xe47(%rip),%rdi
call   1080 <logErrorMessages@plt>
lea    -0xda(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe4b(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x20(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1226 <read_watcher_file+0x96>
jmp    126d <read_watcher_file+0xdd>
lea    -0x90(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x64,%esi
call   1050 <fgets@plt>
lea    -0x90(%rbp),%rdi
lea    -0xda(%rbp),%rsi
call   10b0 <strstr@plt>
cmp    $0x0,%rax
je     1268 <read_watcher_file+0xd8>
lea    -0x90(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <read_from_line@plt>
jmp    1208 <read_watcher_file+0x78>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_watcher_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xb8,%rsp
mov    %rdx,%r14
mov    %esi,%ebp
movaps 0xe87(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xe6b(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe4f(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movw   $0x0,0x40(%rsp)
lea    0xe6e(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1080 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11e7 <read_watcher_file+0x67>
lea    0xe57(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1070 <logErrorMessages@plt>
lea    0xe60(%rip),%rsi
lea    0x6(%rsp),%rdi
mov    %ebp,%edx
call   1090 <sprintf@plt>
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %eax,%eax
je     121c <read_watcher_file+0x9c>
mov    %rbx,%rdi
call   1040 <fclose@plt>
add    $0xb8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x50(%rsp),%rbp
lea    0x6(%rsp),%r15
jmp    123c <read_watcher_file+0xbc>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    1206 <read_watcher_file+0x86>
mov    %rbp,%rdi
mov    $0x64,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
call   10a0 <strstr@plt>
test   %rax,%rax
je     1230 <read_watcher_file+0xb0>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1030 <read_from_line@plt>
jmp    1230 <read_watcher_file+0xb0>

LLVM IR (-O0):
@__const.read_watcher_file.watch_file_name = [50 x i8] c"/root/logs/watcher.txt\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [2 x i8] c"r\00"
@.str.1 = [26 x i8] c"Failed to open watch file\00"
@.str.2 = [3 x i8] c"%d\00"
define void @read_watcher_file(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca [100 x i8]
%9 = alloca [50 x i8]
%10 = alloca [10 x i8]
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%11 = bitcast [50 x i8]* %9 to i8*
%12 = getelementptr inbounds [50 x i8], [50 x i8]* %9, i64 0, i64 0
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %7
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = getelementptr inbounds [50 x i8], [50 x i8]* %9, i64 0, i64 0
%17 = call i32 @logErrorMessages(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %16)
br label %18
18: ; preds = %15, %3
%19 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%20 = load i32, i32* %5
%21 = call i32 @sprintf(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %39, %18
%23 = load i32*, i32** %7
%24 = call i32 @feof(i32* %23)
%25 = icmp ne i32 %24, 0
%26 = xor i1 %25, true
br i1 %26, label %27, label %40
27: ; preds = %22
%28 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%29 = load i32*, i32** %7
%30 = call i32 @fgets(i8* %28, i32 100, i32* %29)
%31 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%32 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%33 = call i64 @strstr(i8* %31, i8* %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %27
%36 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%37 = load i8*, i8** %6
%38 = call i32 @read_from_line(i8* %36, i8* %37)
br label %39
39: ; preds = %35, %27
br label %22
40: ; preds = %22
%41 = load i32*, i32** %7
%42 = call i32 @fclose(i32* %41)
ret void
}

LLVM IR (-O2):
@__const.read_watcher_file.watch_file_name = [50 x i8] c"/root/logs/watcher.txt\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [2 x i8] c"r\00"
@.str.1 = [26 x i8] c"Failed to open watch file\00"
@.str.2 = [3 x i8] c"%d\00"
define void @read_watcher_file(i8* nocapture readnone %0, i32 %1, i8* %2) {
%4 = alloca [100 x i8]
%5 = alloca [50 x i8]
%6 = alloca [10 x i8]
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [50 x i8], [50 x i8]* %5, i64 0, i64 0
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = call i32 @logErrorMessages(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %8)
br label %13
13: ; preds = %11, %3
%14 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%15 = call i32 @sprintf(i8* %14, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %1)
%16 = call i32 @feof(i32* %9)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %13, %24
%19 = call i32 @fgets(i8* %7, i32 100, i32* %9)
%20 = call i64 @strstr(i8* %7, i8* %14)
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = call i32 @read_from_line(i8* %7, i8* %2)
br label %24
24: ; preds = %22, %18
%25 = call i32 @feof(i32* %9)
%26 = icmp eq i32 %25, 0
br i1 %26, label %18, label %27
27: ; preds = %24, %13
%28 = call i32 @fclose(i32* %9)
ret void
}