#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 clone_args {
  int exit_signal;
};
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ args;

/* Variables and functions */
scalar_t__ ENOSYS;
int /*<<< orphan*/ EXIT_SUCCESS;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ ksft_exit_fail_msg(char *);
int /*<<< orphan*/ ksft_exit_skip(char *);
int /*<<< orphan*/ ksft_print_msg(char *);
scalar_t__ sys_clone3(struct clone_args *, int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
void test_clone3_supported(void) {
  pid_t pid;
  struct clone_args args = {};
  if (-1 < 0)
    ksft_exit_skip("clone3() syscall is not supported\n");
  args.exit_signal = -1;
  pid = sys_clone3(&args, sizeof(args));
  if (!pid)
    exit(EXIT_SUCCESS);
  if (pid > 0) {
    wait(NULL);
    ksft_exit_fail_msg(
        "Managed to create child process with invalid exit_signal\n");
  }
  if (errno == ENOSYS)
    ksft_exit_skip("clone3() syscall is not supported\n");
  ksft_print_msg("clone3() syscall supported\n");
}


x86 assembly (clang, -O0):
<test_clone3_supported>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
lea    0xe71(%rip),%rdi
call   1050 <ksft_exit_skip@plt>
movl   $0xffffffff,-0x10(%rbp)
lea    -0x10(%rbp),%rdi
mov    $0x4,%esi
call   1030 <sys_clone3@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11c6 <test_clone3_supported+0x56>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    11e6 <test_clone3_supported+0x76>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
lea    0xe42(%rip),%rdi
call   1040 <ksft_exit_fail_msg@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%rax
mov    0x2df1(%rip),%rcx
cmp    (%rcx),%rax
jne    120c <test_clone3_supported+0x9c>
lea    0xdf9(%rip),%rdi
call   1050 <ksft_exit_skip@plt>
lea    0xe4a(%rip),%rdi
call   1070 <ksft_print_msg@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_clone3_supported>:
push   %rax
lea    0xe98(%rip),%rdi
call   1050 <ksft_exit_skip@plt>
movl   $0xffffffff,(%rsp)
mov    %rsp,%rdi
mov    $0x4,%esi
call   1030 <sys_clone3@plt>
test   %rax,%rax
je     11cb <test_clone3_supported+0x6b>
jle    119b <test_clone3_supported+0x3b>
xor    %edi,%edi
call   1080 <wait@plt>
lea    0xe8d(%rip),%rdi
call   1040 <ksft_exit_fail_msg@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%rax
mov    0x2e3c(%rip),%rcx
cmp    (%rcx),%rax
jne    11bd <test_clone3_supported+0x5d>
lea    0xe48(%rip),%rdi
call   1050 <ksft_exit_skip@plt>
lea    0xe99(%rip),%rdi
call   1060 <ksft_print_msg@plt>
pop    %rax
ret
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.clone_args = type { i32 }
@.str = [35 x i8] c"clone3() syscall is not supported\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [58 x i8] c"Managed to create child process with invalid exit_signal\0A\00"
@errno = global i64 0
@ENOSYS = global i64 0
@.str.2 = [28 x i8] c"clone3() syscall supported\0A\00"
define void @test_clone3_supported() {
%1 = alloca i64
%2 = alloca %struct.clone_args
%3 = bitcast %struct.clone_args* %2 to i8*
%4 = call i32 @ksft_exit_skip(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%5 = getelementptr inbounds %struct.clone_args, %struct.clone_args* %2, i32 0, i32 0
store i32 -1, i32* %5
%6 = call i64 @sys_clone3(%struct.clone_args* %2, i32 4)
store i64 %6, i64* %1
%7 = load i64, i64* %1
%8 = icmp ne i64 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %0
%10 = load i32, i32* @EXIT_SUCCESS
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = load i64, i64* %1
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = call i32 @wait(i32* null)
%17 = call i32 @ksft_exit_fail_msg(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %15, %12
%19 = load i64, i64* @errno
%20 = load i64, i64* @ENOSYS
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @ksft_exit_skip(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %22, %18
%25 = call i32 @ksft_print_msg(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
%struct.clone_args = type { i32 }
@.str = [35 x i8] c"clone3() syscall is not supported\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [58 x i8] c"Managed to create child process with invalid exit_signal\0A\00"
@errno = global i64 0
@ENOSYS = global i64 0
@.str.2 = [28 x i8] c"clone3() syscall supported\0A\00"
define void @test_clone3_supported() {
%1 = alloca %struct.clone_args
%2 = bitcast %struct.clone_args* %1 to i8*
%3 = getelementptr inbounds %struct.clone_args, %struct.clone_args* %1, i64 0, i32 0
%4 = tail call i32 @ksft_exit_skip(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
%5 = call i64 @sys_clone3(%struct.clone_args* %1, i32 4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = load i32, i32* @EXIT_SUCCESS
%9 = call i32 @exit(i32 %8)
unreachable
10: ; preds = %0
%11 = icmp sgt i64 %5, 0
br i1 %11, label %12, label %15
12: ; preds = %10
%13 = call i32 @wait(i32* null)
%14 = call i32 @ksft_exit_fail_msg(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %12, %10
%16 = load i64, i64* @errno
%17 = load i64, i64* @ENOSYS
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 @ksft_exit_skip(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %15
%22 = call i32 @ksft_print_msg(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
ret void
}