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

/* Variables and functions */
int PTHREAD_CANCEL_DISABLE;
int do_wait(int volatile *, int, int /*<<< orphan*/, struct timespec const *,
            int);
int /*<<< orphan*/ pthread_cleanup_pop(int /*<<< orphan*/);
int /*<<< orphan*/ pthread_cleanup_push(void (*)(void *), void *);
int /*<<< orphan*/ pthread_setcancelstate(int, int *);
int __timedwait(volatile int *addr, int val, clockid_t clk,
                const struct timespec *at, void (*cleanup)(void *), void *arg,
                int priv) {
  int r, cs;
  if (!cleanup)
    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
  pthread_cleanup_push(cleanup, arg);
  r = do_wait(addr, val, clk, at, priv);
  pthread_cleanup_pop(0);
  if (!cleanup)
    pthread_setcancelstate(cs, 0);
  return r;
}


x86 assembly (clang, -O0):
<__timedwait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x10(%rbp),%eax
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117e <__timedwait+0x3e>
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1040 <pthread_setcancelstate@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <pthread_cleanup_push@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    0x10(%rbp),%r8d
call   1060 <do_wait@plt>
mov    %eax,-0x2c(%rbp)
xor    %edi,%edi
call   1030 <pthread_cleanup_pop@plt>
cmpq   $0x0,-0x20(%rbp)
jne    11c3 <__timedwait+0x83>
mov    -0x30(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <pthread_setcancelstate@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__timedwait>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,%rbp
mov    %r8,%rbx
mov    %rcx,%r14
mov    %edx,%r12d
mov    %esi,%r13d
mov    %rdi,%r15
test   %r8,%r8
jne    1175 <__timedwait+0x35>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
lea    0x4(%rsp),%rsi
call   1040 <pthread_setcancelstate@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1050 <pthread_cleanup_push@plt>
mov    %r15,%rdi
mov    %r13d,%esi
mov    %r12d,%edx
mov    %r14,%rcx
mov    0x40(%rsp),%r8d
call   1060 <do_wait@plt>
mov    %eax,%ebp
xor    %edi,%edi
call   1030 <pthread_cleanup_pop@plt>
test   %rbx,%rbx
jne    11af <__timedwait+0x6f>
mov    0x4(%rsp),%edi
xor    %esi,%esi
call   1040 <pthread_setcancelstate@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
@PTHREAD_CANCEL_DISABLE = global i32 0
define i32 @__timedwait(i32* %0, i32 %1, i32 %2, %struct.timespec* %3, void (i8*)* %4, i8* %5, i32 %6) {
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.timespec*
%12 = alloca void (i8*)*
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i32* %0, i32** %8
store i32 %1, i32* %9
store i32 %2, i32* %10
store %struct.timespec* %3, %struct.timespec** %11
store void (i8*)* %4, void (i8*)** %12
store i8* %5, i8** %13
store i32 %6, i32* %14
%17 = load void (i8*)*, void (i8*)** %12
%18 = icmp ne void (i8*)* %17, null
br i1 %18, label %22, label %19
19: ; preds = %7
%20 = load i32, i32* @PTHREAD_CANCEL_DISABLE
%21 = call i32 @pthread_setcancelstate(i32 %20, i32* %16)
br label %22
22: ; preds = %19, %7
%23 = load void (i8*)*, void (i8*)** %12
%24 = load i8*, i8** %13
%25 = call i32 @pthread_cleanup_push(void (i8*)* %23, i8* %24)
%26 = load i32*, i32** %8
%27 = load i32, i32* %9
%28 = load i32, i32* %10
%29 = load %struct.timespec*, %struct.timespec** %11
%30 = load i32, i32* %14
%31 = call i32 @do_wait(i32* %26, i32 %27, i32 %28, %struct.timespec* %29, i32 %30)
store i32 %31, i32* %15
%32 = call i32 @pthread_cleanup_pop(i32 0)
%33 = load void (i8*)*, void (i8*)** %12
%34 = icmp ne void (i8*)* %33, null
br i1 %34, label %38, label %35
35: ; preds = %22
%36 = load i32, i32* %16
%37 = call i32 @pthread_setcancelstate(i32 %36, i32* null)
br label %38
38: ; preds = %35, %22
%39 = load i32, i32* %15
ret i32 %39
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@PTHREAD_CANCEL_DISABLE = global i32 0
define i32 @__timedwait(i32* %0, i32 %1, i32 %2, %struct.timespec* %3, void (i8*)* %4, i8* %5, i32 %6) {
%8 = alloca i32
%9 = bitcast i32* %8 to i8*
%10 = icmp eq void (i8*)* %4, null
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = load i32, i32* @PTHREAD_CANCEL_DISABLE
%13 = call i32 @pthread_setcancelstate(i32 %12, i32* %8)
br label %14
14: ; preds = %11, %7
%15 = call i32 @pthread_cleanup_push(void (i8*)* %4, i8* %5)
%16 = call i32 @do_wait(i32* %0, i32 %1, i32 %2, %struct.timespec* %3, i32 %6)
%17 = call i32 @pthread_cleanup_pop(i32 0)
br i1 %10, label %18, label %21
18: ; preds = %14
%19 = load i32, i32* %8
%20 = call i32 @pthread_setcancelstate(i32 %19, i32* null)
br label %21
21: ; preds = %18, %14
ret i32 %16
}