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

/* Variables and functions */
int ESRCH;
int /*<<< orphan*/ EXIT_SUCCESS;
int O_CLOEXEC;
int O_DIRECTORY;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ fork();
int /*<<< orphan*/ ksft_exit_fail_msg(char *, char const *, ...);
int /*<<< orphan*/ ksft_test_result_pass(char *, char const *);
int open(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, scalar_t__);
int sys_pidfd_send_signal(int, int /*<<< orphan*/, int /*<<< orphan*/ *,
                          int /*<<< orphan*/);
int /*<<< orphan*/ wait_for_pid(scalar_t__);
int test_pidfd_send_signal_exited_fail(void) {
  int pidfd, ret, saved_errno;
  char buf[256];
  pid_t pid;
  const char *test_name = "pidfd_send_signal signal exited process";
  pid = fork();
  if (pid < 0)
    ksft_exit_fail_msg("%s test: Failed to create new process\n", test_name);
  if (pid == 0)
    _exit(EXIT_SUCCESS);
  snprintf(buf, sizeof(buf), "/proc/%d", pid);
  pidfd = open(buf, O_DIRECTORY | O_CLOEXEC);
  (void)wait_for_pid(pid);
  if (pidfd < 0)
    ksft_exit_fail_msg("%s test: Failed to open process file descriptor\n",
                       test_name);
  ret = sys_pidfd_send_signal(pidfd, 0, NULL, 0);
  saved_errno = errno;
  close(pidfd);
  if (ret == 0)
    ksft_exit_fail_msg("%s test: Managed to send signal to process even though "
                       "it should have failed\n",
                       test_name);
  if (saved_errno != ESRCH)
    ksft_exit_fail_msg("%s test: Expected to receive ESRCH as errno value but "
                       "received %d instead\n",
                       test_name, saved_errno);
  ksft_test_result_pass("%s test: Failed to send signal as expected\n",
                        test_name);
  return 0;
}


x86 assembly (clang, -O0):
<test_pidfd_send_signal_exited_fail>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
lea    0xe5e(%rip),%rax
mov    %rax,-0x120(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x118(%rbp)
cmpq   $0x0,-0x118(%rbp)
jge    11da <test_pidfd_send_signal_exited_fail+0x4a>
mov    -0x120(%rbp),%rsi
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1060 <ksft_exit_fail_msg@plt>
cmpq   $0x0,-0x118(%rbp)
jne    11f6 <test_pidfd_send_signal_exited_fail+0x66>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   1040 <_exit@plt>
lea    -0x110(%rbp),%rdi
mov    -0x118(%rbp),%rcx
mov    $0x100,%esi
lea    0xe3f(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    0x2d94(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x4(%rbp)
mov    -0x118(%rbp),%rdi
call   1030 <wait_for_pid@plt>
cmpl   $0x0,-0x4(%rbp)
jge    1261 <test_pidfd_send_signal_exited_fail+0xd1>
mov    -0x120(%rbp),%rsi
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1060 <ksft_exit_fail_msg@plt>
mov    -0x4(%rbp),%edi
xor    %ecx,%ecx
xor    %eax,%eax
mov    %eax,%edx
mov    %ecx,%esi
call   1090 <sys_pidfd_send_signal@plt>
mov    %eax,-0x8(%rbp)
mov    0x2d5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
call   1080 <close@plt>
cmpl   $0x0,-0x8(%rbp)
jne    12a7 <test_pidfd_send_signal_exited_fail+0x117>
mov    -0x120(%rbp),%rsi
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1060 <ksft_exit_fail_msg@plt>
mov    -0xc(%rbp),%eax
mov    0x2d07(%rip),%rcx
cmp    (%rcx),%eax
je     12d1 <test_pidfd_send_signal_exited_fail+0x141>
mov    -0x120(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1060 <ksft_exit_fail_msg@plt>
mov    -0x120(%rbp),%rsi
lea    0xe43(%rip),%rdi
call   1050 <ksft_test_result_pass@plt>
xor    %eax,%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_pidfd_send_signal_exited_fail>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
js     11ba <test_pidfd_send_signal_exited_fail+0x2a>
jne    11cf <test_pidfd_send_signal_exited_fail+0x3f>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   1040 <_exit@plt>
lea    0xe67(%rip),%rdi
lea    0xe38(%rip),%rsi
xor    %eax,%eax
call   1060 <ksft_exit_fail_msg@plt>
lea    0xe79(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x100,%esi
mov    %rbx,%rcx
call   1070 <snprintf@plt>
mov    0x2df0(%rip),%rax
mov    0x2dc9(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbp,%rdi
call   10a0 <open@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <wait_for_pid@plt>
test   %ebp,%ebp
jns    1226 <test_pidfd_send_signal_exited_fail+0x96>
lea    0xe40(%rip),%rdi
lea    0xde1(%rip),%rsi
xor    %eax,%eax
call   1060 <ksft_exit_fail_msg@plt>
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1090 <sys_pidfd_send_signal@plt>
mov    %eax,%ebx
mov    0x2d9c(%rip),%rax
mov    (%rax),%r14d
mov    %ebp,%edi
call   1080 <close@plt>
test   %ebx,%ebx
jne    125f <test_pidfd_send_signal_exited_fail+0xcf>
lea    0xe38(%rip),%rdi
lea    0xda8(%rip),%rsi
xor    %eax,%eax
call   1060 <ksft_exit_fail_msg@plt>
mov    0x2d52(%rip),%rax
cmp    (%rax),%r14d
je     1283 <test_pidfd_send_signal_exited_fail+0xf3>
lea    0xe65(%rip),%rdi
lea    0xd87(%rip),%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1060 <ksft_exit_fail_msg@plt>
lea    0xe98(%rip),%rdi
lea    0xd6f(%rip),%rsi
call   1050 <ksft_test_result_pass@plt>
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [40 x i8] c"pidfd_send_signal signal exited process\00"
@.str.1 = [39 x i8] c"%s test: Failed to create new process\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [9 x i8] c"/proc/%d\00"
@O_DIRECTORY = global i32 0
@O_CLOEXEC = global i32 0
@.str.3 = [49 x i8] c"%s test: Failed to open process file descriptor\0A\00"
@errno = global i32 0
@.str.4 = [78 x i8] c"%s test: Managed to send signal to process even though it should have failed\0A\00"
@ESRCH = global i32 0
@.str.5 = [75 x i8] c"%s test: Expected to receive ESRCH as errno value but received %d instead\0A\00"
@.str.6 = [44 x i8] c"%s test: Failed to send signal as expected\0A\00"
define i32 @test_pidfd_send_signal_exited_fail() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = alloca i64
%6 = alloca i8*
store i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8** %6
%7 = call i64 (...) @fork()
store i64 %7, i64* %5
%8 = load i64, i64* %5
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = load i8*, i8** %6
%12 = call i32 (i8*, i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %11)
br label %13
13: ; preds = %10, %0
%14 = load i64, i64* %5
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* @EXIT_SUCCESS
%18 = call i32 @_exit(i32 %17)
unreachable
19: ; preds = %13
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%21 = load i64, i64* %5
%22 = call i32 @snprintf(i8* %20, i32 256, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i64 %21)
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%24 = load i32, i32* @O_DIRECTORY
%25 = load i32, i32* @O_CLOEXEC
%26 = or i32 %24, %25
%27 = call i32 @open(i8* %23, i32 %26)
store i32 %27, i32* %1
%28 = load i64, i64* %5
%29 = call i32 @wait_for_pid(i64 %28)
%30 = load i32, i32* %1
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %19
%33 = load i8*, i8** %6
%34 = call i32 (i8*, i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i8* %33)
br label %35
35: ; preds = %32, %19
%36 = load i32, i32* %1
%37 = call i32 @sys_pidfd_send_signal(i32 %36, i32 0, i32* null, i32 0)
store i32 %37, i32* %2
%38 = load i32, i32* @errno
store i32 %38, i32* %3
%39 = load i32, i32* %1
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* %2
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %35
%44 = load i8*, i8** %6
%45 = call i32 (i8*, i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([78 x i8], [78 x i8]* @.str.4, i64 0, i64 0), i8* %44)
br label %46
46: ; preds = %43, %35
%47 = load i32, i32* %3
%48 = load i32, i32* @ESRCH
%49 = icmp ne i32 %47, %48
br i1 %49, label %50, label %54
50: ; preds = %46
%51 = load i8*, i8** %6
%52 = load i32, i32* %3
%53 = call i32 (i8*, i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.5, i64 0, i64 0), i8* %51, i32 %52)
br label %54
54: ; preds = %50, %46
%55 = load i8*, i8** %6
%56 = call i32 @ksft_test_result_pass(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.6, i64 0, i64 0), i8* %55)
ret i32 0
}

LLVM IR (-O2):
@.str = [40 x i8] c"pidfd_send_signal signal exited process\00"
@.str.1 = [39 x i8] c"%s test: Failed to create new process\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [9 x i8] c"/proc/%d\00"
@O_DIRECTORY = global i32 0
@O_CLOEXEC = global i32 0
@.str.3 = [49 x i8] c"%s test: Failed to open process file descriptor\0A\00"
@errno = global i32 0
@.str.4 = [78 x i8] c"%s test: Managed to send signal to process even though it should have failed\0A\00"
@ESRCH = global i32 0
@.str.5 = [75 x i8] c"%s test: Expected to receive ESRCH as errno value but received %d instead\0A\00"
@.str.6 = [44 x i8] c"%s test: Failed to send signal as expected\0A\00"
define i32 @test_pidfd_send_signal_exited_fail() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 (i8*, i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %12
7: ; preds = %0
%8 = icmp eq i64 %3, 0
br i1 %8, label %9, label %12
9: ; preds = %7
%10 = load i32, i32* @EXIT_SUCCESS
%11 = tail call i32 @_exit(i32 %10)
unreachable
12: ; preds = %5, %7
%13 = call i32 @snprintf(i8* %2, i32 256, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i64 %3)
%14 = load i32, i32* @O_DIRECTORY
%15 = load i32, i32* @O_CLOEXEC
%16 = or i32 %15, %14
%17 = call i32 @open(i8* %2, i32 %16)
%18 = call i32 @wait_for_pid(i64 %3)
%19 = icmp slt i32 %17, 0
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = call i32 (i8*, i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %12
%23 = call i32 @sys_pidfd_send_signal(i32 %17, i32 0, i32* null, i32 0)
%24 = load i32, i32* @errno
%25 = call i32 @close(i32 %17)
%26 = icmp eq i32 %23, 0
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 (i8*, i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([78 x i8], [78 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %27, %22
%30 = load i32, i32* @ESRCH
%31 = icmp eq i32 %24, %30
br i1 %31, label %34, label %32
32: ; preds = %29
%33 = call i32 (i8*, i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %24)
br label %34
34: ; preds = %32, %29
%35 = call i32 @ksft_test_result_pass(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
ret i32 0
}