#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 call_clone3_set_tid(int /*<<< orphan*/ *, size_t, int, int, int);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ ksft_print_msg(char *, int /*<<< orphan*/,
                                  int /*<<< orphan*/, int, ...);
int /*<<< orphan*/ ksft_test_result_fail(char *, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ ksft_test_result_pass(char *, int /*<<< orphan*/, int, int);
void test_clone3_set_tid(pid_t *set_tid, size_t set_tid_size, int flags,
                         int expected, int expected_pid, bool wait_for_it) {
  int ret;
  ksft_print_msg("[%d] Trying clone3() with CLONE_SET_TID to %d and 0x%x\n",
                 getpid(), set_tid[0], flags);
  ret = call_clone3_set_tid(set_tid, set_tid_size, flags, expected_pid,
                            wait_for_it);
  ksft_print_msg("[%d] clone3() with CLONE_SET_TID %d says :%d - expected %d\n",
                 getpid(), set_tid[0], ret, expected);
  if (ret != expected)
    ksft_test_result_fail("[%d] Result (%d) is different than expected (%d)\n",
                          getpid(), ret, expected);
  else
    ksft_test_result_pass("[%d] Result (%d) matches expectation (%d)\n",
                          getpid(), ret, expected);
}


x86 assembly (clang, -O0):
<test_clone3_set_tid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    %r9d,-0x20(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x8(%rbp),%rax
mov    (%rax),%edx
mov    -0x14(%rbp),%ecx
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1060 <ksft_print_msg@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%r8d
call   1070 <call_clone3_set_tid@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x8(%rbp),%rax
mov    (%rax),%edx
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%r8d
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1060 <ksft_print_msg@plt>
mov    -0x24(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     11f8 <test_clone3_set_tid+0xa8>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x24(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe86(%rip),%rdi
call   1050 <ksft_test_result_fail@plt>
jmp    1213 <test_clone3_set_tid+0xc3>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x24(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe98(%rip),%rdi
call   1030 <ksft_test_result_pass@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_clone3_set_tid>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9d,%r15d
mov    %r8d,%r12d
mov    %ecx,%r14d
mov    %edx,%ebp
mov    %rsi,%r13
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    (%rbx),%edx
lea    0xe84(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%ecx
xor    %eax,%eax
call   1060 <ksft_print_msg@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %ebp,%edx
mov    %r12d,%ecx
mov    %r15d,%r8d
call   1070 <call_clone3_set_tid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    (%rbx),%edx
lea    0xe8c(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%ecx
mov    %r14d,%r8d
xor    %eax,%eax
call   1060 <ksft_print_msg@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
cmp    %r14d,%ebp
jne    11e8 <test_clone3_set_tid+0x98>
lea    0xed9(%rip),%rdi
mov    %eax,%esi
mov    %r14d,%edx
mov    %r14d,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <ksft_test_result_pass@plt>
lea    0xe85(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
mov    %r14d,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <ksft_test_result_fail@plt>

LLVM IR (-O0):
@.str = [56 x i8] c"[%d] Trying clone3() with CLONE_SET_TID to %d and 0x%x\0A\00"
@.str.1 = [60 x i8] c"[%d] clone3() with CLONE_SET_TID %d says :%d - expected %d\0A\00"
@.str.2 = [50 x i8] c"[%d] Result (%d) is different than expected (%d)\0A\00"
@.str.3 = [43 x i8] c"[%d] Result (%d) matches expectation (%d)\0A\00"
define void @test_clone3_set_tid(i32* %0, i64 %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32* %0, i32** %7
store i64 %1, i64* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 %5, i32* %12
%14 = call i32 (...) @getpid()
%15 = load i32*, i32** %7
%16 = getelementptr inbounds i32, i32* %15, i64 0
%17 = load i32, i32* %16
%18 = load i32, i32* %9
%19 = call i32 (i8*, i32, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i32 %14, i32 %17, i32 %18)
%20 = load i32*, i32** %7
%21 = load i64, i64* %8
%22 = load i32, i32* %9
%23 = load i32, i32* %11
%24 = load i32, i32* %12
%25 = call i32 @call_clone3_set_tid(i32* %20, i64 %21, i32 %22, i32 %23, i32 %24)
store i32 %25, i32* %13
%26 = call i32 (...) @getpid()
%27 = load i32*, i32** %7
%28 = getelementptr inbounds i32, i32* %27, i64 0
%29 = load i32, i32* %28
%30 = load i32, i32* %13
%31 = load i32, i32* %10
%32 = call i32 (i8*, i32, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.1, i64 0, i64 0), i32 %26, i32 %29, i32 %30, i32 %31)
%33 = load i32, i32* %13
%34 = load i32, i32* %10
%35 = icmp ne i32 %33, %34
br i1 %35, label %36, label %41
36: ; preds = %6
%37 = call i32 (...) @getpid()
%38 = load i32, i32* %13
%39 = load i32, i32* %10
%40 = call i32 @ksft_test_result_fail(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i32 %37, i32 %38, i32 %39)
br label %46
41: ; preds = %6
%42 = call i32 (...) @getpid()
%43 = load i32, i32* %13
%44 = load i32, i32* %10
%45 = call i32 @ksft_test_result_pass(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i32 %42, i32 %43, i32 %44)
br label %46
46: ; preds = %41, %36
ret void
}

LLVM IR (-O2):
@.str = [56 x i8] c"[%d] Trying clone3() with CLONE_SET_TID to %d and 0x%x\0A\00"
@.str.1 = [60 x i8] c"[%d] clone3() with CLONE_SET_TID %d says :%d - expected %d\0A\00"
@.str.2 = [50 x i8] c"[%d] Result (%d) is different than expected (%d)\0A\00"
@.str.3 = [43 x i8] c"[%d] Result (%d) matches expectation (%d)\0A\00"
define void @test_clone3_set_tid(i32* %0, i64 %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = tail call i32 (...) @getpid()
%8 = load i32, i32* %0
%9 = tail call i32 (i8*, i32, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %8, i32 %2)
%10 = tail call i32 @call_clone3_set_tid(i32* %0, i64 %1, i32 %2, i32 %4, i32 %5)
%11 = tail call i32 (...) @getpid()
%12 = load i32, i32* %0
%13 = tail call i32 (i8*, i32, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 %12, i32 %10, i32 %3)
%14 = icmp eq i32 %10, %3
%15 = tail call i32 (...) @getpid()
br i1 %14, label %18, label %16
16: ; preds = %6
%17 = tail call i32 @ksft_test_result_fail(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i32 %15, i32 %10, i32 %3)
br label %20
18: ; preds = %6
%19 = tail call i32 @ksft_test_result_pass(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i32 %15, i32 %3, i32 %3)
br label %20
20: ; preds = %18, %16
ret void
}