#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 pthread_rwlock_tryrdlock(int /*<<< orphan*/ *);
int /*<<< orphan*/ sched_yield();
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock) {
  unsigned int n;
  for (n = 0;;) {
    int r = pthread_rwlock_tryrdlock(rwlock);
    if (r == 0)
      return r;
    if (n < 100) {
      ++n;
      continue;
    }
    sched_yield();
  }
}


x86 assembly (clang, -O0):
<pthread_rwlock_rdlock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <pthread_rwlock_tryrdlock@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    1152 <pthread_rwlock_rdlock+0x32>
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret
cmpl   $0x64,-0xc(%rbp)
jae    116a <pthread_rwlock_rdlock+0x4a>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1133 <pthread_rwlock_rdlock+0x13>
mov    $0x0,%al
call   1040 <sched_yield@plt>
jmp    1133 <pthread_rwlock_rdlock+0x13>

x86 assembly (clang, -O2):
<pthread_rwlock_rdlock>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1030 <pthread_rwlock_tryrdlock@plt>
test   %eax,%eax
je     115d <pthread_rwlock_rdlock+0x3d>
xor    %ebp,%ebp
jmp    1153 <pthread_rwlock_rdlock+0x33>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   1040 <sched_yield@plt>
mov    %rbx,%rdi
call   1030 <pthread_rwlock_tryrdlock@plt>
test   %eax,%eax
je     115d <pthread_rwlock_rdlock+0x3d>
cmp    $0x63,%ebp
ja     1140 <pthread_rwlock_rdlock+0x20>
add    $0x1,%ebp
jmp    1147 <pthread_rwlock_rdlock+0x27>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @pthread_rwlock_rdlock(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
store i32* %0, i32** %2
store i32 0, i32* %3
br label %5
5: ; preds = %18, %15, %1
%6 = load i32*, i32** %2
%7 = call i32 @pthread_rwlock_tryrdlock(i32* %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = load i32, i32* %4
ret i32 %11
12: ; preds = %5
%13 = load i32, i32* %3
%14 = icmp ult i32 %13, 100
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32, i32* %3
%17 = add i32 %16, 1
store i32 %17, i32* %3
br label %5
18: ; preds = %12
%19 = call i32 (...) @sched_yield()
br label %5
}

LLVM IR (-O2):
define i32 @pthread_rwlock_rdlock(i32* %0) {
%2 = tail call i32 @pthread_rwlock_tryrdlock(i32* %0)
%3 = icmp eq i32 %2, 0
br i1 %3, label %15, label %4
4: ; preds = %1, %11
%5 = phi i32 [ %12, %11 ], [ 0, %1 ]
%6 = icmp ult i32 %5, 100
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = add nuw nsw i32 %5, 1
br label %11
9: ; preds = %4
%10 = tail call i32 (...) @sched_yield()
br label %11
11: ; preds = %9, %7
%12 = phi i32 [ %8, %7 ], [ %5, %9 ]
%13 = tail call i32 @pthread_rwlock_tryrdlock(i32* %0)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %4
15: ; preds = %11, %1
ret i32 0
}