#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_INFO;
int /*<<< orphan*/ LOG_USER;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ openlog(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ unlink(char *);
void syslog_writer(char *filename) {
  FILE *fp = fopen(filename, "r");
  char buf[1024];
  if (!fp) {
    printf("wmpower: syslog_writer thread: fatal error: unable to hook to main "
           "thread's stderr!\n");
    abort();
  }
  openlog("wmpower", 0, LOG_USER);
  unlink(filename);
  while (1) {
    fflush(NULL);
    while (fgets(buf, 1023, fp))
      syslog(LOG_INFO, "%s", buf);
    sleep(1);
  }
}


x86 assembly (clang, -O0):
<syslog_writer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xeb1(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d1 <syslog_writer+0x41>
lea    0xe3b(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
lea    0xe74(%rip),%rdi
xor    %esi,%esi
call   10a0 <openlog@plt>
mov    -0x8(%rbp),%rdi
call   1040 <unlink@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <fflush@plt>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x3ff,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     123a <syslog_writer+0xaa>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rdx
lea    0xe2d(%rip),%rsi
call   1080 <syslog@plt>
jmp    11fa <syslog_writer+0x6a>
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    11f1 <syslog_writer+0x61>

x86 assembly (clang, -O2):
<syslog_writer>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%rbx
lea    0xe5e(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1236 <syslog_writer+0xa6>
mov    %rax,%r14
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
lea    0xe37(%rip),%rdi
xor    %esi,%esi
call   10a0 <openlog@plt>
mov    %rbx,%rdi
call   1040 <unlink@plt>
mov    %rsp,%rbx
mov    0x2df6(%rip),%r12
lea    0xe1f(%rip),%r15
jmp    11fa <syslog_writer+0x6a>
nopl   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   10b0 <sleep@plt>
xor    %edi,%edi
call   1070 <fflush@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x3ff,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11f0 <syslog_writer+0x60>
mov    (%r12),%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1080 <syslog@plt>
jmp    1210 <syslog_writer+0x80>
lea    0xdce(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [85 x i8] c"wmpower: syslog_writer thread: fatal error: unable to hook to main thread's stderr!\0A\00"
@.str.2 = [8 x i8] c"wmpower\00"
@LOG_USER = global i32 0
@LOG_INFO = global i32 0
@.str.3 = [3 x i8] c"%s\00"
define void @syslog_writer(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [1024 x i8]
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %3
%7 = load i32*, i32** %3
%8 = icmp ne i32* %7, null
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = call i32 @printf(i8* getelementptr inbounds ([85 x i8], [85 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 (...) @abort()
unreachable
12: ; preds = %1
%13 = load i32, i32* @LOG_USER
%14 = call i32 @openlog(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 0, i32 %13)
%15 = load i8*, i8** %2
%16 = call i32 @unlink(i8* %15)
br label %17
17: ; preds = %12, %28
%18 = call i32 @fflush(i32* null)
br label %19
19: ; preds = %24, %17
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%21 = load i32*, i32** %3
%22 = call i64 @fgets(i8* %20, i32 1023, i32* %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = load i32, i32* @LOG_INFO
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%27 = call i32 @syslog(i32 %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %26)
br label %19
28: ; preds = %19
%29 = call i32 @sleep(i32 1)
br label %17
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"wmpower\00"
@LOG_USER = global i32 0
@LOG_INFO = global i32 0
@.str.3 = [3 x i8] c"%s\00"
@str = [84 x i8] c"wmpower: syslog_writer thread: fatal error: unable to hook to main thread's stderr!\00"
define void @syslog_writer(i8* nocapture %0) {
%2 = alloca [1024 x i8]
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = icmp eq i32* %3, null
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([84 x i8], [84 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 (...) @abort()
unreachable
9: ; preds = %1
%10 = load i32, i32* @LOG_USER
%11 = tail call i32 @openlog(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 0, i32 %10)
%12 = tail call i32 @unlink(i8* %0)
br label %13
13: ; preds = %22, %9
%14 = call i32 @fflush(i32* null)
%15 = call i64 @fgets(i8* %4, i32 1023, i32* %3)
%16 = icmp eq i64 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %13, %17
%18 = load i32, i32* @LOG_INFO
%19 = call i32 @syslog(i32 %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%20 = call i64 @fgets(i8* %4, i32 1023, i32* %3)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %17
22: ; preds = %17, %13
%23 = call i32 @sleep(i32 1)
br label %13
}