#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 */
struct szl_interp {
  int dummy;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ SSL;

/* Variables and functions */
int /*<<< orphan*/ ERR_error_string(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ ERR_get_error();
int SSL_ERROR_WANT_READ;
int SSL_ERROR_WANT_WRITE;
int SSL_ERROR_ZERO_RETURN;
int SSL_RECEIVED_SHUTDOWN;
int SSL_get_error(int /*<<< orphan*/ *, int);
int SSL_get_shutdown(int /*<<< orphan*/ *);
int SSL_read(int /*<<< orphan*/ *, unsigned char *, int);
int /*<<< orphan*/ szl_set_last_str(struct szl_interp *, int /*<<< orphan*/,
                                    int);
ssize_t szl_tls_read(struct szl_interp *interp, void *priv, unsigned char *buf,
                     const size_t len, int *more) {
  int out, err;
  out = SSL_read((SSL *)priv, buf, (int)len);
  if (out > 0)
    return (ssize_t)out;
  err = SSL_get_error((SSL *)priv, out);
  if (SSL_get_shutdown((SSL *)priv) & SSL_RECEIVED_SHUTDOWN)
    *more = 0;
  if ((err == SSL_ERROR_WANT_READ) || (err == SSL_ERROR_WANT_WRITE) ||
      (err == SSL_ERROR_ZERO_RETURN))
    return 0;
  szl_set_last_str(interp, ERR_error_string(ERR_get_error(), NULL), -1);
  return -1;
}


x86 assembly (clang, -O0):
<szl_tls_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    %eax,%edx
call   1050 <SSL_read@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jle    11a7 <szl_tls_read+0x47>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124e <szl_tls_read+0xee>
mov    -0x18(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1030 <SSL_get_error@plt>
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <SSL_get_shutdown@plt>
mov    0x2e02(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11db <szl_tls_read+0x7b>
mov    -0x30(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x38(%rbp),%eax
mov    0x2deb(%rip),%rcx
cmp    (%rcx),%eax
je     1211 <szl_tls_read+0xb1>
mov    -0x38(%rbp),%eax
mov    0x2df9(%rip),%rcx
cmp    (%rcx),%eax
je     1211 <szl_tls_read+0xb1>
mov    -0x38(%rbp),%eax
mov    0x2ddf(%rip),%rcx
cmp    (%rcx),%eax
jne    121d <szl_tls_read+0xbd>
movl   $0x0,-0x4(%rbp)
jmp    124e <szl_tls_read+0xee>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1080 <ERR_get_error@plt>
mov    %eax,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <ERR_error_string@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%esi
mov    $0xffffffff,%edx
call   1070 <szl_set_last_str@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<szl_tls_read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
mov    %rdx,%rsi
mov    %ecx,%edx
call   1050 <SSL_read@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     11f5 <szl_tls_read+0x95>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <SSL_get_error@plt>
mov    %eax,%r12d
mov    %rbx,%rdi
call   1040 <SSL_get_shutdown@plt>
mov    0x2e28(%rip),%rcx
test   %eax,(%rcx)
je     11ab <szl_tls_read+0x4b>
movl   $0x0,(%r15)
mov    0x2e1e(%rip),%rax
xor    %ebp,%ebp
cmp    (%rax),%r12d
je     11f5 <szl_tls_read+0x95>
mov    0x2e30(%rip),%rax
cmp    (%rax),%r12d
je     11f5 <szl_tls_read+0x95>
mov    0x2e1c(%rip),%rax
cmp    (%rax),%r12d
je     11f5 <szl_tls_read+0x95>
xor    %eax,%eax
call   1080 <ERR_get_error@plt>
mov    %eax,%edi
xor    %esi,%esi
call   1060 <ERR_error_string@plt>
mov    $0xffffffff,%ebp
mov    %r14,%rdi
mov    %eax,%esi
mov    $0xffffffff,%edx
call   1070 <szl_set_last_str@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.szl_interp = type { i32 }
@SSL_RECEIVED_SHUTDOWN = global i32 0
@SSL_ERROR_WANT_READ = global i32 0
@SSL_ERROR_WANT_WRITE = global i32 0
@SSL_ERROR_ZERO_RETURN = global i32 0
define i32 @szl_tls_read(%struct.szl_interp* %0, i8* %1, i8* %2, i64 %3, i32* %4) {
%6 = alloca i32
%7 = alloca %struct.szl_interp*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
store %struct.szl_interp* %0, %struct.szl_interp** %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i64 %3, i64* %10
store i32* %4, i32** %11
%14 = load i8*, i8** %8
%15 = bitcast i8* %14 to i32*
%16 = load i8*, i8** %9
%17 = load i64, i64* %10
%18 = trunc i64 %17 to i32
%19 = call i32 @SSL_read(i32* %15, i8* %16, i32 %18)
store i32 %19, i32* %12
%20 = load i32, i32* %12
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %5
%23 = load i32, i32* %12
store i32 %23, i32* %6
br label %55
24: ; preds = %5
%25 = load i8*, i8** %8
%26 = bitcast i8* %25 to i32*
%27 = load i32, i32* %12
%28 = call i32 @SSL_get_error(i32* %26, i32 %27)
store i32 %28, i32* %13
%29 = load i8*, i8** %8
%30 = bitcast i8* %29 to i32*
%31 = call i32 @SSL_get_shutdown(i32* %30)
%32 = load i32, i32* @SSL_RECEIVED_SHUTDOWN
%33 = and i32 %31, %32
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %24
%36 = load i32*, i32** %11
store i32 0, i32* %36
br label %37
37: ; preds = %35, %24
%38 = load i32, i32* %13
%39 = load i32, i32* @SSL_ERROR_WANT_READ
%40 = icmp eq i32 %38, %39
br i1 %40, label %49, label %41
41: ; preds = %37
%42 = load i32, i32* %13
%43 = load i32, i32* @SSL_ERROR_WANT_WRITE
%44 = icmp eq i32 %42, %43
br i1 %44, label %49, label %45
45: ; preds = %41
%46 = load i32, i32* %13
%47 = load i32, i32* @SSL_ERROR_ZERO_RETURN
%48 = icmp eq i32 %46, %47
br i1 %48, label %49, label %50
49: ; preds = %45, %41, %37
store i32 0, i32* %6
br label %55
50: ; preds = %45
%51 = load %struct.szl_interp*, %struct.szl_interp** %7
%52 = call i32 (...) @ERR_get_error()
%53 = call i32 @ERR_error_string(i32 %52, i32* null)
%54 = call i32 @szl_set_last_str(%struct.szl_interp* %51, i32 %53, i32 -1)
store i32 -1, i32* %6
br label %55
55: ; preds = %50, %49, %22
%56 = load i32, i32* %6
ret i32 %56
}

LLVM IR (-O2):
%struct.szl_interp = type { i32 }
@SSL_RECEIVED_SHUTDOWN = global i32 0
@SSL_ERROR_WANT_READ = global i32 0
@SSL_ERROR_WANT_WRITE = global i32 0
@SSL_ERROR_ZERO_RETURN = global i32 0
define i32 @szl_tls_read(%struct.szl_interp* %0, i8* %1, i8* %2, i64 %3, i32* nocapture writeonly %4) {
%6 = bitcast i8* %1 to i32*
%7 = trunc i64 %3 to i32
%8 = tail call i32 @SSL_read(i32* %6, i8* %2, i32 %7)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %30, label %10
10: ; preds = %5
%11 = tail call i32 @SSL_get_error(i32* %6, i32 %8)
%12 = tail call i32 @SSL_get_shutdown(i32* %6)
%13 = load i32, i32* @SSL_RECEIVED_SHUTDOWN
%14 = and i32 %13, %12
%15 = icmp eq i32 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %10
store i32 0, i32* %4
br label %17
17: ; preds = %16, %10
%18 = load i32, i32* @SSL_ERROR_WANT_READ
%19 = icmp eq i32 %11, %18
%20 = load i32, i32* @SSL_ERROR_WANT_WRITE
%21 = icmp eq i32 %11, %20
%22 = select i1 %19, i1 true, i1 %21
%23 = load i32, i32* @SSL_ERROR_ZERO_RETURN
%24 = icmp eq i32 %11, %23
%25 = select i1 %22, i1 true, i1 %24
br i1 %25, label %30, label %26
26: ; preds = %17
%27 = tail call i32 (...) @ERR_get_error()
%28 = tail call i32 @ERR_error_string(i32 %27, i32* null)
%29 = tail call i32 @szl_set_last_str(%struct.szl_interp* %0, i32 %28, i32 -1)
br label %30
30: ; preds = %17, %5, %26
%31 = phi i32 [ -1, %26 ], [ %8, %5 ], [ 0, %17 ]
ret i32 %31
}