#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 EDEADLK;
scalar_t__ pthread_rwlock_destroy(int /*<<< orphan*/ *);
scalar_t__ pthread_rwlock_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int pthread_rwlock_rdlock(int /*<<< orphan*/ *);
scalar_t__ pthread_rwlock_trywrlock(int /*<<< orphan*/ *);
scalar_t__ pthread_rwlock_unlock(int /*<<< orphan*/ *);
int pthread_rwlock_wrlock(int /*<<< orphan*/ *);
int /*<<< orphan*/ puts(char *);
int do_test(void) {
  pthread_rwlock_t r;
  int e;
  if (pthread_rwlock_init(&r, NULL) != 0) {
    puts("rwlock_init failed");
    return 1;
  }
  puts("rwlock_init succeeded");
  if (pthread_rwlock_trywrlock(&r) != 0) {
    puts("rwlock_trywrlock on unlocked rwlock failed");
    return 1;
  }
  puts("rwlock_trywrlock on unlocked rwlock succeeded");
  e = pthread_rwlock_rdlock(&r);
  if (e == 0) {
    puts("rwlock_rdlock on rwlock with writer succeeded");
    return 1;
  }
  if (e != EDEADLK) {
    puts("rwlock_rdlock on rwlock with writer failed != EDEADLK");
    return 1;
  }
  puts("rwlock_rdlock on rwlock with writer failed with EDEADLK");
  e = pthread_rwlock_wrlock(&r);
  if (e == 0) {
    puts("rwlock_wrlock on rwlock with writer succeeded");
    return 1;
  }
  if (e != EDEADLK) {
    puts("rwlock_wrlock on rwlock with writer failed != EDEADLK");
    return 1;
  }
  puts("rwlock_wrlock on rwlock with writer failed with EDEADLK");
  if (pthread_rwlock_unlock(&r) != 0) {
    puts("rwlock_unlock failed");
    return 1;
  }
  puts("rwlock_unlock succeeded");
  if (pthread_rwlock_destroy(&r) != 0) {
    puts("rwlock_destroy failed");
    return 1;
  }
  puts("rwlock_destroy succeeded");
  return 0;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <pthread_rwlock_init@plt>
cmp    $0x0,%rax
je     11a7 <do_test+0x37>
lea    0xe6a(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1327 <do_test+0x1b7>
lea    0xe65(%rip),%rdi
call   1030 <puts@plt>
lea    -0x8(%rbp),%rdi
call   1070 <pthread_rwlock_trywrlock@plt>
cmp    $0x0,%rax
je     11de <do_test+0x6e>
lea    0xe5c(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1327 <do_test+0x1b7>
lea    0xe6f(%rip),%rdi
call   1030 <puts@plt>
lea    -0x8(%rbp),%rdi
call   1060 <pthread_rwlock_rdlock@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1218 <do_test+0xa8>
lea    0xe7b(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1327 <do_test+0x1b7>
mov    -0xc(%rbp),%eax
mov    0x2dc6(%rip),%rcx
cmp    (%rcx),%eax
je     1242 <do_test+0xd2>
lea    0xe7f(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1327 <do_test+0x1b7>
lea    0xe9d(%rip),%rdi
call   1030 <puts@plt>
lea    -0x8(%rbp),%rdi
call   1040 <pthread_rwlock_wrlock@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    127c <do_test+0x10c>
lea    0xeb3(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1327 <do_test+0x1b7>
mov    -0xc(%rbp),%eax
mov    0x2d62(%rip),%rcx
cmp    (%rcx),%eax
je     12a6 <do_test+0x136>
lea    0xeb7(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1327 <do_test+0x1b7>
lea    0xed5(%rip),%rdi
call   1030 <puts@plt>
lea    -0x8(%rbp),%rdi
call   1090 <pthread_rwlock_unlock@plt>
cmp    $0x0,%rax
je     12dd <do_test+0x16d>
lea    0xeee(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1327 <do_test+0x1b7>
lea    0xeeb(%rip),%rdi
call   1030 <puts@plt>
lea    -0x8(%rbp),%rdi
call   1050 <pthread_rwlock_destroy@plt>
cmp    $0x0,%rax
je     1314 <do_test+0x1a4>
lea    0xee4(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1327 <do_test+0x1b7>
lea    0xee2(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %r14
push   %rbx
push   %rax
lea    0x4(%rsp),%rdi
xor    %esi,%esi
call   1080 <pthread_rwlock_init@plt>
mov    $0x1,%ebx
test   %rax,%rax
je     1193 <do_test+0x23>
lea    0xe6f(%rip),%rdi
jmp    11b5 <do_test+0x45>
lea    0xe79(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rdi
call   1070 <pthread_rwlock_trywrlock@plt>
test   %rax,%rax
je     11c4 <do_test+0x54>
lea    0xe74(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe89(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rdi
call   1060 <pthread_rwlock_rdlock@plt>
test   %eax,%eax
je     122d <do_test+0xbd>
mov    0x2e03(%rip),%r14
cmp    (%r14),%eax
jne    1239 <do_test+0xc9>
lea    0xef5(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rdi
call   1040 <pthread_rwlock_wrlock@plt>
test   %eax,%eax
je     1245 <do_test+0xd5>
cmp    (%r14),%eax
jne    1251 <do_test+0xe1>
lea    0xf72(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rdi
call   1090 <pthread_rwlock_unlock@plt>
test   %rax,%rax
je     125d <do_test+0xed>
lea    0xf8f(%rip),%rdi
jmp    11b5 <do_test+0x45>
lea    0xe4e(%rip),%rdi
jmp    11b5 <do_test+0x45>
lea    0xe70(%rip),%rdi
jmp    11b5 <do_test+0x45>
lea    0xed2(%rip),%rdi
jmp    11b5 <do_test+0x45>
lea    0xef4(%rip),%rdi
jmp    11b5 <do_test+0x45>
lea    0xf6b(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rdi
call   1050 <pthread_rwlock_destroy@plt>
xor    %ebx,%ebx
test   %rax,%rax
setne  %bl
lea    0xf7b(%rip),%rax
lea    0xf5e(%rip),%rdi
cmove  %rax,%rdi
jmp    11b5 <do_test+0x45>

LLVM IR (-O0):
@.str = [19 x i8] c"rwlock_init failed\00"
@.str.1 = [22 x i8] c"rwlock_init succeeded\00"
@.str.2 = [43 x i8] c"rwlock_trywrlock on unlocked rwlock failed\00"
@.str.3 = [46 x i8] c"rwlock_trywrlock on unlocked rwlock succeeded\00"
@.str.4 = [46 x i8] c"rwlock_rdlock on rwlock with writer succeeded\00"
@EDEADLK = global i32 0
@.str.5 = [54 x i8] c"rwlock_rdlock on rwlock with writer failed != EDEADLK\00"
@.str.6 = [56 x i8] c"rwlock_rdlock on rwlock with writer failed with EDEADLK\00"
@.str.7 = [46 x i8] c"rwlock_wrlock on rwlock with writer succeeded\00"
@.str.8 = [54 x i8] c"rwlock_wrlock on rwlock with writer failed != EDEADLK\00"
@.str.9 = [56 x i8] c"rwlock_wrlock on rwlock with writer failed with EDEADLK\00"
@.str.10 = [21 x i8] c"rwlock_unlock failed\00"
@.str.11 = [24 x i8] c"rwlock_unlock succeeded\00"
@.str.12 = [22 x i8] c"rwlock_destroy failed\00"
@.str.13 = [25 x i8] c"rwlock_destroy succeeded\00"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%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 @puts(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %54
8: ; preds = %0
%9 = call i32 @puts(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%10 = call i64 @pthread_rwlock_trywrlock(i32* %2)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = call i32 @puts(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %54
14: ; preds = %8
%15 = call i32 @puts(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0))
%16 = call i32 @pthread_rwlock_rdlock(i32* %2)
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 @puts(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %54
21: ; preds = %14
%22 = load i32, i32* %3
%23 = load i32, i32* @EDEADLK
%24 = icmp ne i32 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @puts(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %54
27: ; preds = %21
%28 = call i32 @puts(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.6, i64 0, i64 0))
%29 = call i32 @pthread_rwlock_wrlock(i32* %2)
store i32 %29, i32* %3
%30 = load i32, i32* %3
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = call i32 @puts(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %1
br label %54
34: ; preds = %27
%35 = load i32, i32* %3
%36 = load i32, i32* @EDEADLK
%37 = icmp ne i32 %35, %36
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @puts(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %1
br label %54
40: ; preds = %34
%41 = call i32 @puts(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.9, i64 0, i64 0))
%42 = call i64 @pthread_rwlock_unlock(i32* %2)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = call i32 @puts(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.10, i64 0, i64 0))
store i32 1, i32* %1
br label %54
46: ; preds = %40
%47 = call i32 @puts(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.11, i64 0, i64 0))
%48 = call i64 @pthread_rwlock_destroy(i32* %2)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = call i32 @puts(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.12, i64 0, i64 0))
store i32 1, i32* %1
br label %54
52: ; preds = %46
%53 = call i32 @puts(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.13, i64 0, i64 0))
store i32 0, i32* %1
br label %54
54: ; preds = %52, %50, %44, %38, %32, %25, %19, %12, %6
%55 = load i32, i32* %1
ret i32 %55
}

LLVM IR (-O2):
@.str = [19 x i8] c"rwlock_init failed\00"
@.str.1 = [22 x i8] c"rwlock_init succeeded\00"
@.str.2 = [43 x i8] c"rwlock_trywrlock on unlocked rwlock failed\00"
@.str.3 = [46 x i8] c"rwlock_trywrlock on unlocked rwlock succeeded\00"
@.str.4 = [46 x i8] c"rwlock_rdlock on rwlock with writer succeeded\00"
@EDEADLK = global i32 0
@.str.5 = [54 x i8] c"rwlock_rdlock on rwlock with writer failed != EDEADLK\00"
@.str.6 = [56 x i8] c"rwlock_rdlock on rwlock with writer failed with EDEADLK\00"
@.str.7 = [46 x i8] c"rwlock_wrlock on rwlock with writer succeeded\00"
@.str.8 = [54 x i8] c"rwlock_wrlock on rwlock with writer failed != EDEADLK\00"
@.str.9 = [56 x i8] c"rwlock_wrlock on rwlock with writer failed with EDEADLK\00"
@.str.10 = [21 x i8] c"rwlock_unlock failed\00"
@.str.11 = [24 x i8] c"rwlock_unlock succeeded\00"
@.str.12 = [22 x i8] c"rwlock_destroy failed\00"
@.str.13 = [25 x i8] c"rwlock_destroy succeeded\00"
define i32 @do_test() {
%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 %5, label %34
5: ; preds = %0
%6 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%7 = call i64 @pthread_rwlock_trywrlock(i32* %1)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %34
9: ; preds = %5
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0))
%11 = call i32 @pthread_rwlock_rdlock(i32* %1)
%12 = icmp eq i32 %11, 0
br i1 %12, label %34, label %13
13: ; preds = %9
%14 = load i32, i32* @EDEADLK
%15 = icmp eq i32 %11, %14
br i1 %15, label %16, label %34
16: ; preds = %13
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str.6, i64 0, i64 0))
%18 = call i32 @pthread_rwlock_wrlock(i32* %1)
%19 = icmp eq i32 %18, 0
br i1 %19, label %34, label %20
20: ; preds = %16
%21 = load i32, i32* @EDEADLK
%22 = icmp eq i32 %18, %21
br i1 %22, label %23, label %34
23: ; preds = %20
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str.9, i64 0, i64 0))
%25 = call i64 @pthread_rwlock_unlock(i32* %1)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %23
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.11, i64 0, i64 0))
%29 = call i64 @pthread_rwlock_destroy(i32* %1)
%30 = icmp eq i64 %29, 0
%31 = select i1 %30, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.13, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.12, i64 0, i64 0)
%32 = xor i1 %30, true
%33 = zext i1 %32 to i32
br label %34
34: ; preds = %27, %23, %20, %16, %13, %9, %5, %0
%35 = phi i8* [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), %5 ], [ getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), %9 ], [ getelementptr inbounds ([54 x i8], [54 x i8]* @.str.5, i64 0, i64 0), %13 ], [ getelementptr inbounds ([46 x i8], [46 x i8]* @.str.7, i64 0, i64 0), %16 ], [ getelementptr inbounds ([54 x i8], [54 x i8]* @.str.8, i64 0, i64 0), %20 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.10, i64 0, i64 0), %23 ], [ %31, %27 ]
%36 = phi i32 [ 1, %0 ], [ 1, %5 ], [ 1, %9 ], [ 1, %13 ], [ 1, %16 ], [ 1, %20 ], [ 1, %23 ], [ %33, %27 ]
%37 = call i32 @puts(i8* dereferenceable(1) %35)
ret i32 %36
}