#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 dummy;
};
typedef int /*<<< orphan*/ pthread_mutex_t;
typedef int /*<<< orphan*/ pthread_cond_t;
typedef int /*<<< orphan*/ mtx_t;
typedef int /*<<< orphan*/ cnd_t;

/* Variables and functions */
#define ETIMEDOUT 128
int __pthread_cond_timedwait(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                             struct timespec const *);
int thrd_error;
int thrd_success;
int thrd_timedout;
int cnd_timedwait(cnd_t *restrict c, mtx_t *restrict m,
                  const struct timespec *restrict ts) {
  int ret =
      __pthread_cond_timedwait((pthread_cond_t *)c, (pthread_mutex_t *)m, ts);
  switch (ret) {
  default:
    return thrd_error;
  case 0:
    return thrd_success;
  case ETIMEDOUT:
    return thrd_timedout;
  }
}


x86 assembly (clang, -O0):
<cnd_timedwait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <__pthread_cond_timedwait@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
test   %eax,%eax
je     116f <cnd_timedwait+0x5f>
jmp    114b <cnd_timedwait+0x3b>
mov    -0x28(%rbp),%eax
sub    $0x80,%eax
je     1180 <cnd_timedwait+0x70>
jmp    115e <cnd_timedwait+0x4e>
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    118c <cnd_timedwait+0x7c>
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    118c <cnd_timedwait+0x7c>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cnd_timedwait>:
push   %rax
call   1030 <__pthread_cond_timedwait@plt>
cmp    $0x80,%eax
jne    112a <cnd_timedwait+0x1a>
mov    0x2eb4(%rip),%rcx
test   %eax,%eax
je     1135 <cnd_timedwait+0x25>
jmp    113c <cnd_timedwait+0x2c>
mov    0x2e9f(%rip),%rcx
test   %eax,%eax
jne    113c <cnd_timedwait+0x2c>
mov    0x2eac(%rip),%rcx
mov    (%rcx),%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
@thrd_error = global i32 0
@thrd_success = global i32 0
@thrd_timedout = global i32 0
define i32 @cnd_timedwait(i32* noalias %0, i32* noalias %1, %struct.timespec* noalias %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca %struct.timespec*
%8 = alloca i32
store i32* %0, i32** %5
store i32* %1, i32** %6
store %struct.timespec* %2, %struct.timespec** %7
%9 = load i32*, i32** %5
%10 = load i32*, i32** %6
%11 = load %struct.timespec*, %struct.timespec** %7
%12 = call i32 @__pthread_cond_timedwait(i32* %9, i32* %10, %struct.timespec* %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
switch i32 %13, label %14 [
i32 0, label %16
i32 128, label %18
]
14: ; preds = %3
%15 = load i32, i32* @thrd_error
store i32 %15, i32* %4
br label %20
16: ; preds = %3
%17 = load i32, i32* @thrd_success
store i32 %17, i32* %4
br label %20
18: ; preds = %3
%19 = load i32, i32* @thrd_timedout
store i32 %19, i32* %4
br label %20
20: ; preds = %18, %16, %14
%21 = load i32, i32* %4
ret i32 %21
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@thrd_error = global i32 0
@thrd_success = global i32 0
@thrd_timedout = global i32 0
define i32 @cnd_timedwait(i32* noalias %0, i32* noalias %1, %struct.timespec* noalias %2) {
%4 = tail call i32 @__pthread_cond_timedwait(i32* %0, i32* %1, %struct.timespec* %2)
%5 = icmp eq i32 %4, 128
%6 = icmp eq i32 %4, 0
%7 = load i32, i32* @thrd_success
%8 = load i32, i32* @thrd_timedout
%9 = load i32, i32* @thrd_error
%10 = select i1 %5, i32 %8, i32 %9
%11 = select i1 %6, i32 %7, i32 %10
ret i32 %11
}