#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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ BIO;

/* Variables and functions */
scalar_t__ BIO_read(int /*<<< orphan*/ *, char *, scalar_t__);
int /*<<< orphan*/ BIO_should_retry(int /*<<< orphan*/ *);
int /*<<< orphan*/ print_ssl_error(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
ssize_t read_from_stream(BIO *bio, char *buffer, ssize_t length) {
  ssize_t r = -1;
  while (r < 0) {
    r = BIO_read(bio, buffer, length);
    if (r == 0) {
      print_ssl_error("Reached the end of the data stream.\n", stdout);
      continue;
    } else if (r < 0) {
      if (!BIO_should_retry(bio)) {
        print_ssl_error("BIO_read should retry test failed.\n", stdout);
        continue;
      }
    }
  };
  return r;
}


x86 assembly (clang, -O0):
<read_from_stream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0xffffffffffffffff,-0x20(%rbp)
ff
cmpq   $0x0,-0x20(%rbp)
jge    11d7 <read_from_stream+0xa7>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <BIO_read@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1191 <read_from_stream+0x61>
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xe79(%rip),%rdi
call   1050 <print_ssl_error@plt>
jmp    114c <read_from_stream+0x1c>
cmpq   $0x0,-0x20(%rbp)
jge    11cd <read_from_stream+0x9d>
mov    -0x8(%rbp),%rdi
call   1040 <BIO_should_retry@plt>
cmp    $0x0,%eax
jne    11c8 <read_from_stream+0x98>
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
lea    0xe67(%rip),%rdi
call   1050 <print_ssl_error@plt>
jmp    114c <read_from_stream+0x1c>
jmp    11cd <read_from_stream+0x9d>
jmp    11d2 <read_from_stream+0xa2>
jmp    114c <read_from_stream+0x1c>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_from_stream>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
call   1030 <BIO_read@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11a4 <read_from_stream+0x74>
mov    0x2e88(%rip),%rbp
lea    0xec6(%rip),%r12
jmp    1186 <read_from_stream+0x56>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <BIO_read@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11a4 <read_from_stream+0x74>
test   %rbx,%rbx
jns    11b9 <read_from_stream+0x89>
mov    %r13,%rdi
call   1040 <BIO_should_retry@plt>
test   %eax,%eax
jne    1170 <read_from_stream+0x40>
mov    0x0(%rbp),%esi
mov    %r12,%rdi
call   1050 <print_ssl_error@plt>
jmp    1170 <read_from_stream+0x40>
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
lea    0xe4c(%rip),%rdi
call   1050 <print_ssl_error@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [37 x i8] c"Reached the end of the data stream.\0A\00"
@stdout = global i32 0
@.str.1 = [36 x i8] c"BIO_read should retry test failed.\0A\00"
define i64 @read_from_stream(i32* %0, i8* %1, i64 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i32* %0, i32** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i64 -1, i64* %7
br label %8
8: ; preds = %33, %28, %18, %3
%9 = load i64, i64* %7
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %34
11: ; preds = %8
%12 = load i32*, i32** %4
%13 = load i8*, i8** %5
%14 = load i64, i64* %6
%15 = call i64 @BIO_read(i32* %12, i8* %13, i64 %14)
store i64 %15, i64* %7
%16 = load i64, i64* %7
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %11
%19 = load i32, i32* @stdout
%20 = call i32 @print_ssl_error(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %19)
br label %8
21: ; preds = %11
%22 = load i64, i64* %7
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %21
%25 = load i32*, i32** %4
%26 = call i32 @BIO_should_retry(i32* %25)
%27 = icmp ne i32 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %24
%29 = load i32, i32* @stdout
%30 = call i32 @print_ssl_error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %29)
br label %8
31: ; preds = %24
br label %32
32: ; preds = %31, %21
br label %33
33: ; preds = %32
br label %8
34: ; preds = %8
%35 = load i64, i64* %7
ret i64 %35
}

LLVM IR (-O2):
@.str = [37 x i8] c"Reached the end of the data stream.\0A\00"
@stdout = global i32 0
@.str.1 = [36 x i8] c"BIO_read should retry test failed.\0A\00"
define i64 @read_from_stream(i32* %0, i8* %1, i64 %2) {
%4 = tail call i64 @BIO_read(i32* %0, i8* %1, i64 %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %13
6: ; preds = %10, %3
%7 = phi i64 [ %4, %3 ], [ %11, %10 ]
%8 = load i32, i32* @stdout
%9 = tail call i32 @print_ssl_error(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %22
10: ; preds = %16, %19
%11 = tail call i64 @BIO_read(i32* %0, i8* %1, i64 %2)
%12 = icmp eq i64 %11, 0
br i1 %12, label %6, label %13
13: ; preds = %3, %10
%14 = phi i64 [ %11, %10 ], [ %4, %3 ]
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %13
%17 = tail call i32 @BIO_should_retry(i32* %0)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %10
19: ; preds = %16
%20 = load i32, i32* @stdout
%21 = tail call i32 @print_ssl_error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %20)
br label %10
22: ; preds = %13, %6
%23 = phi i64 [ %7, %6 ], [ %14, %13 ]
ret i64 %23
}