#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ SIGTERM;
scalar_t__ create_daemon(char const *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ kill(scalar_t__, int /*<<< orphan*/);
scalar_t__ load_pid_file();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ remove_pid_file();
int /*<<< orphan*/ save_pid_file(scalar_t__);
char *str_tolower(char const *);
scalar_t__ strcmp(char *, char *);
void daemon_manager(const char *bin, const char *cmd) {
  pid_t pid;
  char *str;
  str = str_tolower(cmd);
  if (strcmp(str, "start") == 0) {
    if (load_pid_file() > 0) {
      printf("Daemon is already running\n");
      exit(EXIT_FAILURE);
    }
    pid = create_daemon(bin);
    if (pid < 0) {
      free(str);
      exit(EXIT_FAILURE);
    }
    if (pid > 0) {
      save_pid_file(pid);
      free(str);
      exit(EXIT_SUCCESS);
    }
  } else if (strcmp(str, "stop") == 0) {
    pid = load_pid_file();
    if (pid <= 0) {
      printf("No pid file, cannot stop\n");
      exit(EXIT_FAILURE);
    }
    kill(pid, SIGTERM);
    remove_pid_file();
    exit(EXIT_SUCCESS);
  } else if (strcmp(str, "restart") == 0) {
  }
  free(str);
}


x86 assembly (clang, -O0):
<daemon_manager>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   10c0 <str_tolower@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe74(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    1261 <daemon_manager+0xc1>
mov    $0x0,%al
call   1090 <load_pid_file@plt>
cmp    $0x0,%rax
jle    1202 <daemon_manager+0x62>
lea    0xe11(%rip),%rdi
call   1040 <printf@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%rdi
call   10a0 <create_daemon@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1231 <daemon_manager+0x91>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    125c <daemon_manager+0xbc>
mov    -0x18(%rbp),%rdi
call   1070 <save_pid_file@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    12fb <daemon_manager+0x15b>
mov    -0x20(%rbp),%rdi
lea    0xdaf(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    12d2 <daemon_manager+0x132>
mov    $0x0,%al
call   1090 <load_pid_file@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jg     12ab <daemon_manager+0x10b>
lea    0xd88(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2d32(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
mov    $0x0,%al
call   1080 <remove_pid_file@plt>
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rdi
lea    0xd5d(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    12f1 <daemon_manager+0x151>
jmp    12f1 <daemon_manager+0x151>
jmp    12f6 <daemon_manager+0x156>
jmp    12fb <daemon_manager+0x15b>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon_manager>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %rsi,%rdi
call   10c0 <str_tolower@plt>
mov    %rax,%rbx
lea    0xe49(%rip),%rsi
mov    %rax,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11eb <daemon_manager+0x4b>
lea    0xe4f(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1215 <daemon_manager+0x75>
lea    0xe1f(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcmp@plt>
jmp    1206 <daemon_manager+0x66>
xor    %eax,%eax
call   1090 <load_pid_file@plt>
test   %rax,%rax
jg     122a <daemon_manager+0x8a>
mov    %r14,%rdi
call   10a0 <create_daemon@plt>
test   %rax,%rax
js     1238 <daemon_manager+0x98>
jne    124e <daemon_manager+0xae>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <free@plt>
xor    %eax,%eax
call   1090 <load_pid_file@plt>
test   %rax,%rax
jg     1260 <daemon_manager+0xc0>
lea    0xde0(%rip),%rdi
jmp    1231 <daemon_manager+0x91>
lea    0xdf0(%rip),%rdi
call   1040 <puts@plt>
jmp    1240 <daemon_manager+0xa0>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    %rax,%rdi
call   1070 <save_pid_file@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    1278 <daemon_manager+0xd8>
mov    0x2d81(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1060 <kill@plt>
xor    %eax,%eax
call   1080 <remove_pid_file@plt>
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"start\00"
@.str.1 = [27 x i8] c"Daemon is already running\0A\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.2 = [5 x i8] c"stop\00"
@.str.3 = [26 x i8] c"No pid file, cannot stop\0A\00"
@SIGTERM = global i32 0
@.str.4 = [8 x i8] c"restart\00"
define void @daemon_manager(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %4
%8 = call i8* @str_tolower(i8* %7)
store i8* %8, i8** %6
%9 = load i8*, i8** %6
%10 = call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %40
12: ; preds = %2
%13 = call i64 (...) @load_pid_file()
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %12
%20 = load i8*, i8** %3
%21 = call i64 @create_daemon(i8* %20)
store i64 %21, i64* %5
%22 = load i64, i64* %5
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %19
%25 = load i8*, i8** %6
%26 = call i32 @free(i8* %25)
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %19
%30 = load i64, i64* %5
%31 = icmp sgt i64 %30, 0
br i1 %31, label %32, label %39
32: ; preds = %29
%33 = load i64, i64* %5
%34 = call i32 @save_pid_file(i64 %33)
%35 = load i8*, i8** %6
%36 = call i32 @free(i8* %35)
%37 = load i32, i32* @EXIT_SUCCESS
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %29
br label %66
40: ; preds = %2
%41 = load i8*, i8** %6
%42 = call i64 @strcmp(i8* %41, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %59
44: ; preds = %40
%45 = call i64 (...) @load_pid_file()
store i64 %45, i64* %5
%46 = load i64, i64* %5
%47 = icmp sle i64 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %44
%49 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %44
%53 = load i64, i64* %5
%54 = load i32, i32* @SIGTERM
%55 = call i32 @kill(i64 %53, i32 %54)
%56 = call i32 (...) @remove_pid_file()
%57 = load i32, i32* @EXIT_SUCCESS
%58 = call i32 @exit(i32 %57)
unreachable
59: ; preds = %40
%60 = load i8*, i8** %6
%61 = call i64 @strcmp(i8* %60, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%62 = icmp eq i64 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %59
br label %64
64: ; preds = %63, %59
br label %65
65: ; preds = %64
br label %66
66: ; preds = %65, %39
%67 = load i8*, i8** %6
%68 = call i32 @free(i8* %67)
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"start\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.2 = [5 x i8] c"stop\00"
@SIGTERM = global i32 0
@.str.4 = [8 x i8] c"restart\00"
@str = [25 x i8] c"No pid file, cannot stop\00"
@str.5 = [26 x i8] c"Daemon is already running\00"
define void @daemon_manager(i8* %0, i8* %1) {
%3 = tail call i8* @str_tolower(i8* %1)
%4 = tail call i64 @strcmp(i8* %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %27
6: ; preds = %2
%7 = tail call i64 (...) @load_pid_file()
%8 = icmp sgt i64 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %6
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.5, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %6
%14 = tail call i64 @create_daemon(i8* %0)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = tail call i32 @free(i8* %3)
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %13
%21 = icmp eq i64 %14, 0
br i1 %21, label %45, label %22
22: ; preds = %20
%23 = tail call i32 @save_pid_file(i64 %14)
%24 = tail call i32 @free(i8* %3)
%25 = load i32, i32* @EXIT_SUCCESS
%26 = tail call i32 @exit(i32 %25)
unreachable
27: ; preds = %2
%28 = tail call i64 @strcmp(i8* %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %43
30: ; preds = %27
%31 = tail call i64 (...) @load_pid_file()
%32 = icmp slt i64 %31, 1
br i1 %32, label %33, label %37
33: ; preds = %30
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = tail call i32 @exit(i32 %35)
unreachable
37: ; preds = %30
%38 = load i32, i32* @SIGTERM
%39 = tail call i32 @kill(i64 %31, i32 %38)
%40 = tail call i32 (...) @remove_pid_file()
%41 = load i32, i32* @EXIT_SUCCESS
%42 = tail call i32 @exit(i32 %41)
unreachable
43: ; preds = %27
%44 = tail call i64 @strcmp(i8* %3, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %45
45: ; preds = %20, %43
%46 = tail call i32 @free(i8* %3)
ret void
}