#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 */

/* Variables and functions */
int /*<<< orphan*/ SIGUSR1;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ kill(int, int /*<<< orphan*/);
int readpid();
scalar_t__ run_daemon();
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void check_daemon(void) {
  int pid;
  if ((pid = readpid()) < 0 || kill(pid, 0) < 0) {
    if (run_daemon()) {
      fprintf(stderr, "Failed to ping daemon and unable to start it up: %s\n",
              strerror(errno));
      exit(1);
    }
  } else if (kill(pid, SIGUSR1)) {
    fprintf(stderr, "Failed to send signal: %s\n", strerror(errno));
    exit(2);
  }
}


x86 assembly (clang, -O0):
<check_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1060 <readpid@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
jl     118f <check_daemon+0x2f>
mov    -0x4(%rbp),%edi
xor    %esi,%esi
call   1050 <kill@plt>
cmp    $0x0,%rax
jge    11db <check_daemon+0x7b>
mov    $0x0,%al
call   1030 <run_daemon@plt>
cmp    $0x0,%rax
je     11d6 <check_daemon+0x76>
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x8(%rbp),%edi
mov    %rax,%rdx
lea    0xe39(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    1231 <check_daemon+0xd1>
mov    -0x4(%rbp),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
call   1050 <kill@plt>
cmp    $0x0,%rax
je     122c <check_daemon+0xcc>
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0xc(%rbp),%edi
mov    %rax,%rdx
lea    0xe18(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
jmp    1231 <check_daemon+0xd1>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_daemon>:
push   %rbx
xor    %eax,%eax
call   1060 <readpid@plt>
test   %eax,%eax
js     11c3 <check_daemon+0x63>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1050 <kill@plt>
test   %rax,%rax
js     11c3 <check_daemon+0x63>
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <kill@plt>
test   %rax,%rax
je     11cf <check_daemon+0x6f>
mov    0x2e40(%rip),%rax
mov    (%rax),%ebx
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe86(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
xor    %eax,%eax
call   1030 <run_daemon@plt>
test   %rax,%rax
jne    11d1 <check_daemon+0x71>
pop    %rbx
ret
mov    0x2e00(%rip),%rax
mov    (%rax),%ebx
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe11(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [53 x i8] c"Failed to ping daemon and unable to start it up: %s\0A\00"
@errno = global i32 0
@SIGUSR1 = global i32 0
@.str.1 = [27 x i8] c"Failed to send signal: %s\0A\00"
define void @check_daemon() {
%1 = alloca i32
%2 = call i32 (...) @readpid()
store i32 %2, i32* %1
%3 = icmp slt i32 %2, 0
br i1 %3, label %8, label %4
4: ; preds = %0
%5 = load i32, i32* %1
%6 = call i64 @kill(i32 %5, i32 0)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %18
8: ; preds = %4, %0
%9 = call i64 (...) @run_daemon()
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %8
br label %30
18: ; preds = %4
%19 = load i32, i32* %1
%20 = load i32, i32* @SIGUSR1
%21 = call i64 @kill(i32 %19, i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = call i32 @exit(i32 2)
unreachable
29: ; preds = %18
br label %30
30: ; preds = %29, %17
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [53 x i8] c"Failed to ping daemon and unable to start it up: %s\0A\00"
@errno = global i32 0
@SIGUSR1 = global i32 0
@.str.1 = [27 x i8] c"Failed to send signal: %s\0A\00"
define void @check_daemon() {
%1 = tail call i32 (...) @readpid()
%2 = icmp slt i32 %1, 0
br i1 %2, label %6, label %3
3: ; preds = %0
%4 = tail call i64 @kill(i32 %1, i32 0)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %15
6: ; preds = %3, %0
%7 = tail call i64 (...) @run_daemon()
%8 = icmp eq i64 %7, 0
br i1 %8, label %25, label %9
9: ; preds = %6
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %3
%16 = load i32, i32* @SIGUSR1
%17 = tail call i64 @kill(i32 %1, i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %25, label %19
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = load i32, i32* @errno
%22 = tail call i8* @strerror(i32 %21)
%23 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = tail call i32 @exit(i32 2)
unreachable
25: ; preds = %15, %6
ret void
}