#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;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ pthread_rwlock_destroy(int /*<<< orphan*/ *);
scalar_t__ pthread_rwlock_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ pthread_rwlock_unlock(int /*<<< orphan*/ *);
int pthread_rwlock_wrlock(int /*<<< orphan*/ *);
int main(void) {
  static pthread_rwlock_t rwlock;
  int rc;
  if (pthread_rwlock_init(&rwlock, NULL) != 0) {
    printf("Error at pthread_rwlock_init()\n");
    return 2;
  }
  printf("main: attempt write lock\n");
  if (pthread_rwlock_wrlock(&rwlock) != 0) {
    printf("Error at pthread_rwlock_wrlock()\n");
    return 2;
  }
  printf("main: acquired write lock\n");
  printf("main: attempt write lock\n");
  rc = pthread_rwlock_wrlock(&rwlock);
  if (pthread_rwlock_unlock(&rwlock) != 0) {
    printf("Error releasing write lock\n");
    exit(2);
  }
  if (pthread_rwlock_destroy(&rwlock) != 0) {
    printf("Error at pthread_rwlock_destroy()\n");
    return 2;
  }
  if (rc != 0) {
    if (rc == EDEADLK) {
      printf("main: correctly got EDEADLK\n");
      printf("Test PASSED\n");
      return 0;
    } else {
      printf("Test FAILED: Incorrect return code %d\n", rc);
      return 1;
    }
  }
  printf("main: acquired write lock\n");
  printf("main: unlock write lock\n");
  printf("Test PASSED: Note*: Returned 0 instead of EDEADLK, but standard "
         "specified _may_ fail.\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0x2ede(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <pthread_rwlock_init@plt>
cmp    $0x0,%rax
je     11a3 <main+0x43>
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12fb <main+0x19b>
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0x2e9c(%rip),%rdi
call   1030 <pthread_rwlock_wrlock@plt>
cmp    $0x0,%eax
je     11e0 <main+0x80>
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12fb <main+0x19b>
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0x2e51(%rip),%rdi
call   1030 <pthread_rwlock_wrlock@plt>
mov    %eax,-0x8(%rbp)
lea    0x2e42(%rip),%rdi
call   1070 <pthread_rwlock_unlock@plt>
cmp    $0x0,%rax
je     1239 <main+0xd9>
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
lea    0x2e14(%rip),%rdi
call   1040 <pthread_rwlock_destroy@plt>
cmp    $0x0,%rax
je     1269 <main+0x109>
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12fb <main+0x19b>
cmpl   $0x0,-0x8(%rbp)
je     12ca <main+0x16a>
mov    -0x8(%rbp),%eax
mov    0x2d6b(%rip),%rcx
cmp    (%rcx),%eax
jne    12ad <main+0x14d>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12fb <main+0x19b>
mov    -0x8(%rbp),%esi
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fb <main+0x19b>
lea    0xd8b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0x2ee2(%rip),%rdi
xor    %esi,%esi
call   1070 <pthread_rwlock_init@plt>
test   %rax,%rax
je     118f <main+0x1f>
lea    0xfc1(%rip),%rdi
jmp    1206 <main+0x96>
lea    0xe91(%rip),%rdi
call   1030 <puts@plt>
lea    0x2eba(%rip),%rdi
call   1040 <pthread_rwlock_wrlock@plt>
test   %eax,%eax
je     11b4 <main+0x44>
lea    0xf7b(%rip),%rdi
jmp    1206 <main+0x96>
lea    0xe85(%rip),%rdi
call   1030 <puts@plt>
lea    0xe60(%rip),%rdi
call   1030 <puts@plt>
lea    0x2e89(%rip),%rbx
mov    %rbx,%rdi
call   1040 <pthread_rwlock_wrlock@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1080 <pthread_rwlock_unlock@plt>
test   %rax,%rax
jne    127a <main+0x10a>
lea    0x2e67(%rip),%rdi
call   1050 <pthread_rwlock_destroy@plt>
test   %rax,%rax
je     1217 <main+0xa7>
lea    0xeea(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
test   %ebp,%ebp
je     123b <main+0xcb>
mov    0x2dc6(%rip),%rax
cmp    (%rax),%ebp
jne    1263 <main+0xf3>
lea    0xe9b(%rip),%rdi
call   1030 <puts@plt>
lea    0xeab(%rip),%rdi
jmp    125a <main+0xea>
lea    0xdfe(%rip),%rdi
call   1030 <puts@plt>
lea    0xe0c(%rip),%rdi
call   1030 <puts@plt>
lea    0xe18(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    1210 <main+0xa0>
lea    0xd96(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%eax
jmp    1210 <main+0xa0>
lea    0xe91(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@main.rwlock = internal global i32 0
@.str = [32 x i8] c"Error at pthread_rwlock_init()\0A\00"
@.str.1 = [26 x i8] c"main: attempt write lock\0A\00"
@.str.2 = [34 x i8] c"Error at pthread_rwlock_wrlock()\0A\00"
@.str.3 = [27 x i8] c"main: acquired write lock\0A\00"
@.str.4 = [28 x i8] c"Error releasing write lock\0A\00"
@.str.5 = [35 x i8] c"Error at pthread_rwlock_destroy()\0A\00"
@EDEADLK = global i32 0
@.str.6 = [29 x i8] c"main: correctly got EDEADLK\0A\00"
@.str.7 = [13 x i8] c"Test PASSED\0A\00"
@.str.8 = [39 x i8] c"Test FAILED: Incorrect return code %d\0A\00"
@.str.9 = [25 x i8] c"main: unlock write lock\0A\00"
@.str.10 = [87 x i8] c"Test PASSED: Note*: Returned 0 instead of EDEADLK, but standard specified _may_ fail.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i64 @pthread_rwlock_init(i32* @main.rwlock, i32* null)
%4 = icmp ne i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = 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 %44
7: ; preds = %0
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @pthread_rwlock_wrlock(i32* @main.rwlock)
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %44
13: ; preds = %7
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @pthread_rwlock_wrlock(i32* @main.rwlock)
store i32 %16, i32* %2
%17 = call i64 @pthread_rwlock_unlock(i32* @main.rwlock)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
%21 = call i32 @exit(i32 2)
unreachable
22: ; preds = %13
%23 = call i64 @pthread_rwlock_destroy(i32* @main.rwlock)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
store i32 2, i32* %1
br label %44
27: ; preds = %22
%28 = load i32, i32* %2
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %40
30: ; preds = %27
%31 = load i32, i32* %2
%32 = load i32, i32* @EDEADLK
%33 = icmp eq i32 %31, %32
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %1
br label %44
37: ; preds = %30
%38 = load i32, i32* %2
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.8, i64 0, i64 0), i32 %38)
store i32 1, i32* %1
br label %44
40: ; preds = %27
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.9, i64 0, i64 0))
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([87 x i8], [87 x i8]* @.str.10, i64 0, i64 0))
store i32 0, i32* %1
br label %44
44: ; preds = %40, %37, %34, %25, %11, %5
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
@main.rwlock = internal global i32 0
@EDEADLK = global i32 0
@.str.8 = [39 x i8] c"Test FAILED: Incorrect return code %d\0A\00"
@str.12 = [25 x i8] c"main: attempt write lock\00"
@str.13 = [26 x i8] c"main: acquired write lock\00"
@str.14 = [24 x i8] c"main: unlock write lock\00"
@str.15 = [86 x i8] c"Test PASSED: Note*: Returned 0 instead of EDEADLK, but standard specified _may_ fail.\00"
@str.16 = [28 x i8] c"main: correctly got EDEADLK\00"
@str.17 = [12 x i8] c"Test PASSED\00"
@str.18 = [34 x i8] c"Error at pthread_rwlock_destroy()\00"
@str.19 = [27 x i8] c"Error releasing write lock\00"
@str.20 = [33 x i8] c"Error at pthread_rwlock_wrlock()\00"
@str.21 = [31 x i8] c"Error at pthread_rwlock_init()\00"
define i32 @main() {
%1 = tail call i64 @pthread_rwlock_init(i32* @main.rwlock, i32* null)
%2 = icmp eq i64 %1, 0
br i1 %2, label %5, label %3
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.21, i64 0, i64 0))
br label %39
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.12, i64 0, i64 0))
%7 = tail call i32 @pthread_rwlock_wrlock(i32* @main.rwlock)
%8 = icmp eq i32 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %5
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.20, i64 0, i64 0))
br label %39
11: ; preds = %5
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.13, i64 0, i64 0))
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.12, i64 0, i64 0))
%14 = tail call i32 @pthread_rwlock_wrlock(i32* @main.rwlock)
%15 = tail call i64 @pthread_rwlock_unlock(i32* @main.rwlock)
%16 = icmp eq i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %11
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.19, i64 0, i64 0))
%19 = tail call i32 @exit(i32 2)
unreachable
20: ; preds = %11
%21 = tail call i64 @pthread_rwlock_destroy(i32* @main.rwlock)
%22 = icmp eq i64 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %20
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.18, i64 0, i64 0))
br label %39
25: ; preds = %20
%26 = icmp eq i32 %14, 0
br i1 %26, label %35, label %27
27: ; preds = %25
%28 = load i32, i32* @EDEADLK
%29 = icmp eq i32 %14, %28
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.16, i64 0, i64 0))
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.17, i64 0, i64 0))
br label %39
33: ; preds = %27
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.8, i64 0, i64 0), i32 %14)
br label %39
35: ; preds = %25
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.13, i64 0, i64 0))
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.14, i64 0, i64 0))
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([86 x i8], [86 x i8]* @str.15, i64 0, i64 0))
br label %39
39: ; preds = %35, %33, %30, %23, %9, %3
%40 = phi i32 [ 2, %3 ], [ 2, %9 ], [ 2, %23 ], [ 0, %30 ], [ 1, %33 ], [ 0, %35 ]
ret i32 %40
}