#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 void (*routine)(void *);
typedef int /*<<< orphan*/ pthread_mutex_t;
typedef int /*<<< orphan*/ pthread_cond_t;

/* Variables and functions */
int /*<<< orphan*/ pthread_cleanup_pop(int /*<<< orphan*/);
int /*<<< orphan*/ pthread_cleanup_push(void (*)(void *), void *);
int pthread_cond_timedwait(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                           struct timespec *);
scalar_t__ pthread_mutex_unlock;
int cond_timedwait_and_unlock_if_cancelled(pthread_cond_t *cond,
                                           pthread_mutex_t *mutex,
                                           struct timespec *abstime) {
  typedef void (*routine)(void *);
  routine r = (routine)pthread_mutex_unlock;
  int err;
  pthread_cleanup_push(r, (void *)mutex);
  err = pthread_cond_timedwait(cond, mutex, abstime);
  pthread_cleanup_pop(0);
  return err;
}


x86 assembly (clang, -O0):
<cond_timedwait_and_unlock_if_cancelled>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e95(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <pthread_cleanup_push@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <pthread_cond_timedwait@plt>
mov    %eax,-0x24(%rbp)
xor    %edi,%edi
call   1030 <pthread_cleanup_pop@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cond_timedwait_and_unlock_if_cancelled>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
mov    0x2e9b(%rip),%rax
mov    (%rax),%rdi
call   1040 <pthread_cleanup_push@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1050 <pthread_cond_timedwait@plt>
mov    %eax,%ebx
xor    %edi,%edi
call   1030 <pthread_cleanup_pop@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
@pthread_mutex_unlock = global i64 0
define i32 @cond_timedwait_and_unlock_if_cancelled(i32* %0, i32* %1, %struct.timespec* %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca %struct.timespec*
%7 = alloca void (i8*)*
%8 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store %struct.timespec* %2, %struct.timespec** %6
%9 = load i64, i64* @pthread_mutex_unlock
%10 = inttoptr i64 %9 to void (i8*)*
store void (i8*)* %10, void (i8*)** %7
%11 = load void (i8*)*, void (i8*)** %7
%12 = load i32*, i32** %5
%13 = bitcast i32* %12 to i8*
%14 = call i32 @pthread_cleanup_push(void (i8*)* %11, i8* %13)
%15 = load i32*, i32** %4
%16 = load i32*, i32** %5
%17 = load %struct.timespec*, %struct.timespec** %6
%18 = call i32 @pthread_cond_timedwait(i32* %15, i32* %16, %struct.timespec* %17)
store i32 %18, i32* %8
%19 = call i32 @pthread_cleanup_pop(i32 0)
%20 = load i32, i32* %8
ret i32 %20
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@pthread_mutex_unlock = global i64 0
define i32 @cond_timedwait_and_unlock_if_cancelled(i32* %0, i32* %1, %struct.timespec* %2) {
%4 = load i64, i64* @pthread_mutex_unlock
%5 = inttoptr i64 %4 to void (i8*)*
%6 = bitcast i32* %1 to i8*
%7 = tail call i32 @pthread_cleanup_push(void (i8*)* %5, i8* %6)
%8 = tail call i32 @pthread_cond_timedwait(i32* %0, i32* %1, %struct.timespec* %2)
%9 = tail call i32 @pthread_cleanup_pop(i32 0)
ret i32 %8
}