#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_write(int /*<<< orphan*/ *);
scalar_t__ BIO_tou_socket_should_retry(int /*<<< orphan*/, int);
int /*<<< orphan*/ clear_tou_socket_error(int /*<<< orphan*/);
int tou_write(int /*<<< orphan*/, char const *, int);
int tou_socket_write(BIO *b, const char *in, int inl) {
  int ret;
  clear_tou_socket_error(BIO_get_fd(b, NULL));
  ret = tou_write(BIO_get_fd(b, NULL), in, inl);
  BIO_clear_retry_flags(b);
  if (ret <= 0) {
    if (BIO_tou_socket_should_retry(BIO_get_fd(b, NULL), ret)) {
      BIO_set_retry_write(b);
    }
  }
  return (ret);
}


x86 assembly (clang, -O0):
<tou_socket_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
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   1030 <tou_write@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <BIO_clear_retry_flags@plt>
cmpl   $0x0,-0x18(%rbp)
jg     11e7 <tou_socket_write+0x87>
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <BIO_get_fd@plt>
mov    %eax,%edi
mov    -0x18(%rbp),%esi
call   1040 <BIO_tou_socket_should_retry@plt>
cmp    $0x0,%rax
je     11e2 <tou_socket_write+0x82>
mov    -0x8(%rbp),%rdi
call   1050 <BIO_set_retry_write@plt>
jmp    11e7 <tou_socket_write+0x87>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tou_socket_write>:
push   %rbp
push   %r14
push   %rbx
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   1030 <tou_write@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1060 <BIO_clear_retry_flags@plt>
test   %ebp,%ebp
jg     11c0 <tou_socket_write+0x60>
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <BIO_get_fd@plt>
mov    %eax,%edi
mov    %ebp,%esi
call   1040 <BIO_tou_socket_should_retry@plt>
test   %rax,%rax
je     11c0 <tou_socket_write+0x60>
mov    %rbx,%rdi
call   1050 <BIO_set_retry_write@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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