#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *);
int system(char *);
int /*<<< orphan*/ unlink(char *);
int kill_running_daemon() {
  FILE *pidfile = fopen("/tmp/cpu_daemon.pid", "r");
  char pid_buffer[14];
  pid_buffer[0] = 'k';
  pid_buffer[1] = 'i';
  pid_buffer[2] = 'l';
  pid_buffer[3] = 'l';
  pid_buffer[4] = ' ';
  pid_buffer[5] = 0;
  if (pidfile) {
    printf("found pidfile\r\n");
    fgets(&(pid_buffer[5]), 10, pidfile);
    fclose(pidfile);
    int return_code = system(pid_buffer);
    if (return_code)
      printf("daemon wasn't running\r\n");
    else
      printf("killed old daemon\r\n");
    unlink("/tmp/cpu_daemon.pid");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<kill_running_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe91(%rip),%rdi
lea    0xe9e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
movb   $0x6b,-0x1e(%rbp)
movb   $0x69,-0x1d(%rbp)
movb   $0x6c,-0x1c(%rbp)
movb   $0x6c,-0x1b(%rbp)
movb   $0x20,-0x1a(%rbp)
movb   $0x0,-0x19(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1218 <kill_running_daemon+0xb8>
lea    0xe6d(%rip),%rdi
call   1060 <printf@plt>
lea    -0x1e(%rbp),%rdi
add    $0x5,%rdi
mov    -0x10(%rbp),%rdx
mov    $0xa,%esi
call   1070 <fgets@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x1e(%rbp),%rdi
call   1050 <system@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11f4 <kill_running_daemon+0x94>
lea    0xe3c(%rip),%rdi
call   1060 <printf@plt>
jmp    1200 <kill_running_daemon+0xa0>
lea    0xe43(%rip),%rdi
call   1060 <printf@plt>
lea    0xdf9(%rip),%rdi
call   1030 <unlink@plt>
movl   $0x1,-0x4(%rbp)
jmp    121f <kill_running_daemon+0xbf>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kill_running_daemon>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xe92(%rip),%rdi
lea    0xe9f(%rip),%rsi
call   1080 <fopen@plt>
movl   $0x6c6c696b,0xa(%rsp)
6c
movw   $0x20,0xe(%rsp)
test   %rax,%rax
je     11f0 <kill_running_daemon+0x90>
mov    %rax,%rbx
lea    0xf(%rsp),%r14
lea    0xe79(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
mov    $0xa,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xa(%rsp),%rdi
call   1060 <system@plt>
test   %eax,%eax
lea    0xe58(%rip),%rax
lea    0xe64(%rip),%rdi
cmove  %rax,%rdi
call   1040 <puts@plt>
lea    0xe1c(%rip),%rdi
call   1030 <unlink@plt>
mov    $0x1,%eax
jmp    11f2 <kill_running_daemon+0x92>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [20 x i8] c"/tmp/cpu_daemon.pid\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [16 x i8] c"found pidfile\0D\0A\00"
@.str.3 = [24 x i8] c"daemon wasn't running\0D\0A\00"
@.str.4 = [20 x i8] c"killed old daemon\0D\0A\00"
define i32 @kill_running_daemon() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [14 x i8]
%4 = alloca i32
%5 = call i32* @fopen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %2
%6 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 0
store i8 107, i8* %6
%7 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 1
store i8 105, i8* %7
%8 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 2
store i8 108, i8* %8
%9 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 3
store i8 108, i8* %9
%10 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 4
store i8 32, i8* %10
%11 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 5
store i8 0, i8* %11
%12 = load i32*, i32** %2
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %31
14: ; preds = %0
%15 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%16 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 5
%17 = load i32*, i32** %2
%18 = call i32 @fgets(i8* %16, i32 10, i32* %17)
%19 = load i32*, i32** %2
%20 = call i32 @fclose(i32* %19)
%21 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 0
%22 = call i32 @system(i8* %21)
store i32 %22, i32* %4
%23 = load i32, i32* %4
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %14
%26 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %29
27: ; preds = %14
%28 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %29
29: ; preds = %27, %25
%30 = call i32 @unlink(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %32
31: ; preds = %0
store i32 0, i32* %1
br label %32
32: ; preds = %31, %29
%33 = load i32, i32* %1
ret i32 %33
}

LLVM IR (-O2):
@.str = [20 x i8] c"/tmp/cpu_daemon.pid\00"
@.str.1 = [2 x i8] c"r\00"
@str = [15 x i8] c"found pidfile\0D\00"
@str.5 = [19 x i8] c"killed old daemon\0D\00"
@str.6 = [23 x i8] c"daemon wasn't running\0D\00"
define i32 @kill_running_daemon() {
%1 = alloca [14 x i8]
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = getelementptr inbounds [14 x i8], [14 x i8]* %1, i64 0, i64 0
store i8 107, i8* %3
%4 = getelementptr inbounds [14 x i8], [14 x i8]* %1, i64 0, i64 1
store i8 105, i8* %4
%5 = getelementptr inbounds [14 x i8], [14 x i8]* %1, i64 0, i64 2
store i8 108, i8* %5
%6 = getelementptr inbounds [14 x i8], [14 x i8]* %1, i64 0, i64 3
store i8 108, i8* %6
%7 = getelementptr inbounds [14 x i8], [14 x i8]* %1, i64 0, i64 4
store i8 32, i8* %7
%8 = getelementptr inbounds [14 x i8], [14 x i8]* %1, i64 0, i64 5
store i8 0, i8* %8
%9 = icmp eq i32* %2, null
br i1 %9, label %19, label %10
10: ; preds = %0
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%12 = call i32 @fgets(i8* %8, i32 10, i32* %2)
%13 = call i32 @fclose(i32* %2)
%14 = call i32 @system(i8* %3)
%15 = icmp eq i32 %14, 0
%16 = select i1 %15, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @str.5, i64 0, i64 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @str.6, i64 0, i64 0)
%17 = call i32 @puts(i8* dereferenceable(1) %16)
%18 = call i32 @unlink(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %0, %10
%20 = phi i32 [ 1, %10 ], [ 0, %0 ]
ret i32 %20
}