#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__ ssize_t;
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int O_CLOEXEC;
int O_RDONLY;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ exit(int);
int openat(int, char *, int);
scalar_t__ pidfd_clone(int, int *);
int pidfd_metadata_fd(scalar_t__, int);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ warnx(char *);
scalar_t__ write(int /*<<< orphan*/, char *, scalar_t__);
int main(int argc, char *argv[]) {
  int pidfd = -1, ret = EXIT_FAILURE;
  char buf[4096] = {0};
  pid_t pid;
  int procfd, statusfd;
  ssize_t bytes;
  pid = pidfd_clone(0x00001000, &pidfd);
  if (pid < 0)
    err(ret, "CLONE_PIDFD");
  if (pidfd == -1) {
    warnx("CLONE_PIDFD is not supported by the kernel");
    goto out;
  }
  procfd = pidfd_metadata_fd(pid, pidfd);
  close(pidfd);
  if (procfd < 0)
    goto out;
  statusfd = openat(procfd, "status", O_RDONLY | O_CLOEXEC);
  close(procfd);
  if (statusfd < 0)
    goto out;
  bytes = read(statusfd, buf, sizeof(buf));
  if (bytes > 0)
    bytes = write(STDOUT_FILENO, buf, bytes);
  close(statusfd);
  ret = EXIT_SUCCESS;
out:
  (void)wait(NULL);
  exit(ret);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1060 <memset@plt>
mov    $0x1000,%edi
lea    -0x14(%rbp),%rsi
call   10a0 <pidfd_clone@plt>
mov    %rax,-0x1028(%rbp)
cmpq   $0x0,-0x1028(%rbp)
jge    1221 <main+0x71>
mov    -0x18(%rbp),%edi
lea    0xde4(%rip),%rsi
call   1090 <err@plt>
cmpl   $0xffffffff,-0x14(%rbp)
jne    123c <main+0x8c>
lea    0xdda(%rip),%rdi
call   1050 <warnx@plt>
jmp    1318 <main+0x168>
mov    -0x1028(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   10d0 <pidfd_metadata_fd@plt>
mov    %eax,-0x102c(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
cmpl   $0x0,-0x102c(%rbp)
jge    126b <main+0xbb>
jmp    1318 <main+0x168>
mov    -0x102c(%rbp),%edi
mov    0x2d68(%rip),%rax
mov    (%rax),%edx
mov    0x2d37(%rip),%rax
or     (%rax),%edx
lea    0xdad(%rip),%rsi
call   1040 <openat@plt>
mov    %eax,-0x1030(%rbp)
mov    -0x102c(%rbp),%edi
call   1070 <close@plt>
cmpl   $0x0,-0x1030(%rbp)
jge    12b2 <main+0x102>
jmp    1318 <main+0x168>
mov    -0x1030(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
mov    %rax,-0x1038(%rbp)
cmpq   $0x0,-0x1038(%rbp)
jle    1301 <main+0x151>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1038(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x1038(%rbp)
mov    -0x1030(%rbp),%edi
call   1070 <close@plt>
mov    0x2cb5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <wait@plt>
mov    -0x18(%rbp),%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1010,%rsp
movl   $0xffffffff,0xc(%rsp)
ff
mov    0x2e06(%rip),%rax
mov    (%rax),%ebx
lea    0x10(%rsp),%rdi
mov    $0x1000,%edx
xor    %esi,%esi
call   1060 <memset@plt>
lea    0xc(%rsp),%rsi
mov    $0x1000,%edi
call   10a0 <pidfd_clone@plt>
mov    %rax,%rbp
test   %rax,%rax
jns    1202 <main+0x52>
lea    0xe05(%rip),%rsi
mov    %ebx,%edi
call   1090 <err@plt>
mov    0xc(%rsp),%esi
cmp    $0xffffffff,%esi
jne    121c <main+0x6c>
lea    0xdfa(%rip),%rdi
call   1050 <warnx@plt>
jmp    12a0 <main+0xf0>
mov    %rbp,%rdi
call   10d0 <pidfd_metadata_fd@plt>
mov    %eax,%ebp
mov    0xc(%rsp),%edi
call   1070 <close@plt>
test   %ebp,%ebp
js     12a0 <main+0xf0>
mov    0x2da6(%rip),%rax
mov    0x2d77(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xdeb(%rip),%rsi
mov    %ebp,%edi
call   1040 <openat@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   1070 <close@plt>
test   %r14d,%r14d
js     12a0 <main+0xf0>
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1000,%edx
call   1080 <read@plt>
test   %rax,%rax
jle    128f <main+0xdf>
mov    0x2d70(%rip),%rcx
mov    (%rcx),%edi
lea    0x10(%rsp),%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    0x2d2a(%rip),%rax
mov    (%rax),%ebx
xor    %edi,%edi
call   10c0 <wait@plt>
mov    %ebx,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@.str = [12 x i8] c"CLONE_PIDFD\00"
@.str.1 = [43 x i8] c"CLONE_PIDFD is not supported by the kernel\00"
@.str.2 = [7 x i8] c"status\00"
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
@STDOUT_FILENO = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [4096 x i8]
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
%13 = load i32, i32* @EXIT_FAILURE
store i32 %13, i32* %7
%14 = bitcast [4096 x i8]* %8 to i8*
%15 = call i64 @pidfd_clone(i32 4096, i32* %6)
store i64 %15, i64* %9
%16 = load i64, i64* %9
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = load i32, i32* %7
%20 = call i32 @err(i32 %19, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %18, %2
%22 = load i32, i32* %6
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @warnx(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
br label %61
26: ; preds = %21
%27 = load i64, i64* %9
%28 = load i32, i32* %6
%29 = call i32 @pidfd_metadata_fd(i64 %27, i32 %28)
store i32 %29, i32* %10
%30 = load i32, i32* %6
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* %10
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %26
br label %61
35: ; preds = %26
%36 = load i32, i32* %10
%37 = load i32, i32* @O_RDONLY
%38 = load i32, i32* @O_CLOEXEC
%39 = or i32 %37, %38
%40 = call i32 @openat(i32 %36, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %39)
store i32 %40, i32* %11
%41 = load i32, i32* %10
%42 = call i32 @close(i32 %41)
%43 = load i32, i32* %11
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %35
br label %61
46: ; preds = %35
%47 = load i32, i32* %11
%48 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%49 = call i64 @read(i32 %47, i8* %48, i32 4096)
store i64 %49, i64* %12
%50 = load i64, i64* %12
%51 = icmp sgt i64 %50, 0
br i1 %51, label %52, label %57
52: ; preds = %46
%53 = load i32, i32* @STDOUT_FILENO
%54 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%55 = load i64, i64* %12
%56 = call i64 @write(i32 %53, i8* %54, i64 %55)
store i64 %56, i64* %12
br label %57
57: ; preds = %52, %46
%58 = load i32, i32* %11
%59 = call i32 @close(i32 %58)
%60 = load i32, i32* @EXIT_SUCCESS
store i32 %60, i32* %7
br label %61
61: ; preds = %57, %45, %34, %24
%62 = call i32 @wait(i32* null)
%63 = load i32, i32* %7
%64 = call i32 @exit(i32 %63)
unreachable
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str = [12 x i8] c"CLONE_PIDFD\00"
@.str.1 = [43 x i8] c"CLONE_PIDFD is not supported by the kernel\00"
@.str.2 = [7 x i8] c"status\00"
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
@STDOUT_FILENO = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca [4096 x i8]
%5 = bitcast i32* %3 to i8*
store i32 -1, i32* %3
%6 = load i32, i32* @EXIT_FAILURE
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%8 = call i64 @pidfd_clone(i32 4096, i32* %3)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @err(i32 %6, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
%13 = load i32, i32* %3
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @warnx(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
br label %38
17: ; preds = %12
%18 = call i32 @pidfd_metadata_fd(i64 %8, i32 %13)
%19 = load i32, i32* %3
%20 = call i32 @close(i32 %19)
%21 = icmp slt i32 %18, 0
br i1 %21, label %38, label %22
22: ; preds = %17
%23 = load i32, i32* @O_RDONLY
%24 = load i32, i32* @O_CLOEXEC
%25 = or i32 %24, %23
%26 = call i32 @openat(i32 %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = call i32 @close(i32 %18)
%28 = icmp slt i32 %26, 0
br i1 %28, label %38, label %29
29: ; preds = %22
%30 = call i64 @read(i32 %26, i8* %7, i32 4096)
%31 = icmp sgt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32, i32* @STDOUT_FILENO
%34 = call i64 @write(i32 %33, i8* %7, i64 %30)
br label %35
35: ; preds = %32, %29
%36 = call i32 @close(i32 %26)
%37 = load i32, i32* @EXIT_SUCCESS
br label %38
38: ; preds = %22, %17, %35, %15
%39 = phi i32 [ %6, %15 ], [ %6, %17 ], [ %6, %22 ], [ %37, %35 ]
%40 = call i32 @wait(i32* null)
%41 = call i32 @exit(i32 %39)
unreachable
}