#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 */
unsigned long long ULONG_MAX;
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ pthread_rwlock_init(int /*<<< orphan*/ *,
                                       int /*<<< orphan*/ *);
int pthread_rwlock_rdlock(int /*<<< orphan*/ *);
char *strerror(int);
void maxrdlocks(void) {
  unsigned long long i;
  pthread_rwlock_t rw;
  int r;
  pthread_rwlock_init(&rw, NULL);
  for (i = 0; i < ULONG_MAX; i++) {
    if ((r = pthread_rwlock_rdlock(&rw)) != 0)
      break;
    if (i % 10000000 == 0)
      printf("%llu\n", i);
  }
  printf("%d %s %llu\n", r, strerror(r), i);
}


x86 assembly (clang, -O0):
<maxrdlocks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0xc(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <pthread_rwlock_init@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    0x2e70(%rip),%rcx
cmp    (%rcx),%rax
jae    11cd <maxrdlocks+0x8d>
lea    -0xc(%rbp),%rdi
call   1040 <pthread_rwlock_rdlock@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
je     118b <maxrdlocks+0x4b>
jmp    11cd <maxrdlocks+0x8d>
mov    -0x8(%rbp),%rax
mov    $0x989680,%ecx
xor    %edx,%edx
div    %rcx
cmp    $0x0,%rdx
jne    11b7 <maxrdlocks+0x77>
mov    -0x8(%rbp),%rax
mov    %eax,%esi
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11bc <maxrdlocks+0x7c>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    115d <maxrdlocks+0x1d>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%edi
call   1060 <strerror@plt>
mov    -0x14(%rbp),%esi
mov    %rax,%rdx
mov    -0x8(%rbp),%rcx
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<maxrdlocks>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %ebx,%ebx
lea    0x4(%rsp),%rdi
xor    %esi,%esi
call   1050 <pthread_rwlock_init@plt>
mov    0x2e78(%rip),%r13
cmpq   $0x0,0x0(%r13)
mov    $0x0,%r12d
je     11c9 <maxrdlocks+0x89>
movabs $0xd6bf94d5e57a42bd,%r15
94 bf d6
lea    0x4(%rsp),%r14
xor    %ebx,%ebx
jmp    118a <maxrdlocks+0x4a>
add    $0x1,%rbx
cmp    0x0(%r13),%rbx
jae    11c1 <maxrdlocks+0x81>
mov    %rbx,%rax
mul    %r15
mov    %rdx,%rbp
mov    %r14,%rdi
call   1040 <pthread_rwlock_rdlock@plt>
test   %eax,%eax
jne    11c6 <maxrdlocks+0x86>
shr    $0x17,%rbp
imul   $0x989680,%rbp,%rax
cmp    %rbx,%rax
jne    1180 <maxrdlocks+0x40>
lea    0xe50(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    1180 <maxrdlocks+0x40>
xor    %r12d,%r12d
jmp    11c9 <maxrdlocks+0x89>
mov    %eax,%r12d
mov    %r12d,%edi
call   1060 <strerror@plt>
lea    0xe28(%rip),%rdi
mov    %r12d,%esi
mov    %rax,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ULONG_MAX = global i64 0
@.str = [6 x i8] c"%llu\0A\00"
@.str.1 = [12 x i8] c"%d %s %llu\0A\00"
define void @maxrdlocks() {
%1 = alloca i64
%2 = alloca i32
%3 = alloca i32
%4 = call i32 @pthread_rwlock_init(i32* %2, i32* null)
store i64 0, i64* %1
br label %5
5: ; preds = %22, %0
%6 = load i64, i64* %1
%7 = load i64, i64* @ULONG_MAX
%8 = icmp ult i64 %6, %7
br i1 %8, label %9, label %25
9: ; preds = %5
%10 = call i32 @pthread_rwlock_rdlock(i32* %2)
store i32 %10, i32* %3
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %9
br label %25
13: ; preds = %9
%14 = load i64, i64* %1
%15 = urem i64 %14, 10000000
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = load i64, i64* %1
%19 = trunc i64 %18 to i32
%20 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %17, %13
br label %22
22: ; preds = %21
%23 = load i64, i64* %1
%24 = add i64 %23, 1
store i64 %24, i64* %1
br label %5
25: ; preds = %12, %5
%26 = load i32, i32* %3
%27 = load i32, i32* %3
%28 = call i8* @strerror(i32 %27)
%29 = load i64, i64* %1
%30 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %26, i8* %28, i64 %29)
ret void
}

LLVM IR (-O2):
@ULONG_MAX = global i64 0
@.str = [6 x i8] c"%llu\0A\00"
@.str.1 = [12 x i8] c"%d %s %llu\0A\00"
define void @maxrdlocks() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = call i32 @pthread_rwlock_init(i32* %1, i32* null)
%4 = load i64, i64* @ULONG_MAX
%5 = icmp eq i64 %4, 0
br i1 %5, label %20, label %6
6: ; preds = %0, %16
%7 = phi i64 [ %17, %16 ], [ 0, %0 ]
%8 = call i32 @pthread_rwlock_rdlock(i32* %1)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %20
10: ; preds = %6
%11 = urem i64 %7, 10000000
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = trunc i64 %7 to i32
%15 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %10, %13
%17 = add nuw i64 %7, 1
%18 = load i64, i64* @ULONG_MAX
%19 = icmp ult i64 %17, %18
br i1 %19, label %6, label %20
20: ; preds = %16, %6, %0
%21 = phi i64 [ 0, %0 ], [ %7, %6 ], [ %17, %16 ]
%22 = phi i32 [ 0, %0 ], [ %8, %6 ], [ 0, %16 ]
%23 = call i8* @strerror(i32 %22)
%24 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %22, i8* %23, i64 %21)
ret void
}