#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;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ EPIPE;
scalar_t__ ERESTART;
scalar_t__ ERR_peek_last_error();
int SSL_ERROR_SSL;
int SSL_ERROR_SYSCALL;
int SSL_ERROR_WANT_READ;
int SSL_ERROR_WANT_WRITE;
int SSL_ERROR_WANT_X509_LOOKUP;
int SSL_ERROR_ZERO_RETURN;
int SSL_RECEIVED_SHUTDOWN;
int SSL_get_error(int /*<<< orphan*/ *, int);
int SSL_get_shutdown(int /*<<< orphan*/ *);
scalar_t__ errno;
int handle_ssl_error(SSL *ssl, int ret) {
  int code;
  if (SSL_get_shutdown(ssl) & SSL_RECEIVED_SHUTDOWN)
    return -1;
  code = SSL_get_error(ssl, ret);
  if (code == SSL_ERROR_WANT_READ || code == SSL_ERROR_WANT_WRITE)
    return 0;
  if (code == SSL_ERROR_ZERO_RETURN)
    return -1;
  if (code == SSL_ERROR_WANT_X509_LOOKUP)
    return -2;
  if (code == SSL_ERROR_SYSCALL) {
    if (ERR_peek_last_error() != 0)
      return -6;
    if (ret == 0)
      return -3;
    if (errno == EAGAIN || errno == ERESTART || errno == EINTR)
      return 0;
    if (errno == EPIPE)
      return -1;
    return -5;
  }
  if (code == SSL_ERROR_SSL)
    return -4;
  return -7;
}


x86 assembly (clang, -O0):
<handle_ssl_error>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <SSL_get_shutdown@plt>
mov    0x2e41(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1166 <handle_ssl_error+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d7 <handle_ssl_error+0x1a7>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <SSL_get_error@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2e29(%rip),%rcx
cmp    (%rcx),%eax
je     1199 <handle_ssl_error+0x69>
mov    -0x18(%rbp),%eax
mov    0x2e57(%rip),%rcx
cmp    (%rcx),%eax
jne    11a5 <handle_ssl_error+0x75>
movl   $0x0,-0x4(%rbp)
jmp    12d7 <handle_ssl_error+0x1a7>
mov    -0x18(%rbp),%eax
mov    0x2e31(%rip),%rcx
cmp    (%rcx),%eax
jne    11c3 <handle_ssl_error+0x93>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d7 <handle_ssl_error+0x1a7>
mov    -0x18(%rbp),%eax
mov    0x2e2b(%rip),%rcx
cmp    (%rcx),%eax
jne    11e1 <handle_ssl_error+0xb1>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12d7 <handle_ssl_error+0x1a7>
mov    -0x18(%rbp),%eax
mov    0x2db5(%rip),%rcx
cmp    (%rcx),%eax
jne    12b2 <handle_ssl_error+0x182>
mov    $0x0,%al
call   1040 <ERR_peek_last_error@plt>
cmp    $0x0,%rax
je     1210 <handle_ssl_error+0xe0>
movl   $0xfffffffa,-0x4(%rbp)
jmp    12d7 <handle_ssl_error+0x1a7>
cmpl   $0x0,-0x14(%rbp)
jne    1226 <handle_ssl_error+0xf6>
movl   $0xfffffffd,-0x4(%rbp)
jmp    12d7 <handle_ssl_error+0x1a7>
mov    0x2d83(%rip),%rax
mov    (%rax),%rax
mov    0x2d49(%rip),%rcx
cmp    (%rcx),%rax
je     1274 <handle_ssl_error+0x144>
mov    0x2d69(%rip),%rax
mov    (%rax),%rax
mov    0x2d7f(%rip),%rcx
cmp    (%rcx),%rax
je     1274 <handle_ssl_error+0x144>
mov    0x2d4f(%rip),%rax
mov    (%rax),%rax
mov    0x2d2d(%rip),%rcx
cmp    (%rcx),%rax
jne    1280 <handle_ssl_error+0x150>
movl   $0x0,-0x4(%rbp)
jmp    12d7 <handle_ssl_error+0x1a7>
mov    0x2d29(%rip),%rax
mov    (%rax),%rax
mov    0x2d27(%rip),%rcx
cmp    (%rcx),%rax
jne    12a6 <handle_ssl_error+0x176>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d7 <handle_ssl_error+0x1a7>
movl   $0xfffffffb,-0x4(%rbp)
jmp    12d7 <handle_ssl_error+0x1a7>
mov    -0x18(%rbp),%eax
mov    0x2d0c(%rip),%rcx
cmp    (%rcx),%eax
jne    12d0 <handle_ssl_error+0x1a0>
movl   $0xfffffffc,-0x4(%rbp)
jmp    12d7 <handle_ssl_error+0x1a7>
movl   $0xfffffff9,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_ssl_error>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
call   1050 <SSL_get_shutdown@plt>
mov    %eax,%ecx
mov    0x2e4a(%rip),%rdx
mov    $0xffffffff,%eax
test   %ecx,(%rdx)
je     1156 <handle_ssl_error+0x26>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <SSL_get_error@plt>
mov    %eax,%ecx
mov    0x2e3f(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%ecx
je     114f <handle_ssl_error+0x1f>
mov    0x2e72(%rip),%rdx
cmp    (%rdx),%ecx
je     114f <handle_ssl_error+0x1f>
mov    0x2e5f(%rip),%rdx
mov    $0xffffffff,%eax
cmp    (%rdx),%ecx
je     114f <handle_ssl_error+0x1f>
mov    0x2e67(%rip),%rdx
mov    $0xfffffffe,%eax
cmp    (%rdx),%ecx
je     114f <handle_ssl_error+0x1f>
mov    0x2dff(%rip),%rax
cmp    (%rax),%ecx
jne    1213 <handle_ssl_error+0xe3>
xor    %eax,%eax
call   1040 <ERR_peek_last_error@plt>
mov    %rax,%rcx
mov    $0xfffffffa,%eax
test   %rcx,%rcx
jne    114f <handle_ssl_error+0x1f>
test   %ebp,%ebp
je     122c <handle_ssl_error+0xfc>
mov    0x2dec(%rip),%rax
mov    (%rax),%rcx
mov    0x2db2(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
je     114f <handle_ssl_error+0x1f>
mov    0x2df0(%rip),%rdx
cmp    (%rdx),%rcx
je     114f <handle_ssl_error+0x1f>
mov    0x2da8(%rip),%rdx
cmp    (%rdx),%rcx
je     114f <handle_ssl_error+0x1f>
mov    0x2db8(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
sete   %al
shl    $0x2,%eax
or     $0xfffffffb,%eax
jmp    114f <handle_ssl_error+0x1f>
mov    0x2dae(%rip),%rax
xor    %edx,%edx
cmp    (%rax),%ecx
sete   %dl
lea    (%rdx,%rdx,2),%eax
add    $0xfffffff9,%eax
jmp    114f <handle_ssl_error+0x1f>
mov    $0xfffffffd,%eax
jmp    114f <handle_ssl_error+0x1f>

LLVM IR (-O0):
@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
@SSL_ERROR_WANT_X509_LOOKUP = global i32 0
@SSL_ERROR_SYSCALL = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@ERESTART = global i64 0
@EINTR = global i64 0
@EPIPE = global i64 0
@SSL_ERROR_SSL = global i32 0
define i32 @handle_ssl_error(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
%7 = load i32*, i32** %4
%8 = call i32 @SSL_get_shutdown(i32* %7)
%9 = load i32, i32* @SSL_RECEIVED_SHUTDOWN
%10 = and i32 %8, %9
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %72
13: ; preds = %2
%14 = load i32*, i32** %4
%15 = load i32, i32* %5
%16 = call i32 @SSL_get_error(i32* %14, i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = load i32, i32* @SSL_ERROR_WANT_READ
%19 = icmp eq i32 %17, %18
br i1 %19, label %24, label %20
20: ; preds = %13
%21 = load i32, i32* %6
%22 = load i32, i32* @SSL_ERROR_WANT_WRITE
%23 = icmp eq i32 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %20, %13
store i32 0, i32* %3
br label %72
25: ; preds = %20
%26 = load i32, i32* %6
%27 = load i32, i32* @SSL_ERROR_ZERO_RETURN
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %25
store i32 -1, i32* %3
br label %72
30: ; preds = %25
%31 = load i32, i32* %6
%32 = load i32, i32* @SSL_ERROR_WANT_X509_LOOKUP
%33 = icmp eq i32 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %30
store i32 -2, i32* %3
br label %72
35: ; preds = %30
%36 = load i32, i32* %6
%37 = load i32, i32* @SSL_ERROR_SYSCALL
%38 = icmp eq i32 %36, %37
br i1 %38, label %39, label %66
39: ; preds = %35
%40 = call i64 (...) @ERR_peek_last_error()
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %39
store i32 -6, i32* %3
br label %72
43: ; preds = %39
%44 = load i32, i32* %5
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %43
store i32 -3, i32* %3
br label %72
47: ; preds = %43
%48 = load i64, i64* @errno
%49 = load i64, i64* @EAGAIN
%50 = icmp eq i64 %48, %49
br i1 %50, label %59, label %51
51: ; preds = %47
%52 = load i64, i64* @errno
%53 = load i64, i64* @ERESTART
%54 = icmp eq i64 %52, %53
br i1 %54, label %59, label %55
55: ; preds = %51
%56 = load i64, i64* @errno
%57 = load i64, i64* @EINTR
%58 = icmp eq i64 %56, %57
br i1 %58, label %59, label %60
59: ; preds = %55, %51, %47
store i32 0, i32* %3
br label %72
60: ; preds = %55
%61 = load i64, i64* @errno
%62 = load i64, i64* @EPIPE
%63 = icmp eq i64 %61, %62
br i1 %63, label %64, label %65
64: ; preds = %60
store i32 -1, i32* %3
br label %72
65: ; preds = %60
store i32 -5, i32* %3
br label %72
66: ; preds = %35
%67 = load i32, i32* %6
%68 = load i32, i32* @SSL_ERROR_SSL
%69 = icmp eq i32 %67, %68
br i1 %69, label %70, label %71
70: ; preds = %66
store i32 -4, i32* %3
br label %72
71: ; preds = %66
store i32 -7, i32* %3
br label %72
72: ; preds = %71, %70, %65, %64, %59, %46, %42, %34, %29, %24, %12
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
@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
@SSL_ERROR_WANT_X509_LOOKUP = global i32 0
@SSL_ERROR_SYSCALL = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@ERESTART = global i64 0
@EINTR = global i64 0
@EPIPE = global i64 0
@SSL_ERROR_SSL = global i32 0
define i32 @handle_ssl_error(i32* %0, i32 %1) {
%3 = tail call i32 @SSL_get_shutdown(i32* %0)
%4 = load i32, i32* @SSL_RECEIVED_SHUTDOWN
%5 = and i32 %4, %3
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %46
7: ; preds = %2
%8 = tail call i32 @SSL_get_error(i32* %0, i32 %1)
%9 = load i32, i32* @SSL_ERROR_WANT_READ
%10 = icmp eq i32 %8, %9
%11 = load i32, i32* @SSL_ERROR_WANT_WRITE
%12 = icmp eq i32 %8, %11
%13 = select i1 %10, i1 true, i1 %12
br i1 %13, label %46, label %14
14: ; preds = %7
%15 = load i32, i32* @SSL_ERROR_ZERO_RETURN
%16 = icmp eq i32 %8, %15
br i1 %16, label %46, label %17
17: ; preds = %14
%18 = load i32, i32* @SSL_ERROR_WANT_X509_LOOKUP
%19 = icmp eq i32 %8, %18
br i1 %19, label %46, label %20
20: ; preds = %17
%21 = load i32, i32* @SSL_ERROR_SYSCALL
%22 = icmp eq i32 %8, %21
br i1 %22, label %23, label %42
23: ; preds = %20
%24 = tail call i64 (...) @ERR_peek_last_error()
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %46
26: ; preds = %23
%27 = icmp eq i32 %1, 0
br i1 %27, label %46, label %28
28: ; preds = %26
%29 = load i64, i64* @errno
%30 = load i64, i64* @EAGAIN
%31 = icmp eq i64 %29, %30
%32 = load i64, i64* @ERESTART
%33 = icmp eq i64 %29, %32
%34 = select i1 %31, i1 true, i1 %33
%35 = load i64, i64* @EINTR
%36 = icmp eq i64 %29, %35
%37 = select i1 %34, i1 true, i1 %36
br i1 %37, label %46, label %38
38: ; preds = %28
%39 = load i64, i64* @EPIPE
%40 = icmp eq i64 %29, %39
%41 = select i1 %40, i32 -1, i32 -5
br label %46
42: ; preds = %20
%43 = load i32, i32* @SSL_ERROR_SSL
%44 = icmp eq i32 %8, %43
%45 = select i1 %44, i32 -4, i32 -7
br label %46
46: ; preds = %42, %38, %28, %26, %23, %17, %14, %7, %2
%47 = phi i32 [ -1, %2 ], [ 0, %7 ], [ -1, %14 ], [ -2, %17 ], [ -6, %23 ], [ -3, %26 ], [ 0, %28 ], [ %41, %38 ], [ %45, %42 ]
ret i32 %47
}