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

/* Variables and functions */
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int, int);
int /*<<< orphan*/ printf(char *, int);
int pthread_rwlock_destroy(int /*<<< orphan*/ *);
int pthread_rwlock_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int pthread_rwlock_rdlock(int /*<<< orphan*/ *);
int pthread_rwlock_unlock(int /*<<< orphan*/ *);
int pthread_rwlock_wrlock(int /*<<< orphan*/ *);
int do_test(void) {
  pthread_rwlock_t r;
  int ret;
  memset(&r, 0xaa, sizeof(r));
  if ((ret = pthread_rwlock_init(&r, NULL)) != 0) {
    printf("rwlock_init failed: %d\n", ret);
    return 1;
  }
  if ((ret = pthread_rwlock_rdlock(&r)) != 0) {
    printf("rwlock_rdlock failed: %d\n", ret);
    return 1;
  }
  if ((ret = pthread_rwlock_unlock(&r)) != 0) {
    printf("rwlock_unlock failed: %d\n", ret);
    return 1;
  }
  if ((ret = pthread_rwlock_wrlock(&r)) != 0) {
    printf("rwlock_wrlock failed: %d\n", ret);
    return 1;
  }
  if ((ret = pthread_rwlock_unlock(&r)) != 0) {
    printf("second rwlock_unlock failed: %d\n", ret);
    return 1;
  }
  if ((ret = pthread_rwlock_destroy(&r)) != 0) {
    printf("second rwlock_destroy failed: %d\n", ret);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    -0x8(%rbp),%rdi
mov    $0xaa,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
lea    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <pthread_rwlock_init@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
je     11bf <do_test+0x4f>
mov    -0xc(%rbp),%esi
lea    0xe52(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b6 <do_test+0x146>
lea    -0x8(%rbp),%rdi
call   1070 <pthread_rwlock_rdlock@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
je     11ef <do_test+0x7f>
mov    -0xc(%rbp),%esi
lea    0xe3a(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b6 <do_test+0x146>
lea    -0x8(%rbp),%rdi
call   1090 <pthread_rwlock_unlock@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
je     121f <do_test+0xaf>
mov    -0xc(%rbp),%esi
lea    0xe45(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b6 <do_test+0x146>
lea    -0x8(%rbp),%rdi
call   1030 <pthread_rwlock_wrlock@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
je     124f <do_test+0xdf>
mov    -0xc(%rbp),%esi
lea    0xdf4(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b6 <do_test+0x146>
lea    -0x8(%rbp),%rdi
call   1090 <pthread_rwlock_unlock@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
je     127f <do_test+0x10f>
mov    -0xc(%rbp),%esi
lea    0xdde(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b6 <do_test+0x146>
lea    -0x8(%rbp),%rdi
call   1040 <pthread_rwlock_destroy@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
je     12af <do_test+0x13f>
mov    -0xc(%rbp),%esi
lea    0xdcf(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b6 <do_test+0x146>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %rbx
sub    $0x10,%rsp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xaa,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <pthread_rwlock_init@plt>
test   %eax,%eax
je     11a3 <do_test+0x33>
lea    0xe5f(%rip),%rdi
jmp    1214 <do_test+0xa4>
lea    0xc(%rsp),%rdi
call   1070 <pthread_rwlock_rdlock@plt>
test   %eax,%eax
je     11ba <do_test+0x4a>
lea    0xe60(%rip),%rdi
jmp    1214 <do_test+0xa4>
lea    0xc(%rsp),%rdi
call   1090 <pthread_rwlock_unlock@plt>
test   %eax,%eax
je     11d1 <do_test+0x61>
lea    0xe84(%rip),%rdi
jmp    1214 <do_test+0xa4>
lea    0xc(%rsp),%rdi
call   1030 <pthread_rwlock_wrlock@plt>
test   %eax,%eax
je     11e8 <do_test+0x78>
lea    0xe4c(%rip),%rdi
jmp    1214 <do_test+0xa4>
lea    0xc(%rsp),%rdi
call   1090 <pthread_rwlock_unlock@plt>
test   %eax,%eax
je     11ff <do_test+0x8f>
lea    0xe4f(%rip),%rdi
jmp    1214 <do_test+0xa4>
lea    0xc(%rsp),%rdi
call   1040 <pthread_rwlock_destroy@plt>
test   %eax,%eax
je     1226 <do_test+0xb6>
lea    0xe59(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
ret
xor    %eax,%eax
jmp    1220 <do_test+0xb0>

LLVM IR (-O0):
@.str = [24 x i8] c"rwlock_init failed: %d\0A\00"
@.str.1 = [26 x i8] c"rwlock_rdlock failed: %d\0A\00"
@.str.2 = [26 x i8] c"rwlock_unlock failed: %d\0A\00"
@.str.3 = [26 x i8] c"rwlock_wrlock failed: %d\0A\00"
@.str.4 = [33 x i8] c"second rwlock_unlock failed: %d\0A\00"
@.str.5 = [34 x i8] c"second rwlock_destroy failed: %d\0A\00"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = call i32 @memset(i32* %2, i32 170, i32 4)
%5 = call i32 @pthread_rwlock_init(i32* %2, i32* null)
store i32 %5, i32* %3
%6 = icmp ne i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = load i32, i32* %3
%9 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 1, i32* %1
br label %41
10: ; preds = %0
%11 = call i32 @pthread_rwlock_rdlock(i32* %2)
store i32 %11, i32* %3
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i32, i32* %3
%15 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %14)
store i32 1, i32* %1
br label %41
16: ; preds = %10
%17 = call i32 @pthread_rwlock_unlock(i32* %2)
store i32 %17, i32* %3
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* %3
%21 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %20)
store i32 1, i32* %1
br label %41
22: ; preds = %16
%23 = call i32 @pthread_rwlock_wrlock(i32* %2)
store i32 %23, i32* %3
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i32, i32* %3
%27 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %26)
store i32 1, i32* %1
br label %41
28: ; preds = %22
%29 = call i32 @pthread_rwlock_unlock(i32* %2)
store i32 %29, i32* %3
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load i32, i32* %3
%33 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %32)
store i32 1, i32* %1
br label %41
34: ; preds = %28
%35 = call i32 @pthread_rwlock_destroy(i32* %2)
store i32 %35, i32* %3
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i32, i32* %3
%39 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i32 %38)
store i32 1, i32* %1
br label %41
40: ; preds = %34
store i32 0, i32* %1
br label %41
41: ; preds = %40, %37, %31, %25, %19, %13, %7
%42 = load i32, i32* %1
ret i32 %42
}

LLVM IR (-O2):
@.str = [24 x i8] c"rwlock_init failed: %d\0A\00"
@.str.1 = [26 x i8] c"rwlock_rdlock failed: %d\0A\00"
@.str.2 = [26 x i8] c"rwlock_unlock failed: %d\0A\00"
@.str.3 = [26 x i8] c"rwlock_wrlock failed: %d\0A\00"
@.str.4 = [33 x i8] c"second rwlock_unlock failed: %d\0A\00"
@.str.5 = [34 x i8] c"second rwlock_destroy failed: %d\0A\00"
define i32 @do_test() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = call i32 @memset(i32* %1, i32 170, i32 4)
%4 = call i32 @pthread_rwlock_init(i32* %1, i32* null)
%5 = icmp eq i32 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %0
%7 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %4)
br label %33
8: ; preds = %0
%9 = call i32 @pthread_rwlock_rdlock(i32* %1)
%10 = icmp eq i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %9)
br label %33
13: ; preds = %8
%14 = call i32 @pthread_rwlock_unlock(i32* %1)
%15 = icmp eq i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %14)
br label %33
18: ; preds = %13
%19 = call i32 @pthread_rwlock_wrlock(i32* %1)
%20 = icmp eq i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %19)
br label %33
23: ; preds = %18
%24 = call i32 @pthread_rwlock_unlock(i32* %1)
%25 = icmp eq i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %24)
br label %33
28: ; preds = %23
%29 = call i32 @pthread_rwlock_destroy(i32* %1)
%30 = icmp eq i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %28
%32 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i32 %29)
br label %33
33: ; preds = %28, %31, %26, %21, %16, %11, %6
%34 = phi i32 [ 1, %6 ], [ 1, %11 ], [ 1, %16 ], [ 1, %21 ], [ 1, %26 ], [ 1, %31 ], [ 0, %28 ]
ret i32 %34
}