#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 timespec {
  int tv_nsec;
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ FUTEX_WAIT;
int IPC_CREAT;
int /*<<< orphan*/ IPC_PRIVATE;
int atoi(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int fork();
int futex(int *, int /*<<< orphan*/, int, struct timespec *,
          int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int /*<<< orphan*/, int, int);
char *strerror(int /*<<< orphan*/);
unsigned long long time_ns();
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ wake_futex_blocking(int *);
int main(int argc, char **argv) {
  if (argc < 2) {
    printf("Usage: %s <parent wait in ms before waking child>\n", argv[0]);
    exit(1);
  }
  int shm_id = shmget(IPC_PRIVATE, 4096, IPC_CREAT | 0666);
  if (shm_id < 0) {
    perror("shmget");
    exit(1);
  }
  int *shared_data = shmat(shm_id, NULL, 0);
  *shared_data = 0;
  int forkstatus = fork();
  if (forkstatus < 0) {
    perror("fork");
    exit(1);
  }
  if (forkstatus == 0) {
    printf("child waiting for A\n");
    struct timespec timeout = {.tv_sec = 0, .tv_nsec = 500000000};
    while (1) {
      unsigned long long t1 = time_ns();
      int futex_rc = futex(shared_data, FUTEX_WAIT, 0xA, &timeout, NULL, 0);
      printf("child woken up rc=%d errno=%s, elapsed=%llu\n", futex_rc,
             futex_rc ? strerror(errno) : "", time_ns() - t1);
      if (futex_rc == 0 && *shared_data == 0xA) {
        break;
      }
    }
  } else {
    printf("parent writin A\n");
    *shared_data = 0xA;
    usleep(atoi(argv[1]) * 1000);
    printf("parent waking child\n");
    wake_futex_blocking(shared_data);
    wait(NULL);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    120f <main+0x3f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
mov    0x2da9(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x1000,%esi
call   1070 <shmget@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1254 <main+0x84>
lea    0xdee(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    129a <main+0xca>
lea    0xdaf(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x24(%rbp)
jne    1376 <main+0x1a6>
lea    0xd94(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0xdef(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   10c0 <time_ns@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2d15(%rip),%rax
mov    (%rax),%esi
mov    $0xa,%edx
lea    -0x30(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
xor    %r9d,%r9d
call   1040 <futex@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     1315 <main+0x145>
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,-0x48(%rbp)
jmp    1325 <main+0x155>
lea    0xd16(%rip),%rax
mov    %rax,-0x48(%rbp)
jmp    1325 <main+0x155>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    $0x0,%al
call   10c0 <time_ns@plt>
mov    -0x40(%rbp),%esi
mov    -0x50(%rbp),%rdx
mov    %rax,%rcx
sub    -0x38(%rbp),%rcx
lea    0xd0b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x3c(%rbp)
jne    136c <main+0x19c>
mov    -0x20(%rbp),%rax
cmpl   $0xa,(%rax)
jne    136c <main+0x19c>
jmp    1371 <main+0x1a1>
jmp    12bd <main+0xed>
jmp    13c6 <main+0x1f6>
lea    0xd04(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
movl   $0xa,(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
imul   $0x3e8,%eax,%edi
call   10f0 <usleep@plt>
lea    0xce5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rdi
call   10b0 <wake_futex_blocking@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10d0 <wait@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x1,%edi
jle    1344 <main+0x164>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
mov    0x2dc1(%rip),%rax
mov    $0x1b6,%edx
or     (%rax),%edx
mov    $0x1000,%esi
call   1080 <shmget@plt>
test   %eax,%eax
js     135f <main+0x17f>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,%r13
movl   $0x0,(%rax)
xor    %eax,%eax
call   10f0 <fork@plt>
test   %eax,%eax
js     1368 <main+0x188>
je     128d <main+0xad>
lea    0xe20(%rip),%rdi
call   1030 <puts@plt>
movl   $0xa,0x0(%r13)
mov    0x8(%r14),%rdi
call   1090 <atoi@plt>
imul   $0x3e8,%eax,%edi
call   1100 <usleep@plt>
lea    0xe08(%rip),%rdi
call   1030 <puts@plt>
mov    %r13,%rdi
call   10c0 <wake_futex_blocking@plt>
xor    %edi,%edi
call   10e0 <wait@plt>
jmp    1333 <main+0x153>
lea    0xdfc(%rip),%rdi
call   1030 <puts@plt>
movq   $0x1dcd6500,(%rsp)
1d
mov    %rsp,%r14
lea    0xd94(%rip),%r15
jmp    12df <main+0xff>
nopl   (%rax)
mov    %eax,%ebp
mov    0x2d17(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%r12
xor    %eax,%eax
call   10d0 <time_ns@plt>
sub    %rbx,%rax
mov    %r15,%rdi
mov    %ebp,%esi
mov    %r12,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   10d0 <time_ns@plt>
mov    %rax,%rbx
mov    0x2cf8(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
mov    $0xa,%edx
mov    %r14,%rcx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <futex@plt>
test   %eax,%eax
jne    12b0 <main+0xd0>
xor    %eax,%eax
call   10d0 <time_ns@plt>
sub    %rbx,%rax
mov    %r15,%rdi
xor    %esi,%esi
lea    0xd10(%rip),%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
cmpl   $0xa,0x0(%r13)
jne    12df <main+0xff>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r14),%rsi
lea    0xcb2(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xccd(%rip),%rdi
jmp    136f <main+0x18f>
lea    0xccb(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@.str = [51 x i8] c"Usage: %s <parent wait in ms before waking child>\0A\00"
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@.str.1 = [7 x i8] c"shmget\00"
@.str.2 = [5 x i8] c"fork\00"
@.str.3 = [21 x i8] c"child waiting for A\0A\00"
@__const.main.timeout = %struct.timespec { i32 500000000, i32 0 }
@FUTEX_WAIT = global i32 0
@.str.4 = [45 x i8] c"child woken up rc=%d errno=%s, elapsed=%llu\0A\00"
@errno = global i32 0
@.str.5 = [1 x i8] zeroinitializer
@.str.6 = [17 x i8] c"parent writin A\0A\00"
@.str.7 = [21 x i8] c"parent waking child\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca %struct.timespec
%10 = alloca i64
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i32, i32* @IPC_PRIVATE
%22 = load i32, i32* @IPC_CREAT
%23 = or i32 %22, 438
%24 = call i32 @shmget(i32 %21, i32 4096, i32 %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %20
%31 = load i32, i32* %6
%32 = call i32* @shmat(i32 %31, i32* null, i32 0)
store i32* %32, i32** %7
%33 = load i32*, i32** %7
store i32 0, i32* %33
%34 = call i32 (...) @fork()
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %30
%38 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %30
%41 = load i32, i32* %8
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %73
43: ; preds = %40
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%45 = bitcast %struct.timespec* %9 to i8*
br label %46
46: ; preds = %43, %71
%47 = call i64 (...) @time_ns()
store i64 %47, i64* %10
%48 = load i32*, i32** %7
%49 = load i32, i32* @FUTEX_WAIT
%50 = call i32 @futex(i32* %48, i32 %49, i32 10, %struct.timespec* %9, i32* null, i32 0)
store i32 %50, i32* %11
%51 = load i32, i32* %11
%52 = load i32, i32* %11
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %46
%55 = load i32, i32* @errno
%56 = call i8* @strerror(i32 %55)
br label %58
57: ; preds = %46
br label %58
58: ; preds = %57, %54
%59 = phi i8* [ %56, %54 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.5, i64 0, i64 0), %57 ]
%60 = call i64 (...) @time_ns()
%61 = load i64, i64* %10
%62 = sub i64 %60, %61
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0), i32 %51, i8* %59, i64 %62)
%64 = load i32, i32* %11
%65 = icmp eq i32 %64, 0
br i1 %65, label %66, label %71
66: ; preds = %58
%67 = load i32*, i32** %7
%68 = load i32, i32* %67
%69 = icmp eq i32 %68, 10
br i1 %69, label %70, label %71
70: ; preds = %66
br label %72
71: ; preds = %66, %58
br label %46
72: ; preds = %70
br label %86
73: ; preds = %40
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
%75 = load i32*, i32** %7
store i32 10, i32* %75
%76 = load i8**, i8*** %5
%77 = getelementptr inbounds i8*, i8** %76, i64 1
%78 = load i8*, i8** %77
%79 = call i32 @atoi(i8* %78)
%80 = mul nsw i32 %79, 1000
%81 = call i32 @usleep(i32 %80)
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
%83 = load i32*, i32** %7
%84 = call i32 @wake_futex_blocking(i32* %83)
%85 = call i32 @wait(i32* null)
br label %86
86: ; preds = %73, %72
ret i32 0
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@.str = [51 x i8] c"Usage: %s <parent wait in ms before waking child>\0A\00"
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@.str.1 = [7 x i8] c"shmget\00"
@.str.2 = [5 x i8] c"fork\00"
@FUTEX_WAIT = global i32 0
@.str.4 = [45 x i8] c"child woken up rc=%d errno=%s, elapsed=%llu\0A\00"
@errno = global i32 0
@.str.5 = [1 x i8] zeroinitializer
@str = [16 x i8] c"parent writin A\00"
@str.8 = [20 x i8] c"parent waking child\00"
@str.9 = [20 x i8] c"child waiting for A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to %struct.timespec*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = load i32, i32* @IPC_PRIVATE
%12 = load i32, i32* @IPC_CREAT
%13 = or i32 %12, 438
%14 = tail call i32 @shmget(i32 %11, i32 4096, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %10
%20 = tail call i32* @shmat(i32 %14, i32* null, i32 0)
store i32 0, i32* %20
%21 = tail call i32 (...) @fork()
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %19
%27 = icmp eq i32 %21, 0
br i1 %27, label %28, label %50
28: ; preds = %26
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.9, i64 0, i64 0))
%30 = bitcast i64* %3 to i8*
store i64 500000000, i64* %3
br label %31
31: ; preds = %42, %28
%32 = call i64 (...) @time_ns()
%33 = load i32, i32* @FUTEX_WAIT
%34 = call i32 @futex(i32* %20, i32 %33, i32 10, %struct.timespec* %4, i32* null, i32 0)
%35 = icmp eq i32 %34, 0
br i1 %35, label %43, label %36
36: ; preds = %31
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i64 (...) @time_ns()
%40 = sub i64 %39, %32
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0), i32 %34, i8* %38, i64 %40)
br label %42
42: ; preds = %36, %43
br label %31
43: ; preds = %31
%44 = call i64 (...) @time_ns()
%45 = sub i64 %44, %32
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0), i32 0, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.5, i64 0, i64 0), i64 %45)
%47 = load i32, i32* %20
%48 = icmp eq i32 %47, 10
br i1 %48, label %49, label %42
49: ; preds = %43
br label %60
50: ; preds = %26
%51 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
store i32 10, i32* %20
%52 = getelementptr inbounds i8*, i8** %1, i64 1
%53 = load i8*, i8** %52
%54 = tail call i32 @atoi(i8* %53)
%55 = mul nsw i32 %54, 1000
%56 = tail call i32 @usleep(i32 %55)
%57 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.8, i64 0, i64 0))
%58 = tail call i32 @wake_futex_blocking(i32* %20)
%59 = tail call i32 @wait(i32* null)
br label %60
60: ; preds = %50, %49
ret i32 0
}