#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_get_fd(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ BIO_set_retry_read(int /*<<< orphan*/ *);
scalar_t__ BIO_tou_socket_should_retry(int /*<<< orphan*/, int);
int /*<<< orphan*/ clear_tou_socket_error(int /*<<< orphan*/);
int tou_read(int /*<<< orphan*/, char *, int);
int tou_socket_read(BIO *b, char *out, int outl) {
  int ret = 0;
  if (out != NULL) {
    clear_tou_socket_error(BIO_get_fd(b, NULL));
    ret = tou_read(BIO_get_fd(b, NULL), out, outl);
    BIO_clear_retry_flags(b);
    if (ret <= 0) {
      if (BIO_tou_socket_should_retry(BIO_get_fd(b, NULL), ret))
        BIO_set_retry_read(b);
    }
  }
  return (ret);
}


x86 assembly (clang, -O0):
<tou_socket_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11fe <tou_socket_read+0x9e>
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <BIO_get_fd@plt>
mov    %eax,%edi
call   1070 <clear_tou_socket_error@plt>
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <BIO_get_fd@plt>
mov    %eax,%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <tou_read@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <BIO_clear_retry_flags@plt>
cmpl   $0x0,-0x18(%rbp)
jg     11f9 <tou_socket_read+0x99>
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <BIO_get_fd@plt>
mov    %eax,%edi
mov    -0x18(%rbp),%esi
call   1030 <BIO_tou_socket_should_retry@plt>
cmp    $0x0,%rax
je     11f4 <tou_socket_read+0x94>
mov    -0x8(%rbp),%rdi
call   1060 <BIO_set_retry_read@plt>
jmp    11f9 <tou_socket_read+0x99>
jmp    11fe <tou_socket_read+0x9e>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tou_socket_read>:
push   %rbp
push   %r14
push   %rbx
test   %rsi,%rsi
je     11c7 <tou_socket_read+0x67>
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %rdi,%rbx
xor    %esi,%esi
call   1080 <BIO_get_fd@plt>
mov    %eax,%edi
call   1070 <clear_tou_socket_error@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <BIO_get_fd@plt>
mov    %eax,%edi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1040 <tou_read@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <BIO_clear_retry_flags@plt>
test   %ebp,%ebp
jg     11c9 <tou_socket_read+0x69>
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <BIO_get_fd@plt>
mov    %eax,%edi
mov    %ebp,%esi
call   1030 <BIO_tou_socket_should_retry@plt>
test   %rax,%rax
je     11c9 <tou_socket_read+0x69>
mov    %rbx,%rdi
call   1060 <BIO_set_retry_read@plt>
jmp    11c9 <tou_socket_read+0x69>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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