#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;
typedef int /*<<< orphan*/ pthread_mutexattr_t;
typedef int /*<<< orphan*/ pthread_mutex_t;

/* Variables and functions */
int EOF;
int /*<<< orphan*/ PTHREAD_MUTEX_RECURSIVE;
int atoi(int /*<<< orphan*/);
int /*<<< orphan*/ delay_ms(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int getopt(int, char **, char *);
int /*<<< orphan*/ optarg;
int /*<<< orphan*/ pthread_mutex_destroy(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutex_init(int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutex_lock(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutex_unlock(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutexattr_destroy(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutexattr_init(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutexattr_settype(int /*<<< orphan*/ *,
                                             int /*<<< orphan*/);
int /*<<< orphan*/ pthread_rwlock_destroy(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_rwlock_init(int /*<<< orphan*/ *,
                                       int /*<<< orphan*/);
int /*<<< orphan*/ pthread_rwlock_rdlock(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_rwlock_unlock(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_rwlock_wrlock(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int interval = 0;
  int optchar;
  pthread_mutex_t mutex;
  pthread_mutexattr_t mutexattr;
  pthread_rwlock_t rwlock;
  while ((optchar = getopt(argc, argv, "i:")) != EOF) {
    switch (optchar) {
    case 'i':
      interval = atoi(optarg);
      break;
    default:
      fprintf(stderr, "Usage: %s [-i <interval time in ms>].\n", argv[0]);
      break;
    }
  }
  fprintf(stderr, "Locking mutex ...\n");
  pthread_mutexattr_init(&mutexattr);
  pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE);
  pthread_mutex_init(&mutex, &mutexattr);
  pthread_mutexattr_destroy(&mutexattr);
  pthread_mutex_lock(&mutex);
  delay_ms(interval);
  pthread_mutex_lock(&mutex);
  pthread_mutex_unlock(&mutex);
  pthread_mutex_unlock(&mutex);
  pthread_mutex_destroy(&mutex);
  fprintf(stderr, "Locking rwlock exclusively ...\n");
  pthread_rwlock_init(&rwlock, 0);
  pthread_rwlock_wrlock(&rwlock);
  delay_ms(interval);
  pthread_rwlock_unlock(&rwlock);
  pthread_rwlock_destroy(&rwlock);
  fprintf(stderr, "Locking rwlock shared ...\n");
  pthread_rwlock_init(&rwlock, 0);
  pthread_rwlock_rdlock(&rwlock);
  delay_ms(interval);
  pthread_rwlock_rdlock(&rwlock);
  pthread_rwlock_unlock(&rwlock);
  pthread_rwlock_unlock(&rwlock);
  pthread_rwlock_destroy(&rwlock);
  fprintf(stderr, "Done.\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xdd5(%rip),%rdx
call   10d0 <getopt@plt>
mov    %eax,-0x18(%rbp)
mov    0x2d9e(%rip),%rcx
cmp    (%rcx),%eax
je     128c <main+0x8c>
mov    -0x18(%rbp),%eax
sub    $0x69,%eax
jne    1269 <main+0x69>
jmp    1253 <main+0x53>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
call   10e0 <atoi@plt>
mov    %eax,-0x14(%rbp)
jmp    1287 <main+0x87>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xd83(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    121d <main+0x1d>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
lea    0xd8e(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
lea    -0x20(%rbp),%rdi
call   10f0 <pthread_mutexattr_init@plt>
mov    0x2d45(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdi
call   1060 <pthread_mutexattr_settype@plt>
lea    -0x1c(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1100 <pthread_mutex_init@plt>
lea    -0x20(%rbp),%rdi
call   1110 <pthread_mutexattr_destroy@plt>
lea    -0x1c(%rbp),%rdi
call   1120 <pthread_mutex_lock@plt>
mov    -0x14(%rbp),%edi
call   10c0 <delay_ms@plt>
lea    -0x1c(%rbp),%rdi
call   1120 <pthread_mutex_lock@plt>
lea    -0x1c(%rbp),%rdi
call   1090 <pthread_mutex_unlock@plt>
lea    -0x1c(%rbp),%rdi
call   1090 <pthread_mutex_unlock@plt>
lea    -0x1c(%rbp),%rdi
call   1050 <pthread_mutex_destroy@plt>
mov    0x2cb8(%rip),%rax
mov    (%rax),%edi
lea    0xd24(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
lea    -0x24(%rbp),%rdi
xor    %esi,%esi
call   10a0 <pthread_rwlock_init@plt>
lea    -0x24(%rbp),%rdi
call   1030 <pthread_rwlock_wrlock@plt>
mov    -0x14(%rbp),%edi
call   10c0 <delay_ms@plt>
lea    -0x24(%rbp),%rdi
call   10b0 <pthread_rwlock_unlock@plt>
lea    -0x24(%rbp),%rdi
call   1040 <pthread_rwlock_destroy@plt>
mov    0x2c73(%rip),%rax
mov    (%rax),%edi
lea    0xcff(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
lea    -0x24(%rbp),%rdi
xor    %esi,%esi
call   10a0 <pthread_rwlock_init@plt>
lea    -0x24(%rbp),%rdi
call   1070 <pthread_rwlock_rdlock@plt>
mov    -0x14(%rbp),%edi
call   10c0 <delay_ms@plt>
lea    -0x24(%rbp),%rdi
call   1070 <pthread_rwlock_rdlock@plt>
lea    -0x24(%rbp),%rdi
call   10b0 <pthread_rwlock_unlock@plt>
lea    -0x24(%rbp),%rdi
call   10b0 <pthread_rwlock_unlock@plt>
lea    -0x24(%rbp),%rdi
call   1040 <pthread_rwlock_destroy@plt>
mov    0x2c1c(%rip),%rax
mov    (%rax),%edi
lea    0xcc3(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xde6(%rip),%rdx
call   10d0 <getopt@plt>
mov    0x2db2(%rip),%r13
xor    %r14d,%r14d
cmp    0x0(%r13),%eax
jne    135e <main+0x15e>
mov    0x2d8e(%rip),%r15
mov    (%r15),%edi
lea    0xde6(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   10f0 <pthread_mutexattr_init@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <pthread_mutexattr_settype@plt>
lea    0x14(%rsp),%rbp
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1100 <pthread_mutex_init@plt>
mov    %rbx,%rdi
call   1110 <pthread_mutexattr_destroy@plt>
mov    %rbp,%rdi
call   1120 <pthread_mutex_lock@plt>
mov    %r14d,%edi
call   10c0 <delay_ms@plt>
mov    %rbp,%rdi
call   1120 <pthread_mutex_lock@plt>
mov    %rbp,%rdi
call   1090 <pthread_mutex_unlock@plt>
mov    %rbp,%rdi
call   1090 <pthread_mutex_unlock@plt>
mov    %rbp,%rdi
call   1050 <pthread_mutex_destroy@plt>
mov    (%r15),%edi
lea    0xd82(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <pthread_rwlock_init@plt>
mov    %rbx,%rdi
call   1030 <pthread_rwlock_wrlock@plt>
mov    %r14d,%edi
call   10c0 <delay_ms@plt>
mov    %rbx,%rdi
call   10b0 <pthread_rwlock_unlock@plt>
mov    %rbx,%rdi
call   1040 <pthread_rwlock_destroy@plt>
mov    (%r15),%edi
lea    0xd62(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <pthread_rwlock_init@plt>
mov    %rbx,%rdi
call   1070 <pthread_rwlock_rdlock@plt>
mov    %r14d,%edi
call   10c0 <delay_ms@plt>
mov    %rbx,%rdi
call   1070 <pthread_rwlock_rdlock@plt>
mov    %rbx,%rdi
call   10b0 <pthread_rwlock_unlock@plt>
mov    %rbx,%rdi
call   10b0 <pthread_rwlock_unlock@plt>
mov    %rbx,%rdi
call   1040 <pthread_rwlock_destroy@plt>
mov    (%r15),%edi
lea    0xd32(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc9b(%rip),%r15
lea    0xc97(%rip),%r12
xor    %r14d,%r14d
jmp    13ad <main+0x1ad>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x2c41(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
mov    %r12,%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   10d0 <getopt@plt>
cmp    0x0(%r13),%eax
je     1233 <main+0x33>
cmp    $0x69,%eax
jne    1380 <main+0x180>
mov    0x2c37(%rip),%rax
mov    (%rax),%edi
call   10e0 <atoi@plt>
mov    %eax,%r14d
jmp    1396 <main+0x196>

LLVM IR (-O0):
@.str = [3 x i8] c"i:\00"
@EOF = global i32 0
@optarg = global i32 0
@stderr = global i32 0
@.str.1 = [39 x i8] c"Usage: %s [-i <interval time in ms>].\0A\00"
@.str.2 = [19 x i8] c"Locking mutex ...\0A\00"
@PTHREAD_MUTEX_RECURSIVE = global i32 0
@.str.3 = [32 x i8] c"Locking rwlock exclusively ...\0A\00"
@.str.4 = [27 x i8] c"Locking rwlock shared ...\0A\00"
@.str.5 = [7 x i8] c"Done.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
br label %11
11: ; preds = %28, %2
%12 = load i32, i32* %4
%13 = load i8**, i8*** %5
%14 = call i32 @getopt(i32 %12, i8** %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32 %14, i32* %7
%15 = load i32, i32* @EOF
%16 = icmp ne i32 %14, %15
br i1 %16, label %17, label %29
17: ; preds = %11
%18 = load i32, i32* %7
switch i32 %18, label %22 [
i32 105, label %19
]
19: ; preds = %17
%20 = load i32, i32* @optarg
%21 = call i32 @atoi(i32 %20)
store i32 %21, i32* %6
br label %28
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 0
%26 = load i8*, i8** %25
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %22, %19
br label %11
29: ; preds = %11
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @pthread_mutexattr_init(i32* %9)
%33 = load i32, i32* @PTHREAD_MUTEX_RECURSIVE
%34 = call i32 @pthread_mutexattr_settype(i32* %9, i32 %33)
%35 = call i32 @pthread_mutex_init(i32* %8, i32* %9)
%36 = call i32 @pthread_mutexattr_destroy(i32* %9)
%37 = call i32 @pthread_mutex_lock(i32* %8)
%38 = load i32, i32* %6
%39 = call i32 @delay_ms(i32 %38)
%40 = call i32 @pthread_mutex_lock(i32* %8)
%41 = call i32 @pthread_mutex_unlock(i32* %8)
%42 = call i32 @pthread_mutex_unlock(i32* %8)
%43 = call i32 @pthread_mutex_destroy(i32* %8)
%44 = load i32, i32* @stderr
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%46 = call i32 @pthread_rwlock_init(i32* %10, i32 0)
%47 = call i32 @pthread_rwlock_wrlock(i32* %10)
%48 = load i32, i32* %6
%49 = call i32 @delay_ms(i32 %48)
%50 = call i32 @pthread_rwlock_unlock(i32* %10)
%51 = call i32 @pthread_rwlock_destroy(i32* %10)
%52 = load i32, i32* @stderr
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
%54 = call i32 @pthread_rwlock_init(i32* %10, i32 0)
%55 = call i32 @pthread_rwlock_rdlock(i32* %10)
%56 = load i32, i32* %6
%57 = call i32 @delay_ms(i32 %56)
%58 = call i32 @pthread_rwlock_rdlock(i32* %10)
%59 = call i32 @pthread_rwlock_unlock(i32* %10)
%60 = call i32 @pthread_rwlock_unlock(i32* %10)
%61 = call i32 @pthread_rwlock_destroy(i32* %10)
%62 = load i32, i32* @stderr
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"i:\00"
@EOF = global i32 0
@optarg = global i32 0
@stderr = global i32 0
@.str.1 = [39 x i8] c"Usage: %s [-i <interval time in ms>].\0A\00"
@.str.2 = [19 x i8] c"Locking mutex ...\0A\00"
@PTHREAD_MUTEX_RECURSIVE = global i32 0
@.str.3 = [32 x i8] c"Locking rwlock exclusively ...\0A\00"
@.str.4 = [27 x i8] c"Locking rwlock shared ...\0A\00"
@.str.5 = [7 x i8] c"Done.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EOF
%11 = icmp eq i32 %9, %10
br i1 %11, label %28, label %12
12: ; preds = %2, %23
%13 = phi i32 [ %25, %23 ], [ %9, %2 ]
%14 = phi i32 [ %24, %23 ], [ 0, %2 ]
%15 = icmp eq i32 %13, 105
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i32, i32* @optarg
%18 = tail call i32 @atoi(i32 %17)
br label %23
19: ; preds = %12
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %1
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %21)
br label %23
23: ; preds = %19, %16
%24 = phi i32 [ %18, %16 ], [ %14, %19 ]
%25 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%26 = load i32, i32* @EOF
%27 = icmp eq i32 %25, %26
br i1 %27, label %28, label %12
28: ; preds = %23, %2
%29 = phi i32 [ 0, %2 ], [ %24, %23 ]
%30 = load i32, i32* @stderr
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @pthread_mutexattr_init(i32* %4)
%33 = load i32, i32* @PTHREAD_MUTEX_RECURSIVE
%34 = call i32 @pthread_mutexattr_settype(i32* %4, i32 %33)
%35 = call i32 @pthread_mutex_init(i32* %3, i32* %4)
%36 = call i32 @pthread_mutexattr_destroy(i32* %4)
%37 = call i32 @pthread_mutex_lock(i32* %3)
%38 = call i32 @delay_ms(i32 %29)
%39 = call i32 @pthread_mutex_lock(i32* %3)
%40 = call i32 @pthread_mutex_unlock(i32* %3)
%41 = call i32 @pthread_mutex_unlock(i32* %3)
%42 = call i32 @pthread_mutex_destroy(i32* %3)
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%45 = call i32 @pthread_rwlock_init(i32* %5, i32 0)
%46 = call i32 @pthread_rwlock_wrlock(i32* %5)
%47 = call i32 @delay_ms(i32 %29)
%48 = call i32 @pthread_rwlock_unlock(i32* %5)
%49 = call i32 @pthread_rwlock_destroy(i32* %5)
%50 = load i32, i32* @stderr
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
%52 = call i32 @pthread_rwlock_init(i32* %5, i32 0)
%53 = call i32 @pthread_rwlock_rdlock(i32* %5)
%54 = call i32 @delay_ms(i32 %29)
%55 = call i32 @pthread_rwlock_rdlock(i32* %5)
%56 = call i32 @pthread_rwlock_unlock(i32* %5)
%57 = call i32 @pthread_rwlock_unlock(i32* %5)
%58 = call i32 @pthread_rwlock_destroy(i32* %5)
%59 = load i32, i32* @stderr
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
ret i32 0
}