#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 uint64_t;
typedef int pid_t;

/* Variables and functions */
int gettid();
int /*<<< orphan*/ ksft_print_msg(char *, int, int);
int /*<<< orphan*/ perror(char *);
int prctl(int, int, int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int rand();
int /*<<< orphan*/ srand(int);
int time(int /*<<< orphan*/ *);
void *execute_thread(void *x) {
  pid_t pid = *((pid_t *)x);
  pid_t tid = gettid();
  uint64_t prctl_tag_mask;
  uint64_t prctl_set;
  uint64_t prctl_get;
  uint64_t prctl_tcf;
  srand(time(NULL) ^ (pid << 16) ^ (tid << 16));
  prctl_tag_mask = rand() & 0xffff;
  if (prctl_tag_mask % 2)
    prctl_tcf = (1UL << 1);
  else
    prctl_tcf = (2UL << 1);
  prctl_set = (1UL << 0) | prctl_tcf | (prctl_tag_mask << 3);
  for (int j = 0; j < 1000; j++) {
    if (prctl(55, prctl_set, 0, 0, 0)) {
      perror("prctl() failed");
      goto fail;
    }
    prctl_get = prctl(56, 0, 0, 0, 0);
    if (prctl_set != prctl_get) {
      ksft_print_msg("Error: prctl_set: 0x%lx != prctl_get: 0x%lx\n", prctl_set,
                     prctl_get);
      goto fail;
    }
  }
  return (void *)0;
fail:
  return (void *)1;
}


x86 assembly (clang, -O0):
<execute_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1030 <gettid@plt>
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %eax,%edi
mov    -0x14(%rbp),%eax
shl    $0x10,%eax
xor    %eax,%edi
mov    -0x18(%rbp),%eax
shl    $0x10,%eax
xor    %eax,%edi
call   1040 <srand@plt>
mov    $0x0,%al
call   1090 <rand@plt>
and    $0xffff,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     11de <execute_thread+0x6e>
movl   $0x2,-0x28(%rbp)
jmp    11e5 <execute_thread+0x75>
movl   $0x4,-0x28(%rbp)
movslq -0x28(%rbp),%rax
or     $0x1,%rax
mov    -0x1c(%rbp),%ecx
shl    $0x3,%ecx
movslq %ecx,%rcx
or     %rcx,%rax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x3e8,-0x2c(%rbp)
jge    128f <execute_thread+0x11f>
mov    -0x20(%rbp),%esi
mov    $0x37,%edi
xor    %r8d,%r8d
mov    %r8d,%edx
mov    %r8d,%ecx
call   1050 <prctl@plt>
cmp    $0x0,%eax
je     1240 <execute_thread+0xd0>
lea    0xdca(%rip),%rdi
call   1080 <perror@plt>
jmp    129c <execute_thread+0x12c>
mov    $0x38,%edi
xor    %r8d,%r8d
mov    %r8d,%esi
mov    %r8d,%edx
mov    %r8d,%ecx
call   1050 <prctl@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x24(%rbp),%eax
je     127c <execute_thread+0x10c>
mov    -0x20(%rbp),%esi
mov    -0x24(%rbp),%edx
lea    0xd9d(%rip),%rdi
call   1070 <ksft_print_msg@plt>
jmp    129c <execute_thread+0x12c>
jmp    1281 <execute_thread+0x111>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1203 <execute_thread+0x93>
movq   $0x0,-0x8(%rbp)
jmp    12a5 <execute_thread+0x135>
mov    $0x1,%eax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execute_thread>:
push   %rbp
push   %r14
push   %rbx
mov    (%rdi),%ebx
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <gettid@plt>
mov    %eax,%ebp
xor    %edi,%edi
call   1060 <time@plt>
shl    $0x10,%ebx
shl    $0x10,%ebp
xor    %eax,%ebp
xor    %ebx,%ebp
mov    %ebp,%edi
call   1040 <srand@plt>
xor    %eax,%eax
call   1090 <rand@plt>
xor    %ecx,%ecx
test   $0x1,%al
sete   %cl
movzwl %ax,%eax
shl    $0x3,%eax
lea    (%rax,%rcx,2),%ebp
add    $0x3,%ebp
mov    $0x3e8,%ebx
nopl   0x0(%rax)
mov    $0x37,%edi
mov    %ebp,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1050 <prctl@plt>
test   %eax,%eax
jne    1213 <execute_thread+0xa3>
mov    $0x38,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1050 <prctl@plt>
cmp    %eax,%ebp
jne    11f5 <execute_thread+0x85>
add    $0xffffffff,%ebx
jne    11c0 <execute_thread+0x50>
jmp    120b <execute_thread+0x9b>
lea    0xe13(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1070 <ksft_print_msg@plt>
mov    $0x1,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xde6(%rip),%rdi
call   1080 <perror@plt>
jmp    1205 <execute_thread+0x95>

LLVM IR (-O0):
@.str = [15 x i8] c"prctl() failed\00"
@.str.1 = [45 x i8] c"Error: prctl_set: 0x%lx != prctl_get: 0x%lx\0A\00"
define i8* @execute_thread(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
%11 = load i8*, i8** %3
%12 = bitcast i8* %11 to i32*
%13 = load i32, i32* %12
store i32 %13, i32* %4
%14 = call i32 (...) @gettid()
store i32 %14, i32* %5
%15 = call i32 @time(i32* null)
%16 = load i32, i32* %4
%17 = shl i32 %16, 16
%18 = xor i32 %15, %17
%19 = load i32, i32* %5
%20 = shl i32 %19, 16
%21 = xor i32 %18, %20
%22 = call i32 @srand(i32 %21)
%23 = call i32 (...) @rand()
%24 = and i32 %23, 65535
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = srem i32 %25, 2
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %1
store i32 2, i32* %9
br label %30
29: ; preds = %1
store i32 4, i32* %9
br label %30
30: ; preds = %29, %28
%31 = load i32, i32* %9
%32 = sext i32 %31 to i64
%33 = or i64 1, %32
%34 = load i32, i32* %6
%35 = shl i32 %34, 3
%36 = sext i32 %35 to i64
%37 = or i64 %33, %36
%38 = trunc i64 %37 to i32
store i32 %38, i32* %7
store i32 0, i32* %10
br label %39
39: ; preds = %58, %30
%40 = load i32, i32* %10
%41 = icmp slt i32 %40, 1000
br i1 %41, label %42, label %61
42: ; preds = %39
%43 = load i32, i32* %7
%44 = call i32 @prctl(i32 55, i32 %43, i32 0, i32 0, i32 0)
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %42
%47 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %62
48: ; preds = %42
%49 = call i32 @prctl(i32 56, i32 0, i32 0, i32 0, i32 0)
store i32 %49, i32* %8
%50 = load i32, i32* %7
%51 = load i32, i32* %8
%52 = icmp ne i32 %50, %51
br i1 %52, label %53, label %57
53: ; preds = %48
%54 = load i32, i32* %7
%55 = load i32, i32* %8
%56 = call i32 @ksft_print_msg(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %54, i32 %55)
br label %62
57: ; preds = %48
br label %58
58: ; preds = %57
%59 = load i32, i32* %10
%60 = add nsw i32 %59, 1
store i32 %60, i32* %10
br label %39
61: ; preds = %39
store i8* null, i8** %2
br label %63
62: ; preds = %53, %46
store i8* inttoptr (i64 1 to i8*), i8** %2
br label %63
63: ; preds = %62, %61
%64 = load i8*, i8** %2
ret i8* %64
}

LLVM IR (-O2):
@.str = [15 x i8] c"prctl() failed\00"
@.str.1 = [45 x i8] c"Error: prctl_set: 0x%lx != prctl_get: 0x%lx\0A\00"
define i8* @execute_thread(i8* nocapture %0) {
%2 = bitcast i8* %0 to i32*
%3 = load i32, i32* %2
%4 = tail call i32 (...) @gettid()
%5 = tail call i32 @time(i32* null)
%6 = shl i32 %3, 16
%7 = xor i32 %5, %6
%8 = shl i32 %4, 16
%9 = xor i32 %7, %8
%10 = tail call i32 @srand(i32 %9)
%11 = tail call i32 (...) @rand()
%12 = and i32 %11, 1
%13 = icmp eq i32 %12, 0
%14 = select i1 %13, i32 5, i32 3
%15 = shl i32 %11, 3
%16 = and i32 %15, 524280
%17 = or i32 %14, %16
br label %21
18: ; preds = %27
%19 = add nuw nsw i32 %22, 1
%20 = icmp eq i32 %19, 1000
br i1 %20, label %32, label %21
21: ; preds = %1, %18
%22 = phi i32 [ 0, %1 ], [ %19, %18 ]
%23 = tail call i32 @prctl(i32 55, i32 %17, i32 0, i32 0, i32 0)
%24 = icmp eq i32 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %21
%26 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %32
27: ; preds = %21
%28 = tail call i32 @prctl(i32 56, i32 0, i32 0, i32 0, i32 0)
%29 = icmp eq i32 %17, %28
br i1 %29, label %18, label %30
30: ; preds = %27
%31 = tail call i32 @ksft_print_msg(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 %28)
br label %32
32: ; preds = %18, %30, %25
%33 = phi i8* [ inttoptr (i64 1 to i8*), %25 ], [ inttoptr (i64 1 to i8*), %30 ], [ null, %18 ]
ret i8* %33
}