#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*/ BIO;

/* Variables and functions */
int /*<<< orphan*/ BIO_clear_retry_flags(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_next(int /*<<< orphan*/ *);
int BIO_read(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ BIO_set_retry_read(int /*<<< orphan*/ *);
scalar_t__ BIO_should_read(int /*<<< orphan*/ *);
int watchccs_read(BIO *bio, char *out, int outl) {
  int ret = 0;
  BIO *next = BIO_next(bio);
  if (outl <= 0)
    return 0;
  if (next == NULL)
    return 0;
  BIO_clear_retry_flags(bio);
  ret = BIO_read(next, out, outl);
  if (ret <= 0 && BIO_should_read(next))
    BIO_set_retry_read(bio);
  return ret;
}


x86 assembly (clang, -O0):
<watchccs_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <BIO_next@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     118d <watchccs_read+0x3d>
movl   $0x0,-0x4(%rbp)
jmp    11ec <watchccs_read+0x9c>
cmpq   $0x0,-0x28(%rbp)
jne    11a4 <watchccs_read+0x54>
movl   $0x0,-0x4(%rbp)
jmp    11ec <watchccs_read+0x9c>
mov    -0x10(%rbp),%rdi
call   1040 <BIO_clear_retry_flags@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <BIO_read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     11e6 <watchccs_read+0x96>
mov    -0x28(%rbp),%rdi
call   1060 <BIO_should_read@plt>
cmp    $0x0,%rax
je     11e6 <watchccs_read+0x96>
mov    -0x10(%rbp),%rdi
call   1050 <BIO_set_retry_read@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<watchccs_read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebx
mov    %rsi,%r12
mov    %rdi,%r14
call   1070 <BIO_next@plt>
xor    %ebp,%ebp
test   %ebx,%ebx
jle    11a3 <watchccs_read+0x53>
mov    %rax,%r15
test   %rax,%rax
je     11a3 <watchccs_read+0x53>
mov    %r14,%rdi
call   1040 <BIO_clear_retry_flags@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1030 <BIO_read@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     11a3 <watchccs_read+0x53>
mov    %r15,%rdi
call   1060 <BIO_should_read@plt>
test   %rax,%rax
je     11a3 <watchccs_read+0x53>
mov    %r14,%rdi
call   1050 <BIO_set_retry_read@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @watchccs_read(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
%10 = load i32*, i32** %5
%11 = call i32* @BIO_next(i32* %10)
store i32* %11, i32** %9
%12 = load i32, i32* %7
%13 = icmp sle i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 0, i32* %4
br label %37
15: ; preds = %3
%16 = load i32*, i32** %9
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %19
18: ; preds = %15
store i32 0, i32* %4
br label %37
19: ; preds = %15
%20 = load i32*, i32** %5
%21 = call i32 @BIO_clear_retry_flags(i32* %20)
%22 = load i32*, i32** %9
%23 = load i8*, i8** %6
%24 = load i32, i32* %7
%25 = call i32 @BIO_read(i32* %22, i8* %23, i32 %24)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp sle i32 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %19
%29 = load i32*, i32** %9
%30 = call i64 @BIO_should_read(i32* %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = load i32*, i32** %5
%34 = call i32 @BIO_set_retry_read(i32* %33)
br label %35
35: ; preds = %32, %28, %19
%36 = load i32, i32* %8
store i32 %36, i32* %4
br label %37
37: ; preds = %35, %18, %14
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
define i32 @watchccs_read(i32* %0, i8* %1, i32 %2) {
%4 = tail call i32* @BIO_next(i32* %0)
%5 = icmp slt i32 %2, 1
%6 = icmp eq i32* %4, null
%7 = select i1 %5, i1 true, i1 %6
br i1 %7, label %17, label %8
8: ; preds = %3
%9 = tail call i32 @BIO_clear_retry_flags(i32* %0)
%10 = tail call i32 @BIO_read(i32* %4, i8* %1, i32 %2)
%11 = icmp slt i32 %10, 1
br i1 %11, label %12, label %17
12: ; preds = %8
%13 = tail call i64 @BIO_should_read(i32* %4)
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = tail call i32 @BIO_set_retry_read(i32* %0)
br label %17
17: ; preds = %8, %12, %15, %3
%18 = phi i32 [ 0, %3 ], [ %10, %15 ], [ %10, %12 ], [ %10, %8 ]
ret i32 %18
}