#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 *, ...);
scalar_t__ pthread_rwlock_destroy(int /*<<< orphan*/ *);
int pthread_rwlock_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int main(void) {
  static pthread_rwlock_t rwlock;
  int rc;
  rc = pthread_rwlock_init(&rwlock, NULL);
  if (rc != 0) {
    printf("Test FAILED: Error at pthread_rwlock_init(), returns %d\n", rc);
    return 1;
  }
  rc = pthread_rwlock_init(&rwlock, NULL);
  if (pthread_rwlock_destroy(&rwlock) != 0) {
    printf("Error at pthread_rwlock_destroy()\n");
    return 2;
  }
  if (rc == EBUSY) {
    printf("Test PASSED\n");
    return 0;
  } else if (rc == 0) {
    printf("Test PASSED: Note*: pthread_rwlock_init() returned 0 instead of "
           "EBUSY, but standard specifies _may_ fail\n");
    return 0;
  } else {
    printf("Test FAILED: Error at pthread_rwlock_init(), 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    0x2ef6(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <pthread_rwlock_init@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1179 <main+0x49>
mov    -0x8(%rbp),%esi
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1224 <main+0xf4>
lea    0x2ebc(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <pthread_rwlock_init@plt>
mov    %eax,-0x8(%rbp)
lea    0x2ea9(%rip),%rdi
call   1030 <pthread_rwlock_destroy@plt>
cmp    $0x0,%rax
je     11bc <main+0x8c>
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1224 <main+0xf4>
mov    -0x8(%rbp),%eax
mov    0x2e1a(%rip),%rcx
cmp    (%rcx),%eax
jne    11e8 <main+0xb8>
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1224 <main+0xf4>
cmpl   $0x0,-0x8(%rbp)
jne    120c <main+0xdc>
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1224 <main+0xf4>
mov    -0x8(%rbp),%esi
lea    0xebd(%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   %rbp
push   %rbx
push   %rax
lea    0x2efa(%rip),%rdi
xor    %esi,%esi
call   1060 <pthread_rwlock_init@plt>
test   %eax,%eax
je     116c <main+0x2c>
lea    0xea4(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
jmp    11d0 <main+0x90>
lea    0x2ed1(%rip),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <pthread_rwlock_init@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <pthread_rwlock_destroy@plt>
test   %rax,%rax
je     119f <main+0x5f>
lea    0xf72(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%eax
jmp    11d0 <main+0x90>
mov    0x2e3a(%rip),%rax
cmp    (%rax),%ebp
jne    11b3 <main+0x73>
lea    0xf48(%rip),%rdi
jmp    11c9 <main+0x89>
test   %ebp,%ebp
je     11c2 <main+0x82>
lea    0xe7b(%rip),%rdi
mov    %ebp,%esi
jmp    115e <main+0x1e>
lea    0xec7(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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