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

/* Variables and functions */
int /*<<< orphan*/ BIO_CLOSE;
int /*<<< orphan*/ BIO_f_buffer();
int /*<<< orphan*/ BIO_f_ssl();
scalar_t__ BIO_flush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_new(int /*<<< orphan*/);
int /*<<< orphan*/ BIO_push(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int BIO_puts(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ BIO_set_ssl(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                               int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ ssl_err_exit(char *);
int ssl_write(int socket, SSL *ssl, char *buf, int buflen) {
  int r;
  BIO *io, *ssl_bio;
  static char tmp[4096 * 4];
  io = BIO_new(BIO_f_buffer());
  ssl_bio = BIO_new(BIO_f_ssl());
  BIO_set_ssl(ssl_bio, ssl, BIO_CLOSE);
  BIO_push(io, ssl_bio);
  if (buflen + 1 > 4096 * 4)
    return -1;
  memcpy(tmp, buf, buflen);
  tmp[buflen] = '\0';
  if ((r = BIO_puts(io, tmp)) <= 0)
    ssl_err_exit("Write error");
  if (BIO_flush(io) < 0)
    ssl_err_exit("Error flushing BIO");
  return r;
}


x86 assembly (clang, -O0):
<ssl_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    $0x0,%al
call   1050 <BIO_f_buffer@plt>
mov    %eax,%edi
call   1070 <BIO_new@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1040 <BIO_f_ssl@plt>
mov    %eax,%edi
call   1070 <BIO_new@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
call   1080 <BIO_set_ssl@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1030 <BIO_push@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cmp    $0x4000,%eax
jle    120a <ssl_write+0x7a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1279 <ssl_write+0xe9>
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    0x2e68(%rip),%rdi
call   1060 <memcpy@plt>
movslq -0x1c(%rbp),%rcx
lea    0x2e58(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
lea    0x2e49(%rip),%rsi
call   10a0 <BIO_puts@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jg     1254 <ssl_write+0xc4>
lea    0xdb1(%rip),%rdi
call   1090 <ssl_err_exit@plt>
mov    -0x28(%rbp),%rdi
call   10b0 <BIO_flush@plt>
cmp    $0x0,%rax
jge    1273 <ssl_write+0xe3>
lea    0xd9e(%rip),%rdi
call   1090 <ssl_err_exit@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ssl_write>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%ebp
mov    %rdx,%r14
mov    %rsi,%r12
xor    %eax,%eax
call   1050 <BIO_f_buffer@plt>
mov    %eax,%edi
call   1070 <BIO_new@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1040 <BIO_f_ssl@plt>
mov    %eax,%edi
call   1070 <BIO_new@plt>
mov    %rax,%rbx
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1080 <BIO_set_ssl@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <BIO_push@plt>
mov    $0xffffffff,%ebx
cmp    $0x3fff,%ebp
jg     123f <ssl_write+0xaf>
lea    0x2e8b(%rip),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1060 <memcpy@plt>
movslq %ebp,%rax
movb   $0x0,(%rax,%rbx,1)
mov    %r15,%rdi
mov    %rbx,%rsi
call   10a0 <BIO_puts@plt>
mov    %eax,%ebx
test   %eax,%eax
jg     1226 <ssl_write+0x96>
lea    0xddf(%rip),%rdi
call   1090 <ssl_err_exit@plt>
mov    %r15,%rdi
call   10b0 <BIO_flush@plt>
test   %rax,%rax
jns    123f <ssl_write+0xaf>
lea    0xdd2(%rip),%rdi
call   1090 <ssl_err_exit@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ssl_write.tmp = internal global [16384 x i8] zeroinitializer
@BIO_CLOSE = global i32 0
@.str = [12 x i8] c"Write error\00"
@.str.1 = [19 x i8] c"Error flushing BIO\00"
define i32 @ssl_write(i32 %0, i32* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32*
store i32 %0, i32* %6
store i32* %1, i32** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%13 = call i32 (...) @BIO_f_buffer()
%14 = call i32* @BIO_new(i32 %13)
store i32* %14, i32** %11
%15 = call i32 (...) @BIO_f_ssl()
%16 = call i32* @BIO_new(i32 %15)
store i32* %16, i32** %12
%17 = load i32*, i32** %12
%18 = load i32*, i32** %7
%19 = load i32, i32* @BIO_CLOSE
%20 = call i32 @BIO_set_ssl(i32* %17, i32* %18, i32 %19)
%21 = load i32*, i32** %11
%22 = load i32*, i32** %12
%23 = call i32 @BIO_push(i32* %21, i32* %22)
%24 = load i32, i32* %9
%25 = add nsw i32 %24, 1
%26 = icmp sgt i32 %25, 16384
br i1 %26, label %27, label %28
27: ; preds = %4
store i32 -1, i32* %5
br label %48
28: ; preds = %4
%29 = load i8*, i8** %8
%30 = load i32, i32* %9
%31 = call i32 @memcpy(i8* getelementptr inbounds ([16384 x i8], [16384 x i8]* @ssl_write.tmp, i64 0, i64 0), i8* %29, i32 %30)
%32 = load i32, i32* %9
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [16384 x i8], [16384 x i8]* @ssl_write.tmp, i64 0, i64 %33
store i8 0, i8* %34
%35 = load i32*, i32** %11
%36 = call i32 @BIO_puts(i32* %35, i8* getelementptr inbounds ([16384 x i8], [16384 x i8]* @ssl_write.tmp, i64 0, i64 0))
store i32 %36, i32* %10
%37 = icmp sle i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %28
%39 = call i32 @ssl_err_exit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %40
40: ; preds = %38, %28
%41 = load i32*, i32** %11
%42 = call i64 @BIO_flush(i32* %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = call i32 @ssl_err_exit(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %46
46: ; preds = %44, %40
%47 = load i32, i32* %10
store i32 %47, i32* %5
br label %48
48: ; preds = %46, %27
%49 = load i32, i32* %5
ret i32 %49
}

LLVM IR (-O2):
@ssl_write.tmp = internal global [16384 x i8] zeroinitializer
@BIO_CLOSE = global i32 0
@.str = [12 x i8] c"Write error\00"
@.str.1 = [19 x i8] c"Error flushing BIO\00"
define i32 @ssl_write(i32 %0, i32* %1, i8* %2, i32 %3) {
%5 = tail call i32 (...) @BIO_f_buffer()
%6 = tail call i32* @BIO_new(i32 %5)
%7 = tail call i32 (...) @BIO_f_ssl()
%8 = tail call i32* @BIO_new(i32 %7)
%9 = load i32, i32* @BIO_CLOSE
%10 = tail call i32 @BIO_set_ssl(i32* %8, i32* %1, i32 %9)
%11 = tail call i32 @BIO_push(i32* %6, i32* %8)
%12 = icmp sgt i32 %3, 16383
br i1 %12, label %26, label %13
13: ; preds = %4
%14 = tail call i32 @memcpy(i8* getelementptr inbounds ([16384 x i8], [16384 x i8]* @ssl_write.tmp, i64 0, i64 0), i8* %2, i32 %3)
%15 = sext i32 %3 to i64
%16 = getelementptr inbounds [16384 x i8], [16384 x i8]* @ssl_write.tmp, i64 0, i64 %15
store i8 0, i8* %16
%17 = tail call i32 @BIO_puts(i32* %6, i8* getelementptr inbounds ([16384 x i8], [16384 x i8]* @ssl_write.tmp, i64 0, i64 0))
%18 = icmp slt i32 %17, 1
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = tail call i32 @ssl_err_exit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %13
%22 = tail call i64 @BIO_flush(i32* %6)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = tail call i32 @ssl_err_exit(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %21, %24, %4
%27 = phi i32 [ -1, %4 ], [ %17, %24 ], [ %17, %21 ]
ret i32 %27
}