#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_timedrdlock(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;
  }
  puts("child calling timedrdlock");
  int err = pthread_rwlock_timedrdlock(r, &ts);
  if (err == 0) {
    puts("rwlock_timedrdlock 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("1st child timedrdlock done");
  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_timedrdlock(r, &ts);
  if (err == 0) {
    puts("2nd timedrdlock succeeded");
    pthread_exit((void *)1l);
  }
  if (err != EINVAL) {
    puts("2nd timedrdlock did not return EINVAL");
    pthread_exit((void *)1l);
  }
  puts("2nd child timedrdlock done");
  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   1050 <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)
lea    0xe1c(%rip),%rdi
call   1030 <puts@plt>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1070 <pthread_rwlock_timedrdlock@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1219 <tf+0x99>
lea    0xe10(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <pthread_exit@plt>
mov    -0x2c(%rbp),%eax
mov    0x2dbd(%rip),%rcx
cmp    (%rcx),%eax
je     1275 <tf+0xf5>
mov    -0x2c(%rbp),%edi
call   1090 <strerror@plt>
mov    %rax,-0x40(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    -0x34(%rbp),%edx
mov    %rax,%rcx
mov    0x2d84(%rip),%rax
mov    (%rax),%r8d
lea    0xdd0(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <pthread_exit@plt>
lea    0xddb(%rip),%rdi
call   1030 <puts@plt>
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
lea    -0x30(%rbp),%rdi
lea    -0x18(%rbp),%rdx
mov    %rdx,%rsi
call   1080 <timersub@plt>
cmpl   $0x30d40,-0x18(%rbp)
jge    12c1 <tf+0x141>
lea    0xdc0(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <pthread_exit@plt>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <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   1070 <pthread_rwlock_timedrdlock@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1322 <tf+0x1a2>
lea    0xd71(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <pthread_exit@plt>
mov    -0x2c(%rbp),%eax
mov    0x2cac(%rip),%rcx
cmp    (%rcx),%eax
je     134a <tf+0x1ca>
lea    0xd63(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <pthread_exit@plt>
lea    0xd73(%rip),%rdi
call   1030 <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   1050 <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    0xe2c(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
call   1070 <pthread_rwlock_timedrdlock@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1202 <tf+0x82>
lea    0xe27(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <pthread_exit@plt>
mov    0x2dd7(%rip),%rbx
cmp    (%rbx),%ebp
je     123f <tf+0xbf>
mov    %ebp,%edi
call   1090 <strerror@plt>
mov    %rax,%r15
mov    (%rbx),%edi
call   1090 <strerror@plt>
mov    (%rbx),%r8d
lea    0xe0e(%rip),%rdi
mov    %r15,%rsi
mov    %ebp,%edx
mov    %rax,%rcx
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <pthread_exit@plt>
lea    0xe11(%rip),%rdi
call   1030 <puts@plt>
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
lea    0x8(%rsp),%rdx
mov    %rbx,%rdi
mov    %rdx,%rsi
call   1080 <timersub@plt>
cmpl   $0x30d3f,0x8(%rsp)
jg     128a <tf+0x10a>
lea    0xdf7(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <pthread_exit@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <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   1070 <pthread_rwlock_timedrdlock@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    12de <tf+0x15e>
lea    0xdb5(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <pthread_exit@plt>
mov    0x2cf3(%rip),%rax
cmp    (%rax),%ebx
je     12ff <tf+0x17f>
lea    0xdae(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <pthread_exit@plt>
lea    0xdbe(%rip),%rdi
call   1030 <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 = [26 x i8] c"child calling timedrdlock\00"
@.str.1 = [28 x i8] c"rwlock_timedrdlock returned\00"
@ETIMEDOUT = global i32 0
@.str.2 = [33 x i8] c"err = %s (%d), expected %s (%d)\0A\00"
@.str.3 = [27 x i8] c"1st child timedrdlock done\00"
@.str.4 = [18 x i8] c"timeout too short\00"
@.str.5 = [26 x i8] c"2nd timedrdlock succeeded\00"
@EINVAL = global i32 0
@.str.6 = [38 x i8] c"2nd timedrdlock did not return EINVAL\00"
@.str.7 = [27 x i8] c"2nd child timedrdlock done\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 = call i32 @puts(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%27 = load i32*, i32** %3
%28 = call i32 @pthread_rwlock_timedrdlock(i32* %27, %struct.timespec* %5)
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %25
%32 = call i32 @puts(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @pthread_exit(i8* inttoptr (i64 1 to i8*))
br label %34
34: ; preds = %31, %25
%35 = load i32, i32* %6
%36 = load i32, i32* @ETIMEDOUT
%37 = icmp ne i32 %35, %36
br i1 %37, label %38, label %47
38: ; preds = %34
%39 = load i32, i32* %6
%40 = call i8* @strerror(i32 %39)
%41 = load i32, i32* %6
%42 = load i32, i32* @ETIMEDOUT
%43 = call i8* @strerror(i32 %42)
%44 = load i32, i32* @ETIMEDOUT
%45 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %40, i32 %41, i8* %43, i32 %44)
%46 = call i32 @pthread_exit(i8* inttoptr (i64 1 to i8*))
br label %47
47: ; preds = %38, %34
%48 = call i32 @puts(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%49 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%50 = call i32 @timersub(%struct.timeval* %7, %struct.timeval* %4, %struct.timeval* %4)
%51 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%52 = load i32, i32* %51
%53 = icmp slt i32 %52, 200000
br i1 %53, label %54, label %57
54: ; preds = %47
%55 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%56 = call i32 @pthread_exit(i8* inttoptr (i64 1 to i8*))
br label %57
57: ; preds = %54, %47
%58 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%59 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %4, %struct.timespec* %5)
%60 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%61 = load i64, i64* %60
%62 = add nsw i64 %61, 10
store i64 %62, i64* %60
%63 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%64 = load i32, i32* %63
%65 = add nsw i32 %64, 1000000000
store i32 %65, i32* %63
%66 = load i32*, i32** %3
%67 = call i32 @pthread_rwlock_timedrdlock(i32* %66, %struct.timespec* %5)
store i32 %67, i32* %6
%68 = load i32, i32* %6
%69 = icmp eq i32 %68, 0
br i1 %69, label %70, label %73
70: ; preds = %57
%71 = call i32 @puts(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
%72 = call i32 @pthread_exit(i8* inttoptr (i64 1 to i8*))
br label %73
73: ; preds = %70, %57
%74 = load i32, i32* %6
%75 = load i32, i32* @EINVAL
%76 = icmp ne i32 %74, %75
br i1 %76, label %77, label %80
77: ; preds = %73
%78 = call i32 @puts(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0))
%79 = call i32 @pthread_exit(i8* inttoptr (i64 1 to i8*))
br label %80
80: ; preds = %77, %73
%81 = call i32 @puts(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0))
ret i8* null
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
%struct.timespec = type { i32, i64 }
@.str = [26 x i8] c"child calling timedrdlock\00"
@.str.1 = [28 x i8] c"rwlock_timedrdlock returned\00"
@ETIMEDOUT = global i32 0
@.str.2 = [33 x i8] c"err = %s (%d), expected %s (%d)\0A\00"
@.str.3 = [27 x i8] c"1st child timedrdlock done\00"
@.str.4 = [18 x i8] c"timeout too short\00"
@.str.5 = [26 x i8] c"2nd timedrdlock succeeded\00"
@EINVAL = global i32 0
@.str.6 = [38 x i8] c"2nd timedrdlock did not return EINVAL\00"
@.str.7 = [27 x i8] c"2nd child timedrdlock done\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 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @pthread_rwlock_timedrdlock(i32* %5, %struct.timespec* %3)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @pthread_exit(i8* inttoptr (i64 1 to i8*))
br label %26
26: ; preds = %23, %19
%27 = load i32, i32* @ETIMEDOUT
%28 = icmp eq i32 %21, %27
br i1 %28, label %36, label %29
29: ; preds = %26
%30 = call i8* @strerror(i32 %21)
%31 = load i32, i32* @ETIMEDOUT
%32 = call i8* @strerror(i32 %31)
%33 = load i32, i32* @ETIMEDOUT
%34 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %30, i32 %21, i8* %32, i32 %33)
%35 = call i32 @pthread_exit(i8* inttoptr (i64 1 to i8*))
br label %36
36: ; preds = %29, %26
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%38 = bitcast %struct.timeval* %4 to i8*
%39 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%40 = call i32 @timersub(%struct.timeval* %4, %struct.timeval* %2, %struct.timeval* %2)
%41 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%42 = load i32, i32* %41
%43 = icmp slt i32 %42, 200000
br i1 %43, label %44, label %47
44: ; preds = %36
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%46 = call i32 @pthread_exit(i8* inttoptr (i64 1 to i8*))
br label %47
47: ; preds = %44, %36
%48 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%49 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %2, %struct.timespec* %3)
%50 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%51 = load i64, i64* %50
%52 = add nsw i64 %51, 10
store i64 %52, i64* %50
%53 = load i32, i32* %10
%54 = add nsw i32 %53, 1000000000
store i32 %54, i32* %10
%55 = call i32 @pthread_rwlock_timedrdlock(i32* %5, %struct.timespec* %3)
%56 = icmp eq i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %47
%58 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
%59 = call i32 @pthread_exit(i8* inttoptr (i64 1 to i8*))
br label %60
60: ; preds = %57, %47
%61 = load i32, i32* @EINVAL
%62 = icmp eq i32 %55, %61
br i1 %62, label %66, label %63
63: ; preds = %60
%64 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0))
%65 = call i32 @pthread_exit(i8* inttoptr (i64 1 to i8*))
br label %66
66: ; preds = %63, %60
%67 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0))
ret i8* null
}