#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*/ LOG_ERR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fprintf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ *stdout;
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *,
                          int /*<<< orphan*/);
void write_watch_file(char *directory_name, int watcher) {
  FILE *watch_file = NULL;
  int ret = -1;
  char watch_file_name[50] = "/root/logs/watcher.txt";
  char str_message[300];
  watch_file = fopen(watch_file_name, "a+");
  memset(str_message, 0, 300);
  char str[10];
  sprintf(str, "%d", watcher);
  strcat(str_message, directory_name);
  strcat(str_message, " ");
  strcat(str_message, str);
  strcat(str_message, "\n");
  ret = fprintf(watch_file, "%s", str_message);
  if (ret < 0) {
    syslog(LOG_ERR, "Can not write to log stream: %s, error: %s",
           (watch_file == stdout) ? "stdout" : "daemon1", strerror(errno));
  }
  ret = fflush(watch_file);
  if (ret != 0) {
    syslog(LOG_ERR, "Can not fflush() log stream: %s, error: %s",
           (watch_file == stdout) ? "stdout" : "daemon1", strerror(errno));
  }
  fclose(watch_file);
}


x86 assembly (clang, -O0):
<write_watch_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1b0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xe34(%rip),%rsi
mov    $0x32,%edx
call   1060 <memcpy@plt>
lea    -0x50(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
lea    -0x180(%rbp),%rdi
xor    %esi,%esi
mov    $0x12c,%edx
call   1040 <memset@plt>
lea    -0x18a(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe27(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x180(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   10a0 <strcat@plt>
lea    -0x180(%rbp),%rdi
lea    0xe07(%rip),%rsi
call   10a0 <strcat@plt>
lea    -0x180(%rbp),%rdi
lea    -0x18a(%rbp),%rsi
call   10a0 <strcat@plt>
lea    -0x180(%rbp),%rdi
lea    0xde3(%rip),%rsi
call   10a0 <strcat@plt>
mov    -0x18(%rbp),%rdi
lea    -0x180(%rbp),%rdx
lea    0xe30(%rip),%rsi
call   1050 <fprintf@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12e2 <write_watch_file+0x142>
mov    0x2d41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x19c(%rbp)
mov    -0x18(%rbp),%rdx
mov    0x2d3e(%rip),%rax
mov    (%rax),%rsi
lea    0xdca(%rip),%rax
lea    0xdbc(%rip),%rcx
cmp    %rsi,%rdx
cmove  %rcx,%rax
mov    %rax,-0x198(%rbp)
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x19c(%rbp),%edi
mov    -0x198(%rbp),%rdx
mov    %eax,%ecx
lea    0xd5f(%rip),%rsi
call   1080 <syslog@plt>
mov    -0x18(%rbp),%rdi
call   1070 <fflush@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     135a <write_watch_file+0x1ba>
mov    0x2cc9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1ac(%rbp)
mov    -0x18(%rbp),%rdx
mov    0x2cc6(%rip),%rax
mov    (%rax),%rsi
lea    0xd52(%rip),%rax
lea    0xd44(%rip),%rcx
cmp    %rsi,%rdx
cmove  %rcx,%rax
mov    %rax,-0x1a8(%rbp)
mov    0x2ca8(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x1ac(%rbp),%edi
mov    -0x1a8(%rbp),%rdx
mov    %eax,%ecx
lea    0xd21(%rip),%rsi
call   1080 <syslog@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x1b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_watch_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x170,%rsp
mov    %esi,%ebp
mov    %rdi,%r15
movaps 0xe75(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe59(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe3d(%rip),%xmm0
movaps %xmm0,(%rsp)
movw   $0x0,0x30(%rsp)
lea    0xe5d(%rip),%rsi
mov    %rsp,%rdi
call   1080 <fopen@plt>
mov    %rax,%r14
lea    0x40(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x12c,%edx
call   1040 <memset@plt>
lea    0xe3a(%rip),%rsi
lea    0x36(%rsp),%r12
mov    %r12,%rdi
mov    %ebp,%edx
call   10a0 <sprintf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1090 <strcat@plt>
lea    0xe1c(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcat@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1090 <strcat@plt>
lea    0xe04(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcat@plt>
lea    0xe59(%rip),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
test   %eax,%eax
jns    129a <write_watch_file+0x10a>
mov    0x2d6d(%rip),%rax
mov    (%rax),%ebx
mov    0x2d74(%rip),%rax
cmp    (%rax),%r14
lea    0xdf9(%rip),%rax
lea    0xdf9(%rip),%rbp
cmove  %rax,%rbp
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdae(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %eax,%ecx
call   1070 <syslog@plt>
mov    %r14,%rdi
call   1060 <fflush@plt>
test   %eax,%eax
je     12ec <write_watch_file+0x15c>
mov    0x2d1b(%rip),%rax
mov    (%rax),%ebx
mov    0x2d22(%rip),%rax
cmp    (%rax),%r14
lea    0xda7(%rip),%rax
lea    0xda7(%rip),%rbp
cmove  %rax,%rbp
mov    0x2d0e(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd96(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %eax,%ecx
call   1070 <syslog@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
add    $0x170,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.write_watch_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 = [3 x i8] c"a+\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [2 x i8] c" \00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [3 x i8] c"%s\00"
@LOG_ERR = global i32 0
@.str.5 = [43 x i8] c"Can not write to log stream: %s, error: %s\00"
@stdout = global i32* null
@.str.6 = [7 x i8] c"stdout\00"
@.str.7 = [8 x i8] c"daemon1\00"
@errno = global i32 0
@.str.8 = [43 x i8] c"Can not fflush() log stream: %s, error: %s\00"
define void @write_watch_file(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca [50 x i8]
%8 = alloca [300 x i8]
%9 = alloca [10 x i8]
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32* null, i32** %5
store i32 -1, i32* %6
%10 = bitcast [50 x i8]* %7 to i8*
%11 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %5
%13 = getelementptr inbounds [300 x i8], [300 x i8]* %8, i64 0, i64 0
%14 = call i32 @memset(i8* %13, i32 0, i32 300)
%15 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%16 = load i32, i32* %4
%17 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds [300 x i8], [300 x i8]* %8, i64 0, i64 0
%19 = load i8*, i8** %3
%20 = call i32 @strcat(i8* %18, i8* %19)
%21 = getelementptr inbounds [300 x i8], [300 x i8]* %8, i64 0, i64 0
%22 = call i32 @strcat(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%23 = getelementptr inbounds [300 x i8], [300 x i8]* %8, i64 0, i64 0
%24 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%25 = call i32 @strcat(i8* %23, i8* %24)
%26 = getelementptr inbounds [300 x i8], [300 x i8]* %8, i64 0, i64 0
%27 = call i32 @strcat(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%28 = load i32*, i32** %5
%29 = getelementptr inbounds [300 x i8], [300 x i8]* %8, i64 0, i64 0
%30 = call i32 @fprintf(i32* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %29)
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %43
33: ; preds = %2
%34 = load i32, i32* @LOG_ERR
%35 = load i32*, i32** %5
%36 = load i32*, i32** @stdout
%37 = icmp eq i32* %35, %36
%38 = zext i1 %37 to i64
%39 = select i1 %37, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0)
%40 = load i32, i32* @errno
%41 = call i32 @strerror(i32 %40)
%42 = call i32 @syslog(i32 %34, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i8* %39, i32 %41)
br label %43
43: ; preds = %33, %2
%44 = load i32*, i32** %5
%45 = call i32 @fflush(i32* %44)
store i32 %45, i32* %6
%46 = load i32, i32* %6
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %58
48: ; preds = %43
%49 = load i32, i32* @LOG_ERR
%50 = load i32*, i32** %5
%51 = load i32*, i32** @stdout
%52 = icmp eq i32* %50, %51
%53 = zext i1 %52 to i64
%54 = select i1 %52, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0)
%55 = load i32, i32* @errno
%56 = call i32 @strerror(i32 %55)
%57 = call i32 @syslog(i32 %49, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.8, i64 0, i64 0), i8* %54, i32 %56)
br label %58
58: ; preds = %48, %43
%59 = load i32*, i32** %5
%60 = call i32 @fclose(i32* %59)
ret void
}

LLVM IR (-O2):
@__const.write_watch_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 = [3 x i8] c"a+\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [2 x i8] c" \00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [3 x i8] c"%s\00"
@LOG_ERR = global i32 0
@.str.5 = [43 x i8] c"Can not write to log stream: %s, error: %s\00"
@stdout = global i32* null
@.str.6 = [7 x i8] c"stdout\00"
@.str.7 = [8 x i8] c"daemon1\00"
@errno = global i32 0
@.str.8 = [43 x i8] c"Can not fflush() log stream: %s, error: %s\00"
define void @write_watch_file(i8* %0, i32 %1) {
%3 = alloca [50 x i8]
%4 = alloca [300 x i8]
%5 = alloca [10 x i8]
%6 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [300 x i8], [300 x i8]* %4, i64 0, i64 0
%8 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @memset(i8* %7, i32 0, i32 300)
%10 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%11 = call i32 @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%12 = call i32 @strcat(i8* %7, i8* %0)
%13 = call i32 @strcat(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%14 = call i32 @strcat(i8* %7, i8* %10)
%15 = call i32 @strcat(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%16 = call i32 @fprintf(i32* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %7)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %26
18: ; preds = %2
%19 = load i32, i32* @LOG_ERR
%20 = load i32*, i32** @stdout
%21 = icmp eq i32* %8, %20
%22 = select i1 %21, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0)
%23 = load i32, i32* @errno
%24 = call i32 @strerror(i32 %23)
%25 = call i32 @syslog(i32 %19, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i8* %22, i32 %24)
br label %26
26: ; preds = %18, %2
%27 = call i32 @fflush(i32* %8)
%28 = icmp eq i32 %27, 0
br i1 %28, label %37, label %29
29: ; preds = %26
%30 = load i32, i32* @LOG_ERR
%31 = load i32*, i32** @stdout
%32 = icmp eq i32* %8, %31
%33 = select i1 %32, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0)
%34 = load i32, i32* @errno
%35 = call i32 @strerror(i32 %34)
%36 = call i32 @syslog(i32 %30, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.8, i64 0, i64 0), i8* %33, i32 %35)
br label %37
37: ; preds = %29, %26
%38 = call i32 @fclose(i32* %8)
ret void
}