#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 */
struct timespec {
  int tv_sec;
  scalar_t__ tv_nsec;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ killpg(scalar_t__, int);
int /*<<< orphan*/ nanosleep(struct timespec *, struct timespec *);
scalar_t__ waitpid(scalar_t__, int /*<<< orphan*/, int /*<<< orphan*/);
int xserver_stop(pid_t pid) {
  struct timespec tspec;
  killpg(pid, 15);
  tspec.tv_sec = 5;
  tspec.tv_nsec = 0;
  while (waitpid(pid, 0, WNOHANG) < 0)
    if (!nanosleep(&tspec, &tspec)) {
      killpg(pid, 9);
      break;
    }
  pid = 0;
  return 0;
}


x86 assembly (clang, -O0):
<xserver_stop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0xf,%esi
call   1040 <killpg@plt>
movl   $0x5,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e84(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <waitpid@plt>
cmp    $0x0,%rax
jge    11a4 <xserver_stop+0x74>
lea    -0x18(%rbp),%rsi
mov    %rsi,%rdi
call   1030 <nanosleep@plt>
cmp    $0x0,%eax
jne    119f <xserver_stop+0x6f>
mov    -0x8(%rbp),%rdi
mov    $0x9,%esi
call   1040 <killpg@plt>
jmp    11a4 <xserver_stop+0x74>
jmp    1159 <xserver_stop+0x29>
movq   $0x0,-0x8(%rbp)
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xserver_stop>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    $0xf,%esi
call   1040 <killpg@plt>
movl   $0x5,(%rsp)
movq   $0x0,0x8(%rsp)
mov    0x2e8b(%rip),%r15
mov    %rsp,%rbx
mov    (%r15),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <waitpid@plt>
test   %rax,%rax
jns    118e <xserver_stop+0x5e>
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
test   %eax,%eax
jne    1160 <xserver_stop+0x30>
mov    %r14,%rdi
mov    $0x9,%esi
call   1040 <killpg@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@WNOHANG = global i32 0
define i32 @xserver_stop(i64 %0) {
%2 = alloca i64
%3 = alloca %struct.timespec
store i64 %0, i64* %2
%4 = load i64, i64* %2
%5 = call i32 @killpg(i64 %4, i32 15)
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 5, i32* %6
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i64 0, i64* %7
br label %8
8: ; preds = %19, %1
%9 = load i64, i64* %2
%10 = load i32, i32* @WNOHANG
%11 = call i64 @waitpid(i64 %9, i32 0, i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %8
%14 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %3)
%15 = icmp ne i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = load i64, i64* %2
%18 = call i32 @killpg(i64 %17, i32 9)
br label %20
19: ; preds = %13
br label %8
20: ; preds = %16, %8
store i64 0, i64* %2
ret i32 0
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@WNOHANG = global i32 0
define i32 @xserver_stop(i64 %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = tail call i32 @killpg(i64 %0, i32 15)
%5 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 5, i32* %5
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 0, i64* %6
br label %7
7: ; preds = %11, %1
%8 = load i32, i32* @WNOHANG
%9 = call i64 @waitpid(i64 %0, i32 0, i32 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %7
%12 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %7
14: ; preds = %11
%15 = call i32 @killpg(i64 %0, i32 9)
br label %16
16: ; preds = %7, %14
ret i32 0
}