#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;
typedef int /*<<< orphan*/ pid_str;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SIGTERM;
scalar_t__ chdir(char const *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ kill(scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ nxweb_die(char *);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ strtol(char *, int /*<<< orphan*/, int);
int nxweb_shutdown_daemon(const char *work_dir, const char *pid_file) {
  if (work_dir && chdir(work_dir) < 0) {
    nxweb_die("chdir(work_dir) failed");
  }
  int fd = open(pid_file, O_RDONLY);
  if (fd != -1) {
    char pid_str[20];
    pid_t pid = 0;
    int len = read(fd, pid_str, sizeof(pid_str) - 1);
    if (len > 0) {
      pid_str[len] = '\0';
      pid = strtol(pid_str, 0, 10);
    }
    close(fd);
    if (pid) {
      kill(pid, SIGTERM);
      return EXIT_SUCCESS;
    }
  }
  fprintf(stderr, "Could not find PID file %s of running NXWEB\n", pid_file);
  return EXIT_FAILURE;
}


x86 assembly (clang, -O0):
<nxweb_shutdown_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11ba <nxweb_shutdown_daemon+0x3a>
mov    -0x10(%rbp),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    11ba <nxweb_shutdown_daemon+0x3a>
lea    0xe4b(%rip),%rdi
call   1080 <nxweb_die@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
je     1257 <nxweb_shutdown_daemon+0xd7>
movq   $0x0,-0x38(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x13,%edx
call   1050 <read@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jle    121c <nxweb_shutdown_daemon+0x9c>
movslq -0x3c(%rbp),%rax
movb   $0x0,-0x30(%rbp,%rax,1)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
cmpq   $0x0,-0x38(%rbp)
je     1252 <nxweb_shutdown_daemon+0xd2>
mov    -0x38(%rbp),%rdi
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
call   1090 <kill@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    127c <nxweb_shutdown_daemon+0xfc>
jmp    1257 <nxweb_shutdown_daemon+0xd7>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdac(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nxweb_shutdown_daemon>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
test   %rdi,%rdi
je     11a6 <nxweb_shutdown_daemon+0x26>
call   1030 <chdir@plt>
test   %rax,%rax
jns    11a6 <nxweb_shutdown_daemon+0x26>
lea    0xe5f(%rip),%rdi
call   1080 <nxweb_die@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1216 <nxweb_shutdown_daemon+0x96>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x13,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    120f <nxweb_shutdown_daemon+0x8f>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1040 <close@plt>
test   %rbx,%rbx
je     1216 <nxweb_shutdown_daemon+0x96>
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <kill@plt>
mov    0x2db3(%rip),%rax
jmp    1235 <nxweb_shutdown_daemon+0xb5>
mov    %ebp,%edi
call   1040 <close@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
lea    0xdf1(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"chdir(work_dir) failed\00"
@O_RDONLY = global i32 0
@SIGTERM = global i32 0
@EXIT_SUCCESS = global i32 0
@stderr = global i32 0
@.str.1 = [45 x i8] c"Could not find PID file %s of running NXWEB\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @nxweb_shutdown_daemon(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [20 x i8]
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i64 @chdir(i8* %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 @nxweb_die(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %12, %2
%19 = load i8*, i8** %5
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp ne i32 %22, -1
br i1 %23, label %24, label %47
24: ; preds = %18
store i64 0, i64* %8
%25 = load i32, i32* %6
%26 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%27 = call i32 @read(i32 %25, i8* %26, i32 19)
store i32 %27, i32* %9
%28 = load i32, i32* %9
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %24
%31 = load i32, i32* %9
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 %32
store i8 0, i8* %33
%34 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%35 = call i64 @strtol(i8* %34, i32 0, i32 10)
store i64 %35, i64* %8
br label %36
36: ; preds = %30, %24
%37 = load i32, i32* %6
%38 = call i32 @close(i32 %37)
%39 = load i64, i64* %8
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %36
%42 = load i64, i64* %8
%43 = load i32, i32* @SIGTERM
%44 = call i32 @kill(i64 %42, i32 %43)
%45 = load i32, i32* @EXIT_SUCCESS
store i32 %45, i32* %3
br label %52
46: ; preds = %36
br label %47
47: ; preds = %46, %18
%48 = load i32, i32* @stderr
%49 = load i8*, i8** %5
%50 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %49)
%51 = load i32, i32* @EXIT_FAILURE
store i32 %51, i32* %3
br label %52
52: ; preds = %47, %41
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@.str = [23 x i8] c"chdir(work_dir) failed\00"
@O_RDONLY = global i32 0
@SIGTERM = global i32 0
@EXIT_SUCCESS = global i32 0
@stderr = global i32 0
@.str.1 = [45 x i8] c"Could not find PID file %s of running NXWEB\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @nxweb_shutdown_daemon(i8* %0, i8* %1) {
%3 = alloca [20 x i8]
%4 = icmp eq i8* %0, null
br i1 %4, label %10, label %5
5: ; preds = %2
%6 = tail call i64 @chdir(i8* %0)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @nxweb_die(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %5, %2
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %1, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %31, label %14
14: ; preds = %10
%15 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%16 = call i32 @read(i32 %12, i8* %15, i32 19)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %14
%19 = tail call i32 @close(i32 %12)
br label %26
20: ; preds = %14
%21 = zext i32 %16 to i64
%22 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 %21
store i8 0, i8* %22
%23 = call i64 @strtol(i8* %15, i32 0, i32 10)
%24 = call i32 @close(i32 %12)
%25 = icmp eq i64 %23, 0
br i1 %25, label %26, label %27
26: ; preds = %20, %18
br label %31
27: ; preds = %20
%28 = load i32, i32* @SIGTERM
%29 = call i32 @kill(i64 %23, i32 %28)
%30 = load i32, i32* @EXIT_SUCCESS
br label %35
31: ; preds = %26, %10
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%34 = load i32, i32* @EXIT_FAILURE
br label %35
35: ; preds = %27, %31
%36 = phi i32 [ %34, %31 ], [ %30, %27 ]
ret i32 %36
}