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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ get_pid_from_fdinfo_file(int, char *, int);
int getpid();
int ksft_exit_fail();
int ksft_exit_pass();
int /*<<< orphan*/ ksft_print_msg(char *, int, ...);
int /*<<< orphan*/ ksft_set_plan(int);
int /*<<< orphan*/ ksft_test_result_pass(char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int sys_pidfd_open(int, int);
int main(int argc, char **argv) {
  int pidfd = -1, ret = 1;
  pid_t pid;
  ksft_set_plan(3);
  pidfd = sys_pidfd_open(-1, 0);
  if (pidfd >= 0) {
    ksft_print_msg("%s - succeeded to open pidfd for invalid pid -1\n",
                   strerror(errno));
    goto on_error;
  }
  ksft_test_result_pass("do not allow invalid pid test: passed\n");
  pidfd = sys_pidfd_open(getpid(), 1);
  if (pidfd >= 0) {
    ksft_print_msg(
        "%s - succeeded to open pidfd with invalid flag value specified\n",
        strerror(errno));
    goto on_error;
  }
  ksft_test_result_pass("do not allow invalid flag test: passed\n");
  pidfd = sys_pidfd_open(getpid(), 0);
  if (pidfd < 0) {
    ksft_print_msg("%s - failed to open pidfd\n", strerror(errno));
    goto on_error;
  }
  ksft_test_result_pass("open a new pidfd test: passed\n");
  pid = get_pid_from_fdinfo_file(pidfd, "Pid:", sizeof("Pid:") - 1);
  ksft_print_msg("pidfd %d refers to process with pid %d\n", pidfd, pid);
  ret = 0;
on_error:
  if (pidfd >= 0)
    close(pidfd);
  return !ret ? ksft_exit_pass() : ksft_exit_fail();
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    $0x3,%edi
call   1050 <ksft_set_plan@plt>
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1090 <sys_pidfd_open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     120a <main+0x6a>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %eax,%esi
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   10b0 <ksft_print_msg@plt>
jmp    12e3 <main+0x143>
lea    0xe20(%rip),%rdi
call   1030 <ksft_test_result_pass@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%edi
mov    $0x1,%esi
call   1090 <sys_pidfd_open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1259 <main+0xb9>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %eax,%esi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   10b0 <ksft_print_msg@plt>
jmp    12e3 <main+0x143>
lea    0xe38(%rip),%rdi
call   1030 <ksft_test_result_pass@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%edi
xor    %esi,%esi
call   1090 <sys_pidfd_open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12a5 <main+0x105>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %eax,%esi
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   10b0 <ksft_print_msg@plt>
jmp    12e3 <main+0x143>
lea    0xe2f(%rip),%rdi
call   1030 <ksft_test_result_pass@plt>
mov    -0x14(%rbp),%edi
lea    0xe3f(%rip),%rsi
mov    $0x4,%edx
call   10a0 <get_pid_from_fdinfo_file@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   10b0 <ksft_print_msg@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     12f5 <main+0x155>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
cmpl   $0x0,-0x18(%rbp)
jne    130e <main+0x16e>
mov    $0x0,%al
call   1060 <ksft_exit_pass@plt>
mov    %eax,-0x20(%rbp)
jmp    1318 <main+0x178>
mov    $0x0,%al
call   1070 <ksft_exit_fail@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x3,%edi
call   1050 <ksft_set_plan@plt>
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1090 <sys_pidfd_open@plt>
test   %eax,%eax
js     11c9 <main+0x29>
mov    %eax,%ebx
lea    0xe39(%rip),%r14
jmp    11f5 <main+0x55>
lea    0xe61(%rip),%rdi
call   1030 <ksft_test_result_pass@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%edi
mov    $0x1,%esi
call   1090 <sys_pidfd_open@plt>
test   %eax,%eax
js     1224 <main+0x84>
mov    %eax,%ebx
lea    0xe63(%rip),%r14
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   10b0 <ksft_print_msg@plt>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1070 <ksft_exit_fail@plt>
lea    0xe6d(%rip),%rdi
call   1030 <ksft_test_result_pass@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%edi
xor    %esi,%esi
call   1090 <sys_pidfd_open@plt>
test   %eax,%eax
js     128c <main+0xec>
mov    %eax,%ebx
lea    0xe8e(%rip),%rdi
call   1030 <ksft_test_result_pass@plt>
lea    0xea1(%rip),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   10a0 <get_pid_from_fdinfo_file@plt>
lea    0xe93(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   10b0 <ksft_print_msg@plt>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <ksft_exit_pass@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xe1f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   10b0 <ksft_print_msg@plt>
jmp    1216 <main+0x76>

LLVM IR (-O0):
@.str = [49 x i8] c"%s - succeeded to open pidfd for invalid pid -1\0A\00"
@errno = global i32 0
@.str.1 = [39 x i8] c"do not allow invalid pid test: passed\0A\00"
@.str.2 = [64 x i8] c"%s - succeeded to open pidfd with invalid flag value specified\0A\00"
@.str.3 = [40 x i8] c"do not allow invalid flag test: passed\0A\00"
@.str.4 = [27 x i8] c"%s - failed to open pidfd\0A\00"
@.str.5 = [31 x i8] c"open a new pidfd test: passed\0A\00"
@.str.6 = [5 x i8] c"Pid:\00"
@.str.7 = [40 x i8] c"pidfd %d refers to process with pid %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i32 1, i32* %7
%9 = call i32 @ksft_set_plan(i32 3)
%10 = call i32 @sys_pidfd_open(i32 -1, i32 0)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp sge i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* @errno
%15 = call i32 @strerror(i32 %14)
%16 = call i32 (i8*, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %15)
br label %44
17: ; preds = %2
%18 = call i32 @ksft_test_result_pass(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 (...) @getpid()
%20 = call i32 @sys_pidfd_open(i32 %19, i32 1)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp sge i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %17
%24 = load i32, i32* @errno
%25 = call i32 @strerror(i32 %24)
%26 = call i32 (i8*, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.2, i64 0, i64 0), i32 %25)
br label %44
27: ; preds = %17
%28 = call i32 @ksft_test_result_pass(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0))
%29 = call i32 (...) @getpid()
%30 = call i32 @sys_pidfd_open(i32 %29, i32 0)
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %27
%34 = load i32, i32* @errno
%35 = call i32 @strerror(i32 %34)
%36 = call i32 (i8*, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %35)
br label %44
37: ; preds = %27
%38 = call i32 @ksft_test_result_pass(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
%39 = load i32, i32* %6
%40 = call i32 @get_pid_from_fdinfo_file(i32 %39, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i32 4)
store i32 %40, i32* %8
%41 = load i32, i32* %6
%42 = load i32, i32* %8
%43 = call i32 (i8*, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.7, i64 0, i64 0), i32 %41, i32 %42)
store i32 0, i32* %7
br label %44
44: ; preds = %37, %33, %23, %13
%45 = load i32, i32* %6
%46 = icmp sge i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = load i32, i32* %6
%49 = call i32 @close(i32 %48)
br label %50
50: ; preds = %47, %44
%51 = load i32, i32* %7
%52 = icmp ne i32 %51, 0
br i1 %52, label %55, label %53
53: ; preds = %50
%54 = call i32 (...) @ksft_exit_pass()
br label %57
55: ; preds = %50
%56 = call i32 (...) @ksft_exit_fail()
br label %57
57: ; preds = %55, %53
%58 = phi i32 [ %54, %53 ], [ %56, %55 ]
ret i32 %58
}

LLVM IR (-O2):
@.str = [49 x i8] c"%s - succeeded to open pidfd for invalid pid -1\0A\00"
@errno = global i32 0
@.str.1 = [39 x i8] c"do not allow invalid pid test: passed\0A\00"
@.str.2 = [64 x i8] c"%s - succeeded to open pidfd with invalid flag value specified\0A\00"
@.str.3 = [40 x i8] c"do not allow invalid flag test: passed\0A\00"
@.str.4 = [27 x i8] c"%s - failed to open pidfd\0A\00"
@.str.5 = [31 x i8] c"open a new pidfd test: passed\0A\00"
@.str.6 = [5 x i8] c"Pid:\00"
@.str.7 = [40 x i8] c"pidfd %d refers to process with pid %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @ksft_set_plan(i32 3)
%4 = tail call i32 @sys_pidfd_open(i32 -1, i32 0)
%5 = icmp sgt i32 %4, -1
br i1 %5, label %20, label %6
6: ; preds = %2
%7 = tail call i32 @ksft_test_result_pass(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 (...) @getpid()
%9 = tail call i32 @sys_pidfd_open(i32 %8, i32 1)
%10 = icmp sgt i32 %9, -1
br i1 %10, label %20, label %11
11: ; preds = %6
%12 = tail call i32 @ksft_test_result_pass(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0))
%13 = tail call i32 (...) @getpid()
%14 = tail call i32 @sys_pidfd_open(i32 %13, i32 0)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %27
16: ; preds = %11
%17 = load i32, i32* @errno
%18 = tail call i32 @strerror(i32 %17)
%19 = tail call i32 (i8*, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %18)
br label %33
20: ; preds = %6, %2
%21 = phi i8* [ getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([64 x i8], [64 x i8]* @.str.2, i64 0, i64 0), %6 ]
%22 = phi i32 [ %4, %2 ], [ %9, %6 ]
%23 = load i32, i32* @errno
%24 = tail call i32 @strerror(i32 %23)
%25 = tail call i32 (i8*, i32, ...) @ksft_print_msg(i8* %21, i32 %24)
%26 = tail call i32 @close(i32 %22)
br label %33
27: ; preds = %11
%28 = tail call i32 @ksft_test_result_pass(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
%29 = tail call i32 @get_pid_from_fdinfo_file(i32 %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i32 4)
%30 = tail call i32 (i8*, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.7, i64 0, i64 0), i32 %14, i32 %29)
%31 = tail call i32 @close(i32 %14)
%32 = tail call i32 (...) @ksft_exit_pass()
br label %35
33: ; preds = %20, %16
%34 = tail call i32 (...) @ksft_exit_fail()
br label %35
35: ; preds = %33, %27
%36 = phi i32 [ %32, %27 ], [ %34, %33 ]
ret i32 %36
}