#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);
  }
  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);
  }
  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    -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    12a9 <tf+0x129>
lea    0xda3(%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    130a <tf+0x18a>
lea    0xd54(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1070 <pthread_exit@plt>
mov    -0x2c(%rbp),%eax
mov    0x2cc4(%rip),%rcx
cmp    (%rcx),%eax
je     1332 <tf+0x1b2>
lea    0xd46(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1070 <pthread_exit@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    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     1272 <tf+0xf2>
lea    0xdda(%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    12c6 <tf+0x146>
lea    0xd98(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1070 <pthread_exit@plt>
mov    0x2d0b(%rip),%rax
cmp    (%rax),%ebx
je     12e7 <tf+0x167>
lea    0xd91(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1070 <pthread_exit@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 = [18 x i8] c"timeout too short\00"
@.str.3 = [26 x i8] c"2nd timedwrlock succeeded\00"
@EINVAL = global i32 0
@.str.4 = [38 x i8] c"2nd timedwrlock did not return 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 @gettimeofday(%struct.timeval* %7, i32* null)
%48 = call i32 @timersub(%struct.timeval* %7, %struct.timeval* %4, %struct.timeval* %4)
%49 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%50 = load i32, i32* %49
%51 = icmp slt i32 %50, 200000
br i1 %51, label %52, label %55
52: ; preds = %46
%53 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%54 = call i32 @pthread_exit(i8* inttoptr (i64 1 to i8*))
br label %55
55: ; preds = %52, %46
%56 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%57 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %4, %struct.timespec* %5)
%58 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%59 = load i64, i64* %58
%60 = add nsw i64 %59, 10
store i64 %60, i64* %58
%61 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%62 = load i32, i32* %61
%63 = add nsw i32 %62, 1000000000
store i32 %63, i32* %61
%64 = load i32*, i32** %3
%65 = call i32 @pthread_rwlock_timedwrlock(i32* %64, %struct.timespec* %5)
store i32 %65, i32* %6
%66 = load i32, i32* %6
%67 = icmp eq i32 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %55
%69 = call i32 @puts(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%70 = call i32 @pthread_exit(i8* inttoptr (i64 1 to i8*))
br label %71
71: ; preds = %68, %55
%72 = load i32, i32* %6
%73 = load i32, i32* @EINVAL
%74 = icmp ne i32 %72, %73
br i1 %74, label %75, label %78
75: ; preds = %71
%76 = call i32 @puts(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0))
%77 = call i32 @pthread_exit(i8* inttoptr (i64 1 to i8*))
br label %78
78: ; preds = %75, %71
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 = [18 x i8] c"timeout too short\00"
@.str.3 = [26 x i8] c"2nd timedwrlock succeeded\00"
@EINVAL = global i32 0
@.str.4 = [38 x i8] c"2nd timedwrlock did not return 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 = bitcast %struct.timeval* %4 to i8*
%37 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%38 = call i32 @timersub(%struct.timeval* %4, %struct.timeval* %2, %struct.timeval* %2)
%39 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%40 = load i32, i32* %39
%41 = icmp slt i32 %40, 200000
br i1 %41, label %42, label %45
42: ; preds = %35
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%44 = call i32 @pthread_exit(i8* inttoptr (i64 1 to i8*))
br label %45
45: ; preds = %42, %35
%46 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%47 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %2, %struct.timespec* %3)
%48 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%49 = load i64, i64* %48
%50 = add nsw i64 %49, 10
store i64 %50, i64* %48
%51 = load i32, i32* %10
%52 = add nsw i32 %51, 1000000000
store i32 %52, i32* %10
%53 = call i32 @pthread_rwlock_timedwrlock(i32* %5, %struct.timespec* %3)
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %45
%56 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%57 = call i32 @pthread_exit(i8* inttoptr (i64 1 to i8*))
br label %58
58: ; preds = %55, %45
%59 = load i32, i32* @EINVAL
%60 = icmp eq i32 %53, %59
br i1 %60, label %64, label %61
61: ; preds = %58
%62 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0))
%63 = call i32 @pthread_exit(i8* inttoptr (i64 1 to i8*))
br label %64
64: ; preds = %61, %58
ret i8* null
}