#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_copy_next_retry(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_next(int /*<<< orphan*/ *);
int BIO_read(int /*<<< orphan*/ *, char *, int);
int dwrap_read(BIO *b, char *out, int outl) {
  int ret;
  if (!b || !out) {
    return 0;
  }
  BIO_clear_retry_flags(b);
  BIO *next = BIO_next(b);
  ret = BIO_read(next, out, outl);
  if (ret <= 0) {
    BIO_copy_next_retry(b);
  }
  return ret;
}


x86 assembly (clang, -O0):
<dwrap_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1169 <dwrap_read+0x29>
cmpq   $0x0,-0x18(%rbp)
jne    1175 <dwrap_read+0x35>
movl   $0x0,-0x4(%rbp)
jmp    11b7 <dwrap_read+0x77>
mov    -0x10(%rbp),%rdi
call   1040 <BIO_clear_retry_flags@plt>
mov    -0x10(%rbp),%rdi
call   1060 <BIO_next@plt>
mov    %rax,-0x28(%rbp)
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     11b1 <dwrap_read+0x71>
mov    -0x10(%rbp),%rdi
call   1050 <BIO_copy_next_retry@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dwrap_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %ebp,%ebp
test   %rdi,%rdi
je     1185 <dwrap_read+0x45>
mov    %rsi,%r15
test   %rsi,%rsi
je     1185 <dwrap_read+0x45>
mov    %edx,%r14d
mov    %rdi,%rbx
call   1040 <BIO_clear_retry_flags@plt>
mov    %rbx,%rdi
call   1060 <BIO_next@plt>
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <BIO_read@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     1185 <dwrap_read+0x45>
mov    %rbx,%rdi
call   1050 <BIO_copy_next_retry@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @dwrap_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
%10 = load i32*, i32** %5
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = load i8*, i8** %6
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %12, %3
store i32 0, i32* %4
br label %32
16: ; preds = %12
%17 = load i32*, i32** %5
%18 = call i32 @BIO_clear_retry_flags(i32* %17)
%19 = load i32*, i32** %5
%20 = call i32* @BIO_next(i32* %19)
store i32* %20, i32** %9
%21 = load i32*, i32** %9
%22 = load i8*, i8** %6
%23 = load i32, i32* %7
%24 = call i32 @BIO_read(i32* %21, i8* %22, i32 %23)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp sle i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %16
%28 = load i32*, i32** %5
%29 = call i32 @BIO_copy_next_retry(i32* %28)
br label %30
30: ; preds = %27, %16
%31 = load i32, i32* %8
store i32 %31, i32* %4
br label %32
32: ; preds = %30, %15
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
define i32 @dwrap_read(i32* %0, i8* %1, i32 %2) {
%4 = icmp ne i32* %0, null
%5 = icmp ne i8* %1, null
%6 = and i1 %4, %5
br i1 %6, label %7, label %14
7: ; preds = %3
%8 = tail call i32 @BIO_clear_retry_flags(i32* %0)
%9 = tail call i32* @BIO_next(i32* %0)
%10 = tail call i32 @BIO_read(i32* %9, i8* %1, i32 %2)
%11 = icmp slt i32 %10, 1
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = tail call i32 @BIO_copy_next_retry(i32* %0)
br label %14
14: ; preds = %7, %12, %3
%15 = phi i32 [ 0, %3 ], [ %10, %12 ], [ %10, %7 ]
ret i32 %15
}