#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 */

/* Variables and functions */
int /*<<< orphan*/ FUTEX_WAIT;
int /*<<< orphan*/ FUTEX_WAKE;
int /*<<< orphan*/ SYS_futex;
int /*<<< orphan*/ sched_yield();
scalar_t__ syscall(int /*<<< orphan*/, int *, int /*<<< orphan*/, int,
                   int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
void *thread(void *restrict ftx) {
  int *futex = (int *)ftx;
  for (int i = 0; i < 100000; i++) {
    sched_yield();
    while (syscall(SYS_futex, futex, FUTEX_WAIT, 0xA, NULL, NULL, 42)) {
      sched_yield();
    }
    *futex = 0xB;
    while (!syscall(SYS_futex, futex, FUTEX_WAKE, 1, NULL, NULL, 42)) {
      sched_yield();
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x186a0,-0x14(%rbp)
jge    1202 <thread+0xe2>
mov    $0x0,%al
call   1030 <sched_yield@plt>
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e85(%rip),%rax
mov    (%rax),%edx
mov    $0xa,%ecx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
movl   $0x2a,(%rsp)
call   1040 <syscall@plt>
cmp    $0x0,%rax
je     1194 <thread+0x74>
mov    $0x0,%al
call   1030 <sched_yield@plt>
jmp    114f <thread+0x2f>
mov    -0x10(%rbp),%rax
movl   $0xb,(%rax)
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%ecx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
movl   $0x2a,(%rsp)
call   1040 <syscall@plt>
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11e3 <thread+0xc3>
jmp    11ef <thread+0xcf>
mov    $0x0,%al
call   1030 <sched_yield@plt>
jmp    119e <thread+0x7e>
jmp    11f4 <thread+0xd4>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    113b <thread+0x1b>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2ea2(%rip),%r14
mov    0x2eab(%rip),%r15
mov    0x2e84(%rip),%r12
xor    %ebp,%ebp
jmp    1160 <thread+0x40>
add    $0x1,%ebp
cmp    $0x186a0,%ebp
je     11cf <thread+0xaf>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   1030 <sched_yield@plt>
mov    (%r14),%edi
mov    (%r15),%edx
movl   $0x2a,(%rsp)
mov    %rbx,%rsi
mov    $0xa,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1040 <syscall@plt>
test   %rax,%rax
jne    1160 <thread+0x40>
movl   $0xb,(%rbx)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r14),%edi
mov    (%r12),%edx
movl   $0x2a,(%rsp)
mov    %rbx,%rsi
mov    $0x1,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1040 <syscall@plt>
test   %rax,%rax
jne    1148 <thread+0x28>
xor    %eax,%eax
call   1030 <sched_yield@plt>
jmp    11a0 <thread+0x80>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SYS_futex = global i32 0
@FUTEX_WAIT = global i32 0
@FUTEX_WAKE = global i32 0
define i8* @thread(i8* noalias %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = bitcast i8* %5 to i32*
store i32* %6, i32** %3
store i32 0, i32* %4
br label %7
7: ; preds = %32, %1
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 100000
br i1 %9, label %10, label %35
10: ; preds = %7
%11 = call i32 (...) @sched_yield()
br label %12
12: ; preds = %18, %10
%13 = load i32, i32* @SYS_futex
%14 = load i32*, i32** %3
%15 = load i32, i32* @FUTEX_WAIT
%16 = call i64 @syscall(i32 %13, i32* %14, i32 %15, i32 10, i32* null, i32* null, i32 42)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = call i32 (...) @sched_yield()
br label %12
20: ; preds = %12
%21 = load i32*, i32** %3
store i32 11, i32* %21
br label %22
22: ; preds = %29, %20
%23 = load i32, i32* @SYS_futex
%24 = load i32*, i32** %3
%25 = load i32, i32* @FUTEX_WAKE
%26 = call i64 @syscall(i32 %23, i32* %24, i32 %25, i32 1, i32* null, i32* null, i32 42)
%27 = icmp ne i64 %26, 0
%28 = xor i1 %27, true
br i1 %28, label %29, label %31
29: ; preds = %22
%30 = call i32 (...) @sched_yield()
br label %22
31: ; preds = %22
br label %32
32: ; preds = %31
%33 = load i32, i32* %4
%34 = add nsw i32 %33, 1
store i32 %34, i32* %4
br label %7
35: ; preds = %7
ret i8* null
}

LLVM IR (-O2):
@SYS_futex = global i32 0
@FUTEX_WAIT = global i32 0
@FUTEX_WAKE = global i32 0
define noalias i8* @thread(i8* noalias %0) {
%2 = bitcast i8* %0 to i32*
br label %4
3: ; preds = %28
ret i8* null
4: ; preds = %1, %28
%5 = phi i32 [ 0, %1 ], [ %29, %28 ]
%6 = tail call i32 (...) @sched_yield()
%7 = load i32, i32* @SYS_futex
%8 = load i32, i32* @FUTEX_WAIT
%9 = tail call i64 @syscall(i32 %7, i32* %2, i32 %8, i32 10, i32* null, i32* null, i32 42)
%10 = icmp eq i64 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %4, %11
%12 = tail call i32 (...) @sched_yield()
%13 = load i32, i32* @SYS_futex
%14 = load i32, i32* @FUTEX_WAIT
%15 = tail call i64 @syscall(i32 %13, i32* %2, i32 %14, i32 10, i32* null, i32* null, i32 42)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %11
17: ; preds = %11, %4
store i32 11, i32* %2
%18 = load i32, i32* @SYS_futex
%19 = load i32, i32* @FUTEX_WAKE
%20 = tail call i64 @syscall(i32 %18, i32* %2, i32 %19, i32 1, i32* null, i32* null, i32 42)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %17, %22
%23 = tail call i32 (...) @sched_yield()
%24 = load i32, i32* @SYS_futex
%25 = load i32, i32* @FUTEX_WAKE
%26 = tail call i64 @syscall(i32 %24, i32* %2, i32 %25, i32 1, i32* null, i32* null, i32 42)
%27 = icmp eq i64 %26, 0
br i1 %27, label %22, label %28
28: ; preds = %22, %17
%29 = add nuw nsw i32 %5, 1
%30 = icmp eq i32 %29, 100000
br i1 %30, label %3, label %4
}