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

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
int pthread_rwlock_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int pthread_rwlock_rdlock(int /*<<< orphan*/ *);
int pthread_rwlock_unlock(int /*<<< orphan*/ *);
int pthread_rwlockattr_destroy(int /*<<< orphan*/ *);
int pthread_rwlockattr_init(int /*<<< orphan*/ *);
int main(void) {
  pthread_rwlockattr_t rwa;
  pthread_rwlock_t rwl1, rwl2;
  int rc;
  rc = pthread_rwlockattr_init(&rwa);
  if (rc != 0) {
    printf("Error at pthread_rwlockattr_init(), returns %d\n", rc);
    return 2;
  }
  if ((rc = pthread_rwlock_init(&rwl1, &rwa)) != 0) {
    printf("Error at 1st pthread_rwlock_init()\n");
    return 2;
  }
  if ((rc = pthread_rwlock_init(&rwl2, &rwa)) != 0) {
    printf("Error at 2nd pthread_rwlock_init()\n");
    return 2;
  }
  if ((rc = pthread_rwlock_rdlock(&rwl1)) != 0) {
    printf("Error at pthread_rwlock_rdlock(): rwl1\n");
    return 2;
  }
  if ((rc = pthread_rwlockattr_destroy(&rwa)) != 0) {
    printf("Error at pthread_rwlockattr_destroy()\n");
    return 2;
  }
  if ((rc = pthread_rwlock_unlock(&rwl1)) != 0) {
    printf("Error at pthread_rwlock_unlock: rwl1\n");
    printf("Test Failed.\n");
    return 1;
  }
  if ((rc = pthread_rwlock_rdlock(&rwl2)) != 0) {
    printf("Error at pthread_rwlock_rdlock():rwl2\n");
    printf("Test Failed.\n");
    return 1;
  }
  if ((rc = pthread_rwlock_unlock(&rwl2)) != 0) {
    printf("Error at pthread_rwlock_unlock:rwl2\n");
    printf("Test Failed.\n");
    return 1;
  }
  printf("Test PASSED\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
call   1050 <pthread_rwlockattr_init@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11a2 <main+0x42>
mov    -0x14(%rbp),%esi
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1332 <main+0x1d2>
lea    -0xc(%rbp),%rdi
lea    -0x8(%rbp),%rsi
call   1060 <pthread_rwlock_init@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     11d5 <main+0x75>
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1332 <main+0x1d2>
lea    -0x10(%rbp),%rdi
lea    -0x8(%rbp),%rsi
call   1060 <pthread_rwlock_init@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     1208 <main+0xa8>
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1332 <main+0x1d2>
lea    -0xc(%rbp),%rdi
call   1040 <pthread_rwlock_rdlock@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     1237 <main+0xd7>
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1332 <main+0x1d2>
lea    -0x8(%rbp),%rdi
call   1080 <pthread_rwlockattr_destroy@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     1266 <main+0x106>
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1332 <main+0x1d2>
lea    -0xc(%rbp),%rdi
call   1070 <pthread_rwlock_unlock@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     12a3 <main+0x143>
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1332 <main+0x1d2>
lea    -0x10(%rbp),%rdi
call   1040 <pthread_rwlock_rdlock@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     12e0 <main+0x180>
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1332 <main+0x1d2>
lea    -0x10(%rbp),%rdi
call   1070 <pthread_rwlock_unlock@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     131d <main+0x1bd>
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1332 <main+0x1d2>
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x18,%rsp
lea    0xc(%rsp),%rdi
call   1060 <pthread_rwlockattr_init@plt>
test   %eax,%eax
je     1194 <main+0x24>
lea    0xe77(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11fd <main+0x8d>
lea    0x14(%rsp),%rdi
lea    0xc(%rsp),%rsi
call   1070 <pthread_rwlock_init@plt>
test   %eax,%eax
je     11b0 <main+0x40>
lea    0xf7a(%rip),%rdi
jmp    11f8 <main+0x88>
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rsi
call   1070 <pthread_rwlock_init@plt>
test   %eax,%eax
je     11cc <main+0x5c>
lea    0xf3b(%rip),%rdi
jmp    11f8 <main+0x88>
lea    0x14(%rsp),%rdi
call   1050 <pthread_rwlock_rdlock@plt>
test   %eax,%eax
je     11e3 <main+0x73>
lea    0xefd(%rip),%rdi
jmp    11f8 <main+0x88>
lea    0xc(%rsp),%rdi
call   1090 <pthread_rwlockattr_destroy@plt>
test   %eax,%eax
je     1207 <main+0x97>
lea    0xec0(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%eax
add    $0x18,%rsp
ret
lea    0x14(%rsp),%rdi
call   1080 <pthread_rwlock_unlock@plt>
test   %eax,%eax
je     121e <main+0xae>
lea    0xe6a(%rip),%rdi
jmp    124a <main+0xda>
lea    0x10(%rsp),%rdi
call   1050 <pthread_rwlock_rdlock@plt>
test   %eax,%eax
je     1235 <main+0xc5>
lea    0xe2d(%rip),%rdi
jmp    124a <main+0xda>
lea    0x10(%rsp),%rdi
call   1080 <pthread_rwlock_unlock@plt>
test   %eax,%eax
je     1265 <main+0xf5>
lea    0xdf2(%rip),%rdi
call   1030 <puts@plt>
lea    0xe55(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x18,%rsp
ret
lea    0xdc4(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
@.str = [48 x i8] c"Error at pthread_rwlockattr_init(), returns %d\0A\00"
@.str.1 = [36 x i8] c"Error at 1st pthread_rwlock_init()\0A\00"
@.str.2 = [36 x i8] c"Error at 2nd pthread_rwlock_init()\0A\00"
@.str.3 = [40 x i8] c"Error at pthread_rwlock_rdlock(): rwl1\0A\00"
@.str.4 = [39 x i8] c"Error at pthread_rwlockattr_destroy()\0A\00"
@.str.5 = [38 x i8] c"Error at pthread_rwlock_unlock: rwl1\0A\00"
@.str.6 = [14 x i8] c"Test Failed.\0A\00"
@.str.7 = [39 x i8] c"Error at pthread_rwlock_rdlock():rwl2\0A\00"
@.str.8 = [37 x i8] c"Error at pthread_rwlock_unlock:rwl2\0A\00"
@.str.9 = [13 x i8] c"Test PASSED\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 @pthread_rwlockattr_init(i32* %2)
store i32 %6, i32* %5
%7 = load i32, i32* %5
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = load i32, i32* %5
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 2, i32* %1
br label %52
12: ; preds = %0
%13 = call i32 @pthread_rwlock_init(i32* %3, i32* %2)
store i32 %13, i32* %5
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %52
17: ; preds = %12
%18 = call i32 @pthread_rwlock_init(i32* %4, i32* %2)
store i32 %18, i32* %5
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %52
22: ; preds = %17
%23 = call i32 @pthread_rwlock_rdlock(i32* %3)
store i32 %23, i32* %5
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0))
store i32 2, i32* %1
br label %52
27: ; preds = %22
%28 = call i32 @pthread_rwlockattr_destroy(i32* %2)
store i32 %28, i32* %5
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
store i32 2, i32* %1
br label %52
32: ; preds = %27
%33 = call i32 @pthread_rwlock_unlock(i32* %3)
store i32 %33, i32* %5
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0))
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %52
38: ; preds = %32
%39 = call i32 @pthread_rwlock_rdlock(i32* %4)
store i32 %39, i32* %5
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0))
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %52
44: ; preds = %38
%45 = call i32 @pthread_rwlock_unlock(i32* %4)
store i32 %45, i32* %5
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.8, i64 0, i64 0))
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %52
50: ; preds = %44
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.9, i64 0, i64 0))
store i32 0, i32* %1
br label %52
52: ; preds = %50, %47, %41, %35, %30, %25, %20, %15, %9
%53 = load i32, i32* %1
ret i32 %53
}

LLVM IR (-O2):
@.str = [48 x i8] c"Error at pthread_rwlockattr_init(), returns %d\0A\00"
@str = [12 x i8] c"Test PASSED\00"
@str.10 = [36 x i8] c"Error at pthread_rwlock_unlock:rwl2\00"
@str.12 = [38 x i8] c"Error at pthread_rwlock_rdlock():rwl2\00"
@str.14 = [37 x i8] c"Error at pthread_rwlock_unlock: rwl1\00"
@str.15 = [13 x i8] c"Test Failed.\00"
@str.16 = [38 x i8] c"Error at pthread_rwlockattr_destroy()\00"
@str.17 = [39 x i8] c"Error at pthread_rwlock_rdlock(): rwl1\00"
@str.18 = [35 x i8] c"Error at 2nd pthread_rwlock_init()\00"
@str.19 = [35 x i8] c"Error at 1st pthread_rwlock_init()\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = bitcast i32* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = call i32 @pthread_rwlockattr_init(i32* %1)
%8 = icmp eq i32 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %51
11: ; preds = %0
%12 = call i32 @pthread_rwlock_init(i32* %2, i32* %1)
%13 = icmp eq i32 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.19, i64 0, i64 0))
br label %51
16: ; preds = %11
%17 = call i32 @pthread_rwlock_init(i32* %3, i32* %1)
%18 = icmp eq i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.18, i64 0, i64 0))
br label %51
21: ; preds = %16
%22 = call i32 @pthread_rwlock_rdlock(i32* %2)
%23 = icmp eq i32 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.17, i64 0, i64 0))
br label %51
26: ; preds = %21
%27 = call i32 @pthread_rwlockattr_destroy(i32* %1)
%28 = icmp eq i32 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %26
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.16, i64 0, i64 0))
br label %51
31: ; preds = %26
%32 = call i32 @pthread_rwlock_unlock(i32* %2)
%33 = icmp eq i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %31
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.14, i64 0, i64 0))
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.15, i64 0, i64 0))
br label %51
37: ; preds = %31
%38 = call i32 @pthread_rwlock_rdlock(i32* %3)
%39 = icmp eq i32 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %37
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.12, i64 0, i64 0))
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.15, i64 0, i64 0))
br label %51
43: ; preds = %37
%44 = call i32 @pthread_rwlock_unlock(i32* %3)
%45 = icmp eq i32 %44, 0
br i1 %45, label %49, label %46
46: ; preds = %43
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.10, i64 0, i64 0))
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.15, i64 0, i64 0))
br label %51
49: ; preds = %43
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %51
51: ; preds = %49, %46, %40, %34, %29, %24, %19, %14, %9
%52 = phi i32 [ 2, %9 ], [ 2, %14 ], [ 2, %19 ], [ 2, %24 ], [ 2, %29 ], [ 1, %34 ], [ 1, %40 ], [ 1, %46 ], [ 0, %49 ]
ret i32 %52
}