#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 EBUSY;
int /*<<< orphan*/ printf(char *, ...);
int pthread_rwlock_destroy(int /*<<< orphan*/ *);
scalar_t__ pthread_rwlock_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ pthread_rwlock_rdlock(int /*<<< orphan*/ *);
int main(void) {
  pthread_rwlock_t rwlock;
  int rc;
  if (pthread_rwlock_init(&rwlock, NULL) != 0) {
    printf("Error at pthread_rwlock_init()\n");
    return 2;
  }
  if (pthread_rwlock_rdlock(&rwlock) != 0) {
    printf("Error at pthread_rwlock_rdlock()\n");
    return 2;
  }
  rc = pthread_rwlock_destroy(&rwlock);
  if (rc == EBUSY) {
    printf("Test PASSED\n");
    return 0;
  } else if (rc == 0) {
    printf("Test PASSED: Note*: pthread_rwlock_destroy() returned 0 instead of "
           "EBUSY, but standard specifies _may_ fail\n");
    return 0;
  } else {
    printf("Test FAILED: Error at pthread_rwlock_destroy(), should return 0 or "
           "EBUSY, but returns %d\n",
           rc);
    return 1;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <pthread_rwlock_init@plt>
cmp    $0x0,%rax
je     1180 <main+0x40>
lea    0xe93(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1221 <main+0xe1>
lea    -0x8(%rbp),%rdi
call   1050 <pthread_rwlock_rdlock@plt>
cmp    $0x0,%rax
je     11ad <main+0x6d>
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1221 <main+0xe1>
lea    -0x8(%rbp),%rdi
call   1030 <pthread_rwlock_destroy@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2e1d(%rip),%rcx
cmp    (%rcx),%eax
jne    11e5 <main+0xa5>
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1221 <main+0xe1>
cmpl   $0x0,-0xc(%rbp)
jne    1209 <main+0xc9>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1221 <main+0xe1>
mov    -0xc(%rbp),%esi
lea    0xea9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0x4(%rsp),%rdi
xor    %esi,%esi
call   1070 <pthread_rwlock_init@plt>
test   %rax,%rax
je     116b <main+0x1b>
lea    0xf8a(%rip),%rdi
jmp    1181 <main+0x31>
lea    0x4(%rsp),%rdi
call   1060 <pthread_rwlock_rdlock@plt>
test   %rax,%rax
je     118d <main+0x3d>
lea    0xf51(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%eax
pop    %rcx
ret
lea    0x4(%rsp),%rdi
call   1040 <pthread_rwlock_destroy@plt>
mov    0x2e42(%rip),%rcx
cmp    (%rcx),%eax
jne    11ab <main+0x5b>
lea    0xf1d(%rip),%rdi
jmp    11cd <main+0x7d>
test   %eax,%eax
je     11c6 <main+0x76>
lea    0xe4a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
pop    %rcx
ret
lea    0xe8d(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [32 x i8] c"Error at pthread_rwlock_init()\0A\00"
@.str.1 = [34 x i8] c"Error at pthread_rwlock_rdlock()\0A\00"
@EBUSY = global i32 0
@.str.2 = [13 x i8] c"Test PASSED\0A\00"
@.str.3 = [109 x i8] c"Test PASSED: Note*: pthread_rwlock_destroy() returned 0 instead of EBUSY, but standard specifies _may_ fail\0A\00"
@.str.4 = [90 x i8] c"Test FAILED: Error at pthread_rwlock_destroy(), should return 0 or EBUSY, but returns %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = call i64 @pthread_rwlock_init(i32* %2, i32* null)
%5 = icmp ne i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %28
8: ; preds = %0
%9 = call i64 @pthread_rwlock_rdlock(i32* %2)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %28
13: ; preds = %8
%14 = call i32 @pthread_rwlock_destroy(i32* %2)
store i32 %14, i32* %3
%15 = load i32, i32* %3
%16 = load i32, i32* @EBUSY
%17 = icmp eq i32 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = 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
20: ; preds = %13
%21 = load i32, i32* %3
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([109 x i8], [109 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %28
25: ; preds = %20
%26 = load i32, i32* %3
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([90 x i8], [90 x i8]* @.str.4, i64 0, i64 0), i32 %26)
store i32 1, i32* %1
br label %28
28: ; preds = %25, %23, %18, %11, %6
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
@EBUSY = global i32 0
@.str.4 = [90 x i8] c"Test FAILED: Error at pthread_rwlock_destroy(), should return 0 or EBUSY, but returns %d\0A\00"
@str = [108 x i8] c"Test PASSED: Note*: pthread_rwlock_destroy() returned 0 instead of EBUSY, but standard specifies _may_ fail\00"
@str.5 = [12 x i8] c"Test PASSED\00"
@str.6 = [33 x i8] c"Error at pthread_rwlock_rdlock()\00"
@str.7 = [31 x i8] c"Error at pthread_rwlock_init()\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = call i64 @pthread_rwlock_init(i32* %1, i32* null)
%4 = icmp eq i64 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %0
%6 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.7, i64 0, i64 0))
br label %24
7: ; preds = %0
%8 = call i64 @pthread_rwlock_rdlock(i32* %1)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.6, i64 0, i64 0))
br label %24
12: ; preds = %7
%13 = call i32 @pthread_rwlock_destroy(i32* %1)
%14 = load i32, i32* @EBUSY
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.5, i64 0, i64 0))
br label %24
18: ; preds = %12
%19 = icmp eq i32 %13, 0
br i1 %19, label %20, label %22
20: ; preds = %18
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([108 x i8], [108 x i8]* @str, i64 0, i64 0))
br label %24
22: ; preds = %18
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([90 x i8], [90 x i8]* @.str.4, i64 0, i64 0), i32 %13)
br label %24
24: ; preds = %22, %20, %16, %10, %5
%25 = phi i32 [ 2, %5 ], [ 2, %10 ], [ 0, %16 ], [ 0, %20 ], [ 1, %22 ]
ret i32 %25
}