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

/* Variables and functions */
#define EPERM 128
int O_CLOEXEC;
int O_DIRECTORY;
int O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int open(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int sys_pidfd_send_signal(int, int /*<<< orphan*/, int /*<<< orphan*/ *,
                          int /*<<< orphan*/);
int /*<<< orphan*/ warn(char *, ...);
int pidfd_metadata_fd(pid_t pid, int pidfd) {
  int procfd, ret;
  char path[100];
  snprintf(path, sizeof(path), "/proc/%d", pid);
  procfd = open(path, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
  if (procfd < 0) {
    warn("Failed to open %s\n", path);
    return -1;
  }
  ret = sys_pidfd_send_signal(pidfd, 0, NULL, 0);
  if (ret < 0) {
    switch (errno) {
    case EPERM:
      break;
    default:
      warn("Failed to signal process\n");
      close(procfd);
      procfd = -1;
    }
  }
  return procfd;
}


x86 assembly (clang, -O0):
<pidfd_metadata_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x64,%esi
lea    0xe8c(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x80(%rbp),%rdi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    0x2e5b(%rip),%rax
or     (%rax),%esi
mov    0x2e2a(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11c8 <pidfd_metadata_fd+0x78>
lea    -0x80(%rbp),%rsi
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122b <pidfd_metadata_fd+0xdb>
mov    -0xc(%rbp),%edi
xor    %ecx,%ecx
xor    %eax,%eax
mov    %eax,%edx
mov    %ecx,%esi
call   1060 <sys_pidfd_send_signal@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1225 <pidfd_metadata_fd+0xd5>
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
sub    $0x80,%eax
jne    1203 <pidfd_metadata_fd+0xb3>
jmp    11fe <pidfd_metadata_fd+0xae>
jmp    1220 <pidfd_metadata_fd+0xd0>
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x10(%rbp)
jmp    1225 <pidfd_metadata_fd+0xd5>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pidfd_metadata_fd>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
mov    %esi,%ebp
mov    %edi,%ecx
lea    0xe9f(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1040 <snprintf@plt>
mov    0x2e60(%rip),%rax
mov    0x2e69(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e36(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11d1 <pidfd_metadata_fd+0x81>
mov    %eax,%ebx
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <sys_pidfd_send_signal@plt>
test   %eax,%eax
jns    11e7 <pidfd_metadata_fd+0x97>
mov    0x2e1e(%rip),%rax
cmpl   $0x80,(%rax)
je     11e7 <pidfd_metadata_fd+0x97>
lea    0xe5b(%rip),%rdi
xor    %eax,%eax
call   1030 <warn@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    11e2 <pidfd_metadata_fd+0x92>
lea    0xe31(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1030 <warn@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/proc/%d\00"
@O_DIRECTORY = global i32 0
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
@.str.1 = [19 x i8] c"Failed to open %s\0A\00"
@errno = global i32 0
@.str.2 = [26 x i8] c"Failed to signal process\0A\00"
define i32 @pidfd_metadata_fd(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [100 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 @snprintf(i8* %9, i32 100, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%13 = load i32, i32* @O_DIRECTORY
%14 = load i32, i32* @O_RDONLY
%15 = or i32 %13, %14
%16 = load i32, i32* @O_CLOEXEC
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %12, i32 %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %2
%22 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%23 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %22)
store i32 -1, i32* %3
br label %39
24: ; preds = %2
%25 = load i32, i32* %5
%26 = call i32 @sys_pidfd_send_signal(i32 %25, i32 0, i32* null, i32 0)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %37
29: ; preds = %24
%30 = load i32, i32* @errno
switch i32 %30, label %32 [
i32 128, label %31
]
31: ; preds = %29
br label %36
32: ; preds = %29
%33 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%34 = load i32, i32* %6
%35 = call i32 @close(i32 %34)
store i32 -1, i32* %6
br label %36
36: ; preds = %32, %31
br label %37
37: ; preds = %36, %24
%38 = load i32, i32* %6
store i32 %38, i32* %3
br label %39
39: ; preds = %37, %21
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@.str = [9 x i8] c"/proc/%d\00"
@O_DIRECTORY = global i32 0
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
@.str.1 = [19 x i8] c"Failed to open %s\0A\00"
@errno = global i32 0
@.str.2 = [26 x i8] c"Failed to signal process\0A\00"
define i32 @pidfd_metadata_fd(i32 %0, i32 %1) {
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 100, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = load i32, i32* @O_DIRECTORY
%7 = load i32, i32* @O_RDONLY
%8 = or i32 %7, %6
%9 = load i32, i32* @O_CLOEXEC
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %4, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %4)
br label %24
15: ; preds = %2
%16 = call i32 @sys_pidfd_send_signal(i32 %1, i32 0, i32* null, i32 0)
%17 = icmp sgt i32 %16, -1
%18 = load i32, i32* @errno
%19 = icmp eq i32 %18, 128
%20 = select i1 %17, i1 true, i1 %19
br i1 %20, label %24, label %21
21: ; preds = %15
%22 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @close(i32 %11)
br label %24
24: ; preds = %15, %21, %13
%25 = phi i32 [ -1, %13 ], [ %11, %15 ], [ -1, %21 ]
ret i32 %25
}