#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 timeval {
  int tv_usec;
};
struct timespec {
  int tv_nsec;
  scalar_t__ tv_sec;
};
typedef int /*<<< orphan*/ pthread_rwlock_t;

/* Variables and functions */
int EINVAL;
int ETIMEDOUT;
int /*<<< orphan*/ TIMEVAL_TO_TIMESPEC(struct timeval *, struct timespec *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *, int, char *, int);
int /*<<< orphan*/ pthread_exit(void *);
int pthread_rwlock_timedwrlock(int /*<<< orphan*/ *, struct timespec *);
int /*<<< orphan*/ puts(char *);
char *strerror(int);
int /*<<< orphan*/ timersub(struct timeval *, struct timeval *,
                            struct timeval *);
void *tf(void *arg) {
  pthread_rwlock_t *r = arg;
  struct timeval tv;
  (void)gettimeofday(&tv, NULL);
  struct timespec ts;
  TIMEVAL_TO_TIMESPEC(&tv, &ts);
  ts.tv_nsec += 300000000;
  if (ts.tv_nsec >= 1000000000) {
    ts.tv_nsec -= 1000000000;
    ++ts.tv_sec;
  }
  int err = pthread_rwlock_timedwrlock(r, &ts);
  if (err == 0) {
    puts("rwlock_timedwrlock returned");
    pthread_exit((void *)1l);
  }
  if (err != ETIMEDOUT) {
    printf("err = %s (%d), expected %s (%d)\n", strerror(err), err,
           strerror(ETIMEDOUT), ETIMEDOUT);
    pthread_exit((void *)1l);
  }
  puts("child: timedwrlock failed with ETIMEDOUT");
  struct timeval tv2;
  (void)gettimeofday(&tv2, NULL);
  timersub(&tv2, &tv, &tv);
  if (tv.tv_usec < 200000) {
    puts("timeout too short");
    pthread_exit((void *)1l);
  }
  (void)gettimeofday(&tv, NULL);
  TIMEVAL_TO_TIMESPEC(&tv, &ts);
  ts.tv_sec += 10;
  ts.tv_nsec += 1000000000;
  err = pthread_rwlock_timedwrlock(r, &ts);
  if (err == 0) {
    puts("2nd timedwrlock succeeded");
    pthread_exit((void *)1l);
  }
  if (err != EINVAL) {
    puts("2nd timedwrlock did not return EINVAL");
    pthread_exit((void *)1l);
  }
  puts("child: timedwrlock failed with EINVAL");
  return NULL;
}


x86 assembly (clang, -O0):
<tf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
lea    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10a0 <TIMEVAL_TO_TIMESPEC@plt>
mov    -0x28(%rbp),%eax
add    $0x11e1a300,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x3b9aca00,-0x28(%rbp)
jl     11dd <tf+0x5d>
mov    -0x28(%rbp),%eax
sub    $0x3b9aca00,%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <pthread_rwlock_timedwrlock@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    120d <tf+0x8d>
lea    0xe02(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1070 <pthread_exit@plt>
mov    -0x2c(%rbp),%eax
mov    0x2dc9(%rip),%rcx
cmp    (%rcx),%eax
je     1269 <tf+0xe9>
mov    -0x2c(%rbp),%edi
call   1090 <strerror@plt>
mov    %rax,-0x40(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    -0x34(%rbp),%edx
mov    %rax,%rcx
mov    0x2d90(%rip),%rax
mov    (%rax),%r8d
lea    0xdc2(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1070 <pthread_exit@plt>
lea    0xdcd(%rip),%rdi
call   1040 <puts@plt>
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
lea    -0x30(%rbp),%rdi
lea    -0x18(%rbp),%rdx
mov    %rdx,%rsi
call   1080 <timersub@plt>
cmpl   $0x30d40,-0x18(%rbp)
jge    12b5 <tf+0x135>
lea    0xdc0(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1070 <pthread_exit@plt>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
lea    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10a0 <TIMEVAL_TO_TIMESPEC@plt>
mov    -0x20(%rbp),%rax
add    $0xa,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%eax
add    $0x3b9aca00,%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <pthread_rwlock_timedwrlock@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1316 <tf+0x196>
lea    0xd71(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1070 <pthread_exit@plt>
mov    -0x2c(%rbp),%eax
mov    0x2cb8(%rip),%rcx
cmp    (%rcx),%eax
je     133e <tf+0x1be>
lea    0xd63(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1070 <pthread_exit@plt>
lea    0xd73(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
lea    0x10(%rsp),%rsi
mov    %rbp,%rdi
call   10a0 <TIMEVAL_TO_TIMESPEC@plt>
mov    0x10(%rsp),%eax
lea    0x11e1a300(%rax),%ecx
mov    %ecx,0x10(%rsp)
cmp    $0x29b92700,%eax
jl     11cd <tf+0x4d>
add    $0xd646d900,%eax
mov    %eax,0x10(%rsp)
addq   $0x1,0x18(%rsp)
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
call   1030 <pthread_rwlock_timedwrlock@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    11f6 <tf+0x76>
lea    0xe19(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1070 <pthread_exit@plt>
mov    0x2de3(%rip),%rbx
cmp    (%rbx),%ebp
je     1233 <tf+0xb3>
mov    %ebp,%edi
call   1090 <strerror@plt>
mov    %rax,%r15
mov    (%rbx),%edi
call   1090 <strerror@plt>
mov    (%rbx),%r8d
lea    0xe00(%rip),%rdi
mov    %r15,%rsi
mov    %ebp,%edx
mov    %rax,%rcx
call   1050 <printf@plt>
mov    $0x1,%edi
call   1070 <pthread_exit@plt>
lea    0xe03(%rip),%rdi
call   1040 <puts@plt>
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
lea    0x8(%rsp),%rdx
mov    %rbx,%rdi
mov    %rdx,%rsi
call   1080 <timersub@plt>
cmpl   $0x30d3f,0x8(%rsp)
jg     127e <tf+0xfe>
lea    0xdf7(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1070 <pthread_exit@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
lea    0x10(%rsp),%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
call   10a0 <TIMEVAL_TO_TIMESPEC@plt>
addq   $0xa,0x18(%rsp)
addl   $0x3b9aca00,0x10(%rsp)
3b
mov    %r14,%rdi
mov    %rbp,%rsi
call   1030 <pthread_rwlock_timedwrlock@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    12d2 <tf+0x152>
lea    0xdb5(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1070 <pthread_exit@plt>
mov    0x2cff(%rip),%rax
cmp    (%rax),%ebx
je     12f3 <tf+0x173>
lea    0xdae(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1070 <pthread_exit@plt>
lea    0xdbe(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
%struct.timespec = type { i32, i64 }
@.str = [28 x i8] c"rwlock_timedwrlock returned\00"
@ETIMEDOUT = global i32 0
@.str.1 = [33 x i8] c"err = %s (%d), expected %s (%d)\0A\00"
@.str.2 = [41 x i8] c"child: timedwrlock failed with ETIMEDOUT\00"
@.str.3 = [18 x i8] c"timeout too short\00"
@.str.4 = [26 x i8] c"2nd timedwrlock succeeded\00"
@EINVAL = global i32 0
@.str.5 = [38 x i8] c"2nd timedwrlock did not return EINVAL\00"
@.str.6 = [38 x i8] c"child: timedwrlock failed with EINVAL\00"
define i8* @tf(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.timeval
%5 = alloca %struct.timespec
%6 = alloca i32
%7 = alloca %struct.timeval
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = bitcast i8* %8 to i32*
store i32* %9, i32** %3
%10 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%11 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %4, %struct.timespec* %5)
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%13 = load i32, i32* %12
%14 = add nsw i32 %13, 300000000
store i32 %14, i32* %12
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%16 = load i32, i32* %15
%17 = icmp sge i32 %16, 1000000000
br i1 %17, label %18, label %25
18: ; preds = %1
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%20 = load i32, i32* %19
%21 = sub nsw i32 %20, 1000000000
store i32 %21, i32* %19
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%23 = load i64, i64* %22
%24 = add nsw i64 %23, 1
store i64 %24, i64* %22
br label %25
25: ; preds = %18, %1
%26 = load i32*, i32** %3
%27 = call i32 @pthread_rwlock_timedwrlock(i32* %26, %struct.timespec* %5)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = call i32 @puts(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%32 = call i32 @pthread_exit(i8* inttoptr (i64 1 to i8*))
br label %33
33: ; preds = %30, %25
%34 = load i32, i32* %6
%35 = load i32, i32* @ETIMEDOUT
%36 = icmp ne i32 %34, %35
br i1 %36, label %37, label %46
37: ; preds = %33
%38 = load i32, i32* %6
%39 = call i8* @strerror(i32 %38)
%40 = load i32, i32* %6
%41 = load i32, i32* @ETIMEDOUT
%42 = call i8* @strerror(i32 %41)
%43 = load i32, i32* @ETIMEDOUT
%44 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %39, i32 %40, i8* %42, i32 %43)
%45 = call i32 @pthread_exit(i8* inttoptr (i64 1 to i8*))
br label %46
46: ; preds = %37, %33
%47 = call i32 @puts(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%48 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%49 = call i32 @timersub(%struct.timeval* %7, %struct.timeval* %4, %struct.timeval* %4)
%50 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%51 = load i32, i32* %50
%52 = icmp slt i32 %51, 200000
br i1 %52, label %53, label %56
53: ; preds = %46
%54 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%55 = call i32 @pthread_exit(i8* inttoptr (i64 1 to i8*))
br label %56
56: ; preds = %53, %46
%57 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%58 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %4, %struct.timespec* %5)
%59 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%60 = load i64, i64* %59
%61 = add nsw i64 %60, 10
store i64 %61, i64* %59
%62 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%63 = load i32, i32* %62
%64 = add nsw i32 %63, 1000000000
store i32 %64, i32* %62
%65 = load i32*, i32** %3
%66 = call i32 @pthread_rwlock_timedwrlock(i32* %65, %struct.timespec* %5)
store i32 %66, i32* %6
%67 = load i32, i32* %6
%68 = icmp eq i32 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %56
%70 = call i32 @puts(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%71 = call i32 @pthread_exit(i8* inttoptr (i64 1 to i8*))
br label %72
72: ; preds = %69, %56
%73 = load i32, i32* %6
%74 = load i32, i32* @EINVAL
%75 = icmp ne i32 %73, %74
br i1 %75, label %76, label %79
76: ; preds = %72
%77 = call i32 @puts(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0))
%78 = call i32 @pthread_exit(i8* inttoptr (i64 1 to i8*))
br label %79
79: ; preds = %76, %72
%80 = call i32 @puts(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0))
ret i8* null
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
%struct.timespec = type { i32, i64 }
@.str = [28 x i8] c"rwlock_timedwrlock returned\00"
@ETIMEDOUT = global i32 0
@.str.1 = [33 x i8] c"err = %s (%d), expected %s (%d)\0A\00"
@.str.2 = [41 x i8] c"child: timedwrlock failed with ETIMEDOUT\00"
@.str.3 = [18 x i8] c"timeout too short\00"
@.str.4 = [26 x i8] c"2nd timedwrlock succeeded\00"
@EINVAL = global i32 0
@.str.5 = [38 x i8] c"2nd timedwrlock did not return EINVAL\00"
@.str.6 = [38 x i8] c"child: timedwrlock failed with EINVAL\00"
define noalias i8* @tf(i8* %0) {
%2 = alloca %struct.timeval
%3 = alloca %struct.timespec
%4 = alloca %struct.timeval
%5 = bitcast i8* %0 to i32*
%6 = bitcast %struct.timeval* %2 to i8*
%7 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%8 = bitcast %struct.timespec* %3 to i8*
%9 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %2, %struct.timespec* %3)
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%11 = load i32, i32* %10
%12 = add nsw i32 %11, 300000000
store i32 %12, i32* %10
%13 = icmp sgt i32 %11, 699999999
br i1 %13, label %14, label %19
14: ; preds = %1
%15 = add nsw i32 %11, -700000000
store i32 %15, i32* %10
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%17 = load i64, i64* %16
%18 = add nsw i64 %17, 1
store i64 %18, i64* %16
br label %19
19: ; preds = %14, %1
%20 = call i32 @pthread_rwlock_timedwrlock(i32* %5, %struct.timespec* %3)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @pthread_exit(i8* inttoptr (i64 1 to i8*))
br label %25
25: ; preds = %22, %19
%26 = load i32, i32* @ETIMEDOUT
%27 = icmp eq i32 %20, %26
br i1 %27, label %35, label %28
28: ; preds = %25
%29 = call i8* @strerror(i32 %20)
%30 = load i32, i32* @ETIMEDOUT
%31 = call i8* @strerror(i32 %30)
%32 = load i32, i32* @ETIMEDOUT
%33 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %29, i32 %20, i8* %31, i32 %32)
%34 = call i32 @pthread_exit(i8* inttoptr (i64 1 to i8*))
br label %35
35: ; preds = %28, %25
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%37 = bitcast %struct.timeval* %4 to i8*
%38 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%39 = call i32 @timersub(%struct.timeval* %4, %struct.timeval* %2, %struct.timeval* %2)
%40 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%41 = load i32, i32* %40
%42 = icmp slt i32 %41, 200000
br i1 %42, label %43, label %46
43: ; preds = %35
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%45 = call i32 @pthread_exit(i8* inttoptr (i64 1 to i8*))
br label %46
46: ; preds = %43, %35
%47 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%48 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %2, %struct.timespec* %3)
%49 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%50 = load i64, i64* %49
%51 = add nsw i64 %50, 10
store i64 %51, i64* %49
%52 = load i32, i32* %10
%53 = add nsw i32 %52, 1000000000
store i32 %53, i32* %10
%54 = call i32 @pthread_rwlock_timedwrlock(i32* %5, %struct.timespec* %3)
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %46
%57 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%58 = call i32 @pthread_exit(i8* inttoptr (i64 1 to i8*))
br label %59
59: ; preds = %56, %46
%60 = load i32, i32* @EINVAL
%61 = icmp eq i32 %54, %60
br i1 %61, label %65, label %62
62: ; preds = %59
%63 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0))
%64 = call i32 @pthread_exit(i8* inttoptr (i64 1 to i8*))
br label %65
65: ; preds = %62, %59
%66 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0))
ret i8* null
}