#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 */
typedef int /*<<< orphan*/ pthread_rwlock_t;

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ pthread_rwlock_destroy(int /*<<< orphan*/ *);
int pthread_rwlock_trywrlock(int /*<<< orphan*/ *);
scalar_t__ pthread_rwlock_unlock(int /*<<< orphan*/ *);
int main(void) {
  static pthread_rwlock_t rwlock;
  int rc;
  rc = pthread_rwlock_trywrlock(&rwlock);
  if (pthread_rwlock_unlock(&rwlock) != 0) {
    printf("main: Error at pthread_rwlock_unlock()\n");
    exit(2);
  }
  if (pthread_rwlock_destroy(&rwlock) != 0) {
    printf("Error at pthread_rwlock_destroy()\n");
    return 2;
  }
  if (rc != 0) {
    if (rc == EINVAL) {
      printf("Test PASSED\n");
      return 0;
    } else {
      printf("Test FAILED: Incorrect return code %d\n", rc);
      return 1;
    }
  }
  printf("Test PASSED: Note*: Returned 0 instead of EINVAL, but standard "
         "specified _may_ fail. \n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0x2ee6(%rip),%rdi
call   1050 <pthread_rwlock_trywrlock@plt>
mov    %eax,-0x8(%rbp)
lea    0x2ed7(%rip),%rdi
call   1060 <pthread_rwlock_unlock@plt>
cmp    $0x0,%rax
je     119c <main+0x4c>
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
lea    0x2ea9(%rip),%rdi
call   1030 <pthread_rwlock_destroy@plt>
cmp    $0x0,%rax
je     11cc <main+0x7c>
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1234 <main+0xe4>
cmpl   $0x0,-0x8(%rbp)
je     121f <main+0xcf>
mov    -0x8(%rbp),%eax
mov    0x2e00(%rip),%rcx
cmp    (%rcx),%eax
jne    1202 <main+0xb2>
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1234 <main+0xe4>
mov    -0x8(%rbp),%esi
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1234 <main+0xe4>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0x2eea(%rip),%rbx
mov    %rbx,%rdi
call   1060 <pthread_rwlock_trywrlock@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1070 <pthread_rwlock_unlock@plt>
test   %rax,%rax
jne    11e9 <main+0x89>
lea    0x2ecc(%rip),%rdi
call   1040 <pthread_rwlock_destroy@plt>
test   %rax,%rax
je     11a5 <main+0x45>
lea    0xef0(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%eax
jmp    11e2 <main+0x82>
test   %ebp,%ebp
je     11bd <main+0x5d>
mov    0x2e30(%rip),%rax
cmp    (%rax),%ebp
jne    11cd <main+0x6d>
lea    0xec2(%rip),%rdi
jmp    11c4 <main+0x64>
lea    0xe63(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    11e2 <main+0x82>
lea    0xe2c(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xebb(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@main.rwlock = internal global i32 0
@.str = [40 x i8] c"main: Error at pthread_rwlock_unlock()\0A\00"
@.str.1 = [35 x i8] c"Error at pthread_rwlock_destroy()\0A\00"
@EINVAL = global i32 0
@.str.2 = [13 x i8] c"Test PASSED\0A\00"
@.str.3 = [39 x i8] c"Test FAILED: Incorrect return code %d\0A\00"
@.str.4 = [87 x i8] c"Test PASSED: Note*: Returned 0 instead of EINVAL, but standard specified _may_ fail. \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i32 @pthread_rwlock_trywrlock(i32* @main.rwlock)
store i32 %3, i32* %2
%4 = call i64 @pthread_rwlock_unlock(i32* @main.rwlock)
%5 = icmp ne i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 2)
unreachable
9: ; preds = %0
%10 = call i64 @pthread_rwlock_destroy(i32* @main.rwlock)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %28
14: ; preds = %9
%15 = load i32, i32* %2
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %26
17: ; preds = %14
%18 = load i32, i32* %2
%19 = load i32, i32* @EINVAL
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %28
23: ; preds = %17
%24 = load i32, i32* %2
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 %24)
store i32 1, i32* %1
br label %28
26: ; preds = %14
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([87 x i8], [87 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %1
br label %28
28: ; preds = %26, %23, %21, %12
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
@main.rwlock = internal global i32 0
@EINVAL = global i32 0
@.str.3 = [39 x i8] c"Test FAILED: Incorrect return code %d\0A\00"
@str = [86 x i8] c"Test PASSED: Note*: Returned 0 instead of EINVAL, but standard specified _may_ fail. \00"
@str.5 = [12 x i8] c"Test PASSED\00"
@str.6 = [34 x i8] c"Error at pthread_rwlock_destroy()\00"
@str.7 = [39 x i8] c"main: Error at pthread_rwlock_unlock()\00"
define i32 @main() {
%1 = tail call i32 @pthread_rwlock_trywrlock(i32* @main.rwlock)
%2 = tail call i64 @pthread_rwlock_unlock(i32* @main.rwlock)
%3 = icmp eq i64 %2, 0
br i1 %3, label %7, label %4
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.7, i64 0, i64 0))
%6 = tail call i32 @exit(i32 2)
unreachable
7: ; preds = %0
%8 = tail call i64 @pthread_rwlock_destroy(i32* @main.rwlock)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.6, i64 0, i64 0))
br label %23
12: ; preds = %7
%13 = icmp eq i32 %1, 0
br i1 %13, label %21, label %14
14: ; preds = %12
%15 = load i32, i32* @EINVAL
%16 = icmp eq i32 %1, %15
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.5, i64 0, i64 0))
br label %23
19: ; preds = %14
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 %1)
br label %23
21: ; preds = %12
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([86 x i8], [86 x i8]* @str, i64 0, i64 0))
br label %23
23: ; preds = %21, %19, %17, %10
%24 = phi i32 [ 2, %10 ], [ 0, %17 ], [ 1, %19 ], [ 0, %21 ]
ret i32 %24
}