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

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *get_pidfile_path();
scalar_t__ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ log_error_and_exit(char *, ...);
int open(char *, int /*<<< orphan*/);
int read(int, char **, int);
int /*<<< orphan*/ sscanf(char *, char *, int *);
void kill_daemon(void) {
  char *pid_file = get_pidfile_path();
  int fd = open(pid_file, O_RDONLY);
  if (fd < 0) {
    log_error_and_exit("Cannot open PID file %s", pid_file);
  }
  free(pid_file);
  char line[5 + 1];
  if (read(fd, &line, sizeof(line)) == -1) {
    log_error_and_exit("Cannot read from PID file");
  }
  close(fd);
  int pid;
  sscanf(line, "%d", &pid);
  if (kill(pid, SIGTERM) < 0) {
    log_error_and_exit("Cannot kill %d process", pid);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<kill_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   10a0 <get_pidfile_path@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11d4 <kill_daemon+0x44>
mov    -0x8(%rbp),%rsi
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1040 <log_error_and_exit@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0xc(%rbp),%edi
lea    -0x12(%rbp),%rsi
mov    $0x6,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
jne    1205 <kill_daemon+0x75>
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1040 <log_error_and_exit@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
lea    -0x12(%rbp),%rdi
lea    0xe1a(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1080 <sscanf@plt>
mov    -0x18(%rbp),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
call   1070 <kill@plt>
cmp    $0x0,%rax
jge    124d <kill_daemon+0xbd>
mov    -0x18(%rbp),%esi
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1040 <log_error_and_exit@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<kill_daemon>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
xor    %eax,%eax
call   10a0 <get_pidfile_path@plt>
mov    %rax,%rbx
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11c8 <kill_daemon+0x38>
lea    0xe42(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <log_error_and_exit@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0x12(%rsp),%rsi
mov    %ebp,%edi
mov    $0x6,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
jne    11f4 <kill_daemon+0x64>
lea    0xe2b(%rip),%rdi
xor    %eax,%eax
call   1040 <log_error_and_exit@plt>
mov    %ebp,%edi
call   1050 <close@plt>
lea    0xe30(%rip),%rsi
lea    0x12(%rsp),%rdi
lea    0xc(%rsp),%rdx
call   1080 <sscanf@plt>
mov    0xc(%rsp),%edi
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
call   1070 <kill@plt>
test   %rax,%rax
jns    123a <kill_daemon+0xaa>
mov    0xc(%rsp),%esi
lea    0xe02(%rip),%rdi
xor    %eax,%eax
call   1040 <log_error_and_exit@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [24 x i8] c"Cannot open PID file %s\00"
@.str.1 = [26 x i8] c"Cannot read from PID file\00"
@.str.2 = [3 x i8] c"%d\00"
@SIGTERM = global i32 0
@.str.3 = [23 x i8] c"Cannot kill %d process\00"
@EXIT_SUCCESS = global i32 0
define void @kill_daemon() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca [6 x i8]
%4 = alloca i32
%5 = call i8* (...) @get_pidfile_path()
store i8* %5, i8** %1
%6 = load i8*, i8** %1
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = load i8*, i8** %1
%13 = call i32 (i8*, ...) @log_error_and_exit(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %14
14: ; preds = %11, %0
%15 = load i8*, i8** %1
%16 = call i32 @free(i8* %15)
%17 = load i32, i32* %2
%18 = bitcast [6 x i8]* %3 to i8**
%19 = call i32 @read(i32 %17, i8** %18, i32 6)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = call i32 (i8*, ...) @log_error_and_exit(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %14
%24 = load i32, i32* %2
%25 = call i32 @close(i32 %24)
%26 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 0
%27 = call i32 @sscanf(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%28 = load i32, i32* %4
%29 = load i32, i32* @SIGTERM
%30 = call i64 @kill(i32 %28, i32 %29)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %23
%33 = load i32, i32* %4
%34 = call i32 (i8*, ...) @log_error_and_exit(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %33)
br label %35
35: ; preds = %32, %23
%36 = load i32, i32* @EXIT_SUCCESS
%37 = call i32 @exit(i32 %36)
unreachable
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [24 x i8] c"Cannot open PID file %s\00"
@.str.1 = [26 x i8] c"Cannot read from PID file\00"
@.str.2 = [3 x i8] c"%d\00"
@SIGTERM = global i32 0
@.str.3 = [23 x i8] c"Cannot kill %d process\00"
@EXIT_SUCCESS = global i32 0
define void @kill_daemon() {
%1 = alloca [6 x i8]
%2 = alloca i32
%3 = tail call i8* (...) @get_pidfile_path()
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %3, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 (i8*, ...) @log_error_and_exit(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %3)
br label %9
9: ; preds = %7, %0
%10 = tail call i32 @free(i8* %3)
%11 = getelementptr inbounds [6 x i8], [6 x i8]* %1, i64 0, i64 0
%12 = bitcast [6 x i8]* %1 to i8**
%13 = call i32 @read(i32 %5, i8** %12, i32 6)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = tail call i32 (i8*, ...) @log_error_and_exit(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %9
%18 = tail call i32 @close(i32 %5)
%19 = bitcast i32* %2 to i8*
%20 = call i32 @sscanf(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%21 = load i32, i32* %2
%22 = load i32, i32* @SIGTERM
%23 = call i64 @kill(i32 %21, i32 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %17
%26 = load i32, i32* %2
%27 = call i32 (i8*, ...) @log_error_and_exit(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %25, %17
%29 = load i32, i32* @EXIT_SUCCESS
%30 = call i32 @exit(i32 %29)
unreachable
}