#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 */
int /*<<< orphan*/ BIO_should_retry(int /*<<< orphan*/ *);
scalar_t__ BIO_write(int /*<<< orphan*/ *, char *, scalar_t__);
int /*<<< orphan*/ print_ssl_error(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int write_to_stream(BIO *bio, char *buffer, ssize_t length) {
  ssize_t r = -1;
  while (r < 0) {
    r = BIO_write(bio, buffer, length);
    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):
<write_to_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    11ad <write_to_stream+0x7d>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <BIO_write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jg     11a8 <write_to_stream+0x78>
mov    -0x8(%rbp),%rdi
call   1030 <BIO_should_retry@plt>
cmp    $0x0,%eax
jne    11a3 <write_to_stream+0x73>
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
lea    0xe67(%rip),%rdi
call   1050 <print_ssl_error@plt>
jmp    114c <write_to_stream+0x1c>
jmp    11a8 <write_to_stream+0x78>
jmp    114c <write_to_stream+0x1c>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_to_stream>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
mov    0x2e95(%rip),%rbp
lea    0xeae(%rip),%r12
jmp    1165 <write_to_stream+0x35>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
test   %rbx,%rbx
jns    1194 <write_to_stream+0x64>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <BIO_write@plt>
mov    %rax,%rbx
test   %rax,%rax
jg     1194 <write_to_stream+0x64>
mov    %r13,%rdi
call   1030 <BIO_should_retry@plt>
test   %eax,%eax
jne    1160 <write_to_stream+0x30>
mov    0x0(%rbp),%esi
mov    %r12,%rdi
call   1050 <print_ssl_error@plt>
jmp    1160 <write_to_stream+0x30>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"BIO_read should retry test failed.\0A\00"
@stdout = global i32 0
define i32 @write_to_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 = %26, %22, %3
%9 = load i64, i64* %7
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %27
11: ; preds = %8
%12 = load i32*, i32** %4
%13 = load i8*, i8** %5
%14 = load i64, i64* %6
%15 = call i64 @BIO_write(i32* %12, i8* %13, i64 %14)
store i64 %15, i64* %7
%16 = load i64, i64* %7
%17 = icmp sle i64 %16, 0
br i1 %17, label %18, label %26
18: ; preds = %11
%19 = load i32*, i32** %4
%20 = call i32 @BIO_should_retry(i32* %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %18
%23 = load i32, i32* @stdout
%24 = call i32 @print_ssl_error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %23)
br label %8
25: ; preds = %18
br label %26
26: ; preds = %25, %11
br label %8
27: ; preds = %8
%28 = load i64, i64* %7
%29 = trunc i64 %28 to i32
ret i32 %29
}

LLVM IR (-O2):
@.str = [36 x i8] c"BIO_read should retry test failed.\0A\00"
@stdout = global i32 0
define i32 @write_to_stream(i32* %0, i8* %1, i64 %2) {
br label %4
4: ; preds = %3, %13
%5 = tail call i64 @BIO_write(i32* %0, i8* %1, i64 %2)
%6 = icmp slt i64 %5, 1
br i1 %6, label %7, label %15
7: ; preds = %4
%8 = tail call i32 @BIO_should_retry(i32* %0)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = load i32, i32* @stdout
%12 = tail call i32 @print_ssl_error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %11)
br label %13
13: ; preds = %7, %10
%14 = icmp slt i64 %5, 0
br i1 %14, label %4, label %15
15: ; preds = %4, %13
%16 = trunc i64 %5 to i32
ret i32 %16
}